Merge branch 't/ipc'
authorAndre Noll <maan@systemlinux.org>
Fri, 10 Aug 2012 11:49:54 +0000 (13:49 +0200)
committerAndre Noll <maan@systemlinux.org>
Fri, 10 Aug 2012 11:49:54 +0000 (13:49 +0200)
4d9f41 mutex_get(): Change parameter from key_t to int.
55c90e Implement --reload.
492928 Add the --kill subcommand.
dd42f7 Use semaphore locking to avoid starting dss multiple times.
17eea8 Introduce get_config_file_name().

That topic was cooking in next for a quite some time now.

dss.c
error.h
exec.c
exec.h
string.c
string.h

diff --git a/dss.c b/dss.c
index 793e919..631b36d 100644 (file)
--- a/dss.c
+++ b/dss.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2010 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2008-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -332,29 +332,22 @@ static int next_snapshot_is_due(void)
        return 0;
 }
 
-static int pre_create_hook(void)
+static void pre_create_hook(void)
 {
-       int ret, fds[3] = {0, 0, 0};
-
        assert(snapshot_creation_status == HS_READY);
        /* make sure that the next snapshot time will be recomputed */
        invalidate_next_snapshot_time();
        DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
-       ret = dss_exec_cmdline_pid(&create_pid,
-               conf.pre_create_hook_arg, fds);
-       if (ret < 0)
-               return ret;
+       dss_exec_cmdline_pid(&create_pid, conf.pre_create_hook_arg);
        snapshot_creation_status = HS_PRE_RUNNING;
-       return ret;
 }
 
-static int pre_remove_hook(struct snapshot *s, const char *why)
+static void pre_remove_hook(struct snapshot *s, const char *why)
 {
-       int ret, fds[3] = {0, 0, 0};
        char *cmd;
 
        if (!s)
-               return 0;
+               return;
        DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
        assert(snapshot_removal_status == HS_READY);
        assert(remove_pid == 0);
@@ -367,18 +360,14 @@ static int pre_remove_hook(struct snapshot *s, const char *why)
        cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
                conf.dest_dir_arg, s->name);
        DSS_DEBUG_LOG("executing %s\n", cmd);
-       ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
+       dss_exec_cmdline_pid(&remove_pid, cmd);
        free(cmd);
-       if (ret < 0)
-               return ret;
        snapshot_removal_status = HS_PRE_RUNNING;
-       return ret;
 }
 
 static int exec_rm(void)
 {
        struct snapshot *s = snapshot_currently_being_removed;
-       int fds[3] = {0, 0, 0};
        char *new_name = being_deleted_name(s);
        char *argv[] = {"rm", "-rf", new_name, NULL};
        int ret;
@@ -390,9 +379,7 @@ static int exec_rm(void)
        ret = dss_rename(s->name, new_name);
        if (ret < 0)
                goto out;
-       ret = dss_exec(&remove_pid, argv[0], argv, fds);
-       if (ret < 0)
-               goto out;
+       dss_exec(&remove_pid, argv[0], argv);
        snapshot_removal_status = HS_RUNNING;
 out:
        free(new_name);
@@ -608,31 +595,24 @@ static int try_to_free_disk_space(void)
        ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
        goto out;
 remove:
-       ret = pre_remove_hook(victim, why);
+       pre_remove_hook(victim, why);
 out:
        free_snapshot_list(&sl);
        return ret;
 }
 
-static int post_create_hook(void)
+static void post_create_hook(void)
 {
-       int ret, fds[3] = {0, 0, 0};
-       char *cmd;
-
-       cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
+       char *cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
                conf.dest_dir_arg, path_to_last_complete_snapshot);
        DSS_NOTICE_LOG("executing %s\n", cmd);
-       ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
+       dss_exec_cmdline_pid(&create_pid, cmd);
        free(cmd);
-       if (ret < 0)
-               return ret;
        snapshot_creation_status = HS_POST_RUNNING;
-       return ret;
 }
 
-static int post_remove_hook(void)
+static void post_remove_hook(void)
 {
-       int ret, fds[3] = {0, 0, 0};
        char *cmd;
        struct snapshot *s = snapshot_currently_being_removed;
 
@@ -641,12 +621,9 @@ static int post_remove_hook(void)
        cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
                conf.dest_dir_arg, s->name);
        DSS_NOTICE_LOG("executing %s\n", cmd);
-       ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
+       dss_exec_cmdline_pid(&remove_pid, cmd);
        free(cmd);
-       if (ret < 0)
-               return ret;
        snapshot_removal_status = HS_POST_RUNNING;
-       return ret;
 }
 
 static void dss_kill(pid_t pid, int sig, const char *msg)
@@ -1181,14 +1158,12 @@ static void free_rsync_argv(char **argv)
 
 static int create_snapshot(char **argv)
 {
-       int ret, fds[3] = {0, 0, 0};
+       int ret;
 
        ret = rename_resume_snap(current_snapshot_creation_time);
        if (ret < 0)
                return ret;
-       ret = dss_exec(&create_pid, argv[0], argv, fds);
-       if (ret < 0)
-               return ret;
+       dss_exec(&create_pid, argv[0], argv);
        snapshot_creation_status = HS_RUNNING;
        return ret;
 }
