s3: Use ARRAY_SIZE in bsd_attr_list
[tridge/samba.git] / source3 / lib / system.c
index 517e347c0f9ffc46843aaecb2e44062c80d713e8..a308014f464766cdc5b6a7e1c5e94be2215ed8de 100644 (file)
 */
 
 #include "includes.h"
+#include "system/syslog.h"
+#include "system/capability.h"
+#include "system/passwd.h"
+#include "system/filesys.h"
+
+#ifdef HAVE_SYS_SYSCTL_H
+#include <sys/sysctl.h>
+#endif
 
 #ifdef HAVE_SYS_PRCTL_H
 #include <sys/prctl.h>
 
 
 
-/*******************************************************************
- A wrapper for memalign
-********************************************************************/
-
-void *sys_memalign( size_t align, size_t size )
-{
-#if defined(HAVE_POSIX_MEMALIGN)
-       void *p = NULL;
-       int ret = posix_memalign( &p, align, size );
-       if ( ret == 0 )
-               return p;
-
-       return NULL;
-#elif defined(HAVE_MEMALIGN)
-       return memalign( align, size );
-#else
-       /* On *BSD systems memaligns doesn't exist, but memory will
-        * be aligned on allocations of > pagesize. */
-#if defined(SYSCONF_SC_PAGESIZE)
-       size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
-#elif defined(HAVE_GETPAGESIZE)
-       size_t pagesize = (size_t)getpagesize();
-#else
-       size_t pagesize = (size_t)-1;
-#endif
-       if (pagesize == (size_t)-1) {
-               DEBUG(0,("memalign functionalaity not available on this platform!\n"));
-               return NULL;
-       }
-       if (size < pagesize) {
-               size = pagesize;
-       }
-       return SMB_MALLOC(size);
-#endif
-}
-
 /*******************************************************************
  A wrapper for usleep in case we don't have one.
 ********************************************************************/
@@ -94,7 +66,7 @@ int sys_usleep(long usecs)
         * is not SPEC1170 complient... grumble... JRA.
         */
 
-       if(usecs < 0 || usecs > 1000000) {
+       if(usecs < 0 || usecs > 999999) {
                errno = EINVAL;
                return -1;
        }
@@ -123,7 +95,11 @@ ssize_t sys_read(int fd, void *buf, size_t count)
 
        do {
                ret = read(fd, buf, count);
-       } while (ret == -1 && errno == EINTR);
+#if defined(EWOULDBLOCK)
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
+#else
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+#endif
        return ret;
 }
 
@@ -137,7 +113,11 @@ ssize_t sys_write(int fd, const void *buf, size_t count)
 
        do {
                ret = write(fd, buf, count);
-       } while (ret == -1 && errno == EINTR);
+#if defined(EWOULDBLOCK)
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
+#else
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+#endif
        return ret;
 }
 
@@ -162,7 +142,11 @@ ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
 
        do {
                ret = writev(fd, iov, iovcnt);
-       } while (ret == -1 && errno == EINTR);
+#if defined(EWOULDBLOCK)
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
+#else
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+#endif
        return ret;
 }
 
@@ -207,7 +191,7 @@ ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off)
 #endif
 
 /*******************************************************************
-A send wrapper that will deal with EINTR.
+A send wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
 ********************************************************************/
 
 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
@@ -216,12 +200,16 @@ ssize_t sys_send(int s, const void *msg, size_t len, int flags)
 
        do {
                ret = send(s, msg, len, flags);
-       } while (ret == -1 && errno == EINTR);
+#if defined(EWOULDBLOCK)
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
+#else
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+#endif
        return ret;
 }
 
 /*******************************************************************
-A sendto wrapper that will deal with EINTR.
+A sendto wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
 ********************************************************************/
 
 ssize_t sys_sendto(int s,  const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
@@ -230,12 +218,16 @@ ssize_t sys_sendto(int s,  const void *msg, size_t len, int flags, const struct
 
        do {
                ret = sendto(s, msg, len, flags, to, tolen);
-       } while (ret == -1 && errno == EINTR);
+#if defined(EWOULDBLOCK)
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
+#else
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+#endif
        return ret;
 }
 
 /*******************************************************************
-A recv wrapper that will deal with EINTR.
+A recv wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
 ********************************************************************/
 
 ssize_t sys_recv(int fd, void *buf, size_t count, int flags)
@@ -244,7 +236,11 @@ ssize_t sys_recv(int fd, void *buf, size_t count, int flags)
 
        do {
                ret = recv(fd, buf, count, flags);
-       } while (ret == -1 && errno == EINTR);
+#if defined(EWOULDBLOCK)
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
+#else
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+#endif
        return ret;
 }
 
