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.

1  2 
dss.c
error.h

diff --combined dss.c
index 968ba597d74eb4fbcd03804f7e0cdd3fa4cab841,793e9194b1c695fb89d50d3fae5f63703dceeccd..631b36dfe12795da7ccaef38ea343b6342a14dda
--- 1/dss.c
--- 2/dss.c
+++ b/dss.c
@@@ -1,5 -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.
   */
@@@ -33,6 -33,7 +33,7 @@@
  #include "df.h"
  #include "time.h"
  #include "snap.h"
+ #include "ipc.h"
  
  /** Command line and config file options. */
  static struct gengetopt_args_info conf;
@@@ -170,7 -171,10 +171,10 @@@ static void dump_dss_config(const char 
        COMMAND(ls) \
        COMMAND(create) \
        COMMAND(prune) \
-       COMMAND(run)
+       COMMAND(run) \
+       COMMAND(kill) \
+       COMMAND(reload) \
  #define COMMAND(x) static int com_ ##x(void);
  COMMANDS
  #undef COMMAND
@@@ -226,6 -230,47 +230,47 @@@ static __printf_1_2 void dss_msg(const 
        va_end(argp);
  }
  
+ static char *get_config_file_name(void)
+ {
+       char *home, *config_file;
+       if (conf.config_file_given)
+               return dss_strdup(conf.config_file_arg);
+       home = get_homedir();
+       config_file = make_message("%s/.dssrc", home);
+       free(home);
+       return config_file;
+ }
+ static int send_signal(int sig)
+ {
+       pid_t pid;
+       char *config_file = get_config_file_name();
+       int ret = get_dss_pid(config_file, &pid);
+       free(config_file);
+       if (ret < 0)
+               return ret;
+       if (conf.dry_run_given) {
+               dss_msg("%d\n", (int)pid);
+               return 0;
+       }
+       ret = kill(pid, sig);
+       if (ret < 0)
+               return -ERRNO_TO_DSS_ERROR(errno);
+       return 1;
+ }
+ static int com_kill(void)
+ {
+       return send_signal(SIGTERM);
+ }
+ static int com_reload(void)
+ {
+       return send_signal(SIGHUP);
+ }
  static void dss_get_snapshot_list(struct snapshot_list *sl)
  {
        get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
@@@ -287,22 -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);
        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;
        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);
@@@ -550,24 -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;
  
        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)
@@@ -883,18 -951,11 +928,11 @@@ static int check_config(void
  static int parse_config_file(int override)
  {
        int ret, config_file_exists;
-       char *config_file;
+       char *config_file = get_config_file_name();
        struct stat statbuf;
        char *old_logfile_arg = NULL;
        int old_daemon_given = 0;
  
-       if (conf.config_file_given)
-               config_file = dss_strdup(conf.config_file_arg);
-       else {
-               char *home = get_homedir();
-               config_file = make_message("%s/.dssrc", home);
-               free(home);
-       }
        if (override) { /* SIGHUP */
                if (conf.logfile_given)
                        old_logfile_arg = dss_strdup(conf.logfile_arg);
@@@ -1120,12 -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;
  }
@@@ -1167,7 -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();
                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:
                                goto out;
                        continue;
                case HS_SUCCESS:
 -                      ret = post_create_hook();
 -                      if (ret < 0)
 -                              goto out;
 +                      post_create_hook();
                        continue;
                }
        }
  
  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)
+ {
+       char *config_file = get_config_file_name();
+       int ret = lock_dss(config_file);
+       free(config_file);
+       if (ret < 0) {
+               DSS_EMERG_LOG("failed to lock: %s\n", dss_strerror(-ret));
+               exit(EXIT_FAILURE);
+       }
+ }
  static int com_run(void)
  {
        int ret;
  
+       lock_dss_or_die();
        if (conf.dry_run_given) {
                DSS_ERROR_LOG("dry_run not supported by this command\n");
                return -E_SYNTAX;
@@@ -1248,6 -1331,7 +1299,7 @@@ static int com_prune(void
        struct disk_space ds;
        const char *why;
  
+       lock_dss_or_die();
        ret = get_disk_space(".", &ds);
        if (ret < 0)
                return ret;
@@@ -1270,7 -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)
                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();
@@@ -1303,6 -1391,7 +1355,7 @@@ static int com_create(void
        int ret, status;
        char **rsync_argv;
  
+       lock_dss_or_die();
        if (conf.dry_run_given) {
                int i;
                char *msg = NULL;
                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 --combined error.h
index ade1a8ec25ea40a0c5a9a96ee622fbfcc7583ff2,3fd75313406670a5683dde315a817c7903420f27..2b62611cc68937517f171148014ac2d124d9dd16
+++ b/error.h
@@@ -21,6 -21,20 +21,6 @@@ __printf_2_3 void dss_log(int ll, cons
  /** 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).
   *
@@@ -54,6 -68,7 +54,7 @@@ static inline char *dss_strerror(int nu
        DSS_ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR") \
        DSS_ERROR(SIGNAL, "caught terminating signal") \
        DSS_ERROR(BUG, "values of beta might cause dom!") \
+       DSS_ERROR(NOT_RUNNING, "dss not running") \
  
  
  /**