s3:net registry import: check return values + codecleanup
[Samba/gebeck_regimport.git] / source3 / lib / system.c
blob3daa041a277e13d229d5d339e108ae109aa7dc93
1 /*
2 Unix SMB/CIFS implementation.
3 Samba system utilities
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/>.
23 #include "includes.h"
24 #include "system/syslog.h"
25 #include "system/capability.h"
26 #include "system/passwd.h"
27 #include "system/filesys.h"
29 #ifdef HAVE_SYS_SYSCTL_H
30 #include <sys/sysctl.h>
31 #endif
33 #ifdef HAVE_SYS_PRCTL_H
34 #include <sys/prctl.h>
35 #endif
38 The idea is that this file will eventually have wrappers around all
39 important system calls in samba. The aims are:
41 - to enable easier porting by putting OS dependent stuff in here
43 - to allow for hooks into other "pseudo-filesystems"
45 - to allow easier integration of things like the japanese extensions
47 - to support the philosophy of Samba to expose the features of
48 the OS within the SMB model. In general whatever file/printer/variable
49 expansions/etc make sense to the OS should be acceptable to Samba.
54 /*******************************************************************
55 A read wrapper that will deal with EINTR.
56 ********************************************************************/
58 ssize_t sys_read(int fd, void *buf, size_t count)
60 ssize_t ret;
62 do {
63 ret = read(fd, buf, count);
64 #if defined(EWOULDBLOCK)
65 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
66 #else
67 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
68 #endif
69 return ret;
72 /*******************************************************************
73 A write wrapper that will deal with EINTR.
74 ********************************************************************/
76 ssize_t sys_write(int fd, const void *buf, size_t count)
78 ssize_t ret;
80 do {
81 ret = write(fd, buf, count);
82 #if defined(EWOULDBLOCK)
83 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
84 #else
85 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
86 #endif
87 return ret;
90 /*******************************************************************
91 A writev wrapper that will deal with EINTR.
92 ********************************************************************/
94 ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
96 ssize_t ret;
98 #if 0
99 /* Try to confuse write_data_iov a bit */
100 if ((random() % 5) == 0) {
101 return sys_write(fd, iov[0].iov_base, iov[0].iov_len);
103 if (iov[0].iov_len > 1) {
104 return sys_write(fd, iov[0].iov_base,
105 (random() % (iov[0].iov_len-1)) + 1);
107 #endif
109 do {
110 ret = writev(fd, iov, iovcnt);
111 #if defined(EWOULDBLOCK)
112 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
113 #else
114 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
115 #endif
116 return ret;
119 /*******************************************************************
120 A pread wrapper that will deal with EINTR
121 ********************************************************************/
123 #if defined(HAVE_PREAD)
124 ssize_t sys_pread(int fd, void *buf, size_t count, off_t off)
126 ssize_t ret;
128 do {
129 ret = pread(fd, buf, count, off);
130 } while (ret == -1 && errno == EINTR);
131 return ret;
133 #endif
135 /*******************************************************************
136 A write wrapper that will deal with EINTR
137 ********************************************************************/
139 #if defined(HAVE_PWRITE)
140 ssize_t sys_pwrite(int fd, const void *buf, size_t count, off_t off)
142 ssize_t ret;
144 do {
145 ret = pwrite(fd, buf, count, off);
146 } while (ret == -1 && errno == EINTR);
147 return ret;
149 #endif
151 /*******************************************************************
152 A send wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
153 ********************************************************************/
155 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
157 ssize_t ret;
159 do {
160 ret = send(s, msg, len, flags);
161 #if defined(EWOULDBLOCK)
162 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
163 #else
164 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
165 #endif
166 return ret;
169 /*******************************************************************
170 A recvfrom wrapper that will deal with EINTR.
171 ********************************************************************/
173 ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
175 ssize_t ret;
177 do {
178 ret = recvfrom(s, buf, len, flags, from, fromlen);
179 #if defined(EWOULDBLOCK)
180 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
181 #else
182 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
183 #endif
184 return ret;
187 /*******************************************************************
188 A fcntl wrapper that will deal with EINTR.
189 ********************************************************************/
191 int sys_fcntl_ptr(int fd, int cmd, void *arg)
193 int ret;
195 do {
196 ret = fcntl(fd, cmd, arg);
197 } while (ret == -1 && errno == EINTR);
198 return ret;
201 /*******************************************************************
202 A fcntl wrapper that will deal with EINTR.
203 ********************************************************************/
205 int sys_fcntl_long(int fd, int cmd, long arg)
207 int ret;
209 do {
210 ret = fcntl(fd, cmd, arg);
211 } while (ret == -1 && errno == EINTR);
212 return ret;
215 /****************************************************************************
216 Get/Set all the possible time fields from a stat struct as a timespec.
217 ****************************************************************************/
219 static struct timespec get_atimespec(const struct stat *pst)
221 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
222 struct timespec ret;
224 /* Old system - no ns timestamp. */
225 ret.tv_sec = pst->st_atime;
226 ret.tv_nsec = 0;
227 return ret;
228 #else
229 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
230 return pst->st_atim;
231 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
232 struct timespec ret;
233 ret.tv_sec = pst->st_atime;
234 ret.tv_nsec = pst->st_atimensec;
235 return ret;
236 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
237 struct timespec ret;
238 ret.tv_sec = pst->st_atime;
239 ret.tv_nsec = pst->st_atime_n;
240 return ret;
241 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
242 struct timespec ret;
243 ret.tv_sec = pst->st_atime;
244 ret.tv_nsec = pst->st_uatime * 1000;
245 return ret;
246 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
247 return pst->st_atimespec;
248 #else
249 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
250 #endif
251 #endif
254 static struct timespec get_mtimespec(const struct stat *pst)
256 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
257 struct timespec ret;
259 /* Old system - no ns timestamp. */
260 ret.tv_sec = pst->st_mtime;
261 ret.tv_nsec = 0;
262 return ret;
263 #else
264 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
265 return pst->st_mtim;
266 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
267 struct timespec ret;
268 ret.tv_sec = pst->st_mtime;
269 ret.tv_nsec = pst->st_mtimensec;
270 return ret;
271 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
272 struct timespec ret;
273 ret.tv_sec = pst->st_mtime;
274 ret.tv_nsec = pst->st_mtime_n;
275 return ret;
276 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
277 struct timespec ret;
278 ret.tv_sec = pst->st_mtime;
279 ret.tv_nsec = pst->st_umtime * 1000;
280 return ret;
281 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
282 return pst->st_mtimespec;
283 #else
284 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
285 #endif
286 #endif
289 static struct timespec get_ctimespec(const struct stat *pst)
291 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
292 struct timespec ret;
294 /* Old system - no ns timestamp. */
295 ret.tv_sec = pst->st_ctime;
296 ret.tv_nsec = 0;
297 return ret;
298 #else
299 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
300 return pst->st_ctim;
301 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
302 struct timespec ret;
303 ret.tv_sec = pst->st_ctime;
304 ret.tv_nsec = pst->st_ctimensec;
305 return ret;
306 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
307 struct timespec ret;
308 ret.tv_sec = pst->st_ctime;
309 ret.tv_nsec = pst->st_ctime_n;
310 return ret;
311 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
312 struct timespec ret;
313 ret.tv_sec = pst->st_ctime;
314 ret.tv_nsec = pst->st_uctime * 1000;
315 return ret;
316 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
317 return pst->st_ctimespec;
318 #else
319 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
320 #endif
321 #endif
324 /****************************************************************************
325 Return the best approximation to a 'create time' under UNIX from a stat
326 structure.
327 ****************************************************************************/
329 static struct timespec calc_create_time_stat(const struct stat *st)
331 struct timespec ret, ret1;
332 struct timespec c_time = get_ctimespec(st);
333 struct timespec m_time = get_mtimespec(st);
334 struct timespec a_time = get_atimespec(st);
336 ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
337 ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
339 if(!null_timespec(ret1)) {
340 return ret1;
344 * One of ctime, mtime or atime was zero (probably atime).
345 * Just return MIN(ctime, mtime).
347 return ret;
350 /****************************************************************************
351 Return the best approximation to a 'create time' under UNIX from a stat_ex
352 structure.
353 ****************************************************************************/
355 static struct timespec calc_create_time_stat_ex(const struct stat_ex *st)
357 struct timespec ret, ret1;
358 struct timespec c_time = st->st_ex_ctime;
359 struct timespec m_time = st->st_ex_mtime;
360 struct timespec a_time = st->st_ex_atime;
362 ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
363 ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
365 if(!null_timespec(ret1)) {
366 return ret1;
370 * One of ctime, mtime or atime was zero (probably atime).
371 * Just return MIN(ctime, mtime).
373 return ret;
376 /****************************************************************************
377 Return the 'create time' from a stat struct if it exists (birthtime) or else
378 use the best approximation.
379 ****************************************************************************/
381 static void make_create_timespec(const struct stat *pst, struct stat_ex *dst,
382 bool fake_dir_create_times)
384 if (S_ISDIR(pst->st_mode) && fake_dir_create_times) {
385 dst->st_ex_btime.tv_sec = 315493200L; /* 1/1/1980 */
386 dst->st_ex_btime.tv_nsec = 0;
389 dst->st_ex_calculated_birthtime = false;
391 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
392 dst->st_ex_btime = pst->st_birthtimespec;
393 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
394 dst->st_ex_btime.tv_sec = pst->st_birthtime;
395 dst->st_ex_btime.tv_nsec = pst->st_birthtimenspec;
396 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
397 dst->st_ex_btime.tv_sec = pst->st_birthtime;
398 dst->st_ex_btime.tv_nsec = 0;
399 #else
400 dst->st_ex_btime = calc_create_time_stat(pst);
401 dst->st_ex_calculated_birthtime = true;
402 #endif
404 /* Deal with systems that don't initialize birthtime correctly.
405 * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
407 if (null_timespec(dst->st_ex_btime)) {
408 dst->st_ex_btime = calc_create_time_stat(pst);
409 dst->st_ex_calculated_birthtime = true;
413 /****************************************************************************
414 If we update a timestamp in a stat_ex struct we may have to recalculate
415 the birthtime. For now only implement this for write time, but we may
416 also need to do it for atime and ctime. JRA.
417 ****************************************************************************/
419 void update_stat_ex_mtime(struct stat_ex *dst,
420 struct timespec write_ts)
422 dst->st_ex_mtime = write_ts;
424 /* We may have to recalculate btime. */
425 if (dst->st_ex_calculated_birthtime) {
426 dst->st_ex_btime = calc_create_time_stat_ex(dst);
430 void update_stat_ex_create_time(struct stat_ex *dst,
431 struct timespec create_time)
433 dst->st_ex_btime = create_time;
434 dst->st_ex_calculated_birthtime = false;
437 void init_stat_ex_from_stat (struct stat_ex *dst,
438 const struct stat *src,
439 bool fake_dir_create_times)
441 dst->st_ex_dev = src->st_dev;
442 dst->st_ex_ino = src->st_ino;
443 dst->st_ex_mode = src->st_mode;
444 dst->st_ex_nlink = src->st_nlink;
445 dst->st_ex_uid = src->st_uid;
446 dst->st_ex_gid = src->st_gid;
447 dst->st_ex_rdev = src->st_rdev;
448 dst->st_ex_size = src->st_size;
449 dst->st_ex_atime = get_atimespec(src);
450 dst->st_ex_mtime = get_mtimespec(src);
451 dst->st_ex_ctime = get_ctimespec(src);
452 make_create_timespec(src, dst, fake_dir_create_times);
453 #ifdef HAVE_STAT_ST_BLKSIZE
454 dst->st_ex_blksize = src->st_blksize;
455 #else
456 dst->st_ex_blksize = STAT_ST_BLOCKSIZE;
457 #endif
459 #ifdef HAVE_STAT_ST_BLOCKS
460 dst->st_ex_blocks = src->st_blocks;
461 #else
462 dst->st_ex_blocks = src->st_size / dst->st_ex_blksize + 1;
463 #endif
465 #ifdef HAVE_STAT_ST_FLAGS
466 dst->st_ex_flags = src->st_flags;
467 #else
468 dst->st_ex_flags = 0;
469 #endif
472 /*******************************************************************
473 A stat() wrapper.
474 ********************************************************************/
476 int sys_stat(const char *fname, SMB_STRUCT_STAT *sbuf,
477 bool fake_dir_create_times)
479 int ret;
480 struct stat statbuf;
481 ret = stat(fname, &statbuf);
482 if (ret == 0) {
483 /* we always want directories to appear zero size */
484 if (S_ISDIR(statbuf.st_mode)) {
485 statbuf.st_size = 0;
487 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
489 return ret;
492 /*******************************************************************
493 An fstat() wrapper.
494 ********************************************************************/
496 int sys_fstat(int fd, SMB_STRUCT_STAT *sbuf, bool fake_dir_create_times)
498 int ret;
499 struct stat statbuf;
500 ret = fstat(fd, &statbuf);
501 if (ret == 0) {
502 /* we always want directories to appear zero size */
503 if (S_ISDIR(statbuf.st_mode)) {
504 statbuf.st_size = 0;
506 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
508 return ret;
511 /*******************************************************************
512 An lstat() wrapper.
513 ********************************************************************/
515 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf,
516 bool fake_dir_create_times)
518 int ret;
519 struct stat statbuf;
520 ret = lstat(fname, &statbuf);
521 if (ret == 0) {
522 /* we always want directories to appear zero size */
523 if (S_ISDIR(statbuf.st_mode)) {
524 statbuf.st_size = 0;
526 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
528 return ret;
531 /*******************************************************************
532 An posix_fallocate() wrapper.
533 ********************************************************************/
534 int sys_posix_fallocate(int fd, off_t offset, off_t len)
536 #if defined(HAVE_POSIX_FALLOCATE) && !defined(HAVE_BROKEN_POSIX_FALLOCATE)
537 return posix_fallocate(fd, offset, len);
538 #elif defined(F_RESVSP64)
539 /* this handles XFS on IRIX */
540 struct flock64 fl;
541 off_t new_len = offset + len;
542 int ret;
543 struct stat64 sbuf;
545 /* unlikely to get a too large file on a 64bit system but ... */
546 if (new_len < 0)
547 return EFBIG;
549 fl.l_whence = SEEK_SET;
550 fl.l_start = offset;
551 fl.l_len = len;
553 ret=fcntl(fd, F_RESVSP64, &fl);
555 if (ret != 0)
556 return errno;
558 /* Make sure the file gets enlarged after we allocated space: */
559 fstat64(fd, &sbuf);
560 if (new_len > sbuf.st_size)
561 ftruncate64(fd, new_len);
562 return 0;
563 #else
564 return ENOSYS;
565 #endif
568 /*******************************************************************
569 An fallocate() function that matches the semantics of the Linux one.
570 ********************************************************************/
572 #ifdef HAVE_LINUX_FALLOC_H
573 #include <linux/falloc.h>
574 #endif
576 int sys_fallocate(int fd, enum vfs_fallocate_mode mode, off_t offset, off_t len)
578 #if defined(HAVE_LINUX_FALLOCATE64) || defined(HAVE_LINUX_FALLOCATE)
579 int lmode;
580 switch (mode) {
581 case VFS_FALLOCATE_EXTEND_SIZE:
582 lmode = 0;
583 break;
584 case VFS_FALLOCATE_KEEP_SIZE:
585 lmode = FALLOC_FL_KEEP_SIZE;
586 break;
587 default:
588 errno = EINVAL;
589 return -1;
591 #if defined(HAVE_LINUX_FALLOCATE)
592 return fallocate(fd, lmode, offset, len);
593 #endif
594 #else
595 /* TODO - plumb in fallocate from other filesysetms like VXFS etc. JRA. */
596 errno = ENOSYS;
597 return -1;
598 #endif
601 #if HAVE_KERNEL_SHARE_MODES
602 #ifndef LOCK_MAND
603 #define LOCK_MAND 32 /* This is a mandatory flock */
604 #define LOCK_READ 64 /* ... Which allows concurrent read operations */
605 #define LOCK_WRITE 128 /* ... Which allows concurrent write operations */
606 #define LOCK_RW 192 /* ... Which allows concurrent read & write ops */
607 #endif
608 #endif
610 /*******************************************************************
611 A flock() wrapper that will perform the kernel flock.
612 ********************************************************************/
614 void kernel_flock(int fd, uint32 share_mode, uint32 access_mask)
616 #if HAVE_KERNEL_SHARE_MODES
617 int kernel_mode = 0;
618 if (share_mode == FILE_SHARE_WRITE) {
619 kernel_mode = LOCK_MAND|LOCK_WRITE;
620 } else if (share_mode == FILE_SHARE_READ) {
621 kernel_mode = LOCK_MAND|LOCK_READ;
622 } else if (share_mode == FILE_SHARE_NONE) {
623 kernel_mode = LOCK_MAND;
625 if (kernel_mode) {
626 flock(fd, kernel_mode);
628 #endif
634 /*******************************************************************
635 An fdopendir wrapper.
636 Ugly hack - we need dirfd for this to work correctly in the
637 calling code.. JRA.
638 ********************************************************************/
640 DIR *sys_fdopendir(int fd)
642 #if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
643 return fdopendir(fd);
644 #else
645 errno = ENOSYS;
646 return NULL;
647 #endif
650 /*******************************************************************
651 An mknod() wrapper.
652 ********************************************************************/
654 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
656 #if defined(HAVE_MKNOD)
657 return mknod(path, mode, dev);
658 #else
659 /* No mknod system call. */
660 errno = ENOSYS;
661 return -1;
662 #endif
665 /*******************************************************************
666 The wait() calls vary between systems
667 ********************************************************************/
669 int sys_waitpid(pid_t pid,int *status,int options)
671 #ifdef HAVE_WAITPID
672 return waitpid(pid,status,options);
673 #else /* HAVE_WAITPID */
674 return wait4(pid, status, options, NULL);
675 #endif /* HAVE_WAITPID */
678 /*******************************************************************
679 System wrapper for getwd. Always returns MALLOC'ed memory, or NULL
680 on error (malloc fail usually).
681 ********************************************************************/
683 char *sys_getwd(void)
685 #ifdef GETCWD_TAKES_NULL
686 return getcwd(NULL, 0);
687 #elif HAVE_GETCWD
688 char *wd = NULL, *s = NULL;
689 size_t allocated = PATH_MAX;
691 while (1) {
692 s = SMB_REALLOC_ARRAY(s, char, allocated);
693 if (s == NULL) {
694 return NULL;
696 wd = getcwd(s, allocated);
697 if (wd) {
698 break;
700 if (errno != ERANGE) {
701 SAFE_FREE(s);
702 break;
704 allocated *= 2;
705 if (allocated < PATH_MAX) {
706 SAFE_FREE(s);
707 break;
710 return wd;
711 #else
712 char *s = SMB_MALLOC_ARRAY(char, PATH_MAX);
713 if (s == NULL) {
714 return NULL;
716 return getwd(s);
717 #endif
720 #if defined(HAVE_POSIX_CAPABILITIES)
722 /**************************************************************************
723 Try and abstract process capabilities (for systems that have them).
724 ****************************************************************************/
726 /* Set the POSIX capabilities needed for the given purpose into the effective
727 * capability set of the current process. Make sure they are always removed
728 * from the inheritable set, because there is no circumstance in which our
729 * children should inherit our elevated privileges.
731 static bool set_process_capability(enum smbd_capability capability,
732 bool enable)
734 cap_value_t cap_vals[2] = {0};
735 int num_cap_vals = 0;
737 cap_t cap;
739 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
740 /* On Linux, make sure that any capabilities we grab are sticky
741 * across UID changes. We expect that this would allow us to keep both
742 * the effective and permitted capability sets, but as of circa 2.6.16,
743 * only the permitted set is kept. It is a bug (which we work around)
744 * that the effective set is lost, but we still require the effective
745 * set to be kept.
747 if (!prctl(PR_GET_KEEPCAPS)) {
748 prctl(PR_SET_KEEPCAPS, 1);
750 #endif
752 cap = cap_get_proc();
753 if (cap == NULL) {
754 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
755 strerror(errno)));
756 return False;
759 switch (capability) {
760 case KERNEL_OPLOCK_CAPABILITY:
761 #ifdef CAP_NETWORK_MGT
762 /* IRIX has CAP_NETWORK_MGT for oplocks. */
763 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
764 #endif
765 break;
766 case DMAPI_ACCESS_CAPABILITY:
767 #ifdef CAP_DEVICE_MGT
768 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
769 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
770 #elif CAP_MKNOD
771 /* Linux has CAP_MKNOD for DMAPI access. */
772 cap_vals[num_cap_vals++] = CAP_MKNOD;
773 #endif
774 break;
775 case LEASE_CAPABILITY:
776 #ifdef CAP_LEASE
777 cap_vals[num_cap_vals++] = CAP_LEASE;
778 #endif
779 break;
782 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
784 if (num_cap_vals == 0) {
785 cap_free(cap);
786 return True;
789 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
790 enable ? CAP_SET : CAP_CLEAR);
792 /* We never want to pass capabilities down to our children, so make
793 * sure they are not inherited.
795 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
797 if (cap_set_proc(cap) == -1) {
798 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
799 strerror(errno)));
800 cap_free(cap);
801 return False;
804 cap_free(cap);
805 return True;
808 #endif /* HAVE_POSIX_CAPABILITIES */
810 /****************************************************************************
811 Gain the oplock capability from the kernel if possible.
812 ****************************************************************************/
814 void set_effective_capability(enum smbd_capability capability)
816 #if defined(HAVE_POSIX_CAPABILITIES)
817 set_process_capability(capability, True);
818 #endif /* HAVE_POSIX_CAPABILITIES */
821 void drop_effective_capability(enum smbd_capability capability)
823 #if defined(HAVE_POSIX_CAPABILITIES)
824 set_process_capability(capability, False);
825 #endif /* HAVE_POSIX_CAPABILITIES */
828 /**************************************************************************
829 Wrapper for random().
830 ****************************************************************************/
832 long sys_random(void)
834 #if defined(HAVE_RANDOM)
835 return (long)random();
836 #elif defined(HAVE_RAND)
837 return (long)rand();
838 #else
839 DEBUG(0,("Error - no random function available !\n"));
840 exit(1);
841 #endif
844 /**************************************************************************
845 Wrapper for srandom().
846 ****************************************************************************/
848 void sys_srandom(unsigned int seed)
850 #if defined(HAVE_SRANDOM)
851 srandom(seed);
852 #elif defined(HAVE_SRAND)
853 srand(seed);
854 #else
855 DEBUG(0,("Error - no srandom function available !\n"));
856 exit(1);
857 #endif
860 #ifndef NGROUPS_MAX
861 #define NGROUPS_MAX 32 /* Guess... */
862 #endif
864 /**************************************************************************
865 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
866 ****************************************************************************/
868 int groups_max(void)
870 #if defined(SYSCONF_SC_NGROUPS_MAX)
871 int ret = sysconf(_SC_NGROUPS_MAX);
872 return (ret == -1) ? NGROUPS_MAX : ret;
873 #else
874 return NGROUPS_MAX;
875 #endif
878 /**************************************************************************
879 Wrap setgroups and getgroups for systems that declare getgroups() as
880 returning an array of gid_t, but actuall return an array of int.
881 ****************************************************************************/
883 #if defined(HAVE_BROKEN_GETGROUPS)
885 #ifdef HAVE_BROKEN_GETGROUPS
886 #define GID_T int
887 #else
888 #define GID_T gid_t
889 #endif
891 static int sys_broken_getgroups(int setlen, gid_t *gidset)
893 GID_T gid;
894 GID_T *group_list;
895 int i, ngroups;
897 if(setlen == 0) {
898 return getgroups(setlen, &gid);
902 * Broken case. We need to allocate a
903 * GID_T array of size setlen.
906 if(setlen < 0) {
907 errno = EINVAL;
908 return -1;
911 if (setlen == 0)
912 setlen = groups_max();
914 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
915 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
916 return -1;
919 if((ngroups = getgroups(setlen, group_list)) < 0) {
920 int saved_errno = errno;
921 SAFE_FREE(group_list);
922 errno = saved_errno;
923 return -1;
926 for(i = 0; i < ngroups; i++)
927 gidset[i] = (gid_t)group_list[i];
929 SAFE_FREE(group_list);
930 return ngroups;
933 static int sys_broken_setgroups(int setlen, gid_t *gidset)
935 GID_T *group_list;
936 int i ;
938 if (setlen == 0)
939 return 0 ;
941 if (setlen < 0 || setlen > groups_max()) {
942 errno = EINVAL;
943 return -1;
947 * Broken case. We need to allocate a
948 * GID_T array of size setlen.
951 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
952 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
953 return -1;
956 for(i = 0; i < setlen; i++)
957 group_list[i] = (GID_T) gidset[i];
959 if(setgroups(setlen, group_list) != 0) {
960 int saved_errno = errno;
961 SAFE_FREE(group_list);
962 errno = saved_errno;
963 return -1;
966 SAFE_FREE(group_list);
967 return 0 ;
970 #endif /* HAVE_BROKEN_GETGROUPS */
972 /* This is a list of systems that require the first GID passed to setgroups(2)
973 * to be the effective GID. If your system is one of these, add it here.
975 #if defined (FREEBSD) || defined (DARWINOS)
976 #define USE_BSD_SETGROUPS
977 #endif
979 #if defined(USE_BSD_SETGROUPS)
980 /* Depending on the particular BSD implementation, the first GID that is
981 * passed to setgroups(2) will either be ignored or will set the credential's
982 * effective GID. In either case, the right thing to do is to guarantee that
983 * gidset[0] is the effective GID.
985 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
987 gid_t *new_gidset = NULL;
988 int max;
989 int ret;
991 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
992 max = groups_max();
994 /* No group list, just make sure we are setting the efective GID. */
995 if (setlen == 0) {
996 return setgroups(1, &primary_gid);
999 /* If the primary gid is not the first array element, grow the array
1000 * and insert it at the front.
1002 if (gidset[0] != primary_gid) {
1003 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
1004 if (new_gidset == NULL) {
1005 return -1;
1008 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
1009 new_gidset[0] = primary_gid;
1010 setlen++;
1013 if (setlen > max) {
1014 DEBUG(3, ("forced to truncate group list from %d to %d\n",
1015 setlen, max));
1016 setlen = max;
1019 #if defined(HAVE_BROKEN_GETGROUPS)
1020 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
1021 #else
1022 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
1023 #endif
1025 if (new_gidset) {
1026 int errsav = errno;
1027 SAFE_FREE(new_gidset);
1028 errno = errsav;
1031 return ret;
1034 #endif /* USE_BSD_SETGROUPS */
1036 /**************************************************************************
1037 Wrapper for getgroups. Deals with broken (int) case.
1038 ****************************************************************************/
1040 int sys_getgroups(int setlen, gid_t *gidset)
1042 #if defined(HAVE_BROKEN_GETGROUPS)
1043 return sys_broken_getgroups(setlen, gidset);
1044 #else
1045 return getgroups(setlen, gidset);
1046 #endif
1049 /**************************************************************************
1050 Wrapper for setgroups. Deals with broken (int) case and BSD case.
1051 ****************************************************************************/
1053 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
1055 #if !defined(HAVE_SETGROUPS)
1056 errno = ENOSYS;
1057 return -1;
1058 #endif /* HAVE_SETGROUPS */
1060 #if defined(USE_BSD_SETGROUPS)
1061 return sys_bsd_setgroups(primary_gid, setlen, gidset);
1062 #elif defined(HAVE_BROKEN_GETGROUPS)
1063 return sys_broken_setgroups(setlen, gidset);
1064 #else
1065 return setgroups(setlen, gidset);
1066 #endif
1069 /**************************************************************************
1070 Extract a command into an arg list.
1071 ****************************************************************************/
1073 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
1075 char *trunc_cmd;
1076 char *saveptr;
1077 char *ptr;
1078 int argcl;
1079 char **argl = NULL;
1080 int i;
1082 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1083 DEBUG(0, ("talloc failed\n"));
1084 goto nomem;
1087 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1088 TALLOC_FREE(trunc_cmd);
1089 errno = EINVAL;
1090 return NULL;
1094 * Count the args.
1097 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1098 argcl++;
1100 TALLOC_FREE(trunc_cmd);
1102 if (!(argl = talloc_array(mem_ctx, char *, argcl + 1))) {
1103 goto nomem;
1107 * Now do the extraction.
1110 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1111 goto nomem;
1114 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1115 i = 0;
1117 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1118 goto nomem;
1121 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1123 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1124 goto nomem;
1128 argl[i++] = NULL;
1129 TALLOC_FREE(trunc_cmd);
1130 return argl;
1132 nomem:
1133 DEBUG(0, ("talloc failed\n"));
1134 TALLOC_FREE(trunc_cmd);
1135 TALLOC_FREE(argl);
1136 errno = ENOMEM;
1137 return NULL;
1140 /**************************************************************************
1141 Wrapper for popen. Safer as it doesn't search a path.
1142 Modified from the glibc sources.
1143 modified by tridge to return a file descriptor. We must kick our FILE* habit
1144 ****************************************************************************/
1146 typedef struct _popen_list
1148 int fd;
1149 pid_t child_pid;
1150 struct _popen_list *next;
1151 } popen_list;
1153 static popen_list *popen_chain;
1155 int sys_popen(const char *command)
1157 int parent_end, child_end;
1158 int pipe_fds[2];
1159 popen_list *entry = NULL;
1160 char **argl = NULL;
1162 if (pipe(pipe_fds) < 0)
1163 return -1;
1165 parent_end = pipe_fds[0];
1166 child_end = pipe_fds[1];
1168 if (!*command) {
1169 errno = EINVAL;
1170 goto err_exit;
1173 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1174 goto err_exit;
1176 ZERO_STRUCTP(entry);
1179 * Extract the command and args into a NULL terminated array.
1182 if(!(argl = extract_args(NULL, command)))
1183 goto err_exit;
1185 entry->child_pid = fork();
1187 if (entry->child_pid == -1) {
1188 goto err_exit;
1191 if (entry->child_pid == 0) {
1194 * Child !
1197 int child_std_end = STDOUT_FILENO;
1198 popen_list *p;
1200 close(parent_end);
1201 if (child_end != child_std_end) {
1202 dup2 (child_end, child_std_end);
1203 close (child_end);
1207 * POSIX.2: "popen() shall ensure that any streams from previous
1208 * popen() calls that remain open in the parent process are closed
1209 * in the new child process."
1212 for (p = popen_chain; p; p = p->next)
1213 close(p->fd);
1215 execv(argl[0], argl);
1216 _exit (127);
1220 * Parent.
1223 close (child_end);
1224 TALLOC_FREE(argl);
1226 /* Link into popen_chain. */
1227 entry->next = popen_chain;
1228 popen_chain = entry;
1229 entry->fd = parent_end;
1231 return entry->fd;
1233 err_exit:
1235 SAFE_FREE(entry);
1236 TALLOC_FREE(argl);
1237 close(pipe_fds[0]);
1238 close(pipe_fds[1]);
1239 return -1;
1242 /**************************************************************************
1243 Wrapper for pclose. Modified from the glibc sources.
1244 ****************************************************************************/
1246 int sys_pclose(int fd)
1248 int wstatus;
1249 popen_list **ptr = &popen_chain;
1250 popen_list *entry = NULL;
1251 pid_t wait_pid;
1252 int status = -1;
1254 /* Unlink from popen_chain. */
1255 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1256 if ((*ptr)->fd == fd) {
1257 entry = *ptr;
1258 *ptr = (*ptr)->next;
1259 status = 0;
1260 break;
1264 if (status < 0 || close(entry->fd) < 0)
1265 return -1;
1268 * As Samba is catching and eating child process
1269 * exits we don't really care about the child exit
1270 * code, a -1 with errno = ECHILD will do fine for us.
1273 do {
1274 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1275 } while (wait_pid == -1 && errno == EINTR);
1277 SAFE_FREE(entry);
1279 if (wait_pid == -1)
1280 return -1;
1281 return wstatus;
1284 /**************************************************************************
1285 Wrapper for Admin Logs.
1286 ****************************************************************************/
1288 void sys_adminlog(int priority, const char *format_str, ...)
1290 va_list ap;
1291 int ret;
1292 char *msgbuf = NULL;
1294 va_start( ap, format_str );
1295 ret = vasprintf( &msgbuf, format_str, ap );
1296 va_end( ap );
1298 if (ret == -1)
1299 return;
1301 #if defined(HAVE_SYSLOG)
1302 syslog( priority, "%s", msgbuf );
1303 #else
1304 DEBUG(0,("%s", msgbuf ));
1305 #endif
1306 SAFE_FREE(msgbuf);
1309 /****************************************************************************
1310 Return the major devicenumber for UNIX extensions.
1311 ****************************************************************************/
1313 uint32 unix_dev_major(SMB_DEV_T dev)
1315 #if defined(HAVE_DEVICE_MAJOR_FN)
1316 return (uint32)major(dev);
1317 #else
1318 return (uint32)(dev >> 8);
1319 #endif
1322 /****************************************************************************
1323 Return the minor devicenumber for UNIX extensions.
1324 ****************************************************************************/
1326 uint32 unix_dev_minor(SMB_DEV_T dev)
1328 #if defined(HAVE_DEVICE_MINOR_FN)
1329 return (uint32)minor(dev);
1330 #else
1331 return (uint32)(dev & 0xff);
1332 #endif
1335 #if 0
1336 /*******************************************************************
1337 Return the number of CPUs.
1338 ********************************************************************/
1340 int sys_get_number_of_cores(void)
1342 int ret = -1;
1344 #if defined(HAVE_SYSCONF)
1345 #if defined(_SC_NPROCESSORS_ONLN)
1346 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
1347 #endif
1348 #if defined(_SC_NPROCESSORS_CONF)
1349 if (ret < 1) {
1350 ret = (int)sysconf(_SC_NPROCESSORS_CONF);
1352 #endif
1353 #elif defined(HAVE_SYSCTL) && defined(CTL_HW)
1354 int name[2];
1355 unsigned int len = sizeof(ret);
1357 name[0] = CTL_HW;
1358 #if defined(HW_AVAILCPU)
1359 name[1] = HW_AVAILCPU;
1361 if (sysctl(name, 2, &ret, &len, NULL, 0) == -1) {
1362 ret = -1;
1364 #endif
1365 #if defined(HW_NCPU)
1366 if(ret < 1) {
1367 name[0] = CTL_HW;
1368 name[1] = HW_NCPU;
1369 if (sysctl(nm, 2, &count, &len, NULL, 0) == -1) {
1370 ret = -1;
1373 #endif
1374 #endif
1375 if (ret < 1) {
1376 ret = 1;
1378 return ret;
1380 #endif
1382 #if defined(HAVE_AIO)
1384 /*******************************************************************
1385 An aio_read wrapper.
1386 ********************************************************************/
1388 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
1390 #if defined(HAVE_AIO_READ)
1391 return aio_read(aiocb);
1392 #else
1393 errno = ENOSYS;
1394 return -1;
1395 #endif
1398 /*******************************************************************
1399 An aio_write wrapper.
1400 ********************************************************************/
1402 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
1404 #if defined(HAVE_AIO_WRITE)
1405 return aio_write(aiocb);
1406 #else
1407 errno = ENOSYS;
1408 return -1;
1409 #endif
1412 /*******************************************************************
1413 An aio_return wrapper.
1414 ********************************************************************/
1416 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
1418 #if defined(HAVE_AIO_RETURN)
1419 return aio_return(aiocb);
1420 #else
1421 errno = ENOSYS;
1422 return -1;
1423 #endif
1426 /*******************************************************************
1427 An aio_cancel wrapper.
1428 ********************************************************************/
1430 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
1432 #if defined(HAVE_AIO_CANCEL)
1433 return aio_cancel(fd, aiocb);
1434 #else
1435 errno = ENOSYS;
1436 return -1;
1437 #endif
1440 /*******************************************************************
1441 An aio_error wrapper.
1442 ********************************************************************/
1444 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
1446 #if defined(HAVE_AIO_ERROR)
1447 return aio_error(aiocb);
1448 #else
1449 errno = ENOSYS;
1450 return -1;
1451 #endif
1454 /*******************************************************************
1455 An aio_fsync wrapper.
1456 ********************************************************************/
1458 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
1460 #if defined(HAVE_AIO_FSYNC)
1461 return aio_fsync(op, aiocb);
1462 #else
1463 errno = ENOSYS;
1464 return -1;
1465 #endif
1468 /*******************************************************************
1469 An aio_fsync wrapper.
1470 ********************************************************************/
1472 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
1474 #if defined(HAVE_AIO_FSYNC)
1475 return aio_suspend(cblist, n, timeout);
1476 #else
1477 errno = ENOSYS;
1478 return -1;
1479 #endif
1481 #else /* !HAVE_AIO */
1483 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
1485 errno = ENOSYS;
1486 return -1;
1489 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
1491 errno = ENOSYS;
1492 return -1;
1495 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
1497 errno = ENOSYS;
1498 return -1;
1501 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
1503 errno = ENOSYS;
1504 return -1;
1507 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
1509 errno = ENOSYS;
1510 return -1;
1513 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
1515 errno = ENOSYS;
1516 return -1;
1519 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
1521 errno = ENOSYS;
1522 return -1;
1524 #endif /* HAVE_AIO */