@@ -258,7 +254,11 @@ ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *f
 
        do {
                ret = recvfrom(s, buf, len, flags, from, fromlen);
-       } while (ret == -1 && errno == EINTR);
+#if defined(EWOULDBLOCK)
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
+#else
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+#endif
        return ret;
 }
 
@@ -290,20 +290,291 @@ int sys_fcntl_long(int fd, int cmd, long arg)
        return ret;
 }
 
+/****************************************************************************
+ Get/Set all the possible time fields from a stat struct as a timespec.
+****************************************************************************/
+
+static struct timespec get_atimespec(const struct stat *pst)
+{
+#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
+       struct timespec ret;
+
+       /* Old system - no ns timestamp. */
+       ret.tv_sec = pst->st_atime;
+       ret.tv_nsec = 0;
+       return ret;
+#else
+#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
+       return pst->st_atim;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
+       struct timespec ret;
+       ret.tv_sec = pst->st_atime;
+       ret.tv_nsec = pst->st_atimensec;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
+       struct timespec ret;
+       ret.tv_sec = pst->st_atime;
+       ret.tv_nsec = pst->st_atime_n;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
+       struct timespec ret;
+       ret.tv_sec = pst->st_atime;
+       ret.tv_nsec = pst->st_uatime * 1000;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
+       return pst->st_atimespec;
+#else
+#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
+#endif
+#endif
+}
+
+static struct timespec get_mtimespec(const struct stat *pst)
+{
+#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
+       struct timespec ret;
+
+       /* Old system - no ns timestamp. */
+       ret.tv_sec = pst->st_mtime;
+       ret.tv_nsec = 0;
+       return ret;
+#else
+#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
+       return pst->st_mtim;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
+       struct timespec ret;
+       ret.tv_sec = pst->st_mtime;
+       ret.tv_nsec = pst->st_mtimensec;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
+       struct timespec ret;
+       ret.tv_sec = pst->st_mtime;
+       ret.tv_nsec = pst->st_mtime_n;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
+       struct timespec ret;
+       ret.tv_sec = pst->st_mtime;
+       ret.tv_nsec = pst->st_umtime * 1000;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
+       return pst->st_mtimespec;
+#else
+#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
+#endif
+#endif
+}
+
+static struct timespec get_ctimespec(const struct stat *pst)
+{
+#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
+       struct timespec ret;
+
+       /* Old system - no ns timestamp. */
+       ret.tv_sec = pst->st_ctime;
+       ret.tv_nsec = 0;
+       return ret;
+#else
+#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
+       return pst->st_ctim;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
+       struct timespec ret;
+       ret.tv_sec = pst->st_ctime;
+       ret.tv_nsec = pst->st_ctimensec;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
+       struct timespec ret;
+       ret.tv_sec = pst->st_ctime;
+       ret.tv_nsec = pst->st_ctime_n;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
+       struct timespec ret;
+       ret.tv_sec = pst->st_ctime;
+       ret.tv_nsec = pst->st_uctime * 1000;
+       return ret;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
+       return pst->st_ctimespec;
+#else
+#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
+#endif
+#endif
+}
+
+/****************************************************************************
+ Return the best approximation to a 'create time' under UNIX from a stat
+ structure.
+****************************************************************************/
+
+static struct timespec calc_create_time_stat(const struct stat *st)
+{
+       struct timespec ret, ret1;
+       struct timespec c_time = get_ctimespec(st);
+       struct timespec m_time = get_mtimespec(st);
+       struct timespec a_time = get_atimespec(st);
+
+       ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
+       ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
+
+       if(!null_timespec(ret1)) {
+               return ret1;
+       }
+
+       /*
+        * One of ctime, mtime or atime was zero (probably atime).
+        * Just return MIN(ctime, mtime).
+        */
+       return ret;
+}
+
+/****************************************************************************
+ Return the best approximation to a 'create time' under UNIX from a stat_ex
+ structure.
+****************************************************************************/
+
+static struct timespec calc_create_time_stat_ex(const struct stat_ex *st)
+{
+       struct timespec ret, ret1;
+       struct timespec c_time = st->st_ex_ctime;
+       struct timespec m_time = st->st_ex_mtime;
+       struct timespec a_time = st->st_ex_atime;
+
+       ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
+       ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
+
+       if(!null_timespec(ret1)) {
+               return ret1;
+       }
+
+       /*
+        * One of ctime, mtime or atime was zero (probably atime).
+        * Just return MIN(ctime, mtime).
+        */
+       return ret;
+}
+
+/****************************************************************************
+ Return the 'create time' from a stat struct if it exists (birthtime) or else
+ use the best approximation.
+****************************************************************************/
+
+static void make_create_timespec(const struct stat *pst, struct stat_ex *dst,
+                                bool fake_dir_create_times)
+{
+       if (S_ISDIR(pst->st_mode) && fake_dir_create_times) {
+               dst->st_ex_btime.tv_sec = 315493200L;          /* 1/1/1980 */
+               dst->st_ex_btime.tv_nsec = 0;
+       }
+
+       dst->st_ex_calculated_birthtime = false;
+
+#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
+       dst->st_ex_btime = pst->st_birthtimespec;
+#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
+       dst->st_ex_btime.tv_sec = pst->st_birthtime;
+       dst->st_ex_btime.tv_nsec = pst->st_birthtimenspec;
+#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
+       dst->st_ex_btime.tv_sec = pst->st_birthtime;
+       dst->st_ex_btime.tv_nsec = 0;
+#else
+       dst->st_ex_btime = calc_create_time_stat(pst);
+       dst->st_ex_calculated_birthtime = true;
+#endif
+
+       /* Deal with systems that don't initialize birthtime correctly.
+        * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
+        */
+       if (null_timespec(dst->st_ex_btime)) {
+               dst->st_ex_btime = calc_create_time_stat(pst);
+               dst->st_ex_calculated_birthtime = true;
+       }
+}
+
+/****************************************************************************
+ If we update a timestamp in a stat_ex struct we may have to recalculate
+ the birthtime. For now only implement this for write time, but we may
+ also need to do it for atime and ctime. JRA.
+****************************************************************************/
+
+void update_stat_ex_mtime(struct stat_ex *dst,
+                               struct timespec write_ts)
+{
+       dst->st_ex_mtime = write_ts;
+
+       /* We may have to recalculate btime. */
+       if (dst->st_ex_calculated_birthtime) {
+               dst->st_ex_btime = calc_create_time_stat_ex(dst);
+       }
+}
+
+void update_stat_ex_create_time(struct stat_ex *dst,
+                                struct timespec create_time)
+{
+       dst->st_ex_btime = create_time;
+       dst->st_ex_calculated_birthtime = false;
+}
+
+#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
+void init_stat_ex_from_stat (struct stat_ex *dst,
+                           const struct stat64 *src,
+                           bool fake_dir_create_times)
+#else
+void init_stat_ex_from_stat (struct stat_ex *dst,
+                           const struct stat *src,
+                           bool fake_dir_create_times)
+#endif
+{
+       dst->st_ex_dev = src->st_dev;
+       dst->st_ex_ino = src->st_ino;
+       dst->st_ex_mode = src->st_mode;
+       dst->st_ex_nlink = src->st_nlink;
+       dst->st_ex_uid = src->st_uid;
+       dst->st_ex_gid = src->st_gid;
+       dst->st_ex_rdev = src->st_rdev;
+       dst->st_ex_size = src->st_size;
+       dst->st_ex_atime = get_atimespec(src);
+       dst->st_ex_mtime = get_mtimespec(src);
+       dst->st_ex_ctime = get_ctimespec(src);
+       make_create_timespec(src, dst, fake_dir_create_times);
+#ifdef HAVE_STAT_ST_BLKSIZE
+       dst->st_ex_blksize = src->st_blksize;
+#else
+       dst->st_ex_blksize = STAT_ST_BLOCKSIZE;
+#endif
+
+#ifdef HAVE_STAT_ST_BLOCKS
+       dst->st_ex_blocks = src->st_blocks;
+#else
+       dst->st_ex_blocks = src->st_size / dst->st_ex_blksize + 1;
+#endif
+
+#ifdef HAVE_STAT_ST_FLAGS
+       dst->st_ex_flags = src->st_flags;
+#else
+       dst->st_ex_flags = 0;
+#endif
+}
+
 /*******************************************************************
 A stat() wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
 
-int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
+int sys_stat(const char *fname, SMB_STRUCT_STAT *sbuf,
+            bool fake_dir_create_times)
 {
        int ret;
 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
-       ret = stat64(fname, sbuf);
+       struct stat64 statbuf;
+       ret = stat64(fname, &statbuf);
 #else
-       ret = stat(fname, sbuf);
+       struct stat statbuf;
+       ret = stat(fname, &statbuf);
 #endif
-       /* we always want directories to appear zero size */
-       if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
+       if (ret == 0) {
+               /* we always want directories to appear zero size */
+               if (S_ISDIR(statbuf.st_mode)) {
+                       statbuf.st_size = 0;
+               }
+               init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
+       }
        return ret;
 }
 
@@ -311,16 +582,23 @@ int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
  An fstat() wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
 
-int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
+int sys_fstat(int fd, SMB_STRUCT_STAT *sbuf, bool fake_dir_create_times)
 {
        int ret;
 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
-       ret = fstat64(fd, sbuf);
+       struct stat64 statbuf;
+       ret = fstat64(fd, &statbuf);
 #else
-       ret = fstat(fd, sbuf);
+       struct stat statbuf;
+       ret = fstat(fd, &statbuf);
 #endif
-       /* we always want directories to appear zero size */
-       if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
+       if (ret == 0) {
+               /* we always want directories to appear zero size */
+               if (S_ISDIR(statbuf.st_mode)) {
+                       statbuf.st_size = 0;
+               }
+               init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
+       }
        return ret;
 }
 
@@ -328,19 +606,101 @@ int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
  An lstat() wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
 
-int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
+int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf,
+             bool fake_dir_create_times)
 {
        int ret;
 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
-       ret = lstat64(fname, sbuf);
+       struct stat64 statbuf;
+       ret = lstat64(fname, &statbuf);
 #else
-       ret = lstat(fname, sbuf);
+       struct stat statbuf;
+       ret = lstat(fname, &statbuf);
 #endif
-       /* we always want directories to appear zero size */
-       if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
+       if (ret == 0) {
+               /* we always want directories to appear zero size */
+               if (S_ISDIR(statbuf.st_mode)) {
+                       statbuf.st_size = 0;
+               }
+               init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
+       }
        return ret;
 }
 
