diff options
author | Michal Zidek <mzidek@redhat.com> | 2012-09-10 18:16:26 +0200 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2012-09-24 13:09:53 +0200 |
commit | 7c2e91ac48b20e6699d5c98c9912ea6427453c95 (patch) | |
tree | c5a56be85cb085fd97b346bbd3fd590a53937aa6 | |
parent | 1241d9f9daa17ef245c39f69dad4fd100367d299 (diff) | |
download | sssd-7c2e91ac48b20e6699d5c98c9912ea6427453c95.tar.gz sssd-7c2e91ac48b20e6699d5c98c9912ea6427453c95.tar.bz2 sssd-7c2e91ac48b20e6699d5c98c9912ea6427453c95.zip |
tools_util.h provides signal_sssd function.
-rw-r--r-- | src/tools/sss_debuglevel.c | 100 | ||||
-rw-r--r-- | src/tools/tools_util.c | 93 | ||||
-rw-r--r-- | src/tools/tools_util.h | 5 |
3 files changed, 99 insertions, 99 deletions
diff --git a/src/tools/sss_debuglevel.c b/src/tools/sss_debuglevel.c index 908518b1..74a63eb2 100644 --- a/src/tools/sss_debuglevel.c +++ b/src/tools/sss_debuglevel.c @@ -35,9 +35,6 @@ #include "tools/tools_util.h" #include "confdb/confdb.h" -#define SSSD_PIDFILE ""PID_PATH"/sssd.pid" -#define MAX_PID_LENGTH 10 - #define CHECK(expr, done, msg) do { \ if (expr) { \ ERROR(msg "\n"); \ @@ -52,12 +49,9 @@ struct debuglevel_tool_ctx { static errno_t set_debug_level(struct debuglevel_tool_ctx *tool_ctx, int debug_to_set, const char *config_file); -static errno_t send_sighup(void); static errno_t connect_to_confdb(TALLOC_CTX *ctx, struct confdb_ctx **cdb_ctx); static errno_t get_confdb_sections(TALLOC_CTX *ctx, struct confdb_ctx *confdb, char ***output_sections); -static errno_t get_sssd_pid(pid_t *out_pid); -static pid_t parse_pid(const char *strpid); static int parse_debug_level(const char *strlevel); int main(int argc, const char **argv) @@ -142,7 +136,7 @@ int main(int argc, const char **argv) ret = set_debug_level(ctx, debug_to_set, config_file); CHECK(ret != EOK, fini, "Could not set debug level."); - ret = send_sighup(); + ret = signal_sssd(SIGHUP); CHECK(ret != EOK, fini, "Could not force sssd processes to reload configuration. " "Is sssd running?"); @@ -204,26 +198,6 @@ done: return ret; } -errno_t send_sighup() -{ - int ret; - pid_t pid; - - ret = get_sssd_pid(&pid); - if (ret != EOK) { - return ret; - } - - if (kill(pid, SIGHUP) != 0) { - ret = errno; - DEBUG(SSSDBG_CRIT_FAILURE, ("Could not send SIGHUP to process %d: %s\n", - pid, strerror(errno))); - return ret; - } - - return EOK; -} - errno_t connect_to_confdb(TALLOC_CTX *ctx, struct confdb_ctx **cdb_ctx) { int ret; @@ -316,78 +290,6 @@ fail: return ret; } -errno_t get_sssd_pid(pid_t *out_pid) -{ - int ret; - size_t fsize; - FILE *pid_file = NULL; - char pid_str[MAX_PID_LENGTH] = {'\0'}; - - *out_pid = 0; - - errno = 0; - pid_file = fopen(SSSD_PIDFILE, "r"); - if (pid_file == NULL) { - ret = errno; - DEBUG(SSSDBG_MINOR_FAILURE, ("Unable to open pid file \"%s\": %s\n", - SSSD_PIDFILE, strerror(ret))); - goto done; - } - - fsize = fread(pid_str, sizeof(char), MAX_PID_LENGTH * sizeof(char), - pid_file); - if (!feof(pid_file)) { - /* eof not reached */ - ret = ferror(pid_file); - if (ret != 0) { - DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to read from file \"%s\": %s\n", - SSSD_PIDFILE, strerror(ret))); - } else { - DEBUG(SSSDBG_CRIT_FAILURE, ("File \"%s\" contains invalid pid.\n", - SSSD_PIDFILE)); - } - goto done; - } - if (fsize == 0) { - DEBUG(SSSDBG_CRIT_FAILURE, ("File \"%s\" contains no pid.\n", - SSSD_PIDFILE)); - ret = EINVAL; - goto done; - } - - pid_str[MAX_PID_LENGTH-1] = '\0'; - *out_pid = parse_pid(pid_str); - if (*out_pid == 0) { - DEBUG(SSSDBG_CRIT_FAILURE, - ("File \"%s\" contains invalid pid.\n", SSSD_PIDFILE)); - ret = EINVAL; - goto done; - } - - ret = EOK; - -done: - if (pid_file != NULL) { - fclose(pid_file); - } - return ret; -} - -pid_t parse_pid(const char *strpid) -{ - long value; - char *endptr; - - errno = 0; - value = strtol(strpid, &endptr, 10); - if ((errno != 0) || (endptr == strpid) - || ((*endptr != '\0') && (*endptr != '\n'))) { - return 0; - } - - return value; -} - int parse_debug_level(const char *strlevel) { long value; diff --git a/src/tools/tools_util.c b/src/tools/tools_util.c index fbb1d81b..049a4f58 100644 --- a/src/tools/tools_util.c +++ b/src/tools/tools_util.c @@ -578,3 +578,96 @@ done: talloc_free(conf_path); return ret; } + +static pid_t parse_pid(const char *strpid) +{ + long value; + char *endptr; + + errno = 0; + value = strtol(strpid, &endptr, 10); + if ((errno != 0) || (endptr == strpid) + || ((*endptr != '\0') && (*endptr != '\n'))) { + return 0; + } + + return value; +} + +static errno_t get_sssd_pid(pid_t *out_pid) +{ + int ret; + size_t fsize; + FILE *pid_file = NULL; + char pid_str[MAX_PID_LENGTH] = {'\0'}; + + *out_pid = 0; + + errno = 0; + pid_file = fopen(SSSD_PIDFILE, "r"); + if (pid_file == NULL) { + ret = errno; + DEBUG(SSSDBG_MINOR_FAILURE, ("Unable to open pid file \"%s\": %s\n", + SSSD_PIDFILE, strerror(ret))); + goto done; + } + + fsize = fread(pid_str, sizeof(char), MAX_PID_LENGTH * sizeof(char), + pid_file); + if (!feof(pid_file)) { + /* eof not reached */ + ret = ferror(pid_file); + if (ret != 0) { + DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to read from file \"%s\": %s\n", + SSSD_PIDFILE, strerror(ret))); + } else { + DEBUG(SSSDBG_CRIT_FAILURE, ("File \"%s\" contains invalid pid.\n", + SSSD_PIDFILE)); + } + goto done; + } + if (fsize == 0) { + DEBUG(SSSDBG_CRIT_FAILURE, ("File \"%s\" contains no pid.\n", + SSSD_PIDFILE)); + ret = EINVAL; + goto done; + } + + pid_str[MAX_PID_LENGTH-1] = '\0'; + *out_pid = parse_pid(pid_str); + if (*out_pid == 0) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("File \"%s\" contains invalid pid.\n", SSSD_PIDFILE)); + ret = EINVAL; + goto done; + } + + ret = EOK; + +done: + if (pid_file != NULL) { + fclose(pid_file); + } + return ret; +} + +errno_t signal_sssd(int signum) +{ + int ret; + pid_t pid; + + ret = get_sssd_pid(&pid); + if (ret != EOK) { + return ret; + } + + if (kill(pid, signum) != 0) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, + ("Could not send signal %d to process %d: %s\n", + signum, pid, strerror(errno))); + return ret; + } + + return EOK; +} diff --git a/src/tools/tools_util.h b/src/tools/tools_util.h index fd26b890..1be17e8e 100644 --- a/src/tools/tools_util.h +++ b/src/tools/tools_util.h @@ -27,6 +27,9 @@ #include "util/util.h" +#define SSSD_PIDFILE ""PID_PATH"/sssd.pid" +#define MAX_PID_LENGTH 10 + #define BAD_POPT_PARAMS(pc, msg, val, label) do { \ usage(pc, msg); \ val = EXIT_FAILURE; \ @@ -99,6 +102,8 @@ int remove_homedir(TALLOC_CTX *mem_ctx, int run_userdel_cmd(struct tools_ctx *tctx); +errno_t signal_sssd(int signum); + /* from files.c */ int remove_tree(const char *root); |