s3: Remove madvise support
[ira/wip.git] / source3 / lib / util.c
index 2c4cd20f31c52dd54b729ab1cfc7e9692c375175..ec794ca74e765e24a4578fd193a7f874fbd4f5ff 100644 (file)
@@ -57,9 +57,6 @@ extern unsigned int global_clobber_region_line;
 
 enum protocol_types Protocol = PROTOCOL_COREPLUS;
 
-/* this is used by the chaining code */
-int chain_size = 0;
-
 static enum remote_arch_types ra_type = RA_UNKNOWN;
 
 /***********************************************************************
@@ -541,6 +538,15 @@ bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st)
        return ret;
 }
 
+/*******************************************************************
+ Returns the size in bytes of the named given the stat struct.
+********************************************************************/
+
+uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
+{
+       return sbuf->st_size;
+}
+
 /*******************************************************************
  Returns the size in bytes of the named file.
 ********************************************************************/
@@ -551,7 +557,7 @@ SMB_OFF_T get_file_size(char *file_name)
        buf.st_size = 0;
        if(sys_stat(file_name,&buf) != 0)
                return (SMB_OFF_T)-1;
-       return(buf.st_size);
+       return get_file_size_stat(&buf);
 }
 
 /*******************************************************************
@@ -791,43 +797,6 @@ char *clean_name(TALLOC_CTX *ctx, const char *s)
        return unix_clean_name(ctx, str);
 }
 
-/*******************************************************************
- Close the low 3 fd's and open dev/null in their place.
-********************************************************************/
-
-void close_low_fds(bool stderr_too)
-{
-#ifndef VALGRIND
-       int fd;
-       int i;
-
-       close(0);
-       close(1);
-
-       if (stderr_too)
-               close(2);
-
-       /* try and use up these file descriptors, so silly
-               library routines writing to stdout etc won't cause havoc */
-       for (i=0;i<3;i++) {
-               if (i == 2 && !stderr_too)
-                       continue;
-
-               fd = sys_open("/dev/null",O_RDWR,0);
-               if (fd < 0)
-                       fd = sys_open("/dev/null",O_WRONLY,0);
-               if (fd < 0) {
-                       DEBUG(0,("Can't open /dev/null\n"));
-                       return;
-               }
-               if (fd != i) {
-                       DEBUG(0,("Didn't get file descriptor %d\n",i));
-                       return;
-               }
-       }
-#endif
-}
-
 /*******************************************************************
  Write data into an fd at a given offset. Ignore seek errors.
 ********************************************************************/
@@ -918,37 +887,8 @@ void smb_msleep(unsigned int t)
 #endif
 }
 
-/****************************************************************************
- Become a daemon, discarding the controlling terminal.
-****************************************************************************/
-
-void become_daemon(bool Fork, bool no_process_group)
-{
-       if (Fork) {
-               if (sys_fork()) {
-                       _exit(0);
-               }
-       }
-
-  /* detach from the terminal */
-#ifdef HAVE_SETSID
-       if (!no_process_group) setsid();
-#elif defined(TIOCNOTTY)
-       if (!no_process_group) {
-               int i = sys_open("/dev/tty", O_RDWR, 0);
-               if (i != -1) {
-                       ioctl(i, (int) TIOCNOTTY, (char *)0);      
-                       close(i);
-               }
-       }
-#endif /* HAVE_SETSID */
-
-       /* Close fd's 0,1,2. Needed if started by rsh */
-       close_low_fds(False);  /* Don't close stderr, let the debug system
-                                 attach it to the logfile */
-}
-
 bool reinit_after_fork(struct messaging_context *msg_ctx,
+                      struct event_context *ev_ctx,
                       bool parent_longlived)
 {
        NTSTATUS status;
@@ -965,15 +905,21 @@ bool reinit_after_fork(struct messaging_context *msg_ctx,
                return false;
        }
 
-       /*
-        * For clustering, we need to re-init our ctdbd connection after the
-        * fork
-        */
-       status = messaging_reinit(msg_ctx);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("messaging_reinit() failed: %s\n",
-                        nt_errstr(status)));
-               return false;
+       if (ev_ctx) {
+               event_context_reinit(ev_ctx);
+       }
+
+       if (msg_ctx) {
+               /*
+                * For clustering, we need to re-init our ctdbd connection after the
+                * fork
+                */
+               status = messaging_reinit(msg_ctx);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,("messaging_reinit() failed: %s\n",
+                                nt_errstr(status)));
+                       return false;
+               }
        }
 
        return true;