@@ -1230,9 +1205,7 @@ static int select_loop(void)
                        continue;
                }
                if (snapshot_removal_status == HS_SUCCESS) {
-                       ret = post_remove_hook();
-                       if (ret < 0)
-                               goto out;
+                       post_remove_hook();
                        continue;
                }
                ret = try_to_free_disk_space();
@@ -1247,9 +1220,7 @@ static int select_loop(void)
                case HS_READY:
                        if (!next_snapshot_is_due())
                                continue;
-                       ret = pre_create_hook();
-                       if (ret < 0)
-                               goto out;
+                       pre_create_hook();
                        continue;
                case HS_PRE_RUNNING:
                case HS_RUNNING:
@@ -1272,9 +1243,7 @@ static int select_loop(void)
                                goto out;
                        continue;
                case HS_SUCCESS:
-                       ret = post_create_hook();
-                       if (ret < 0)
-                               goto out;
+                       post_create_hook();
                        continue;
                }
        }
@@ -1284,12 +1253,11 @@ out:
 
 static void exit_hook(int exit_code)
 {
-       int fds[3] = {0, 0, 0};
        char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
        pid_t pid;
 
        DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
-       dss_exec(&pid, conf.exit_hook_arg, argv, fds);
+       dss_exec(&pid, conf.exit_hook_arg, argv);
 }
 
 static void lock_dss_or_die(void)
@@ -1354,9 +1322,7 @@ rm:
                ret = 0;
                goto out;
        }
-       ret = pre_remove_hook(victim, why);
-       if (ret < 0)
-               goto out;
+       pre_remove_hook(victim, why);
        if (snapshot_removal_status == HS_PRE_RUNNING) {
                ret = wait_for_remove_process();
                if (ret < 0)
@@ -1372,9 +1338,7 @@ rm:
                goto out;
        if (snapshot_removal_status != HS_SUCCESS)
                goto out;
-       ret = post_remove_hook();
-       if (ret < 0)
-               goto out;
+       post_remove_hook();
        if (snapshot_removal_status != HS_POST_RUNNING)
                goto out;
        ret = wait_for_remove_process();
@@ -1407,9 +1371,7 @@ static int com_create(void)
                free(msg);
                return 1;
        }
