s3-build: remove EXEEXT from Makefiles
[gd/samba-autobuild/.git] / source3 / lib / system.c
index 02322b72b51b56b7d0c9ca1c6f1c7be198ae1a22..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.
 ********************************************************************/
@@ -541,13 +513,13 @@ void update_stat_ex_create_time(struct stat_ex *dst,
 }
 
 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
-static void init_stat_ex_from_stat (struct stat_ex *dst,
-                                   const struct stat64 *src,
-                                   bool fake_dir_create_times)
+void init_stat_ex_from_stat (struct stat_ex *dst,
+                           const struct stat64 *src,
+                           bool fake_dir_create_times)
 #else
-static void init_stat_ex_from_stat (struct stat_ex *dst,
-                                   const struct stat *src,
-                                   bool fake_dir_create_times)
+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;
@@ -694,6 +666,41 @@ int sys_posix_fallocate(int fd, SMB_OFF_T offset, SMB_OFF_T len)
 #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.
 ********************************************************************/
@@ -730,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
@@ -794,6 +803,15 @@ 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.
 ********************************************************************/
@@ -831,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.
 ********************************************************************/
@@ -929,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)
@@ -1083,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.
 ****************************************************************************/
@@ -1103,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;
@@ -1281,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.
 ****************************************************************************/
@@ -1358,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;
        }
 
@@ -1492,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;
@@ -1767,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:
@@ -2343,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));
@@ -2352,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;
                        }
                }
        }
@@ -2452,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)
 
 /*******************************************************************
@@ -2609,74 +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
-#if defined(HAVE_GETPEEREID)
-       gid_t gid;
-       return getpeereid(s, uid, &gid);
-#endif
-       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);
-}