+/*******************************************************************
+ An posix_fallocate() wrapper that will deal with 64 bit filesizes.
+********************************************************************/
+int sys_posix_fallocate(int fd, SMB_OFF_T offset, SMB_OFF_T len)
+{
+#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_POSIX_FALLOCATE64) && !defined(HAVE_BROKEN_POSIX_FALLOCATE)
+       return posix_fallocate64(fd, offset, len);
+#elif defined(HAVE_POSIX_FALLOCATE) && !defined(HAVE_BROKEN_POSIX_FALLOCATE)
+       return posix_fallocate(fd, offset, len);
+#elif defined(F_RESVSP64)
+       /* this handles XFS on IRIX */
+       struct flock64 fl;
+       SMB_OFF_T new_len = offset + len;
+       int ret;
+       struct stat64 sbuf;
+
+       /* unlikely to get a too large file on a 64bit system but ... */
+       if (new_len < 0)
+               return EFBIG;
+
+       fl.l_whence = SEEK_SET;
+       fl.l_start = offset;
+       fl.l_len = len;
+
+       ret=fcntl(fd, F_RESVSP64, &fl);
+
+       if (ret != 0)
+               return errno;
+
+       /* Make sure the file gets enlarged after we allocated space: */
+       fstat64(fd, &sbuf);
+       if (new_len > sbuf.st_size)
+               ftruncate64(fd, new_len);
+       return 0;
+#else
+       return ENOSYS;
+#endif
+}
+
+/*******************************************************************
+ An fallocate() function that matches the semantics of the Linux one.
+********************************************************************/
+
+#ifdef HAVE_LINUX_FALLOC_H
+#include <linux/falloc.h>
+#endif
+
+int sys_fallocate(int fd, enum vfs_fallocate_mode mode, SMB_OFF_T offset, SMB_OFF_T len)
+{
+#if defined(HAVE_LINUX_FALLOCATE64) || defined(HAVE_LINUX_FALLOCATE)
+       int lmode;
+       switch (mode) {
+       case VFS_FALLOCATE_EXTEND_SIZE:
+               lmode = 0;
+               break;
+       case VFS_FALLOCATE_KEEP_SIZE:
+               lmode = FALLOC_FL_KEEP_SIZE;
+               break;
+       default:
+               errno = EINVAL;
+               return -1;
+       }
+#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LINUX_FALLOCATE64)
+       return fallocate64(fd, lmode, offset, len);
+#elif defined(HAVE_LINUX_FALLOCATE)
+       return fallocate(fd, lmode, offset, len);
+#endif
+#else
+       /* TODO - plumb in fallocate from other filesysetms like VXFS etc. JRA. */
+       errno = ENOSYS;
+       return -1;
+#endif
+}
+
 /*******************************************************************
  An ftruncate() wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
@@ -377,6 +737,8 @@ int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
        return fseek64(fp, offset, whence);
 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
        return fseeko64(fp, offset, whence);
+#elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO)
+       return fseeko(fp, offset, whence);
 #else
        return fseek(fp, offset, whence);
 #endif
@@ -441,11 +803,20 @@ FILE *sys_fopen(const char *path, const char *type)
 }
 
 
+#if HAVE_KERNEL_SHARE_MODES
+#ifndef LOCK_MAND
+#define LOCK_MAND      32      /* This is a mandatory flock */
+#define LOCK_READ      64      /* ... Which allows concurrent read operations */
+#define LOCK_WRITE     128     /* ... Which allows concurrent write operations */
+#define LOCK_RW                192     /* ... Which allows concurrent read & write ops */
+#endif
+#endif
+
 /*******************************************************************
  A flock() wrapper that will perform the kernel flock.
 ********************************************************************/
 