-       ret = pre_create_hook();
-       if (ret < 0)
-               return ret;
+       pre_create_hook();
        if (create_pid) {
                ret = wait_for_process(create_pid, &status);
                if (ret < 0)
diff --git a/error.h b/error.h
index 3fd7531..2b62611 100644 (file)
--- a/error.h
+++ b/error.h
@@ -21,20 +21,6 @@ __printf_2_3 void dss_log(int ll, const char* fmt,...);
 /** Set the system error bit for the given number. */
 #define ERRNO_TO_DSS_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
 
-/** Check whether a given number is a system error number.
- *
- * \param num The value to be checked.
- * \param _errno The system error number.
- *
- * \return True if \a num is dss' representation of the system
- * error identified by \a _errno.
- */
-static inline int is_errno(int num, int _errno)
-{
-       assert(num > 0 && _errno > 0);
-       return ERRNO_TO_DSS_ERROR(_errno) == num;
-}
-
 /**
  * dss' version of strerror(3).
  *
diff --git a/exec.c b/exec.c
index 2887cb2..adfd24d 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2010 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2003-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 /** \file exec.c Helper functions for spawning new processes. */
 
 #include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <assert.h>
 #include <stdlib.h>
 #include <signal.h>
-
+#include <errno.h>
 
 #include "gcc-compat.h"
 #include "log.h"
 #include "error.h"
 #include "string.h"
 
-
 /**
- * Spawn a new process and redirect fd 0, 1, and 2.
+ * Spawn a new process using execvp().
  *
  * \param pid Will hold the pid of the created process upon return.
  * \param file Path of the executable to execute.
  * \param args The argument array for the command.
- * \param fds a Pointer to a value-result array.
  *
  * \return Standard.
  *
- * \sa null(4), pipe(2), dup2(2), fork(2), exec(3).
+ * \sa fork(2), exec(3).
  */
-int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds)
+void dss_exec(pid_t *pid, const char *file, char *const *const args)
 {
-       int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
-               null = -1; /* ;) */
-
-       ret = -E_DUP_PIPE;
-       if (fds[0] > 0 && pipe(in) < 0)
-               goto err_out;
-       if (fds[1] > 0 && pipe(out) < 0)
-               goto err_out;
-       if (fds[2] > 0 && pipe(err) < 0)
-               goto err_out;
-       if (!fds[0] || !fds[1] || !fds[2]) {
-               ret = -E_NULL_OPEN;
-               null = open("/dev/null", O_RDWR);
-               if (null < 0)
-                       goto err_out;
-       }
-       if ((*pid = fork()) < 0)
+       if ((*pid = fork()) < 0) {
+               DSS_EMERG_LOG("fork error: %s\n", strerror(errno));
                exit(EXIT_FAILURE);
-       if (!(*pid)) { /* child */
-               if (fds[0] >= 0) {
-                       if (fds[0]) {
-                               close(in[1]);
-                               if (in[0] != STDIN_FILENO)
-                                       dup2(in[0], STDIN_FILENO);
-                       } else
-                               dup2(null, STDIN_FILENO);
-               }
-               if (fds[1] >= 0) {
-                       if (fds[1]) {
-                               close(out[0]);
-                               if (out[1] != STDOUT_FILENO)
-                                       dup2(out[1], STDOUT_FILENO);
-                       } else
-                               dup2(null, STDOUT_FILENO);
-               }
-               if (fds[2] >= 0) {
-                       if (fds[2]) {
-                               close(err[0]);
-                               if (err[1] != STDERR_FILENO)
-                                       dup2(err[1], STDERR_FILENO);
-                       } else
-                               dup2(null, STDERR_FILENO);
-               }
-               if (null >= 0)
-                       close(null);
-               signal(SIGINT, SIG_DFL);
-               signal(SIGTERM, SIG_DFL);
-               signal(SIGCHLD, SIG_DFL);
-               execvp(file, args);
-               _exit(EXIT_FAILURE);
-       }
-       if (fds[0] > 0) {
-               close(in[0]);
-               *fds = in[1];
-       }
-       if (fds[1] > 0) {
-               close(out[1]);
-               *(fds + 1) = out[0];
-       }
-       if (fds[2] > 0) {
-               close(err[1]);
-               *(fds + 2) = err[0];
        }
-       if (null >= 0)
-               close(null);
-       return 1;
-err_out:
-       DSS_ERROR_LOG("failed to exec %s\n", file);
-       if (err[0] >= 0)
-               close(err[0]);
-       if (err[1] >= 0)
-               close(err[1]);
-       if (out[0] >= 0)
-               close(out[0]);
-       if (out[1] >= 0)
-               close(out[1]);
-       if (in[0] >= 0)
-               close(in[0]);
-       if (in[1] >= 0)
-               close(in[1]);
-       if (null >= 0)
-               close(null);
-       return ret;
+       if (*pid) /* parent */
+               return;
+       signal(SIGINT, SIG_DFL);
+       signal(SIGTERM, SIG_DFL);
+       signal(SIGCHLD, SIG_DFL);
+       execvp(file, args);
+       DSS_EMERG_LOG("execvp error: %s\n", strerror(errno));
+       _exit(EXIT_FAILURE);
 }
 
-
 /**
- * Exec the given command.
+ * Exec the command given as a command line.
  *
  * \param pid Will hold the pid of the created process upon return.
  * \param cmdline Holds the command and its arguments, seperated by spaces.
- * \param fds A pointer to a value-result array.
- *
- * This function uses fork/exec to create a new process. \a fds must be a
- * pointer to three integers, corresponding to stdin, stdout and stderr
- * respectively. It specifies how to deal with fd 0, 1, 2 in the child. The
- * contents of \a fds are interpreted as follows:
- *
- *     - fd[i] < 0: leave fd \a i alone.
- *     - fd[i] = 0: dup fd \a i to \p /dev/null.
- *     - fd[i] > 0: create a pipe and dup i to one end of that pipe.
- *     Upon return, fd[i] contains the file descriptor of the pipe.
  *
- *     In any case, all unneeded filedescriptors are closed.
+ * This function uses fork/exec to create a new process.
  *
  * \return Standard.
  */
-int dss_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds)
+void dss_exec_cmdline_pid(pid_t *pid, const char *cmdline)
 {
-       int argc, ret;
-       char **argv;
-       char *tmp = dss_strdup(cmdline);
+       char **argv, *tmp = dss_strdup(cmdline);
 
-       if (!tmp)
-               exit(EXIT_FAILURE);
-       argc = split_args(tmp, &argv, " \t");
-       ret = dss_exec(pid, argv[0], argv, fds);
+       split_args(tmp, &argv, " \t");
+       dss_exec(pid, argv[0], argv);
        free(argv);
        free(tmp);
-       return ret;
 }
-
-
diff --git a/exec.h b/exec.h
index b95aba1..31efc27 100644 (file)
--- a/exec.h
+++ b/exec.h
@@ -1,3 +1,2 @@
-int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds);
-int dss_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
-
+void dss_exec(pid_t *pid, const char *file, char *const *const args);
+void dss_exec_cmdline_pid(pid_t *pid, const char *cmdline);
index d370a81..22d3905 100644 (file)
--- a/string.c
+++ b/string.c
@@ -239,7 +239,7 @@ __must_check __malloc char *dss_logname(void)
  *
  * \return The number of substrings found in \a args.
  */
-__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
+unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
 {
        char *p = args;
        char **argv;
index a4cbdbe..0f158fe 100644 (file)
--- a/string.h
+++ b/string.h
@@ -6,7 +6,7 @@ __must_check __malloc char *dss_strdup(const char *s);
 __must_check __malloc char *get_homedir(void);
 int dss_atoi64(const char *str, int64_t *value);
 __must_check __malloc char *dss_logname(void);
-__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim);
+unsigned split_args(char *args, char *** const argv_ptr, const char *delim);
 
 
 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */