From aa6204816cde0a7d75b9303916d038ed06e467ba Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Sun, 8 May 2016 14:41:35 +0200
Subject: [PATCH 1/3] MONITOR: Remove the no longer used diag_cmd command
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

After introducing the watchdog, the diag_cmd is longer used and makes no
sense trying to make it usable by watchdog as the result of "pstack %p"
seems next to useless in this context.

Co-author: Fabiano Fidêncio <fidencio@redhat.com>

Related:
https://fedorahosted.org/sssd/ticket/3051
---
 src/confdb/confdb.h                  |   1 -
 src/config/SSSDConfig/__init__.py.in |   1 -
 src/config/SSSDConfigTest.py         |   1 -
 src/config/cfg_rules.ini             |   9 --
 src/config/etc/sssd.api.conf         |   1 -
 src/monitor/monitor.c                | 163 -----------------------------------
 6 files changed, 176 deletions(-)

diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h
index 72adbd8..58a085b 100644
--- a/src/confdb/confdb.h
+++ b/src/confdb/confdb.h
@@ -73,7 +73,6 @@
 #define CONFDB_MONITOR_DEFAULT_DOMAIN "default_domain_suffix"
 #define CONFDB_MONITOR_OVERRIDE_SPACE "override_space"
 #define CONFDB_MONITOR_USER_RUNAS "user"
-#define CONFDB_MONITOR_PRE_KILL_CMD "diag_cmd"
 #define CONFDB_MONITOR_CERT_VERIFICATION "certificate_verification"
 
 /* Both monitor and domains */
diff --git a/src/config/SSSDConfig/__init__.py.in b/src/config/SSSDConfig/__init__.py.in
index ac53878..f99ded9 100644
--- a/src/config/SSSDConfig/__init__.py.in
+++ b/src/config/SSSDConfig/__init__.py.in
@@ -51,7 +51,6 @@ option_strings = {
     'reconnection_retries' : _('Number of times to attempt connection to Data Providers'),
     'fd_limit' : _('The number of file descriptors that may be opened by this responder'),
     'client_idle_timeout' : _('Idle time before automatic disconnection of a client'),
-    'diag_cmd' : _('The command to run when a service ping times out'),
 
     # [sssd]
     'services' : _('SSSD Services to start'),
diff --git a/src/config/SSSDConfigTest.py b/src/config/SSSDConfigTest.py
index 00c688f..a9ad001 100755
--- a/src/config/SSSDConfigTest.py
+++ b/src/config/SSSDConfigTest.py
@@ -309,7 +309,6 @@ class SSSDConfigTestSSSDService(unittest.TestCase):
             'reconnection_retries',
             'fd_limit',
             'client_idle_timeout',
-            'diag_cmd',
             'description',
             'certificate_verification',
             'override_space']
diff --git a/src/config/cfg_rules.ini b/src/config/cfg_rules.ini
index bd0116f..bc26ef5 100644
--- a/src/config/cfg_rules.ini
+++ b/src/config/cfg_rules.ini
@@ -25,7 +25,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 # Monitor service
 option = services
@@ -57,7 +56,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 # Name service
 option = user_attributes
@@ -96,7 +94,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 # Authentication service
 option = offline_credentials_expiration
@@ -130,7 +127,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 # sudo service
 option = sudo_timed
@@ -152,7 +148,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 # autofs service
 option = autofs_negative_timeout
@@ -173,7 +168,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 # ssh service
 option = ssh_hash_known_hosts
@@ -196,7 +190,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 # PAC responder
 option = allowed_uids
@@ -218,7 +211,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 # InfoPipe responder
 option = allowed_uids
@@ -239,7 +231,6 @@ option = fd_limit
 option = client_idle_timeout
 option = force_timeout
 option = description
-option = diag_cmd
 
 #Available provider types
 option = id_provider
diff --git a/src/config/etc/sssd.api.conf b/src/config/etc/sssd.api.conf
index 5ac6f79..59d6fae 100644
--- a/src/config/etc/sssd.api.conf
+++ b/src/config/etc/sssd.api.conf
@@ -15,7 +15,6 @@ fd_limit = int, None, false
 client_idle_timeout = int, None, false
 force_timeout = int, None, false
 description = str, None, false
-diag_cmd = str, None, false
 
 [sssd]
 # Monitor service
diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index f154d8e..c76ca11 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -112,7 +112,6 @@ struct mt_svc {
     char *identity;
     pid_t pid;
 
-    char *diag_cmd;
     int kill_time;
 
     struct tevent_timer *kill_timer;
@@ -373,77 +372,6 @@ static int add_svc_conn_spy(struct mt_svc *svc)
     return EOK;
 }
 