-void kernel_flock(int fd, uint32 share_mode)
+void kernel_flock(int fd, uint32 share_mode, uint32 access_mask)
 {
 #if HAVE_KERNEL_SHARE_MODES
        int kernel_mode = 0;
@@ -478,6 +849,24 @@ SMB_STRUCT_DIR *sys_opendir(const char *name)
 #endif
 }
 
+/*******************************************************************
+ An fdopendir wrapper that will deal with 64 bit filesizes.
+ Ugly hack - we need dirfd for this to work correctly in the
+ calling code.. JRA.
+********************************************************************/
+
+SMB_STRUCT_DIR *sys_fdopendir(int fd)
+{
+#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FDOPENDIR64) && defined(HAVE_DIRFD)
+       return fdopendir64(fd);
+#elif defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
+       return fdopendir(fd);
+#else
+       errno = ENOSYS;
+       return NULL;
+#endif
+}
+
 /*******************************************************************
  A readdir wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
@@ -576,18 +965,45 @@ int sys_waitpid(pid_t pid,int *status,int options)
 }
 
 /*******************************************************************
- System wrapper for getwd
+ System wrapper for getwd. Always returns MALLOC'ed memory, or NULL
+ on error (malloc fail usually).
 ********************************************************************/
 
-char *sys_getwd(char *s)
+char *sys_getwd(void)
 {
-       char *wd;
-#ifdef HAVE_GETCWD
-       wd = (char *)getcwd(s, PATH_MAX);
+#ifdef GETCWD_TAKES_NULL
+       return getcwd(NULL, 0);
+#elif HAVE_GETCWD
+       char *wd = NULL, *s = NULL;
+       size_t allocated = PATH_MAX;
+
+       while (1) {
+               s = SMB_REALLOC_ARRAY(s, char, allocated);
+               if (s == NULL) {
+                       return NULL;
+               }
+               wd = getcwd(s, allocated);
+               if (wd) {
+                       break;
+               }
+               if (errno != ERANGE) {
+                       SAFE_FREE(s);
+                       break;
+               }
+               allocated *= 2;
+               if (allocated < PATH_MAX) {
+                       SAFE_FREE(s);
+                       break;
+               }
+       }
+       return wd;
 #else
-       wd = (char *)getwd(s);
+       char *s = SMB_MALLOC_ARRAY(char, PATH_MAX);
+       if (s == NULL) {
+               return NULL;
+       }
+       return getwd(s);
 #endif
-       return wd;
 }
 
 #if defined(HAVE_POSIX_CAPABILITIES)