@@ -1195,35 +1141,6 @@ void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
        *array_size = -1;
 }
 
-/****************************************************************************
- Get my own name and IP.
-****************************************************************************/
-
-char *talloc_get_myname(TALLOC_CTX *ctx)
-{
-       char *p;
-       char hostname[HOST_NAME_MAX];
-
-       *hostname = 0;
-
-       /* get my host name */
-       if (gethostname(hostname, sizeof(hostname)) == -1) {
-               DEBUG(0,("gethostname failed\n"));
-               return False;
-       }
-
-       /* Ensure null termination. */
-       hostname[sizeof(hostname)-1] = '\0';
-
-       /* split off any parts after an initial . */
-       p = strchr_m(hostname,'.');
-       if (p) {
-               *p = 0;
-       }
-
-       return talloc_strdup(ctx, hostname);
-}
-
 /****************************************************************************
  Get my own domain name, or "" if we have none.
 ****************************************************************************/
@@ -2224,14 +2141,19 @@ char *myhostname(void)
        if (ret == NULL) {
                /* This is cached forever so
                 * use talloc_autofree_context() ctx. */
-               ret = talloc_get_myname(talloc_autofree_context());
+               ret = get_myname(talloc_autofree_context());
        }
        return ret;
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba pid directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file concatenating the provided
+ * @a rootpath and @a basename
+ *
+ * @param name Filename, relative to @a rootpath
+ *
+ * @retval Pointer to a string containing the full path.
+ **/
 
 static char *xx_path(const char *name, const char *rootpath)
 {
@@ -2244,7 +2166,9 @@ static char *xx_path(const char *name, const char *rootpath)
        trim_string(fname,"","/");
 
        if (!directory_exist(fname)) {
-               mkdir(fname,0755);
+               if (!mkdir(fname,0755))
+                       DEBUG(1, ("Unable to create directory %s for file %s. "
+                             "Error was %s\n", fname, name, strerror(errno)));
        }
 
        return talloc_asprintf(talloc_tos(),
@@ -2253,18 +2177,26 @@ static char *xx_path(const char *name, const char *rootpath)
                                name);
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba lock directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba lock directory.
+ *
+ * @param name File to find, relative to LOCKDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *lock_path(const char *name)
 {
        return xx_path(name, lp_lockdir());
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba pid directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba pid directory.
+ *
+ * @param name File to find, relative to PIDDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *pid_path(const char *name)
 {
@@ -2310,13 +2242,30 @@ char *data_path(const char *name)
        return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_CODEPAGEDIR(), name);
 }
 
-/*****************************************************************
-a useful function for returning a path in the Samba state directory
- *****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba state directory.
+ *
+ * @param name File to find, relative to STATEDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *state_path(const char *name)
 {
-       return xx_path(name, get_dyn_STATEDIR());
+       return xx_path(name, lp_statedir());
+}
+
+/**
+ * @brief Returns an absolute path to a file in the Samba cache directory.
+ *
+ * @param name File to find, relative to CACHEDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
+
+char *cache_path(const char *name)
+{
+       return xx_path(name, lp_cachedir());
 }
 
 /**
@@ -2334,8 +2283,8 @@ const char *shlib_ext(void)
  Given a filename - get its directory name
 ********************************************************************/
 
-bool parent_dirname_talloc(TALLOC_CTX *mem_ctx, const char *dir,
-                          char **parent, const char **name)
+bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
+                   const char **name)
 {
        char *p;
        ptrdiff_t len;