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
);
772 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO)
773 return fseeko(fp
, offset
, whence
);
775 return fseek(fp
, offset
, whence
);
779 /*******************************************************************
780 An ftell() wrapper that will deal with 64 bit filesizes.
781 ********************************************************************/
783 SMB_OFF_T
sys_ftell(FILE *fp
)
785 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
786 return (SMB_OFF_T
)ftell64(fp
);
787 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
788 return (SMB_OFF_T
)ftello64(fp
);
790 return (SMB_OFF_T
)ftell(fp
);
794 /*******************************************************************
795 A creat() wrapper that will deal with 64 bit filesizes.
796 ********************************************************************/
798 int sys_creat(const char *path
, mode_t mode
)
800 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
801 return creat64(path
, mode
);
804 * If creat64 isn't defined then ensure we call a potential open64.
807 return sys_open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
811 /*******************************************************************
812 An open() wrapper that will deal with 64 bit filesizes.
813 ********************************************************************/
815 int sys_open(const char *path
, int oflag
, mode_t mode
)
817 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
818 return open64(path
, oflag
, mode
);
820 return open(path
, oflag
, mode
);
824 /*******************************************************************
825 An fopen() wrapper that will deal with 64 bit filesizes.
826 ********************************************************************/
828 FILE *sys_fopen(const char *path
, const char *type
)
830 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
831 return fopen64(path
, type
);
833 return fopen(path
, type
);
838 #if HAVE_KERNEL_SHARE_MODES
840 #define LOCK_MAND 32 /* This is a mandatory flock */
841 #define LOCK_READ 64 /* ... Which allows concurrent read operations */
842 #define LOCK_WRITE 128 /* ... Which allows concurrent write operations */
843 #define LOCK_RW 192 /* ... Which allows concurrent read & write ops */
847 /*******************************************************************
848 A flock() wrapper that will perform the kernel flock.
849 ********************************************************************/
851 void kernel_flock(int fd
, uint32 share_mode
, uint32 access_mask
)
853 #if HAVE_KERNEL_SHARE_MODES
855 if (share_mode
== FILE_SHARE_WRITE
) {
856 kernel_mode
= LOCK_MAND
|LOCK_WRITE
;
857 } else if (share_mode
== FILE_SHARE_READ
) {
858 kernel_mode
= LOCK_MAND
|LOCK_READ
;
859 } else if (share_mode
== FILE_SHARE_NONE
) {
860 kernel_mode
= LOCK_MAND
;
863 flock(fd
, kernel_mode
);
871 /*******************************************************************
872 An opendir wrapper that will deal with 64 bit filesizes.
873 ********************************************************************/
875 SMB_STRUCT_DIR
*sys_opendir(const char *name
)
877 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
878 return opendir64(name
);
880 return opendir(name
);
884 /*******************************************************************
885 An fdopendir wrapper that will deal with 64 bit filesizes.
886 Ugly hack - we need dirfd for this to work correctly in the
888 ********************************************************************/
890 SMB_STRUCT_DIR
*sys_fdopendir(int fd
)
892 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FDOPENDIR64) && defined(HAVE_DIRFD)
893 return fdopendir64(fd
);
894 #elif defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
895 return fdopendir(fd
);
902 /*******************************************************************
903 A readdir wrapper that will deal with 64 bit filesizes.
904 ********************************************************************/
906 SMB_STRUCT_DIRENT
*sys_readdir(SMB_STRUCT_DIR
*dirp
)
908 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
909 return readdir64(dirp
);
911 return readdir(dirp
);
915 /*******************************************************************
916 A seekdir wrapper that will deal with 64 bit filesizes.
917 ********************************************************************/
919 void sys_seekdir(SMB_STRUCT_DIR
*dirp
, long offset
)
921 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
922 seekdir64(dirp
, offset
);
924 seekdir(dirp
, offset
);
928 /*******************************************************************
929 A telldir wrapper that will deal with 64 bit filesizes.
930 ********************************************************************/
932 long sys_telldir(SMB_STRUCT_DIR
*dirp
)
934 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
935 return (long)telldir64(dirp
);
937 return (long)telldir(dirp
);
941 /*******************************************************************
942 A rewinddir wrapper that will deal with 64 bit filesizes.
943 ********************************************************************/
945 void sys_rewinddir(SMB_STRUCT_DIR
*dirp
)
947 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
954 /*******************************************************************
955 A close wrapper that will deal with 64 bit filesizes.
956 ********************************************************************/
958 int sys_closedir(SMB_STRUCT_DIR
*dirp
)
960 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
961 return closedir64(dirp
);
963 return closedir(dirp
);
967 /*******************************************************************
968 An mknod() wrapper that will deal with 64 bit filesizes.
969 ********************************************************************/
971 int sys_mknod(const char *path
, mode_t mode
, SMB_DEV_T dev
)
973 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
974 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
975 return mknod64(path
, mode
, dev
);
977 return mknod(path
, mode
, dev
);
980 /* No mknod system call. */
986 /*******************************************************************
987 The wait() calls vary between systems
988 ********************************************************************/
990 int sys_waitpid(pid_t pid
,int *status
,int options
)
993 return waitpid(pid
,status
,options
);
994 #else /* HAVE_WAITPID */
995 return wait4(pid
, status
, options
, NULL
);
996 #endif /* HAVE_WAITPID */
999 /*******************************************************************
1000 System wrapper for getwd
1001 ********************************************************************/
1003 char *sys_getwd(char *s
)
1007 wd
= (char *)getcwd(s
, PATH_MAX
);
1009 wd
= (char *)getwd(s
);
1014 #if defined(HAVE_POSIX_CAPABILITIES)
1016 /**************************************************************************
1017 Try and abstract process capabilities (for systems that have them).
1018 ****************************************************************************/
1020 /* Set the POSIX capabilities needed for the given purpose into the effective
1021 * capability set of the current process. Make sure they are always removed
1022 * from the inheritable set, because there is no circumstance in which our
1023 * children should inherit our elevated privileges.
1025 static bool set_process_capability(enum smbd_capability capability
,
1028 cap_value_t cap_vals
[2] = {0};
1029 int num_cap_vals
= 0;
1033 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
1034 /* On Linux, make sure that any capabilities we grab are sticky
1035 * across UID changes. We expect that this would allow us to keep both
1036 * the effective and permitted capability sets, but as of circa 2.6.16,
1037 * only the permitted set is kept. It is a bug (which we work around)
1038 * that the effective set is lost, but we still require the effective
1041 if (!prctl(PR_GET_KEEPCAPS
)) {
1042 prctl(PR_SET_KEEPCAPS
, 1);
1046 cap
= cap_get_proc();
1048 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
1053 switch (capability
) {
1054 case KERNEL_OPLOCK_CAPABILITY
:
1055 #ifdef CAP_NETWORK_MGT
1056 /* IRIX has CAP_NETWORK_MGT for oplocks. */
1057 cap_vals
[num_cap_vals
++] = CAP_NETWORK_MGT
;
1060 case DMAPI_ACCESS_CAPABILITY
:
1061 #ifdef CAP_DEVICE_MGT
1062 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
1063 cap_vals
[num_cap_vals
++] = CAP_DEVICE_MGT
;
1065 /* Linux has CAP_MKNOD for DMAPI access. */
1066 cap_vals
[num_cap_vals
++] = CAP_MKNOD
;
1069 case LEASE_CAPABILITY
:
1071 cap_vals
[num_cap_vals
++] = CAP_LEASE
;
1076 SMB_ASSERT(num_cap_vals
<= ARRAY_SIZE(cap_vals
));
1078 if (num_cap_vals
== 0) {
1083 cap_set_flag(cap
, CAP_EFFECTIVE
, num_cap_vals
, cap_vals
,
1084 enable
? CAP_SET
: CAP_CLEAR
);
1086 /* We never want to pass capabilities down to our children, so make
1087 * sure they are not inherited.
1089 cap_set_flag(cap
, CAP_INHERITABLE
, num_cap_vals
, cap_vals
, CAP_CLEAR
);
1091 if (cap_set_proc(cap
) == -1) {
1092 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
1102 #endif /* HAVE_POSIX_CAPABILITIES */
1104 /****************************************************************************
1105 Gain the oplock capability from the kernel if possible.
1106 ****************************************************************************/
1108 void set_effective_capability(enum smbd_capability capability
)
1110 #if defined(HAVE_POSIX_CAPABILITIES)
1111 set_process_capability(capability
, True
);
1112 #endif /* HAVE_POSIX_CAPABILITIES */
1115 void drop_effective_capability(enum smbd_capability capability
)
1117 #if defined(HAVE_POSIX_CAPABILITIES)
1118 set_process_capability(capability
, False
);
1119 #endif /* HAVE_POSIX_CAPABILITIES */
1122 /**************************************************************************
1123 Wrapper for random().
1124 ****************************************************************************/
1126 long sys_random(void)
1128 #if defined(HAVE_RANDOM)
1129 return (long)random();
1130 #elif defined(HAVE_RAND)
1131 return (long)rand();
1133 DEBUG(0,("Error - no random function available !\n"));
1138 /**************************************************************************
1139 Wrapper for srandom().
1140 ****************************************************************************/
1142 void sys_srandom(unsigned int seed
)
1144 #if defined(HAVE_SRANDOM)
1146 #elif defined(HAVE_SRAND)
1149 DEBUG(0,("Error - no srandom function available !\n"));
1155 #define NGROUPS_MAX 32 /* Guess... */
1158 /**************************************************************************
1159 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
1160 ****************************************************************************/
1162 int groups_max(void)
1164 #if defined(SYSCONF_SC_NGROUPS_MAX)
1165 int ret
= sysconf(_SC_NGROUPS_MAX
);
1166 return (ret
== -1) ? NGROUPS_MAX
: ret
;
1172 /**************************************************************************
1173 Wrap setgroups and getgroups for systems that declare getgroups() as
1174 returning an array of gid_t, but actuall return an array of int.
1175 ****************************************************************************/
1177 #if defined(HAVE_BROKEN_GETGROUPS)
1179 #ifdef HAVE_BROKEN_GETGROUPS
1185 static int sys_broken_getgroups(int setlen
, gid_t
*gidset
)
1192 return getgroups(setlen
, &gid
);
1196 * Broken case. We need to allocate a
1197 * GID_T array of size setlen.
1206 setlen
= groups_max();
1208 if((group_list
= SMB_MALLOC_ARRAY(GID_T
, setlen
)) == NULL
) {
1209 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
1213 if((ngroups
= getgroups(setlen
, group_list
)) < 0) {
1214 int saved_errno
= errno
;
1215 SAFE_FREE(group_list
);
1216 errno
= saved_errno
;
1220 for(i
= 0; i
< ngroups
; i
++)
1221 gidset
[i
] = (gid_t
)group_list
[i
];
1223 SAFE_FREE(group_list
);
1227 static int sys_broken_setgroups(int setlen
, gid_t
*gidset
)
1235 if (setlen
< 0 || setlen
> groups_max()) {
1241 * Broken case. We need to allocate a
1242 * GID_T array of size setlen.
1245 if((group_list
= SMB_MALLOC_ARRAY(GID_T
, setlen
)) == NULL
) {
1246 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
1250 for(i
= 0; i
< setlen
; i
++)
1251 group_list
[i
] = (GID_T
) gidset
[i
];
1253 if(setgroups(setlen
, group_list
) != 0) {
1254 int saved_errno
= errno
;
1255 SAFE_FREE(group_list
);
1256 errno
= saved_errno
;
1260 SAFE_FREE(group_list
);
1264 #endif /* HAVE_BROKEN_GETGROUPS */
1266 /* This is a list of systems that require the first GID passed to setgroups(2)
1267 * to be the effective GID. If your system is one of these, add it here.
1269 #if defined (FREEBSD) || defined (DARWINOS)
1270 #define USE_BSD_SETGROUPS
1273 #if defined(USE_BSD_SETGROUPS)
1274 /* Depending on the particular BSD implementation, the first GID that is
1275 * passed to setgroups(2) will either be ignored or will set the credential's
1276 * effective GID. In either case, the right thing to do is to guarantee that
1277 * gidset[0] is the effective GID.
1279 static int sys_bsd_setgroups(gid_t primary_gid
, int setlen
, const gid_t
*gidset
)
1281 gid_t
*new_gidset
= NULL
;
1285 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
1288 /* No group list, just make sure we are setting the efective GID. */
1290 return setgroups(1, &primary_gid
);
1293 /* If the primary gid is not the first array element, grow the array
1294 * and insert it at the front.
1296 if (gidset
[0] != primary_gid
) {
1297 new_gidset
= SMB_MALLOC_ARRAY(gid_t
, setlen
+ 1);
1298 if (new_gidset
== NULL
) {
1302 memcpy(new_gidset
+ 1, gidset
, (setlen
* sizeof(gid_t
)));
1303 new_gidset
[0] = primary_gid
;
1308 DEBUG(3, ("forced to truncate group list from %d to %d\n",
1313 #if defined(HAVE_BROKEN_GETGROUPS)
1314 ret
= sys_broken_setgroups(setlen
, new_gidset
? new_gidset
: gidset
);
1316 ret
= setgroups(setlen
, new_gidset
? new_gidset
: gidset
);
1321 SAFE_FREE(new_gidset
);
1328 #endif /* USE_BSD_SETGROUPS */
1330 /**************************************************************************
1331 Wrapper for getgroups. Deals with broken (int) case.
1332 ****************************************************************************/
1334 int sys_getgroups(int setlen
, gid_t
*gidset
)
1336 #if defined(HAVE_BROKEN_GETGROUPS)
1337 return sys_broken_getgroups(setlen
, gidset
);
1339 return getgroups(setlen
, gidset
);
1343 /**************************************************************************
1344 Wrapper for setgroups. Deals with broken (int) case and BSD case.
1345 ****************************************************************************/
1347 int sys_setgroups(gid_t
UNUSED(primary_gid
), int setlen
, gid_t
*gidset
)
1349 #if !defined(HAVE_SETGROUPS)
1352 #endif /* HAVE_SETGROUPS */
1354 #if defined(USE_BSD_SETGROUPS)
1355 return sys_bsd_setgroups(primary_gid
, setlen
, gidset
);
1356 #elif defined(HAVE_BROKEN_GETGROUPS)
1357 return sys_broken_setgroups(setlen
, gidset
);
1359 return setgroups(setlen
, gidset
);
1363 /**************************************************************************
1364 Extract a command into an arg list.
1365 ****************************************************************************/
1367 static char **extract_args(TALLOC_CTX
*mem_ctx
, const char *command
)
1376 if (!(trunc_cmd
= talloc_strdup(mem_ctx
, command
))) {
1377 DEBUG(0, ("talloc failed\n"));
1381 if(!(ptr
= strtok_r(trunc_cmd
, " \t", &saveptr
))) {
1382 TALLOC_FREE(trunc_cmd
);
1391 for( argcl
= 1; ptr
; ptr
= strtok_r(NULL
, " \t", &saveptr
))
1394 TALLOC_FREE(trunc_cmd
);
1396 if (!(argl
= TALLOC_ARRAY(mem_ctx
, char *, argcl
+ 1))) {
1401 * Now do the extraction.
1404 if (!(trunc_cmd
= talloc_strdup(mem_ctx
, command
))) {
1408 ptr
= strtok_r(trunc_cmd
, " \t", &saveptr
);
1411 if (!(argl
[i
++] = talloc_strdup(argl
, ptr
))) {
1415 while((ptr
= strtok_r(NULL
, " \t", &saveptr
)) != NULL
) {
1417 if (!(argl
[i
++] = talloc_strdup(argl
, ptr
))) {
1423 TALLOC_FREE(trunc_cmd
);
1427 DEBUG(0, ("talloc failed\n"));
1428 TALLOC_FREE(trunc_cmd
);
1434 /**************************************************************************
1435 Wrapper for popen. Safer as it doesn't search a path.
1436 Modified from the glibc sources.
1437 modified by tridge to return a file descriptor. We must kick our FILE* habit
1438 ****************************************************************************/
1440 typedef struct _popen_list
1444 struct _popen_list
*next
;
1447 static popen_list
*popen_chain
;
1449 int sys_popen(const char *command
)
1451 int parent_end
, child_end
;
1453 popen_list
*entry
= NULL
;
1456 if (pipe(pipe_fds
) < 0)
1459 parent_end
= pipe_fds
[0];
1460 child_end
= pipe_fds
[1];
1467 if((entry
= SMB_MALLOC_P(popen_list
)) == NULL
)
1470 ZERO_STRUCTP(entry
);
1473 * Extract the command and args into a NULL terminated array.
1476 if(!(argl
= extract_args(NULL
, command
)))
1479 entry
->child_pid
= sys_fork();
1481 if (entry
->child_pid
== -1) {
1485 if (entry
->child_pid
== 0) {
1491 int child_std_end
= STDOUT_FILENO
;
1495 if (child_end
!= child_std_end
) {
1496 dup2 (child_end
, child_std_end
);
1501 * POSIX.2: "popen() shall ensure that any streams from previous
1502 * popen() calls that remain open in the parent process are closed
1503 * in the new child process."
1506 for (p
= popen_chain
; p
; p
= p
->next
)
1509 execv(argl
[0], argl
);
1520 /* Link into popen_chain. */
1521 entry
->next
= popen_chain
;
1522 popen_chain
= entry
;
1523 entry
->fd
= parent_end
;
1536 /**************************************************************************
1537 Wrapper for pclose. Modified from the glibc sources.
1538 ****************************************************************************/
1540 int sys_pclose(int fd
)
1543 popen_list
**ptr
= &popen_chain
;
1544 popen_list
*entry
= NULL
;
1548 /* Unlink from popen_chain. */
1549 for ( ; *ptr
!= NULL
; ptr
= &(*ptr
)->next
) {
1550 if ((*ptr
)->fd
== fd
) {
1552 *ptr
= (*ptr
)->next
;
1558 if (status
< 0 || close(entry
->fd
) < 0)
1562 * As Samba is catching and eating child process
1563 * exits we don't really care about the child exit
1564 * code, a -1 with errno = ECHILD will do fine for us.
1568 wait_pid
= sys_waitpid (entry
->child_pid
, &wstatus
, 0);
1569 } while (wait_pid
== -1 && errno
== EINTR
);
1578 /**************************************************************************
1579 Wrapper for Admin Logs.
1580 ****************************************************************************/
1582 void sys_adminlog(int priority
, const char *format_str
, ...)
1586 char *msgbuf
= NULL
;
1588 va_start( ap
, format_str
);
1589 ret
= vasprintf( &msgbuf
, format_str
, ap
);
1595 #if defined(HAVE_SYSLOG)
1596 syslog( priority
, "%s", msgbuf
);
1598 DEBUG(0,("%s", msgbuf
));
1603 /******** Solaris EA helper function prototypes ********/
1604 #ifdef HAVE_ATTROPEN
1605 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1606 static int solaris_write_xattr(int attrfd
, const char *value
, size_t size
);
1607 static ssize_t
solaris_read_xattr(int attrfd
, void *value
, size_t size
);
1608 static ssize_t
solaris_list_xattr(int attrdirfd
, char *list
, size_t size
);
1609 static int solaris_unlinkat(int attrdirfd
, const char *name
);
1610 static int solaris_attropen(const char *path
, const char *attrpath
, int oflag
, mode_t mode
);
1611 static int solaris_openat(int fildes
, const char *path
, int oflag
, mode_t mode
);
1614 /**************************************************************************
1615 Wrappers for extented attribute calls. Based on the Linux package with
1616 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1617 ****************************************************************************/
1619 ssize_t
sys_getxattr (const char *path
, const char *name
, void *value
, size_t size
)
1621 #if defined(HAVE_GETXATTR)
1622 #ifndef XATTR_ADD_OPT
1623 return getxattr(path
, name
, value
, size
);
1626 return getxattr(path
, name
, value
, size
, 0, options
);
1628 #elif defined(HAVE_GETEA)
1629 return getea(path
, name
, value
, size
);
1630 #elif defined(HAVE_EXTATTR_GET_FILE)
1633 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1634 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1635 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1637 * The BSD implementation has a nasty habit of silently truncating
1638 * the returned value to the size of the buffer, so we have to check
1639 * that the buffer is large enough to fit the returned value.
1641 if((retval
=extattr_get_file(path
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1646 if((retval
=extattr_get_file(path
, attrnamespace
, attrname
, value
, size
)) >= 0)
1650 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno
)));
1652 #elif defined(HAVE_ATTR_GET)
1653 int retval
, flags
= 0;
1654 int valuelength
= (int)size
;
1655 char *attrname
= strchr(name
,'.') + 1;
1657 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1659 retval
= attr_get(path
, attrname
, (char *)value
, &valuelength
, flags
);
1661 return retval
? retval
: valuelength
;
1662 #elif defined(HAVE_ATTROPEN)
1664 int attrfd
= solaris_attropen(path
, name
, O_RDONLY
, 0);
1666 ret
= solaris_read_xattr(attrfd
, value
, size
);
1676 ssize_t
sys_lgetxattr (const char *path
, const char *name
, void *value
, size_t size
)
1678 #if defined(HAVE_LGETXATTR)
1679 return lgetxattr(path
, name
, value
, size
);
1680 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1681 int options
= XATTR_NOFOLLOW
;
1682 return getxattr(path
, name
, value
, size
, 0, options
);
1683 #elif defined(HAVE_LGETEA)
1684 return lgetea(path
, name
, value
, size
);
1685 #elif defined(HAVE_EXTATTR_GET_LINK)
1688 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1689 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1690 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1692 if((retval
=extattr_get_link(path
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1697 if((retval
=extattr_get_link(path
, attrnamespace
, attrname
, value
, size
)) >= 0)
1701 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno
)));
1703 #elif defined(HAVE_ATTR_GET)
1704 int retval
, flags
= ATTR_DONTFOLLOW
;
1705 int valuelength
= (int)size
;
1706 char *attrname
= strchr(name
,'.') + 1;
1708 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1710 retval
= attr_get(path
, attrname
, (char *)value
, &valuelength
, flags
);
1712 return retval
? retval
: valuelength
;
1713 #elif defined(HAVE_ATTROPEN)
1715 int attrfd
= solaris_attropen(path
, name
, O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1717 ret
= solaris_read_xattr(attrfd
, value
, size
);
1727 ssize_t
sys_fgetxattr (int filedes
, const char *name
, void *value
, size_t size
)
1729 #if defined(HAVE_FGETXATTR)
1730 #ifndef XATTR_ADD_OPT
1731 return fgetxattr(filedes
, name
, value
, size
);
1734 return fgetxattr(filedes
, name
, value
, size
, 0, options
);
1736 #elif defined(HAVE_FGETEA)
1737 return fgetea(filedes
, name
, value
, size
);
1738 #elif defined(HAVE_EXTATTR_GET_FD)
1741 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1742 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1743 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1745 if((retval
=extattr_get_fd(filedes
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1750 if((retval
=extattr_get_fd(filedes
, attrnamespace
, attrname
, value
, size
)) >= 0)
1754 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno
)));
1756 #elif defined(HAVE_ATTR_GETF)
1757 int retval
, flags
= 0;
1758 int valuelength
= (int)size
;
1759 char *attrname
= strchr(name
,'.') + 1;
1761 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1763 retval
= attr_getf(filedes
, attrname
, (char *)value
, &valuelength
, flags
);
1765 return retval
? retval
: valuelength
;
1766 #elif defined(HAVE_ATTROPEN)
1768 int attrfd
= solaris_openat(filedes
, name
, O_RDONLY
|O_XATTR
, 0);
1770 ret
= solaris_read_xattr(attrfd
, value
, size
);
1780 #if defined(HAVE_EXTATTR_LIST_FILE)
1782 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1790 { EXTATTR_NAMESPACE_SYSTEM
, EXTATTR_PREFIX("system.") },
1791 { EXTATTR_NAMESPACE_USER
, EXTATTR_PREFIX("user.") },
1799 static ssize_t
bsd_attr_list (int type
, extattr_arg arg
, char *list
, size_t size
)
1801 ssize_t list_size
, total_size
= 0;
1804 /* Iterate through extattr(2) namespaces */
1805 for(t
= 0; t
< (sizeof(extattr
)/sizeof(extattr
[0])); t
++) {
1807 #if defined(HAVE_EXTATTR_LIST_FILE)
1809 list_size
= extattr_list_file(arg
.path
, extattr
[t
].space
, list
, size
);
1812 #if defined(HAVE_EXTATTR_LIST_LINK)
1814 list_size
= extattr_list_link(arg
.path
, extattr
[t
].space
, list
, size
);
1817 #if defined(HAVE_EXTATTR_LIST_FD)
1819 list_size
= extattr_list_fd(arg
.filedes
, extattr
[t
].space
, list
, size
);
1826 /* Some error happend. Errno should be set by the previous call */
1832 /* XXX: Call with an empty buffer may be used to calculate
1833 necessary buffer size. Unfortunately, we can't say, how
1834 many attributes were returned, so here is the potential
1835 problem with the emulation.
1838 /* Take the worse case of one char attribute names -
1839 two bytes per name plus one more for sanity.
1841 total_size
+= list_size
+ (list_size
/2 + 1)*extattr
[t
].len
;
1844 /* Count necessary offset to fit namespace prefixes */
1846 for(i
= 0; i
< list_size
; i
+= list
[i
] + 1)
1847 len
+= extattr
[t
].len
;
1849 total_size
+= list_size
+ len
;
1850 /* Buffer is too small to fit the results */
1851 if(total_size
> size
) {
1855 /* Shift results back, so we can prepend prefixes */
1856 buf
= (char *)memmove(list
+ len
, list
, list_size
);
1858 for(i
= 0; i
< list_size
; i
+= len
+ 1) {
1860 strncpy(list
, extattr
[t
].name
, extattr
[t
].len
+ 1);
1861 list
+= extattr
[t
].len
;
1862 strncpy(list
, buf
+ i
+ 1, len
);
1873 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1874 static char attr_buffer
[ATTR_MAX_VALUELEN
];
1876 static ssize_t
irix_attr_list(const char *path
, int filedes
, char *list
, size_t size
, int flags
)
1878 int retval
= 0, index
;
1879 attrlist_cursor_t
*cursor
= 0;
1881 attrlist_t
* al
= (attrlist_t
*)attr_buffer
;
1883 size_t ent_size
, left
= size
;
1888 retval
= attr_listf(filedes
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1890 retval
= attr_list(path
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1892 for (index
= 0; index
< al
->al_count
; index
++) {
1893 ae
= ATTR_ENTRY(attr_buffer
, index
);
1894 ent_size
= strlen(ae
->a_name
) + sizeof("user.");
1895 if (left
>= ent_size
) {
1896 strncpy(bp
, "user.", sizeof("user."));
1897 strncat(bp
, ae
->a_name
, ent_size
- sizeof("user."));
1905 total_size
+= ent_size
;
1907 if (al
->al_more
== 0) break;
1914 retval
= attr_listf(filedes
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1916 retval
= attr_list(path
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1918 for (index
= 0; index
< al
->al_count
; index
++) {
1919 ae
= ATTR_ENTRY(attr_buffer
, index
);
1920 ent_size
= strlen(ae
->a_name
) + sizeof("system.");
1921 if (left
>= ent_size
) {
1922 strncpy(bp
, "system.", sizeof("system."));
1923 strncat(bp
, ae
->a_name
, ent_size
- sizeof("system."));
1931 total_size
+= ent_size
;
1933 if (al
->al_more
== 0) break;
1936 return (ssize_t
)(retval
? retval
: total_size
);
1941 ssize_t
sys_listxattr (const char *path
, char *list
, size_t size
)
1943 #if defined(HAVE_LISTXATTR)
1944 #ifndef XATTR_ADD_OPT
1945 return listxattr(path
, list
, size
);
1948 return listxattr(path
, list
, size
, options
);
1950 #elif defined(HAVE_LISTEA)
1951 return listea(path
, list
, size
);
1952 #elif defined(HAVE_EXTATTR_LIST_FILE)
1955 return bsd_attr_list(0, arg
, list
, size
);
1956 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1957 return irix_attr_list(path
, 0, list
, size
, 0);
1958 #elif defined(HAVE_ATTROPEN)
1960 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
, 0);
1961 if (attrdirfd
>= 0) {
1962 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1972 ssize_t
sys_llistxattr (const char *path
, char *list
, size_t size
)
1974 #if defined(HAVE_LLISTXATTR)
1975 return llistxattr(path
, list
, size
);
1976 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1977 int options
= XATTR_NOFOLLOW
;
1978 return listxattr(path
, list
, size
, options
);
1979 #elif defined(HAVE_LLISTEA)
1980 return llistea(path
, list
, size
);
1981 #elif defined(HAVE_EXTATTR_LIST_LINK)
1984 return bsd_attr_list(1, arg
, list
, size
);
1985 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1986 return irix_attr_list(path
, 0, list
, size
, ATTR_DONTFOLLOW
);
1987 #elif defined(HAVE_ATTROPEN)
1989 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1990 if (attrdirfd
>= 0) {
1991 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
2001 ssize_t
sys_flistxattr (int filedes
, char *list
, size_t size
)
2003 #if defined(HAVE_FLISTXATTR)
2004 #ifndef XATTR_ADD_OPT
2005 return flistxattr(filedes
, list
, size
);
2008 return flistxattr(filedes
, list
, size
, options
);
2010 #elif defined(HAVE_FLISTEA)
2011 return flistea(filedes
, list
, size
);
2012 #elif defined(HAVE_EXTATTR_LIST_FD)
2014 arg
.filedes
= filedes
;
2015 return bsd_attr_list(2, arg
, list
, size
);
2016 #elif defined(HAVE_ATTR_LISTF)
2017 return irix_attr_list(NULL
, filedes
, list
, size
, 0);
2018 #elif defined(HAVE_ATTROPEN)
2020 int attrdirfd
= solaris_openat(filedes
, ".", O_RDONLY
|O_XATTR
, 0);
2021 if (attrdirfd
>= 0) {
2022 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
2032 int sys_removexattr (const char *path
, const char *name
)
2034 #if defined(HAVE_REMOVEXATTR)
2035 #ifndef XATTR_ADD_OPT
2036 return removexattr(path
, name
);
2039 return removexattr(path
, name
, options
);
2041 #elif defined(HAVE_REMOVEEA)
2042 return removeea(path
, name
);
2043 #elif defined(HAVE_EXTATTR_DELETE_FILE)
2045 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2046 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2047 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2049 return extattr_delete_file(path
, attrnamespace
, attrname
);
2050 #elif defined(HAVE_ATTR_REMOVE)
2052 char *attrname
= strchr(name
,'.') + 1;
2054 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
2056 return attr_remove(path
, attrname
, flags
);
2057 #elif defined(HAVE_ATTROPEN)
2059 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
, 0);
2060 if (attrdirfd
>= 0) {
2061 ret
= solaris_unlinkat(attrdirfd
, name
);
2071 int sys_lremovexattr (const char *path
, const char *name
)
2073 #if defined(HAVE_LREMOVEXATTR)
2074 return lremovexattr(path
, name
);
2075 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
2076 int options
= XATTR_NOFOLLOW
;
2077 return removexattr(path
, name
, options
);
2078 #elif defined(HAVE_LREMOVEEA)
2079 return lremoveea(path
, name
);
2080 #elif defined(HAVE_EXTATTR_DELETE_LINK)
2082 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2083 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2084 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2086 return extattr_delete_link(path
, attrnamespace
, attrname
);
2087 #elif defined(HAVE_ATTR_REMOVE)
2088 int flags
= ATTR_DONTFOLLOW
;
2089 char *attrname
= strchr(name
,'.') + 1;
2091 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
2093 return attr_remove(path
, attrname
, flags
);
2094 #elif defined(HAVE_ATTROPEN)
2096 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
2097 if (attrdirfd
>= 0) {
2098 ret
= solaris_unlinkat(attrdirfd
, name
);
2108 int sys_fremovexattr (int filedes
, const char *name
)
2110 #if defined(HAVE_FREMOVEXATTR)
2111 #ifndef XATTR_ADD_OPT
2112 return fremovexattr(filedes
, name
);
2115 return fremovexattr(filedes
, name
, options
);
2117 #elif defined(HAVE_FREMOVEEA)
2118 return fremoveea(filedes
, name
);
2119 #elif defined(HAVE_EXTATTR_DELETE_FD)
2121 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2122 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2123 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2125 return extattr_delete_fd(filedes
, attrnamespace
, attrname
);
2126 #elif defined(HAVE_ATTR_REMOVEF)
2128 char *attrname
= strchr(name
,'.') + 1;
2130 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
2132 return attr_removef(filedes
, attrname
, flags
);
2133 #elif defined(HAVE_ATTROPEN)
2135 int attrdirfd
= solaris_openat(filedes
, ".", O_RDONLY
|O_XATTR
, 0);
2136 if (attrdirfd
>= 0) {
2137 ret
= solaris_unlinkat(attrdirfd
, name
);
2147 int sys_setxattr (const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
2149 #if defined(HAVE_SETXATTR)
2150 #ifndef XATTR_ADD_OPT
2151 return setxattr(path
, name
, value
, size
, flags
);
2154 return setxattr(path
, name
, value
, size
, 0, options
);
2156 #elif defined(HAVE_SETEA)
2157 return setea(path
, name
, value
, size
, flags
);
2158 #elif defined(HAVE_EXTATTR_SET_FILE)
2161 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2162 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2163 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2165 /* Check attribute existence */
2166 retval
= extattr_get_file(path
, attrnamespace
, attrname
, NULL
, 0);
2168 /* REPLACE attribute, that doesn't exist */
2169 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
2173 /* Ignore other errors */
2176 /* CREATE attribute, that already exists */
2177 if (flags
& XATTR_CREATE
) {
2183 retval
= extattr_set_file(path
, attrnamespace
, attrname
, value
, size
);
2184 return (retval
< 0) ? -1 : 0;
2185 #elif defined(HAVE_ATTR_SET)
2187 char *attrname
= strchr(name
,'.') + 1;
2189 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
2190 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
2191 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
2193 return attr_set(path
, attrname
, (const char *)value
, size
, myflags
);
2194 #elif defined(HAVE_ATTROPEN)
2196 int myflags
= O_RDWR
;
2198 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
2199 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
2200 attrfd
= solaris_attropen(path
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
2202 ret
= solaris_write_xattr(attrfd
, value
, size
);
2212 int sys_lsetxattr (const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
2214 #if defined(HAVE_LSETXATTR)
2215 return lsetxattr(path
, name
, value
, size
, flags
);
2216 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2217 int options
= XATTR_NOFOLLOW
;
2218 return setxattr(path
, name
, value
, size
, 0, options
);
2219 #elif defined(LSETEA)
2220 return lsetea(path
, name
, value
, size
, flags
);
2221 #elif defined(HAVE_EXTATTR_SET_LINK)
2224 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2225 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2226 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2228 /* Check attribute existence */
2229 retval
= extattr_get_link(path
, attrnamespace
, attrname
, NULL
, 0);
2231 /* REPLACE attribute, that doesn't exist */
2232 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
2236 /* Ignore other errors */
2239 /* CREATE attribute, that already exists */
2240 if (flags
& XATTR_CREATE
) {
2247 retval
= extattr_set_link(path
, attrnamespace
, attrname
, value
, size
);
2248 return (retval
< 0) ? -1 : 0;
2249 #elif defined(HAVE_ATTR_SET)
2250 int myflags
= ATTR_DONTFOLLOW
;
2251 char *attrname
= strchr(name
,'.') + 1;
2253 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
2254 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
2255 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
2257 return attr_set(path
, attrname
, (const char *)value
, size
, myflags
);
2258 #elif defined(HAVE_ATTROPEN)
2260 int myflags
= O_RDWR
| AT_SYMLINK_NOFOLLOW
;
2262 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
2263 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
2264 attrfd
= solaris_attropen(path
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
2266 ret
= solaris_write_xattr(attrfd
, value
, size
);
2276 int sys_fsetxattr (int filedes
, const char *name
, const void *value
, size_t size
, int flags
)
2278 #if defined(HAVE_FSETXATTR)
2279 #ifndef XATTR_ADD_OPT
2280 return fsetxattr(filedes
, name
, value
, size
, flags
);
2283 return fsetxattr(filedes
, name
, value
, size
, 0, options
);
2285 #elif defined(HAVE_FSETEA)
2286 return fsetea(filedes
, name
, value
, size
, flags
);
2287 #elif defined(HAVE_EXTATTR_SET_FD)
2290 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2291 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2292 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2294 /* Check attribute existence */
2295 retval
= extattr_get_fd(filedes
, attrnamespace
, attrname
, NULL
, 0);
2297 /* REPLACE attribute, that doesn't exist */
2298 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
2302 /* Ignore other errors */
2305 /* CREATE attribute, that already exists */
2306 if (flags
& XATTR_CREATE
) {
2312 retval
= extattr_set_fd(filedes
, attrnamespace
, attrname
, value
, size
);
2313 return (retval
< 0) ? -1 : 0;
2314 #elif defined(HAVE_ATTR_SETF)
2316 char *attrname
= strchr(name
,'.') + 1;
2318 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
2319 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
2320 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
2322 return attr_setf(filedes
, attrname
, (const char *)value
, size
, myflags
);
2323 #elif defined(HAVE_ATTROPEN)
2325 int myflags
= O_RDWR
| O_XATTR
;
2327 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
2328 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
2329 attrfd
= solaris_openat(filedes
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
2331 ret
= solaris_write_xattr(attrfd
, value
, size
);
2341 /**************************************************************************
2342 helper functions for Solaris' EA support
2343 ****************************************************************************/
2344 #ifdef HAVE_ATTROPEN
2345 static ssize_t
solaris_read_xattr(int attrfd
, void *value
, size_t size
)
2349 if (fstat(attrfd
, &sbuf
) == -1) {
2354 /* This is to return the current size of the named extended attribute */
2356 return sbuf
.st_size
;
2359 /* check size and read xattr */
2360 if (sbuf
.st_size
> size
) {
2365 return read(attrfd
, value
, sbuf
.st_size
);
2368 static ssize_t
solaris_list_xattr(int attrdirfd
, char *list
, size_t size
)
2373 int newfd
= dup(attrdirfd
);
2374 /* CAUTION: The originating file descriptor should not be
2375 used again following the call to fdopendir().
2376 For that reason we dup() the file descriptor
2377 here to make things more clear. */
2378 dirp
= fdopendir(newfd
);
2380 while ((de
= readdir(dirp
))) {
2381 size_t listlen
= strlen(de
->d_name
);
2382 if (!strcmp(de
->d_name
, ".") || !strcmp(de
->d_name
, "..")) {
2383 /* we don't want "." and ".." here: */
2384 DEBUG(10,("skipped EA %s\n",de
->d_name
));
2389 /* return the current size of the list of extended attribute names*/
2392 /* check size and copy entrieѕ + nul into list. */
2393 if ((len
+ listlen
+ 1) > size
) {
2398 safe_strcpy(list
+ len
, de
->d_name
, listlen
);
2406 if (closedir(dirp
) == -1) {
2407 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno
)));
2413 static int solaris_unlinkat(int attrdirfd
, const char *name
)
2415 if (unlinkat(attrdirfd
, name
, 0) == -1) {
2416 if (errno
== ENOENT
) {
2424 static int solaris_attropen(const char *path
, const char *attrpath
, int oflag
, mode_t mode
)
2426 int filedes
= attropen(path
, attrpath
, oflag
, mode
);
2427 if (filedes
== -1) {
2428 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path
,attrpath
,strerror(errno
)));
2429 if (errno
== EINVAL
) {
2438 static int solaris_openat(int fildes
, const char *path
, int oflag
, mode_t mode
)
2440 int filedes
= openat(fildes
, path
, oflag
, mode
);
2441 if (filedes
== -1) {
2442 DEBUG(10,("openat FAILED: fd: %d, path: %s, errno: %s\n",filedes
,path
,strerror(errno
)));
2443 if (errno
== EINVAL
) {
2452 static int solaris_write_xattr(int attrfd
, const char *value
, size_t size
)
2454 if ((ftruncate(attrfd
, 0) == 0) && (write(attrfd
, value
, size
) == size
)) {
2457 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2461 #endif /*HAVE_ATTROPEN*/
2464 /****************************************************************************
2465 Return the major devicenumber for UNIX extensions.
2466 ****************************************************************************/
2468 uint32
unix_dev_major(SMB_DEV_T dev
)
2470 #if defined(HAVE_DEVICE_MAJOR_FN)
2471 return (uint32
)major(dev
);
2473 return (uint32
)(dev
>> 8);
2477 /****************************************************************************
2478 Return the minor devicenumber for UNIX extensions.
2479 ****************************************************************************/
2481 uint32
unix_dev_minor(SMB_DEV_T dev
)
2483 #if defined(HAVE_DEVICE_MINOR_FN)
2484 return (uint32
)minor(dev
);
2486 return (uint32
)(dev
& 0xff);
2490 #if defined(WITH_AIO)
2492 /*******************************************************************
2493 An aio_read wrapper that will deal with 64-bit sizes.
2494 ********************************************************************/
2496 int sys_aio_read(SMB_STRUCT_AIOCB
*aiocb
)
2498 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2499 return aio_read64(aiocb
);
2500 #elif defined(HAVE_AIO_READ)
2501 return aio_read(aiocb
);
2508 /*******************************************************************
2509 An aio_write wrapper that will deal with 64-bit sizes.
2510 ********************************************************************/
2512 int sys_aio_write(SMB_STRUCT_AIOCB
*aiocb
)
2514 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2515 return aio_write64(aiocb
);
2516 #elif defined(HAVE_AIO_WRITE)
2517 return aio_write(aiocb
);
2524 /*******************************************************************
2525 An aio_return wrapper that will deal with 64-bit sizes.
2526 ********************************************************************/
2528 ssize_t
sys_aio_return(SMB_STRUCT_AIOCB
*aiocb
)
2530 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2531 return aio_return64(aiocb
);
2532 #elif defined(HAVE_AIO_RETURN)
2533 return aio_return(aiocb
);
2540 /*******************************************************************
2541 An aio_cancel wrapper that will deal with 64-bit sizes.
2542 ********************************************************************/
2544 int sys_aio_cancel(int fd
, SMB_STRUCT_AIOCB
*aiocb
)
2546 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2547 return aio_cancel64(fd
, aiocb
);
2548 #elif defined(HAVE_AIO_CANCEL)
2549 return aio_cancel(fd
, aiocb
);
2556 /*******************************************************************
2557 An aio_error wrapper that will deal with 64-bit sizes.
2558 ********************************************************************/
2560 int sys_aio_error(const SMB_STRUCT_AIOCB
*aiocb
)
2562 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2563 return aio_error64(aiocb
);
2564 #elif defined(HAVE_AIO_ERROR)
2565 return aio_error(aiocb
);
2572 /*******************************************************************
2573 An aio_fsync wrapper that will deal with 64-bit sizes.
2574 ********************************************************************/
2576 int sys_aio_fsync(int op
, SMB_STRUCT_AIOCB
*aiocb
)
2578 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2579 return aio_fsync64(op
, aiocb
);
2580 #elif defined(HAVE_AIO_FSYNC)
2581 return aio_fsync(op
, aiocb
);
2588 /*******************************************************************
2589 An aio_fsync wrapper that will deal with 64-bit sizes.
2590 ********************************************************************/
2592 int sys_aio_suspend(const SMB_STRUCT_AIOCB
* const cblist
[], int n
, const struct timespec
*timeout
)
2594 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2595 return aio_suspend64(cblist
, n
, timeout
);
2596 #elif defined(HAVE_AIO_FSYNC)
2597 return aio_suspend(cblist
, n
, timeout
);
2603 #else /* !WITH_AIO */
2605 int sys_aio_read(SMB_STRUCT_AIOCB
*aiocb
)
2611 int sys_aio_write(SMB_STRUCT_AIOCB
*aiocb
)
2617 ssize_t
sys_aio_return(SMB_STRUCT_AIOCB
*aiocb
)
2623 int sys_aio_cancel(int fd
, SMB_STRUCT_AIOCB
*aiocb
)
2629 int sys_aio_error(const SMB_STRUCT_AIOCB
*aiocb
)
2635 int sys_aio_fsync(int op
, SMB_STRUCT_AIOCB
*aiocb
)
2641 int sys_aio_suspend(const SMB_STRUCT_AIOCB
* const cblist
[], int n
, const struct timespec
*timeout
)
2646 #endif /* WITH_AIO */