@@ -730,6 +1146,10 @@ void sys_srandom(unsigned int seed)
 #endif
 }
 
+#ifndef NGROUPS_MAX
+#define NGROUPS_MAX 32 /* Guess... */
+#endif
+
 /**************************************************************************
  Returns equivalent to NGROUPS_MAX - using sysconf if needed.
 ****************************************************************************/
@@ -750,6 +1170,13 @@ int groups_max(void)
 ****************************************************************************/
 
 #if defined(HAVE_BROKEN_GETGROUPS)
+
+#ifdef HAVE_BROKEN_GETGROUPS
+#define GID_T int
+#else
+#define GID_T gid_t
+#endif
+
 static int sys_broken_getgroups(int setlen, gid_t *gidset)
 {
        GID_T gid;
@@ -928,50 +1355,6 @@ int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
 #endif
 }
 
-/**************************************************************************
- Wrappers for setpwent(), getpwent() and endpwent()
-****************************************************************************/
-
-void sys_setpwent(void)
-{
-       setpwent();
-}
-
-struct passwd *sys_getpwent(void)
-{
-       return getpwent();
-}
-
-void sys_endpwent(void)
-{
-       endpwent();
-}
-
-/**************************************************************************
- Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
-****************************************************************************/
-
-
-struct passwd *sys_getpwnam(const char *name)
-{
-       return getpwnam(name);
-}
-
-struct passwd *sys_getpwuid(uid_t uid)
-{
-       return getpwuid(uid);
-}
-
-struct group *sys_getgrnam(const char *name)
-{
-       return getgrnam(name);
-}
-
-struct group *sys_getgrgid(gid_t gid)
-{
-       return getgrgid(gid);
-}
-
 /**************************************************************************
  Extract a command into an arg list.
 ****************************************************************************/
@@ -1005,7 +1388,7 @@ static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
 
        TALLOC_FREE(trunc_cmd);
 
-       if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
+       if (!(argl = talloc_array(mem_ctx, char *, argcl + 1))) {
                goto nomem;
        }
 
@@ -1032,6 +1415,7 @@ static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
        }
 
        argl[i++] = NULL;
+       TALLOC_FREE(trunc_cmd);
        return argl;
 
  nomem:
@@ -1138,7 +1522,7 @@ int sys_popen(const char *command)
 err_exit:
 
        SAFE_FREE(entry);
-       SAFE_FREE(argl);
+       TALLOC_FREE(argl);
        close(pipe_fds[0]);
        close(pipe_fds[1]);
        return -1;
@@ -1413,7 +1797,7 @@ static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size
        int i, t, len;
        char *buf;
        /* Iterate through extattr(2) namespaces */
