>From 7275f9b0e73a541cebc6ebc8207f3f4dac78d82d Mon Sep 17 00:00:00 2001
From: Michal Zidek <mzidek@redhat.com>
Date: Mon, 10 Sep 2012 18:16:26 +0200
Subject: [PATCH 1/2] tools_util.h provides signal_sssd function.

---
 src/tools/sss_debuglevel.c | 100 +--------------------------------------------
 src/tools/tools_util.c     |  94 ++++++++++++++++++++++++++++++++++++++++++
 src/tools/tools_util.h     |   5 +++
 3 files changed, 100 insertions(+), 99 deletions(-)

diff --git a/src/tools/sss_debuglevel.c b/src/tools/sss_debuglevel.c
index 908518b..74a63eb 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 fbb1d81..c3b39ad 100644
--- a/src/tools/tools_util.c
+++ b/src/tools/tools_util.c
@@ -578,3 +578,97 @@ 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 fd26b89..1be17e8 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);
 
-- 
1.7.11.2

