source: trunk/server/source3/lib/system.c@ 901

Last change on this file since 901 was 874, checked in by Silvan Scherrer, 11 years ago

samba server 3.6: update trunk to 3.6.24

File size: 71.3 KB
Line 
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
8
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.
13
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.
18
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/>.
21*/
22
23#include "includes.h"
24#include "system/syslog.h"
25#include "system/capability.h"
26#include "system/passwd.h"
27#include "system/filesys.h"
28
29#ifdef HAVE_SYS_PRCTL_H
30#include <sys/prctl.h>
31#endif
32#ifdef __OS2__
33#define pipe(A) os2_pipe(A)
34#endif
35
36/*
37 The idea is that this file will eventually have wrappers around all
38 important system calls in samba. The aims are:
39
40 - to enable easier porting by putting OS dependent stuff in here
41
42 - to allow for hooks into other "pseudo-filesystems"
43
44 - to allow easier integration of things like the japanese extensions
45
46 - to support the philosophy of Samba to expose the features of
47 the OS within the SMB model. In general whatever file/printer/variable
48 expansions/etc make sense to the OS should be acceptable to Samba.
49*/
50
51
52
53/*******************************************************************
54 A wrapper for memalign
55********************************************************************/
56
57void *sys_memalign( size_t align, size_t size )
58{
59#if defined(HAVE_POSIX_MEMALIGN)
60 void *p = NULL;
61 int ret = posix_memalign( &p, align, size );
62 if ( ret == 0 )
63 return p;
64
65 return NULL;
66#elif defined(HAVE_MEMALIGN)
67 return memalign( align, size );
68#else
69 /* On *BSD systems memaligns doesn't exist, but memory will
70 * be aligned on allocations of > pagesize. */
71#if defined(SYSCONF_SC_PAGESIZE)
72 size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
73#elif defined(HAVE_GETPAGESIZE)
74 size_t pagesize = (size_t)getpagesize();
75#else
76 size_t pagesize = (size_t)-1;
77#endif
78 if (pagesize == (size_t)-1) {
79 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
80 return NULL;
81 }
82 if (size < pagesize) {
83 size = pagesize;
84 }
85 return SMB_MALLOC(size);
86#endif
87}
88
89/*******************************************************************
90 A wrapper for usleep in case we don't have one.
91********************************************************************/
92
93int sys_usleep(long usecs)
94{
95#ifndef HAVE_USLEEP
96 struct timeval tval;
97#endif
98
99 /*
100 * We need this braindamage as the glibc usleep
101 * is not SPEC1170 complient... grumble... JRA.
102 */
103
104 if(usecs < 0 || usecs > 999999) {
105 errno = EINVAL;
106 return -1;
107 }
108
109#if HAVE_USLEEP
110 usleep(usecs);
111 return 0;
112#else /* HAVE_USLEEP */
113 /*
114 * Fake it with select...
115 */
116 tval.tv_sec = 0;
117 tval.tv_usec = usecs/1000;
118 select(0,NULL,NULL,NULL,&tval);
119 return 0;
120#endif /* HAVE_USLEEP */
121}
122
123/*******************************************************************
124A read wrapper that will deal with EINTR.
125********************************************************************/
126
127ssize_t sys_read(int fd, void *buf, size_t count)
128{
129 ssize_t ret;
130
131 do {
132 ret = read(fd, buf, count);
133#if defined(EWOULDBLOCK)
134 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
135#else
136 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
137#endif
138 return ret;
139}
140
141/*******************************************************************
142A write wrapper that will deal with EINTR.
143********************************************************************/
144
145ssize_t sys_write(int fd, const void *buf, size_t count)
146{
147 ssize_t ret;
148
149 do {
150 ret = write(fd, buf, count);
151#if defined(EWOULDBLOCK)
152 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
153#else
154 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
155#endif
156 return ret;
157}
158
159/*******************************************************************
160A writev wrapper that will deal with EINTR.
161********************************************************************/
162
163ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
164{
165 ssize_t ret;
166
167#if 0
168 /* Try to confuse write_data_iov a bit */
169 if ((random() % 5) == 0) {
170 return sys_write(fd, iov[0].iov_base, iov[0].iov_len);
171 }
172 if (iov[0].iov_len > 1) {
173 return sys_write(fd, iov[0].iov_base,
174 (random() % (iov[0].iov_len-1)) + 1);
175 }
176#endif
177
178 do {
179 ret = writev(fd, iov, iovcnt);
180#if defined(EWOULDBLOCK)
181 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
182#else
183 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
184#endif
185 return ret;
186}
187
188/*******************************************************************
189A pread wrapper that will deal with EINTR and 64-bit file offsets.
190********************************************************************/
191
192#if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
193ssize_t sys_pread(int fd, void *buf, size_t count, SMB_OFF_T off)
194{
195 ssize_t ret;
196
197 do {
198#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
199 ret = pread64(fd, buf, count, off);
200#else
201 ret = pread(fd, buf, count, off);
202#endif
203 } while (ret == -1 && errno == EINTR);
204 return ret;
205}
206#endif
207
208/*******************************************************************
209A write wrapper that will deal with EINTR and 64-bit file offsets.
210********************************************************************/
211
212#if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
213ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off)
214{
215 ssize_t ret;
216
217 do {
218#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
219 ret = pwrite64(fd, buf, count, off);
220#else
221 ret = pwrite(fd, buf, count, off);
222#endif
223 } while (ret == -1 && errno == EINTR);
224 return ret;
225}
226#endif
227
228/*******************************************************************
229A send wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
230********************************************************************/
231
232ssize_t sys_send(int s, const void *msg, size_t len, int flags)
233{
234 ssize_t ret;
235
236 do {
237 ret = send(s, msg, len, flags);
238#if defined(EWOULDBLOCK)
239 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
240#else
241 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
242#endif
243 return ret;
244}
245
246/*******************************************************************
247A sendto wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
248********************************************************************/
249
250ssize_t sys_sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
251{
252 ssize_t ret;
253
254 do {
255 ret = sendto(s, msg, len, flags, to, tolen);
256#if defined(EWOULDBLOCK)
257 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
258#else
259 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
260#endif
261 return ret;
262}
263
264/*******************************************************************
265A recv wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
266********************************************************************/
267
268ssize_t sys_recv(int fd, void *buf, size_t count, int flags)
269{
270 ssize_t ret;
271
272 do {
273 ret = recv(fd, buf, count, flags);
274#if defined(EWOULDBLOCK)
275 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
276#else
277 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
278#endif
279 return ret;
280}
281
282/*******************************************************************
283A recvfrom wrapper that will deal with EINTR.
284NB. As used with non-blocking sockets, return on EAGAIN/EWOULDBLOCK
285********************************************************************/
286
287ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
288{
289 ssize_t ret;
290
291 do {
292 ret = recvfrom(s, buf, len, flags, from, fromlen);
293 } while (ret == -1 && (errno == EINTR));
294 return ret;
295}
296
297/*******************************************************************
298A fcntl wrapper that will deal with EINTR.
299********************************************************************/
300
301int sys_fcntl_ptr(int fd, int cmd, void *arg)
302{
303 int ret;
304
305 do {
306 ret = fcntl(fd, cmd, arg);
307 } while (ret == -1 && errno == EINTR);
308 return ret;
309}
310
311/*******************************************************************
312A fcntl wrapper that will deal with EINTR.
313********************************************************************/
314
315int sys_fcntl_long(int fd, int cmd, long arg)
316{
317 int ret;
318
319 do {
320 ret = fcntl(fd, cmd, arg);
321 } while (ret == -1 && errno == EINTR);
322 return ret;
323}
324
325/****************************************************************************
326 Get/Set all the possible time fields from a stat struct as a timespec.
327****************************************************************************/
328
329static struct timespec get_atimespec(const struct stat *pst)
330{
331#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
332 struct timespec ret;
333
334 /* Old system - no ns timestamp. */
335 ret.tv_sec = pst->st_atime;
336 ret.tv_nsec = 0;
337 return ret;
338#else
339#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
340 return pst->st_atim;
341#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
342 struct timespec ret;
343 ret.tv_sec = pst->st_atime;
344 ret.tv_nsec = pst->st_atimensec;
345 return ret;
346#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
347 struct timespec ret;
348 ret.tv_sec = pst->st_atime;
349 ret.tv_nsec = pst->st_atime_n;
350 return ret;
351#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
352 struct timespec ret;
353 ret.tv_sec = pst->st_atime;
354 ret.tv_nsec = pst->st_uatime * 1000;
355 return ret;
356#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
357 return pst->st_atimespec;
358#else
359#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
360#endif
361#endif
362}
363
364static struct timespec get_mtimespec(const struct stat *pst)
365{
366#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
367 struct timespec ret;
368
369 /* Old system - no ns timestamp. */
370 ret.tv_sec = pst->st_mtime;
371 ret.tv_nsec = 0;
372 return ret;
373#else
374#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
375 return pst->st_mtim;
376#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
377 struct timespec ret;
378 ret.tv_sec = pst->st_mtime;
379 ret.tv_nsec = pst->st_mtimensec;
380 return ret;
381#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
382 struct timespec ret;
383 ret.tv_sec = pst->st_mtime;
384 ret.tv_nsec = pst->st_mtime_n;
385 return ret;
386#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
387 struct timespec ret;
388 ret.tv_sec = pst->st_mtime;
389 ret.tv_nsec = pst->st_umtime * 1000;
390 return ret;
391#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
392 return pst->st_mtimespec;
393#else
394#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
395#endif
396#endif
397}
398
399static struct timespec get_ctimespec(const struct stat *pst)
400{
401#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
402 struct timespec ret;
403
404 /* Old system - no ns timestamp. */
405 ret.tv_sec = pst->st_ctime;
406 ret.tv_nsec = 0;
407 return ret;
408#else
409#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
410 return pst->st_ctim;
411#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
412 struct timespec ret;
413 ret.tv_sec = pst->st_ctime;
414 ret.tv_nsec = pst->st_ctimensec;
415 return ret;
416#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
417 struct timespec ret;
418 ret.tv_sec = pst->st_ctime;
419 ret.tv_nsec = pst->st_ctime_n;
420 return ret;
421#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
422 struct timespec ret;
423 ret.tv_sec = pst->st_ctime;
424 ret.tv_nsec = pst->st_uctime * 1000;
425 return ret;
426#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
427 return pst->st_ctimespec;
428#else
429#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
430#endif
431#endif
432}
433
434/****************************************************************************
435 Return the best approximation to a 'create time' under UNIX from a stat
436 structure.
437****************************************************************************/
438
439static struct timespec calc_create_time_stat(const struct stat *st)
440{
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);
445
446 ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
447 ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
448
449 if(!null_timespec(ret1)) {
450 return ret1;
451 }
452
453 /*
454 * One of ctime, mtime or atime was zero (probably atime).
455 * Just return MIN(ctime, mtime).
456 */
457 return ret;
458}
459
460/****************************************************************************
461 Return the best approximation to a 'create time' under UNIX from a stat_ex
462 structure.
463****************************************************************************/
464
465static struct timespec calc_create_time_stat_ex(const struct stat_ex *st)
466{
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;
471
472 ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
473 ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
474
475 if(!null_timespec(ret1)) {
476 return ret1;
477 }
478
479 /*
480 * One of ctime, mtime or atime was zero (probably atime).
481 * Just return MIN(ctime, mtime).
482 */
483 return ret;
484}
485
486/****************************************************************************
487 Return the 'create time' from a stat struct if it exists (birthtime) or else
488 use the best approximation.
489****************************************************************************/
490
491static void make_create_timespec(const struct stat *pst, struct stat_ex *dst,
492 bool fake_dir_create_times)
493{
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;
497 }
498
499 dst->st_ex_calculated_birthtime = false;
500
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;
509#else
510 dst->st_ex_btime = calc_create_time_stat(pst);
511 dst->st_ex_calculated_birthtime = true;
512#endif
513
514 /* Deal with systems that don't initialize birthtime correctly.
515 * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
516 */
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;
520 }
521}
522
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****************************************************************************/
528
529void update_stat_ex_mtime(struct stat_ex *dst,
530 struct timespec write_ts)
531{
532 dst->st_ex_mtime = write_ts;
533
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);
537 }
538}
539
540void update_stat_ex_create_time(struct stat_ex *dst,
541 struct timespec create_time)
542{
543 dst->st_ex_btime = create_time;
544 dst->st_ex_calculated_birthtime = false;
545}
546
547#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
548static void init_stat_ex_from_stat (struct stat_ex *dst,
549 const struct stat64 *src,
550 bool fake_dir_create_times)
551#else
552static void init_stat_ex_from_stat (struct stat_ex *dst,
553 const struct stat *src,
554 bool fake_dir_create_times)
555#endif
556{
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;
571#else
572 dst->st_ex_blksize = STAT_ST_BLOCKSIZE;
573#endif
574
575#ifdef HAVE_STAT_ST_BLOCKS
576 dst->st_ex_blocks = src->st_blocks;
577#else
578 dst->st_ex_blocks = src->st_size / dst->st_ex_blksize + 1;
579#endif
580
581#ifdef HAVE_STAT_ST_FLAGS
582 dst->st_ex_flags = src->st_flags;
583#else
584 dst->st_ex_flags = 0;
585#endif
586}
587
588/*******************************************************************
589A stat() wrapper that will deal with 64 bit filesizes.
590********************************************************************/
591
592int sys_stat(const char *fname, SMB_STRUCT_STAT *sbuf,
593 bool fake_dir_create_times)
594{
595 int ret;
596#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
597 struct stat64 statbuf;
598 ret = stat64(fname, &statbuf);
599#else
600 struct stat statbuf;
601 ret = stat(fname, &statbuf);
602#endif
603 if (ret == 0) {
604 /* we always want directories to appear zero size */
605 if (S_ISDIR(statbuf.st_mode)) {
606 statbuf.st_size = 0;
607 }
608 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
609 }
610 return ret;
611}
612
613/*******************************************************************
614 An fstat() wrapper that will deal with 64 bit filesizes.
615********************************************************************/
616
617int sys_fstat(int fd, SMB_STRUCT_STAT *sbuf, bool fake_dir_create_times)
618{
619 int ret;
620#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
621 struct stat64 statbuf;
622 ret = fstat64(fd, &statbuf);
623#else
624 struct stat statbuf;
625 ret = fstat(fd, &statbuf);
626#endif
627 if (ret == 0) {
628 /* we always want directories to appear zero size */
629 if (S_ISDIR(statbuf.st_mode)) {
630 statbuf.st_size = 0;
631 }
632 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
633 }
634 return ret;
635}
636
637/*******************************************************************
638 An lstat() wrapper that will deal with 64 bit filesizes.
639********************************************************************/
640
641int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf,
642 bool fake_dir_create_times)
643{
644 int ret;
645#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
646 struct stat64 statbuf;
647 ret = lstat64(fname, &statbuf);
648#else
649 struct stat statbuf;
650 ret = lstat(fname, &statbuf);
651#endif
652 if (ret == 0) {
653 /* we always want directories to appear zero size */
654 if (S_ISDIR(statbuf.st_mode)) {
655 statbuf.st_size = 0;
656 }
657 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
658 }
659 return ret;
660}
661
662/*******************************************************************
663 An posix_fallocate() wrapper that will deal with 64 bit filesizes.
664********************************************************************/
665int sys_posix_fallocate(int fd, SMB_OFF_T offset, SMB_OFF_T len)
666{
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 */
673 struct flock64 fl;
674 SMB_OFF_T new_len = offset + len;
675 int ret;
676 struct stat64 sbuf;
677
678 /* unlikely to get a too large file on a 64bit system but ... */
679 if (new_len < 0)
680 return EFBIG;
681
682 fl.l_whence = SEEK_SET;
683 fl.l_start = offset;
684 fl.l_len = len;
685
686 ret=fcntl(fd, F_RESVSP64, &fl);
687
688 if (ret != 0)
689 return errno;
690
691 /* Make sure the file gets enlarged after we allocated space: */
692 fstat64(fd, &sbuf);
693 if (new_len > sbuf.st_size)
694 ftruncate64(fd, new_len);
695 return 0;
696#else
697 return ENOSYS;
698#endif
699}
700
701/*******************************************************************
702 An fallocate() function that matches the semantics of the Linux one.
703********************************************************************/
704
705#ifdef HAVE_LINUX_FALLOC_H
706#include <linux/falloc.h>
707#endif
708
709int sys_fallocate(int fd, enum vfs_fallocate_mode mode, SMB_OFF_T offset, SMB_OFF_T len)
710{
711#if defined(HAVE_LINUX_FALLOCATE64) || defined(HAVE_LINUX_FALLOCATE)
712 int lmode;
713 switch (mode) {
714 case VFS_FALLOCATE_EXTEND_SIZE:
715 lmode = 0;
716 break;
717 case VFS_FALLOCATE_KEEP_SIZE:
718 lmode = FALLOC_FL_KEEP_SIZE;
719 break;
720 default:
721 errno = EINVAL;
722 return -1;
723 }
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);
728#endif
729#else
730 /* TODO - plumb in fallocate from other filesysetms like VXFS etc. JRA. */
731 errno = ENOSYS;
732 return -1;
733#endif
734}
735
736/*******************************************************************
737 An ftruncate() wrapper that will deal with 64 bit filesizes.
738********************************************************************/
739
740int sys_ftruncate(int fd, SMB_OFF_T offset)
741{
742#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
743 return ftruncate64(fd, offset);
744#elif defined(__OS2__)
745 return os2_ftruncate(fd, offset);
746#else
747 return ftruncate(fd, offset);
748#endif
749}
750
751/*******************************************************************
752 An lseek() wrapper that will deal with 64 bit filesizes.
753********************************************************************/
754
755SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
756{
757#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
758 return lseek64(fd, offset, whence);
759#else
760 return lseek(fd, offset, whence);
761#endif
762}
763
764/*******************************************************************
765 An fseek() wrapper that will deal with 64 bit filesizes.
766********************************************************************/
767
768int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
769{
770#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
771 return fseek64(fp, offset, whence);
772#elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
773 return fseeko64(fp, offset, whence);
774#elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO)
775 return fseeko(fp, offset, whence);
776#else
777 return fseek(fp, offset, whence);
778#endif
779}
780
781/*******************************************************************
782 An ftell() wrapper that will deal with 64 bit filesizes.
783********************************************************************/
784
785SMB_OFF_T sys_ftell(FILE *fp)
786{
787#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
788 return (SMB_OFF_T)ftell64(fp);
789#elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
790 return (SMB_OFF_T)ftello64(fp);
791#else
792 return (SMB_OFF_T)ftell(fp);
793#endif
794}
795
796/*******************************************************************
797 A creat() wrapper that will deal with 64 bit filesizes.
798********************************************************************/
799
800int sys_creat(const char *path, mode_t mode)
801{
802#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
803 return creat64(path, mode);
804#else
805 /*
806 * If creat64 isn't defined then ensure we call a potential open64.
807 * JRA.
808 */
809 return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
810#endif
811}
812
813/*******************************************************************
814 An open() wrapper that will deal with 64 bit filesizes.
815********************************************************************/
816
817int sys_open(const char *path, int oflag, mode_t mode)
818{
819#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
820 return open64(path, oflag, mode);
821#else
822 return open(path, oflag, mode);
823#endif
824}
825
826/*******************************************************************
827 An fopen() wrapper that will deal with 64 bit filesizes.
828********************************************************************/
829
830FILE *sys_fopen(const char *path, const char *type)
831{
832#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
833 return fopen64(path, type);
834#else
835 return fopen(path, type);
836#endif
837}
838
839
840#if HAVE_KERNEL_SHARE_MODES
841#ifndef LOCK_MAND
842#define LOCK_MAND 32 /* This is a mandatory flock */
843#define LOCK_READ 64 /* ... Which allows concurrent read operations */
844#define LOCK_WRITE 128 /* ... Which allows concurrent write operations */
845#define LOCK_RW 192 /* ... Which allows concurrent read & write ops */
846#endif
847#endif
848
849/*******************************************************************
850 A flock() wrapper that will perform the kernel flock.
851********************************************************************/
852
853void kernel_flock(int fd, uint32 share_mode, uint32 access_mask)
854{
855#if HAVE_KERNEL_SHARE_MODES
856 int kernel_mode = 0;
857 if (share_mode == FILE_SHARE_WRITE) {
858 kernel_mode = LOCK_MAND|LOCK_WRITE;
859 } else if (share_mode == FILE_SHARE_READ) {
860 kernel_mode = LOCK_MAND|LOCK_READ;
861 } else if (share_mode == FILE_SHARE_NONE) {
862 kernel_mode = LOCK_MAND;
863 }
864 if (kernel_mode) {
865 flock(fd, kernel_mode);
866 }
867#endif
868 ;
869}
870
871
872
873/*******************************************************************
874 An opendir wrapper that will deal with 64 bit filesizes.
875********************************************************************/
876
877SMB_STRUCT_DIR *sys_opendir(const char *name)
878{
879#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
880 return opendir64(name);
881#else
882 return opendir(name);
883#endif
884}
885
886/*******************************************************************
887 An fdopendir wrapper.
888********************************************************************/
889
890SMB_STRUCT_DIR *sys_fdopendir(int fd)
891{
892#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FDOPENDIR64)
893 return fdopendir64(fd);
894#elif defined(HAVE_FDOPENDIR)
895 return fdopendir(fd);
896#else
897 errno = ENOSYS;
898 return NULL;
899#endif
900}
901
902/*******************************************************************
903 A readdir wrapper that will deal with 64 bit filesizes.
904********************************************************************/
905
906SMB_STRUCT_DIRENT *sys_readdir(SMB_STRUCT_DIR *dirp)
907{
908#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
909 return readdir64(dirp);
910#else
911 return readdir(dirp);
912#endif
913}
914
915/*******************************************************************
916 A seekdir wrapper that will deal with 64 bit filesizes.
917********************************************************************/
918
919void sys_seekdir(SMB_STRUCT_DIR *dirp, long offset)
920{
921#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
922 seekdir64(dirp, offset);
923#else
924 seekdir(dirp, offset);
925#endif
926}
927
928/*******************************************************************
929 A telldir wrapper that will deal with 64 bit filesizes.
930********************************************************************/
931
932long sys_telldir(SMB_STRUCT_DIR *dirp)
933{
934#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
935 return (long)telldir64(dirp);
936#else
937 return (long)telldir(dirp);
938#endif
939}
940
941/*******************************************************************
942 A rewinddir wrapper that will deal with 64 bit filesizes.
943********************************************************************/
944
945void sys_rewinddir(SMB_STRUCT_DIR *dirp)
946{
947#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
948 rewinddir64(dirp);
949#else
950 rewinddir(dirp);
951#endif
952}
953
954/*******************************************************************
955 A close wrapper that will deal with 64 bit filesizes.
956********************************************************************/
957
958int sys_closedir(SMB_STRUCT_DIR *dirp)
959{
960#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
961 return closedir64(dirp);
962#else
963 return closedir(dirp);
964#endif
965}
966
967/*******************************************************************
968 An mknod() wrapper that will deal with 64 bit filesizes.
969********************************************************************/
970
971int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
972{
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);
976#else
977 return mknod(path, mode, dev);
978#endif
979#else
980 /* No mknod system call. */
981 errno = ENOSYS;
982 return -1;
983#endif
984}
985
986/*******************************************************************
987The wait() calls vary between systems
988********************************************************************/
989
990int sys_waitpid(pid_t pid,int *status,int options)
991{
992#ifdef HAVE_WAITPID
993 return waitpid(pid,status,options);
994#else /* HAVE_WAITPID */
995 return wait4(pid, status, options, NULL);
996#endif /* HAVE_WAITPID */
997}
998
999/*******************************************************************
1000 System wrapper for getwd
1001********************************************************************/
1002
1003char *sys_getwd(char *s)
1004{
1005 char *wd;
1006#ifdef HAVE_GETCWD
1007 wd = (char *)getcwd(s, PATH_MAX);
1008#else
1009 wd = (char *)getwd(s);
1010#endif
1011 return wd;
1012}
1013
1014#if defined(HAVE_POSIX_CAPABILITIES)
1015
1016/**************************************************************************
1017 Try and abstract process capabilities (for systems that have them).
1018****************************************************************************/
1019
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.
1024 */
1025static bool set_process_capability(enum smbd_capability capability,
1026 bool enable)
1027{
1028 cap_value_t cap_vals[2] = {0};
1029 int num_cap_vals = 0;
1030
1031 cap_t cap;
1032
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
1039 * set to be kept.
1040 */
1041 if (!prctl(PR_GET_KEEPCAPS)) {
1042 prctl(PR_SET_KEEPCAPS, 1);
1043 }
1044#endif
1045
1046 cap = cap_get_proc();
1047 if (cap == NULL) {
1048 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
1049 strerror(errno)));
1050 return False;
1051 }
1052
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;
1058#endif
1059 break;
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;
1064#elif CAP_MKNOD
1065 /* Linux has CAP_MKNOD for DMAPI access. */
1066 cap_vals[num_cap_vals++] = CAP_MKNOD;
1067#endif
1068 break;
1069 case LEASE_CAPABILITY:
1070#ifdef CAP_LEASE
1071 cap_vals[num_cap_vals++] = CAP_LEASE;
1072#endif
1073 break;
1074 }
1075
1076 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
1077
1078 if (num_cap_vals == 0) {
1079 cap_free(cap);
1080 return True;
1081 }
1082
1083 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
1084 enable ? CAP_SET : CAP_CLEAR);
1085
1086 /* We never want to pass capabilities down to our children, so make
1087 * sure they are not inherited.
1088 */
1089 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
1090
1091 if (cap_set_proc(cap) == -1) {
1092 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
1093 strerror(errno)));
1094 cap_free(cap);
1095 return False;
1096 }
1097
1098 cap_free(cap);
1099 return True;
1100}
1101
1102#endif /* HAVE_POSIX_CAPABILITIES */
1103
1104/****************************************************************************
1105 Gain the oplock capability from the kernel if possible.
1106****************************************************************************/
1107
1108void set_effective_capability(enum smbd_capability capability)
1109{
1110#if defined(HAVE_POSIX_CAPABILITIES)
1111 set_process_capability(capability, True);
1112#endif /* HAVE_POSIX_CAPABILITIES */
1113}
1114
1115void drop_effective_capability(enum smbd_capability capability)
1116{
1117#if defined(HAVE_POSIX_CAPABILITIES)
1118 set_process_capability(capability, False);
1119#endif /* HAVE_POSIX_CAPABILITIES */
1120}
1121
1122/**************************************************************************
1123 Wrapper for random().
1124****************************************************************************/
1125
1126long sys_random(void)
1127{
1128#if defined(HAVE_RANDOM)
1129 return (long)random();
1130#elif defined(HAVE_RAND)
1131 return (long)rand();
1132#else
1133 DEBUG(0,("Error - no random function available !\n"));
1134 exit(1);
1135#endif
1136}
1137
1138/**************************************************************************
1139 Wrapper for srandom().
1140****************************************************************************/
1141
1142void sys_srandom(unsigned int seed)
1143{
1144#if defined(HAVE_SRANDOM)
1145 srandom(seed);
1146#elif defined(HAVE_SRAND)
1147 srand(seed);
1148#else
1149 DEBUG(0,("Error - no srandom function available !\n"));
1150 exit(1);
1151#endif
1152}
1153
1154#ifndef NGROUPS_MAX
1155#define NGROUPS_MAX 32 /* Guess... */
1156#endif
1157
1158/**************************************************************************
1159 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
1160****************************************************************************/
1161
1162int groups_max(void)
1163{
1164#if defined(SYSCONF_SC_NGROUPS_MAX)
1165 int ret = sysconf(_SC_NGROUPS_MAX);
1166 return (ret == -1) ? NGROUPS_MAX : ret;
1167#else
1168 return NGROUPS_MAX;
1169#endif
1170}
1171
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****************************************************************************/
1176
1177#if defined(HAVE_BROKEN_GETGROUPS)
1178
1179#ifdef HAVE_BROKEN_GETGROUPS
1180#define GID_T int
1181#else
1182#define GID_T gid_t
1183#endif
1184
1185static int sys_broken_getgroups(int setlen, gid_t *gidset)
1186{
1187 GID_T gid;
1188 GID_T *group_list;
1189 int i, ngroups;
1190
1191 if(setlen == 0) {
1192 return getgroups(setlen, &gid);
1193 }
1194
1195 /*
1196 * Broken case. We need to allocate a
1197 * GID_T array of size setlen.
1198 */
1199
1200 if(setlen < 0) {
1201 errno = EINVAL;
1202 return -1;
1203 }
1204
1205 if (setlen == 0)
1206 setlen = groups_max();
1207
1208 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
1209 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
1210 return -1;
1211 }
1212
1213 if((ngroups = getgroups(setlen, group_list)) < 0) {
1214 int saved_errno = errno;
1215 SAFE_FREE(group_list);
1216 errno = saved_errno;
1217 return -1;
1218 }
1219
1220 for(i = 0; i < ngroups; i++)
1221 gidset[i] = (gid_t)group_list[i];
1222
1223 SAFE_FREE(group_list);
1224 return ngroups;
1225}
1226
1227static int sys_broken_setgroups(int setlen, gid_t *gidset)
1228{
1229 GID_T *group_list;
1230 int i ;
1231
1232 if (setlen == 0)
1233 return 0 ;
1234
1235 if (setlen < 0 || setlen > groups_max()) {
1236 errno = EINVAL;
1237 return -1;
1238 }
1239
1240 /*
1241 * Broken case. We need to allocate a
1242 * GID_T array of size setlen.
1243 */
1244
1245 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
1246 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
1247 return -1;
1248 }
1249
1250 for(i = 0; i < setlen; i++)
1251 group_list[i] = (GID_T) gidset[i];
1252
1253 if(setgroups(setlen, group_list) != 0) {
1254 int saved_errno = errno;
1255 SAFE_FREE(group_list);
1256 errno = saved_errno;
1257 return -1;
1258 }
1259
1260 SAFE_FREE(group_list);
1261 return 0 ;
1262}
1263
1264#endif /* HAVE_BROKEN_GETGROUPS */
1265
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.
1268 */
1269#if defined (FREEBSD) || defined (DARWINOS)
1270#define USE_BSD_SETGROUPS
1271#endif
1272
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.
1278 */
1279static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
1280{
1281 gid_t *new_gidset = NULL;
1282 int max;
1283 int ret;
1284
1285 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
1286 max = groups_max();
1287
1288 /* No group list, just make sure we are setting the efective GID. */
1289 if (setlen == 0) {
1290 return setgroups(1, &primary_gid);
1291 }
1292
1293 /* If the primary gid is not the first array element, grow the array
1294 * and insert it at the front.
1295 */
1296 if (gidset[0] != primary_gid) {
1297 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
1298 if (new_gidset == NULL) {
1299 return -1;
1300 }
1301
1302 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
1303 new_gidset[0] = primary_gid;
1304 setlen++;
1305 }
1306
1307 if (setlen > max) {
1308 DEBUG(3, ("forced to truncate group list from %d to %d\n",
1309 setlen, max));
1310 setlen = max;
1311 }
1312
1313#if defined(HAVE_BROKEN_GETGROUPS)
1314 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
1315#else
1316 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
1317#endif
1318
1319 if (new_gidset) {
1320 int errsav = errno;
1321 SAFE_FREE(new_gidset);
1322 errno = errsav;
1323 }
1324
1325 return ret;
1326}
1327
1328#endif /* USE_BSD_SETGROUPS */
1329
1330/**************************************************************************
1331 Wrapper for getgroups. Deals with broken (int) case.
1332****************************************************************************/
1333
1334int sys_getgroups(int setlen, gid_t *gidset)
1335{
1336#if defined(HAVE_BROKEN_GETGROUPS)
1337 return sys_broken_getgroups(setlen, gidset);
1338#else
1339 return getgroups(setlen, gidset);
1340#endif
1341}
1342
1343/**************************************************************************
1344 Wrapper for setgroups. Deals with broken (int) case and BSD case.
1345****************************************************************************/
1346
1347int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
1348{
1349#if !defined(HAVE_SETGROUPS)
1350 errno = ENOSYS;
1351 return -1;
1352#endif /* HAVE_SETGROUPS */
1353
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);
1358#else
1359 return setgroups(setlen, gidset);
1360#endif
1361}
1362
1363/**************************************************************************
1364 Extract a command into an arg list.
1365****************************************************************************/
1366
1367static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
1368{
1369 char *trunc_cmd;
1370 char *saveptr;
1371 char *ptr;
1372 int argcl;
1373 char **argl = NULL;
1374 int i;
1375
1376 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1377 DEBUG(0, ("talloc failed\n"));
1378 goto nomem;
1379 }
1380
1381 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1382 TALLOC_FREE(trunc_cmd);
1383 errno = EINVAL;
1384 return NULL;
1385 }
1386
1387 /*
1388 * Count the args.
1389 */
1390
1391 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1392 argcl++;
1393
1394 TALLOC_FREE(trunc_cmd);
1395
1396 if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
1397 goto nomem;
1398 }
1399
1400 /*
1401 * Now do the extraction.
1402 */
1403
1404 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1405 goto nomem;
1406 }
1407
1408 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1409 i = 0;
1410
1411 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1412 goto nomem;
1413 }
1414
1415 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1416
1417 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1418 goto nomem;
1419 }
1420 }
1421
1422 argl[i++] = NULL;
1423 TALLOC_FREE(trunc_cmd);
1424 return argl;
1425
1426 nomem:
1427 DEBUG(0, ("talloc failed\n"));
1428 TALLOC_FREE(trunc_cmd);
1429 TALLOC_FREE(argl);
1430 errno = ENOMEM;
1431 return NULL;
1432}
1433
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****************************************************************************/
1439
1440typedef struct _popen_list
1441{
1442 int fd;
1443 pid_t child_pid;
1444 struct _popen_list *next;
1445} popen_list;
1446
1447static popen_list *popen_chain;
1448
1449int sys_popen(const char *command)
1450{
1451 int parent_end, child_end;
1452 int pipe_fds[2];
1453 popen_list *entry = NULL;
1454 char **argl = NULL;
1455
1456 if (pipe(pipe_fds) < 0)
1457 return -1;
1458
1459 parent_end = pipe_fds[0];
1460 child_end = pipe_fds[1];
1461
1462 if (!*command) {
1463 errno = EINVAL;
1464 goto err_exit;
1465 }
1466
1467 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1468 goto err_exit;
1469
1470 ZERO_STRUCTP(entry);
1471
1472 /*
1473 * Extract the command and args into a NULL terminated array.
1474 */
1475
1476 if(!(argl = extract_args(NULL, command)))
1477 goto err_exit;
1478
1479 entry->child_pid = sys_fork();
1480
1481 if (entry->child_pid == -1) {
1482 goto err_exit;
1483 }
1484
1485 if (entry->child_pid == 0) {
1486
1487 /*
1488 * Child !
1489 */
1490
1491 int child_std_end = STDOUT_FILENO;
1492 popen_list *p;
1493
1494 close(parent_end);
1495 if (child_end != child_std_end) {
1496 dup2 (child_end, child_std_end);
1497 close (child_end);
1498 }
1499
1500 /*
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."
1504 */
1505
1506 for (p = popen_chain; p; p = p->next)
1507 close(p->fd);
1508
1509 execv(argl[0], argl);
1510 _exit (127);
1511 }
1512
1513 /*
1514 * Parent.
1515 */
1516
1517 close (child_end);
1518 TALLOC_FREE(argl);
1519
1520 /* Link into popen_chain. */
1521 entry->next = popen_chain;
1522 popen_chain = entry;
1523 entry->fd = parent_end;
1524
1525 return entry->fd;
1526
1527err_exit:
1528
1529 SAFE_FREE(entry);
1530 TALLOC_FREE(argl);
1531 close(pipe_fds[0]);
1532 close(pipe_fds[1]);
1533 return -1;
1534}
1535
1536/**************************************************************************
1537 Wrapper for pclose. Modified from the glibc sources.
1538****************************************************************************/
1539
1540int sys_pclose(int fd)
1541{
1542 int wstatus;
1543 popen_list **ptr = &popen_chain;
1544 popen_list *entry = NULL;
1545 pid_t wait_pid;
1546 int status = -1;
1547
1548 /* Unlink from popen_chain. */
1549 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1550 if ((*ptr)->fd == fd) {
1551 entry = *ptr;
1552 *ptr = (*ptr)->next;
1553 status = 0;
1554 break;
1555 }
1556 }
1557
1558 if (status < 0 || close(entry->fd) < 0)
1559 return -1;
1560
1561 /*
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.
1565 */
1566
1567 do {
1568 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1569 } while (wait_pid == -1 && errno == EINTR);
1570
1571 SAFE_FREE(entry);
1572
1573 if (wait_pid == -1)
1574 return -1;
1575 return wstatus;
1576}
1577
1578/**************************************************************************
1579 Wrapper for Admin Logs.
1580****************************************************************************/
1581
1582 void sys_adminlog(int priority, const char *format_str, ...)
1583{
1584 va_list ap;
1585 int ret;
1586 char *msgbuf = NULL;
1587
1588 va_start( ap, format_str );
1589 ret = vasprintf( &msgbuf, format_str, ap );
1590 va_end( ap );
1591
1592 if (ret == -1)
1593 return;
1594
1595#if defined(HAVE_SYSLOG)
1596 syslog( priority, "%s", msgbuf );
1597#else
1598 DEBUG(0,("%s", msgbuf ));
1599#endif
1600 SAFE_FREE(msgbuf);
1601}
1602
1603/******** Solaris EA helper function prototypes ********/
1604#ifdef HAVE_ATTROPEN
1605#define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1606static int solaris_write_xattr(int attrfd, const char *value, size_t size);
1607static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size);
1608static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size);
1609static int solaris_unlinkat(int attrdirfd, const char *name);
1610static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode);
1611static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode);
1612#endif
1613
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****************************************************************************/
1618
1619ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1620{
1621#if defined(HAVE_GETXATTR)
1622#ifndef XATTR_ADD_OPT
1623 return getxattr(path, name, value, size);
1624#else
1625 int options = 0;
1626 return getxattr(path, name, value, size, 0, options);
1627#endif
1628#elif defined(HAVE_GETEA)
1629 return getea(path, name, value, size);
1630#elif defined(HAVE_EXTATTR_GET_FILE)
1631 char *s;
1632 ssize_t retval;
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;
1636 /*
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.
1640 */
1641 if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1642 if(retval > size) {
1643 errno = ERANGE;
1644 return -1;
1645 }
1646 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1647 return retval;
1648 }
1649
1650 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1651 return -1;
1652#elif defined(HAVE_ATTR_GET)
1653 int retval, flags = 0;
1654 int valuelength = (int)size;
1655 char *attrname = strchr(name,'.') + 1;
1656
1657 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1658
1659 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1660
1661 return retval ? retval : valuelength;
1662#elif defined(HAVE_ATTROPEN)
1663 ssize_t ret = -1;
1664 int attrfd = solaris_attropen(path, name, O_RDONLY, 0);
1665 if (attrfd >= 0) {
1666 ret = solaris_read_xattr(attrfd, value, size);
1667 close(attrfd);
1668 }
1669 return ret;
1670#elif defined(__OS2__)
1671 return unigetxattr(path, 0, name, value, size);
1672#else
1673 errno = ENOSYS;
1674 return -1;
1675#endif
1676}
1677
1678ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1679{
1680#if defined(HAVE_LGETXATTR)
1681 return lgetxattr(path, name, value, size);
1682#elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1683 int options = XATTR_NOFOLLOW;
1684 return getxattr(path, name, value, size, 0, options);
1685#elif defined(HAVE_LGETEA)
1686 return lgetea(path, name, value, size);
1687#elif defined(HAVE_EXTATTR_GET_LINK)
1688 char *s;
1689 ssize_t retval;
1690 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1691 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1692 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1693
1694 if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1695 if(retval > size) {
1696 errno = ERANGE;
1697 return -1;
1698 }
1699 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1700 return retval;
1701 }
1702
1703 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1704 return -1;
1705#elif defined(HAVE_ATTR_GET)
1706 int retval, flags = ATTR_DONTFOLLOW;
1707 int valuelength = (int)size;
1708 char *attrname = strchr(name,'.') + 1;
1709
1710 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1711
1712 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1713
1714 return retval ? retval : valuelength;
1715#elif defined(HAVE_ATTROPEN)
1716 ssize_t ret = -1;
1717 int attrfd = solaris_attropen(path, name, O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1718 if (attrfd >= 0) {
1719 ret = solaris_read_xattr(attrfd, value, size);
1720 close(attrfd);
1721 }
1722 return ret;
1723#elif defined(__OS2__)
1724 return unigetxattr(path, 0, name, value, size);
1725#else
1726 errno = ENOSYS;
1727 return -1;
1728#endif
1729}
1730
1731ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1732{
1733#if defined(HAVE_FGETXATTR)
1734#ifndef XATTR_ADD_OPT
1735 return fgetxattr(filedes, name, value, size);
1736#else
1737 int options = 0;
1738 return fgetxattr(filedes, name, value, size, 0, options);
1739#endif
1740#elif defined(HAVE_FGETEA)
1741 return fgetea(filedes, name, value, size);
1742#elif defined(HAVE_EXTATTR_GET_FD)
1743 char *s;
1744 ssize_t retval;
1745 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1746 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1747 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1748
1749 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1750 if(retval > size) {
1751 errno = ERANGE;
1752 return -1;
1753 }
1754 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1755 return retval;
1756 }
1757
1758 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1759 return -1;
1760#elif defined(HAVE_ATTR_GETF)
1761 int retval, flags = 0;
1762 int valuelength = (int)size;
1763 char *attrname = strchr(name,'.') + 1;
1764
1765 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1766
1767 retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1768
1769 return retval ? retval : valuelength;
1770#elif defined(HAVE_ATTROPEN)
1771 ssize_t ret = -1;
1772 int attrfd = solaris_openat(filedes, name, O_RDONLY|O_XATTR, 0);
1773 if (attrfd >= 0) {
1774 ret = solaris_read_xattr(attrfd, value, size);
1775 close(attrfd);
1776 }
1777 return ret;
1778#elif defined(__OS2__)
1779 return unigetxattr(0, filedes, name, value, size);
1780#else
1781 errno = ENOSYS;
1782 return -1;
1783#endif
1784}
1785
1786#if defined(HAVE_EXTATTR_LIST_FILE)
1787
1788#define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1789
1790static struct {
1791 int space;
1792 const char *name;
1793 size_t len;
1794}
1795extattr[] = {
1796 { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1797 { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1798};
1799
1800typedef union {
1801 const char *path;
1802 int filedes;
1803} extattr_arg;
1804
1805static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1806{
1807 ssize_t list_size, total_size = 0;
1808 int i, t, len;
1809 char *buf;
1810 /* Iterate through extattr(2) namespaces */
1811 for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1812 if (t != EXTATTR_NAMESPACE_USER && geteuid() != 0) {
1813 /* ignore all but user namespace when we are not root, see bug 10247 */
1814 continue;
1815 }
1816 switch(type) {
1817#if defined(HAVE_EXTATTR_LIST_FILE)
1818 case 0:
1819 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1820 break;
1821#endif
1822#if defined(HAVE_EXTATTR_LIST_LINK)
1823 case 1:
1824 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1825 break;
1826#endif
1827#if defined(HAVE_EXTATTR_LIST_FD)
1828 case 2:
1829 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1830 break;
1831#endif
1832 default:
1833 errno = ENOSYS;
1834 return -1;
1835 }
1836 /* Some error happend. Errno should be set by the previous call */
1837 if(list_size < 0)
1838 return -1;
1839 /* No attributes */
1840 if(list_size == 0)
1841 continue;
1842 /* XXX: Call with an empty buffer may be used to calculate
1843 necessary buffer size. Unfortunately, we can't say, how
1844 many attributes were returned, so here is the potential
1845 problem with the emulation.
1846 */
1847 if(list == NULL) {
1848 /* Take the worse case of one char attribute names -
1849 two bytes per name plus one more for sanity.
1850 */
1851 total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1852 continue;
1853 }
1854 /* Count necessary offset to fit namespace prefixes */
1855 len = 0;
1856 for(i = 0; i < list_size; i += list[i] + 1)
1857 len += extattr[t].len;
1858
1859 total_size += list_size + len;
1860 /* Buffer is too small to fit the results */
1861 if(total_size > size) {
1862 errno = ERANGE;
1863 return -1;
1864 }
1865 /* Shift results back, so we can prepend prefixes */
1866 buf = (char *)memmove(list + len, list, list_size);
1867
1868 for(i = 0; i < list_size; i += len + 1) {
1869 len = buf[i];
1870 strncpy(list, extattr[t].name, extattr[t].len + 1);
1871 list += extattr[t].len;
1872 strncpy(list, buf + i + 1, len);
1873 list[len] = '\0';
1874 list += len + 1;
1875 }
1876 size -= total_size;
1877 }
1878 return total_size;
1879}
1880
1881#endif
1882
1883#if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1884static char attr_buffer[ATTR_MAX_VALUELEN];
1885
1886static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1887{
1888 int retval = 0, index;
1889 attrlist_cursor_t *cursor = 0;
1890 int total_size = 0;
1891 attrlist_t * al = (attrlist_t *)attr_buffer;
1892 attrlist_ent_t *ae;
1893 size_t ent_size, left = size;
1894 char *bp = list;
1895
1896 while (True) {
1897 if (filedes)
1898 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1899 else
1900 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1901 if (retval) break;
1902 for (index = 0; index < al->al_count; index++) {
1903 ae = ATTR_ENTRY(attr_buffer, index);
1904 ent_size = strlen(ae->a_name) + sizeof("user.");
1905 if (left >= ent_size) {
1906 strncpy(bp, "user.", sizeof("user."));
1907 strncat(bp, ae->a_name, ent_size - sizeof("user."));
1908 bp += ent_size;
1909 left -= ent_size;
1910 } else if (size) {
1911 errno = ERANGE;
1912 retval = -1;
1913 break;
1914 }
1915 total_size += ent_size;
1916 }
1917 if (al->al_more == 0) break;
1918 }
1919 if (retval == 0) {
1920 flags |= ATTR_ROOT;
1921 cursor = 0;
1922 while (True) {
1923 if (filedes)
1924 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1925 else
1926 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1927 if (retval) break;
1928 for (index = 0; index < al->al_count; index++) {
1929 ae = ATTR_ENTRY(attr_buffer, index);
1930 ent_size = strlen(ae->a_name) + sizeof("system.");
1931 if (left >= ent_size) {
1932 strncpy(bp, "system.", sizeof("system."));
1933 strncat(bp, ae->a_name, ent_size - sizeof("system."));
1934 bp += ent_size;
1935 left -= ent_size;
1936 } else if (size) {
1937 errno = ERANGE;
1938 retval = -1;
1939 break;
1940 }
1941 total_size += ent_size;
1942 }
1943 if (al->al_more == 0) break;
1944 }
1945 }
1946 return (ssize_t)(retval ? retval : total_size);
1947}
1948
1949#endif
1950
1951ssize_t sys_listxattr (const char *path, char *list, size_t size)
1952{
1953#if defined(HAVE_LISTXATTR)
1954#ifndef XATTR_ADD_OPT
1955 return listxattr(path, list, size);
1956#else
1957 int options = 0;
1958 return listxattr(path, list, size, options);
1959#endif
1960#elif defined(HAVE_LISTEA)
1961 return listea(path, list, size);
1962#elif defined(HAVE_EXTATTR_LIST_FILE)
1963 extattr_arg arg;
1964 arg.path = path;
1965 return bsd_attr_list(0, arg, list, size);
1966#elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1967 return irix_attr_list(path, 0, list, size, 0);
1968#elif defined(HAVE_ATTROPEN)
1969 ssize_t ret = -1;
1970 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1971 if (attrdirfd >= 0) {
1972 ret = solaris_list_xattr(attrdirfd, list, size);
1973 close(attrdirfd);
1974 }
1975 return ret;
1976#elif defined(__OS2__)
1977 return unilistxattr(path, 0, list, size);
1978#else
1979 errno = ENOSYS;
1980 return -1;
1981#endif
1982}
1983
1984ssize_t sys_llistxattr (const char *path, char *list, size_t size)
1985{
1986#if defined(HAVE_LLISTXATTR)
1987 return llistxattr(path, list, size);
1988#elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1989 int options = XATTR_NOFOLLOW;
1990 return listxattr(path, list, size, options);
1991#elif defined(HAVE_LLISTEA)
1992 return llistea(path, list, size);
1993#elif defined(HAVE_EXTATTR_LIST_LINK)
1994 extattr_arg arg;
1995 arg.path = path;
1996 return bsd_attr_list(1, arg, list, size);
1997#elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1998 return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
1999#elif defined(HAVE_ATTROPEN)
2000 ssize_t ret = -1;
2001 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
2002 if (attrdirfd >= 0) {
2003 ret = solaris_list_xattr(attrdirfd, list, size);
2004 close(attrdirfd);
2005 }
2006 return ret;
2007#elif defined(__OS2__)
2008 return unilistxattr(path, 0, list, size);
2009#else
2010 errno = ENOSYS;
2011 return -1;
2012#endif
2013}
2014
2015ssize_t sys_flistxattr (int filedes, char *list, size_t size)
2016{
2017#if defined(HAVE_FLISTXATTR)
2018#ifndef XATTR_ADD_OPT
2019 return flistxattr(filedes, list, size);
2020#else
2021 int options = 0;
2022 return flistxattr(filedes, list, size, options);
2023#endif
2024#elif defined(HAVE_FLISTEA)
2025 return flistea(filedes, list, size);
2026#elif defined(HAVE_EXTATTR_LIST_FD)
2027 extattr_arg arg;
2028 arg.filedes = filedes;
2029 return bsd_attr_list(2, arg, list, size);
2030#elif defined(HAVE_ATTR_LISTF)
2031 return irix_attr_list(NULL, filedes, list, size, 0);
2032#elif defined(HAVE_ATTROPEN)
2033 ssize_t ret = -1;
2034 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
2035 if (attrdirfd >= 0) {
2036 ret = solaris_list_xattr(attrdirfd, list, size);
2037 close(attrdirfd);
2038 }
2039 return ret;
2040#elif defined(__OS2__)
2041 return unilistxattr(0, filedes, list, size);
2042#else
2043 errno = ENOSYS;
2044 return -1;
2045#endif
2046}
2047
2048int sys_removexattr (const char *path, const char *name)
2049{
2050#if defined(HAVE_REMOVEXATTR)
2051#ifndef XATTR_ADD_OPT
2052 return removexattr(path, name);
2053#else
2054 int options = 0;
2055 return removexattr(path, name, options);
2056#endif
2057#elif defined(HAVE_REMOVEEA)
2058 return removeea(path, name);
2059#elif defined(HAVE_EXTATTR_DELETE_FILE)
2060 char *s;
2061 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2062 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2063 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2064
2065 return extattr_delete_file(path, attrnamespace, attrname);
2066#elif defined(HAVE_ATTR_REMOVE)
2067 int flags = 0;
2068 char *attrname = strchr(name,'.') + 1;
2069
2070 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2071
2072 return attr_remove(path, attrname, flags);
2073#elif defined(HAVE_ATTROPEN)
2074 int ret = -1;
2075 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
2076 if (attrdirfd >= 0) {
2077 ret = solaris_unlinkat(attrdirfd, name);
2078 close(attrdirfd);
2079 }
2080 return ret;
2081#elif defined(__OS2__)
2082 return uniremovexattr (path, 0, name);
2083#else
2084 errno = ENOSYS;
2085 return -1;
2086#endif
2087}
2088
2089int sys_lremovexattr (const char *path, const char *name)
2090{
2091#if defined(HAVE_LREMOVEXATTR)
2092 return lremovexattr(path, name);
2093#elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
2094 int options = XATTR_NOFOLLOW;
2095 return removexattr(path, name, options);
2096#elif defined(HAVE_LREMOVEEA)
2097 return lremoveea(path, name);
2098#elif defined(HAVE_EXTATTR_DELETE_LINK)
2099 char *s;
2100 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2101 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2102 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2103
2104 return extattr_delete_link(path, attrnamespace, attrname);
2105#elif defined(HAVE_ATTR_REMOVE)
2106 int flags = ATTR_DONTFOLLOW;
2107 char *attrname = strchr(name,'.') + 1;
2108
2109 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2110
2111 return attr_remove(path, attrname, flags);
2112#elif defined(HAVE_ATTROPEN)
2113 int ret = -1;
2114 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
2115 if (attrdirfd >= 0) {
2116 ret = solaris_unlinkat(attrdirfd, name);
2117 close(attrdirfd);
2118 }
2119 return ret;
2120#elif defined(__OS2__)
2121 return uniremovexattr (path, 0, name);
2122#else
2123 errno = ENOSYS;
2124 return -1;
2125#endif
2126}
2127
2128int sys_fremovexattr (int filedes, const char *name)
2129{
2130#if defined(HAVE_FREMOVEXATTR)
2131#ifndef XATTR_ADD_OPT
2132 return fremovexattr(filedes, name);
2133#else
2134 int options = 0;
2135 return fremovexattr(filedes, name, options);
2136#endif
2137#elif defined(HAVE_FREMOVEEA)
2138 return fremoveea(filedes, name);
2139#elif defined(HAVE_EXTATTR_DELETE_FD)
2140 char *s;
2141 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2142 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2143 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2144
2145 return extattr_delete_fd(filedes, attrnamespace, attrname);
2146#elif defined(HAVE_ATTR_REMOVEF)
2147 int flags = 0;
2148 char *attrname = strchr(name,'.') + 1;
2149
2150 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2151
2152 return attr_removef(filedes, attrname, flags);
2153#elif defined(HAVE_ATTROPEN)
2154 int ret = -1;
2155 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
2156 if (attrdirfd >= 0) {
2157 ret = solaris_unlinkat(attrdirfd, name);
2158 close(attrdirfd);
2159 }
2160 return ret;
2161#elif defined(__OS2__)
2162 return uniremovexattr (0, filedes, name);
2163#else
2164 errno = ENOSYS;
2165 return -1;
2166#endif
2167}
2168
2169int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2170{
2171#if defined(HAVE_SETXATTR)
2172#ifndef XATTR_ADD_OPT
2173 return setxattr(path, name, value, size, flags);
2174#else
2175 int options = 0;
2176 return setxattr(path, name, value, size, 0, options);
2177#endif
2178#elif defined(HAVE_SETEA)
2179 return setea(path, name, value, size, flags);
2180#elif defined(HAVE_EXTATTR_SET_FILE)
2181 char *s;
2182 int retval = 0;
2183 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2184 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2185 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2186 if (flags) {
2187 /* Check attribute existence */
2188 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
2189 if (retval < 0) {
2190 /* REPLACE attribute, that doesn't exist */
2191 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2192 errno = ENOATTR;
2193 return -1;
2194 }
2195 /* Ignore other errors */
2196 }
2197 else {
2198 /* CREATE attribute, that already exists */
2199 if (flags & XATTR_CREATE) {
2200 errno = EEXIST;
2201 return -1;
2202 }
2203 }
2204 }
2205 retval = extattr_set_file(path, attrnamespace, attrname, value, size);
2206 return (retval < 0) ? -1 : 0;
2207#elif defined(HAVE_ATTR_SET)
2208 int myflags = 0;
2209 char *attrname = strchr(name,'.') + 1;
2210
2211 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2212 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2213 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2214
2215 return attr_set(path, attrname, (const char *)value, size, myflags);
2216#elif defined(HAVE_ATTROPEN)
2217 int ret = -1;
2218 int myflags = O_RDWR;
2219 int attrfd;
2220 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2221 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2222 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2223 if (attrfd >= 0) {
2224 ret = solaris_write_xattr(attrfd, value, size);
2225 close(attrfd);
2226 }
2227 return ret;
2228#elif defined(__OS2__)
2229 return unisetxattr (path, 0, name, value, size, flags);
2230#else
2231 errno = ENOSYS;
2232 return -1;
2233#endif
2234}
2235
2236int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2237{
2238#if defined(HAVE_LSETXATTR)
2239 return lsetxattr(path, name, value, size, flags);
2240#elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2241 int options = XATTR_NOFOLLOW;
2242 return setxattr(path, name, value, size, 0, options);
2243#elif defined(LSETEA)
2244 return lsetea(path, name, value, size, flags);
2245#elif defined(HAVE_EXTATTR_SET_LINK)
2246 char *s;
2247 int retval = 0;
2248 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2249 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2250 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2251 if (flags) {
2252 /* Check attribute existence */
2253 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
2254 if (retval < 0) {
2255 /* REPLACE attribute, that doesn't exist */
2256 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2257 errno = ENOATTR;
2258 return -1;
2259 }
2260 /* Ignore other errors */
2261 }
2262 else {
2263 /* CREATE attribute, that already exists */
2264 if (flags & XATTR_CREATE) {
2265 errno = EEXIST;
2266 return -1;
2267 }
2268 }
2269 }
2270
2271 retval = extattr_set_link(path, attrnamespace, attrname, value, size);
2272 return (retval < 0) ? -1 : 0;
2273#elif defined(HAVE_ATTR_SET)
2274 int myflags = ATTR_DONTFOLLOW;
2275 char *attrname = strchr(name,'.') + 1;
2276
2277 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2278 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2279 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2280
2281 return attr_set(path, attrname, (const char *)value, size, myflags);
2282#elif defined(HAVE_ATTROPEN)
2283 int ret = -1;
2284 int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW;
2285 int attrfd;
2286 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2287 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2288 attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2289 if (attrfd >= 0) {
2290 ret = solaris_write_xattr(attrfd, value, size);
2291 close(attrfd);
2292 }
2293 return ret;
2294#elif defined(__OS2__)
2295 return unisetxattr (path, 0, name, value, size, flags);
2296#else
2297 errno = ENOSYS;
2298 return -1;
2299#endif
2300}
2301
2302int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
2303{
2304#if defined(HAVE_FSETXATTR)
2305#ifndef XATTR_ADD_OPT
2306 return fsetxattr(filedes, name, value, size, flags);
2307#else
2308 int options = 0;
2309 return fsetxattr(filedes, name, value, size, 0, options);
2310#endif
2311#elif defined(HAVE_FSETEA)
2312 return fsetea(filedes, name, value, size, flags);
2313#elif defined(HAVE_EXTATTR_SET_FD)
2314 char *s;
2315 int retval = 0;
2316 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2317 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2318 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2319 if (flags) {
2320 /* Check attribute existence */
2321 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
2322 if (retval < 0) {
2323 /* REPLACE attribute, that doesn't exist */
2324 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2325 errno = ENOATTR;
2326 return -1;
2327 }
2328 /* Ignore other errors */
2329 }
2330 else {
2331 /* CREATE attribute, that already exists */
2332 if (flags & XATTR_CREATE) {
2333 errno = EEXIST;
2334 return -1;
2335 }
2336 }
2337 }
2338 retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
2339 return (retval < 0) ? -1 : 0;
2340#elif defined(HAVE_ATTR_SETF)
2341 int myflags = 0;
2342 char *attrname = strchr(name,'.') + 1;
2343
2344 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2345 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2346 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2347
2348 return attr_setf(filedes, attrname, (const char *)value, size, myflags);
2349#elif defined(HAVE_ATTROPEN)
2350 int ret = -1;
2351 int myflags = O_RDWR | O_XATTR;
2352 int attrfd;
2353 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2354 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2355 attrfd = solaris_openat(filedes, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2356 if (attrfd >= 0) {
2357 ret = solaris_write_xattr(attrfd, value, size);
2358 close(attrfd);
2359 }
2360 return ret;
2361#elif defined(__OS2__)
2362 return unisetxattr (0, filedes, name, value, size, flags);
2363#else
2364 errno = ENOSYS;
2365 return -1;
2366#endif
2367}
2368
2369/**************************************************************************
2370 helper functions for Solaris' EA support
2371****************************************************************************/
2372#ifdef HAVE_ATTROPEN
2373static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size)
2374{
2375 struct stat sbuf;
2376
2377 if (fstat(attrfd, &sbuf) == -1) {
2378 errno = ENOATTR;
2379 return -1;
2380 }
2381
2382 /* This is to return the current size of the named extended attribute */
2383 if (size == 0) {
2384 return sbuf.st_size;
2385 }
2386
2387 /* check size and read xattr */
2388 if (sbuf.st_size > size) {
2389 errno = ERANGE;
2390 return -1;
2391 }
2392
2393 return read(attrfd, value, sbuf.st_size);
2394}
2395
2396static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
2397{
2398 ssize_t len = 0;
2399 DIR *dirp;
2400 struct dirent *de;
2401 int newfd = dup(attrdirfd);
2402 /* CAUTION: The originating file descriptor should not be
2403 used again following the call to fdopendir().
2404 For that reason we dup() the file descriptor
2405 here to make things more clear. */
2406 dirp = fdopendir(newfd);
2407
2408 while ((de = readdir(dirp))) {
2409 size_t listlen = strlen(de->d_name);
2410 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
2411 /* we don't want "." and ".." here: */
2412 DEBUG(10,("skipped EA %s\n",de->d_name));
2413 continue;
2414 }
2415
2416 if (size == 0) {
2417 /* return the current size of the list of extended attribute names*/
2418 len += listlen + 1;
2419 } else {
2420 /* check size and copy entrieѕ + nul into list. */
2421 if ((len + listlen + 1) > size) {
2422 errno = ERANGE;
2423 len = -1;
2424 break;
2425 } else {
2426 safe_strcpy(list + len, de->d_name, listlen);
2427 len += listlen;
2428 list[len] = '\0';
2429 ++len;
2430 }
2431 }
2432 }
2433
2434 if (closedir(dirp) == -1) {
2435 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno)));
2436 return -1;
2437 }
2438 return len;
2439}
2440
2441static int solaris_unlinkat(int attrdirfd, const char *name)
2442{
2443 if (unlinkat(attrdirfd, name, 0) == -1) {
2444 if (errno == ENOENT) {
2445 errno = ENOATTR;
2446 }
2447 return -1;
2448 }
2449 return 0;
2450}
2451
2452static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode)
2453{
2454 int filedes = attropen(path, attrpath, oflag, mode);
2455 if (filedes == -1) {
2456 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path,attrpath,strerror(errno)));
2457 if (errno == EINVAL) {
2458 errno = ENOTSUP;
2459 } else {
2460 errno = ENOATTR;
2461 }
2462 }
2463 return filedes;
2464}
2465
2466static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode)
2467{
2468 int filedes = openat(fildes, path, oflag, mode);
2469 if (filedes == -1) {
2470 DEBUG(10,("openat FAILED: fd: %d, path: %s, errno: %s\n",filedes,path,strerror(errno)));
2471 if (errno == EINVAL) {
2472 errno = ENOTSUP;
2473 } else {
2474 errno = ENOATTR;
2475 }
2476 }
2477 return filedes;
2478}
2479
2480static int solaris_write_xattr(int attrfd, const char *value, size_t size)
2481{
2482 if ((ftruncate(attrfd, 0) == 0) && (write(attrfd, value, size) == size)) {
2483 return 0;
2484 } else {
2485 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2486 return -1;
2487 }
2488}
2489#endif /*HAVE_ATTROPEN*/
2490
2491
2492/****************************************************************************
2493 Return the major devicenumber for UNIX extensions.
2494****************************************************************************/
2495
2496uint32 unix_dev_major(SMB_DEV_T dev)
2497{
2498#if defined(HAVE_DEVICE_MAJOR_FN)
2499 return (uint32)major(dev);
2500#else
2501 return (uint32)(dev >> 8);
2502#endif
2503}
2504
2505/****************************************************************************
2506 Return the minor devicenumber for UNIX extensions.
2507****************************************************************************/
2508
2509uint32 unix_dev_minor(SMB_DEV_T dev)
2510{
2511#if defined(HAVE_DEVICE_MINOR_FN)
2512 return (uint32)minor(dev);
2513#else
2514 return (uint32)(dev & 0xff);
2515#endif
2516}
2517
2518#if defined(WITH_AIO)
2519
2520/*******************************************************************
2521 An aio_read wrapper that will deal with 64-bit sizes.
2522********************************************************************/
2523
2524int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2525{
2526#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2527 return aio_read64(aiocb);
2528#elif defined(HAVE_AIO_READ)
2529 return aio_read(aiocb);
2530#else
2531 errno = ENOSYS;
2532 return -1;
2533#endif
2534}
2535
2536/*******************************************************************
2537 An aio_write wrapper that will deal with 64-bit sizes.
2538********************************************************************/
2539
2540int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2541{
2542#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2543 return aio_write64(aiocb);
2544#elif defined(HAVE_AIO_WRITE)
2545 return aio_write(aiocb);
2546#else
2547 errno = ENOSYS;
2548 return -1;
2549#endif
2550}
2551
2552/*******************************************************************
2553 An aio_return wrapper that will deal with 64-bit sizes.
2554********************************************************************/
2555
2556ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2557{
2558#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2559 return aio_return64(aiocb);
2560#elif defined(HAVE_AIO_RETURN)
2561 return aio_return(aiocb);
2562#else
2563 errno = ENOSYS;
2564 return -1;
2565#endif
2566}
2567
2568/*******************************************************************
2569 An aio_cancel wrapper that will deal with 64-bit sizes.
2570********************************************************************/
2571
2572int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2573{
2574#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2575 return aio_cancel64(fd, aiocb);
2576#elif defined(HAVE_AIO_CANCEL)
2577 return aio_cancel(fd, aiocb);
2578#else
2579 errno = ENOSYS;
2580 return -1;
2581#endif
2582}
2583
2584/*******************************************************************
2585 An aio_error wrapper that will deal with 64-bit sizes.
2586********************************************************************/
2587
2588int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2589{
2590#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2591 return aio_error64(aiocb);
2592#elif defined(HAVE_AIO_ERROR)
2593 return aio_error(aiocb);
2594#else
2595 errno = ENOSYS;
2596 return -1;
2597#endif
2598}
2599
2600/*******************************************************************
2601 An aio_fsync wrapper that will deal with 64-bit sizes.
2602********************************************************************/
2603
2604int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2605{
2606#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2607 return aio_fsync64(op, aiocb);
2608#elif defined(HAVE_AIO_FSYNC)
2609 return aio_fsync(op, aiocb);
2610#else
2611 errno = ENOSYS;
2612 return -1;
2613#endif
2614}
2615
2616/*******************************************************************
2617 An aio_fsync wrapper that will deal with 64-bit sizes.
2618********************************************************************/
2619
2620int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2621{
2622#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2623 return aio_suspend64(cblist, n, timeout);
2624#elif defined(HAVE_AIO_FSYNC)
2625 return aio_suspend(cblist, n, timeout);
2626#else
2627 errno = ENOSYS;
2628 return -1;
2629#endif
2630}
2631#else /* !WITH_AIO */
2632
2633int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2634{
2635 errno = ENOSYS;
2636 return -1;
2637}
2638
2639int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2640{
2641 errno = ENOSYS;
2642 return -1;
2643}
2644
2645ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2646{
2647 errno = ENOSYS;
2648 return -1;
2649}
2650
2651int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2652{
2653 errno = ENOSYS;
2654 return -1;
2655}
2656
2657int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2658{
2659 errno = ENOSYS;
2660 return -1;
2661}
2662
2663int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2664{
2665 errno = ENOSYS;
2666 return -1;
2667}
2668
2669int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2670{
2671 errno = ENOSYS;
2672 return -1;
2673}
2674#endif /* WITH_AIO */
Note: See TracBrowser for help on using the repository browser.