-static char *expand_diag_cmd(struct mt_svc *svc,
-                             const char *template)
-{
-    TALLOC_CTX *tmp_ctx = NULL;
-    char *copy;
-    char *p_copy;
-    char *n;
-    char *result = NULL;
-    char action;
-    char *res = NULL;
-
-    if (template == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "Missing template.\n");
-        return NULL;
-    }
-
-    tmp_ctx = talloc_new(NULL);
-    if (!tmp_ctx) return NULL;
-
-    copy = talloc_strdup(tmp_ctx, template);
-    if (copy == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup failed.\n");
-        goto done;
-    }
-
-    result = talloc_strdup(tmp_ctx, "");
-    if (result == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup failed.\n");
-        goto done;
-    }
-
-    p_copy = copy;
-    while ((n = strchr(p_copy, '%')) != NULL) {
-        *n = '\0';
-        n++;
-        if ( *n == '\0' ) {
-            DEBUG(SSSDBG_CRIT_FAILURE,
-                  "format error, single %% at the end of the template.\n");
-            goto done;
-        }
-
-        action = *n;
-        switch (action) {
-        case 'p':
-            result = talloc_asprintf_append(result, "%s%d", p_copy, svc->pid);
-            break;
-        default:
-            DEBUG(SSSDBG_CRIT_FAILURE,
-                  "format error, unknown template [%%%c].\n", *n);
-            goto done;
-        }
-
-        if (result == NULL) {
-            DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf_append failed.\n");
-            goto done;
-        }
-
-        p_copy = n + 1;
-    }
-
-    result = talloc_asprintf_append(result, "%s", p_copy);
-    if (result == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf_append failed.\n");
-        goto done;
-    }
-
-    res = talloc_move(svc, &result);
-done:
-    talloc_zfree(tmp_ctx);
-    return res;
-}
 
 static void svc_child_info(struct mt_svc *svc, int wait_status)
 {
@@ -467,82 +395,6 @@ static void svc_child_info(struct mt_svc *svc, int wait_status)
     }
 }
 
-static void svc_diag_cmd_exit_handler(int pid, int wait_status, void *pvt)
-{
-    struct mt_svc *svc = talloc_get_type(pvt, struct mt_svc);
-
-    svc_child_info(svc, wait_status);
-}
-
-static void svc_run_diag_cmd(struct mt_svc *svc)
-{
-    pid_t pkc_pid;
-    char **args;
-    int ret;
-    int debug_fd;
-    char *diag_cmd;
-    struct sss_child_ctx *diag_child_ctx;
-
-    if (svc->diag_cmd == NULL) {
-        return;
-    }
-
-    pkc_pid = fork();
-    if (pkc_pid != 0) {
-        /* parent, schedule SIGKILL */
-
-        ret = sss_child_register(svc,
-                                 svc->mt_ctx->sigchld_ctx,
-                                 pkc_pid,
-                                 svc_diag_cmd_exit_handler,
-                                 svc,
-                                 &diag_child_ctx);
-        if (ret != EOK) {
-            DEBUG(SSSDBG_CRIT_FAILURE, "Cannot register child %d\n", pkc_pid);
-            /* Try to go on ... */
-        }
-
-        return;
-    }
-
-    /* child, execute diagnostics */
-    diag_cmd = expand_diag_cmd(svc, svc->diag_cmd);
-    if (diag_cmd == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE,
-              "Failed to expand [%s]\n", svc->diag_cmd);
-        _exit(1);
-    }
-
-    if (debug_level >= SSSDBG_TRACE_LIBS) {
-        debug_fd = get_fd_from_debug_file();
-        ret = dup2(debug_fd, STDERR_FILENO);
-        if (ret == -1) {
-            ret = errno;
-            DEBUG(SSSDBG_MINOR_FAILURE,
-                "dup2 failed for stderr [%d][%s].\n", ret, sss_strerror(ret));
-            /* failure to redirect stderr is not fatal */
-        }
-
-        ret = dup2(debug_fd, STDOUT_FILENO);
-        if (ret == -1) {
-            ret = errno;
-            DEBUG(SSSDBG_MINOR_FAILURE,
-                "dup2 failed for stdout [%d][%s].\n", ret, sss_strerror(ret));
-            /* failure to redirect stdout is not fatal */
-        }
-    }
-
-    args = parse_args(diag_cmd);
-    execvp(args[0], args);
-
-    /* If we are here, exec() has failed
-     * Print errno and abort quickly */
-    ret = errno;
-    DEBUG(SSSDBG_FATAL_FAILURE,
-          "Could not exec %s, reason: %s\n", svc->diag_cmd, strerror(ret));
-    _exit(1);
-}
-
 static int mark_service_as_started(struct mt_svc *svc)
 {
     struct mt_ctx *ctx = svc->mt_ctx;
@@ -712,8 +564,6 @@ static int monitor_kill_service (struct mt_svc *svc)
         return EOK;
     }
 
-    svc_run_diag_cmd(svc);
-
     /* Set up a timer to send SIGKILL if this process
      * doesn't exit within the configured interval
      */
@@ -1147,19 +997,6 @@ static errno_t get_kill_config(struct mt_ctx *ctx, const char *path,
 {
     errno_t ret;
 
-    ret = confdb_get_string(ctx->cdb, svc, path,
-                            CONFDB_MONITOR_PRE_KILL_CMD,
-                            NULL, &svc->diag_cmd);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_CRIT_FAILURE,
-              "Failed to get diagnostics command for %s\n", svc->name);
-        return ret;
-    }
-    if (svc->diag_cmd) {
-        DEBUG(SSSDBG_CONF_SETTINGS,
-              "Diagnostics command: [%s]\n", svc->diag_cmd);
-    }
-
     ret = confdb_get_int(ctx->cdb, path,
                          CONFDB_SERVICE_FORCE_TIMEOUT,
                          MONITOR_DEF_FORCE_TIME, &svc->kill_time);
-- 
2.7.4