-       for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
+       for(t = 0; t < ARRAY_SIZE(extattr); t++) {
                switch(type) {
 #if defined(HAVE_EXTATTR_LIST_FILE)
                        case 0:
@@ -1464,7 +1848,7 @@ static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size
                        return -1;
                }
                /* Shift results back, so we can prepend prefixes */
-               buf = memmove(list + len, list, list_size);
+               buf = (char *)memmove(list + len, list, list_size);
 
                for(i = 0; i < list_size; i += len + 1) {
                        len = buf[i];
@@ -1989,7 +2373,7 @@ static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
        dirp = fdopendir(newfd);
 
        while ((de = readdir(dirp))) {
-               size_t listlen = strlen(de->d_name);
+               size_t listlen = strlen(de->d_name) + 1;
                if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
                        /* we don't want "." and ".." here: */
                        DEBUG(10,("skipped EA %s\n",de->d_name));
@@ -1998,18 +2382,16 @@ static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
 
                if (size == 0) {
                        /* return the current size of the list of extended attribute names*/
-                       len += listlen + 1;
+                       len += listlen;
                } else {
                        /* check size and copy entrieŃ• + nul into list. */
-                       if ((len + listlen + 1) > size) {
+                       if ((len + listlen) > size) {
                                errno = ERANGE;
                                len = -1;
                                break;
                        } else {
-                               safe_strcpy(list + len, de->d_name, listlen);
+                               strlcpy(list + len, de->d_name, listlen);
                                len += listlen;
-                               list[len] = '\0';
-                               ++len;
                        }
                }
        }
@@ -2098,6 +2480,53 @@ uint32 unix_dev_minor(SMB_DEV_T dev)
 #endif
 }
 
+#if 0
+/*******************************************************************
+ Return the number of CPUs.
+********************************************************************/
+
+int sys_get_number_of_cores(void)
+{
+       int ret = -1;
+
+#if defined(HAVE_SYSCONF)
+#if defined(_SC_NPROCESSORS_ONLN)
+       ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
+#endif
+#if defined(_SC_NPROCESSORS_CONF)
+       if (ret < 1) {
+               ret = (int)sysconf(_SC_NPROCESSORS_CONF);
+       }
+#endif
+#elif defined(HAVE_SYSCTL) && defined(CTL_HW)
+       int name[2];
+       unsigned int len = sizeof(ret);
+
+       name[0] = CTL_HW;
+#if defined(HW_AVAILCPU)
+       name[1] = HW_AVAILCPU;
+
+       if (sysctl(name, 2, &ret, &len, NULL, 0) == -1) {
+               ret = -1;
+       }
+#endif
+#if defined(HW_NCPU)
+       if(ret < 1) {
+               name[0] = CTL_HW;
+               name[1] = HW_NCPU;
+               if (sysctl(nm, 2, &count, &len, NULL, 0) == -1) {
+                       ret = -1;
+               }
+       }
+#endif
+#endif
+       if (ret < 1) {
+               ret = 1;
+       }
+       return ret;
+}
+#endif
+
 #if defined(WITH_AIO)
 
 /*******************************************************************
@@ -2255,70 +2684,3 @@ int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct
        return -1;
 }
 #endif /* WITH_AIO */
-
-int sys_getpeereid( int s, uid_t *uid)
-{
-#if defined(HAVE_PEERCRED)
-       struct ucred cred;
-       socklen_t cred_len = sizeof(struct ucred);
-       int ret;
-
-       ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
-       if (ret != 0) {
-               return -1;
-       }
-
-       if (cred_len != sizeof(struct ucred)) {
-               errno = EINVAL;
-               return -1;
-       }
-
-       *uid = cred.uid;
-       return 0;
-#else
-       errno = ENOSYS;
-       return -1;
-#endif
-}
-
-int sys_getnameinfo(const struct sockaddr *psa,
-                       socklen_t salen,
-                       char *host,
-                       size_t hostlen,
-                       char *service,
-                       size_t servlen,
-                       int flags)
-{
-       /*
-        * For Solaris we must make sure salen is the
-        * correct length for the incoming sa_family.
-        */
-
-       if (salen == sizeof(struct sockaddr_storage)) {
-               salen = sizeof(struct sockaddr_in);
-#if defined(HAVE_IPV6)
-               if (psa->sa_family == AF_INET6) {
-                       salen = sizeof(struct sockaddr_in6);
-               }
-#endif
-       }
-       return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
-}
-
-int sys_connect(int fd, const struct sockaddr * addr)
-{
-       socklen_t salen = -1;
-
-       if (addr->sa_family == AF_INET) {
-           salen = sizeof(struct sockaddr_in);
-       } else if (addr->sa_family == AF_UNIX) {
-           salen = sizeof(struct sockaddr_un);
-       }
-#if defined(HAVE_IPV6)
-       else if (addr->sa_family == AF_INET6) {
-           salen = sizeof(struct sockaddr_in6);
-       }
-#endif
-
-       return connect(fd, addr, salen);
-}