URL: https://github.com/SSSD/sssd/pull/48
Author: mzidek-rh
 Title: #48: sssctl: Flags for commadn initialization
Action: synchronized

To pull the PR as Git branch:
git remote add ghsssd https://github.com/SSSD/sssd
git fetch ghsssd pull/48/head:pr48
git checkout pr48
From 75d10030aea805975fefee8e4ae5fe15d7236705 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzi...@redhat.com>
Date: Wed, 12 Oct 2016 13:09:37 +0200
Subject: [PATCH] sssctl: Flags for command initialization

Allow passing flags for command specific initialization. Currently
only one flag is available to skip the confdb initialization which is
required to improve config-check command.

Resolves:
https://fedorahosted.org/sssd/ticket/3209
---
 src/tools/common/sss_tools.c | 93 +++++++++++++++++++++++++-------------------
 src/tools/common/sss_tools.h | 12 ++++--
 src/tools/sss_override.c     | 24 ++++++------
 src/tools/sssctl/sssctl.c    | 24 ++++++------
 4 files changed, 86 insertions(+), 67 deletions(-)

diff --git a/src/tools/common/sss_tools.c b/src/tools/common/sss_tools.c
index 686b53a..acea9be 100644
--- a/src/tools/common/sss_tools.c
+++ b/src/tools/common/sss_tools.c
@@ -182,7 +182,6 @@ errno_t sss_tool_init(TALLOC_CTX *mem_ctx,
                       struct sss_tool_ctx **_tool_ctx)
 {
     struct sss_tool_ctx *tool_ctx;
-    errno_t ret;
 
     tool_ctx = talloc_zero(mem_ctx, struct sss_tool_ctx);
     if (tool_ctx == NULL) {
@@ -192,45 +191,9 @@ errno_t sss_tool_init(TALLOC_CTX *mem_ctx,
 
     sss_tool_common_opts(tool_ctx, argc, argv);
 
-    /* Connect to confdb. */
-    ret = sss_tool_confdb_init(tool_ctx, &tool_ctx->confdb);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to open confdb [%d]: %s\n",
-                                   ret, sss_strerror(ret));
-        goto done;
-    }
-
-    /* Setup domains. */
-    ret = sss_tool_domains_init(tool_ctx, tool_ctx->confdb, &tool_ctx->domains);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
-                                   ret, sss_strerror(ret));
-        goto done;
-    }
-
-    ret = confdb_get_string(tool_ctx->confdb, tool_ctx,
-                            CONFDB_MONITOR_CONF_ENTRY,
-                            CONFDB_MONITOR_DEFAULT_DOMAIN,
-                            NULL, &tool_ctx->default_domain);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_OP_FAILURE, "Cannot get the default domain [%d]: %s\n",
-                                 ret, strerror(ret));
-        goto done;
-    }
-
-    ret = EOK;
-
-done:
-    switch (ret) {
-    case EOK:
-    case ERR_SYSDB_VERSION_TOO_OLD:
-        *_tool_ctx = tool_ctx;
-        break;
-    default:
-        break;
-    }
+    *_tool_ctx = tool_ctx;
 
-    return ret;
+    return EOK;
 }
 
 static bool sss_tool_is_delimiter(struct sss_route_cmd *command)
@@ -300,6 +263,49 @@ void sss_tool_usage(const char *tool_name, struct sss_route_cmd *commands)
     sss_tool_print_common_opts(min_len);
 }
 
+static int tool_cmd_init(struct sss_tool_ctx *tool_ctx,
+                         struct sss_route_cmd *command)
+{
+    int ret;
+
+    if (command->flags & SSS_TOOL_CMD_FLAG_NO_CONFDB) {
+        /* This tool does not need to connect to confdb.
+         * Nothing to do. */
+        return EOK;
+    }
+
+    /* Connect to confdb. */
+    ret = sss_tool_confdb_init(tool_ctx, &tool_ctx->confdb);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to open confdb [%d]: %s\n",
+              ret, sss_strerror(ret));
+        goto done;
+    }
+
+    /* Setup domains. */
+    ret = sss_tool_domains_init(tool_ctx, tool_ctx->confdb, &tool_ctx->domains);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
+              ret, sss_strerror(ret));
+        goto done;
+    }
+
+    ret = confdb_get_string(tool_ctx->confdb, tool_ctx,
+                            CONFDB_MONITOR_CONF_ENTRY,
+                            CONFDB_MONITOR_DEFAULT_DOMAIN,
+                            NULL, &tool_ctx->default_domain);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE, "Cannot get the default domain [%d]: %s\n",
+              ret, strerror(ret));
+        goto done;
+    }
+
+    ret = EOK;
+
+done:
+    return ret;
+}
+
 errno_t sss_tool_route(int argc, const char **argv,
                        struct sss_tool_ctx *tool_ctx,
                        struct sss_route_cmd *commands,
@@ -308,6 +314,7 @@ errno_t sss_tool_route(int argc, const char **argv,
     struct sss_cmdline cmdline;
     const char *cmd;
     int i;
+    int ret;
 
     if (commands == NULL) {
         DEBUG(SSSDBG_CRIT_FAILURE, "Bug: commands can't be NULL!\n");
@@ -339,6 +346,14 @@ errno_t sss_tool_route(int argc, const char **argv,
                 return tool_ctx->init_err;
             }
 
+            ret = tool_cmd_init(tool_ctx, &commands[i]);
+            if (ret != 0) {
+                DEBUG(SSSDBG_FATAL_FAILURE,
+                      "Command initialization failed [%d] %s\n",
+                      ret, sss_strerror(ret));
+                return ret;
+            }
+
             return commands[i].fn(&cmdline, tool_ctx, pvt);
         }
     }
diff --git a/src/tools/common/sss_tools.h b/src/tools/common/sss_tools.h
index 6d24642..64c0c5a 100644
--- a/src/tools/common/sss_tools.h
+++ b/src/tools/common/sss_tools.h
@@ -45,16 +45,20 @@ typedef errno_t
                 struct sss_tool_ctx *tool_ctx,
                 void *pvt);
 
-#define SSS_TOOL_COMMAND(cmd, msg, err, fn) {cmd, _(msg), err, fn}
-#define SSS_TOOL_COMMAND_NOMSG(cmd, err, fn) {cmd, NULL, err, fn}
-#define SSS_TOOL_DELIMITER(message) {"", _(message), 0, NULL}
-#define SSS_TOOL_LAST {NULL, NULL, 0, NULL}
+#define SSS_TOOL_COMMAND(cmd, msg, err, fn, flags) \
+    {cmd, _(msg), err, fn, flags}
+#define SSS_TOOL_COMMAND_NOMSG(cmd, err, fn, flags) {cmd, NULL, err, fn, flags}
+#define SSS_TOOL_DELIMITER(message) {"", _(message), 0, NULL, 0}
+#define SSS_TOOL_LAST {NULL, NULL, 0, NULL, 0}
+
+#define SSS_TOOL_CMD_FLAG_NO_CONFDB 0x01
 
 struct sss_route_cmd {
     const char *command;
     const char *description;
     errno_t handles_init_err;
     sss_route_fn fn;
+    int flags;
 };
 
 void sss_tool_usage(const char *tool_name,
diff --git a/src/tools/sss_override.c b/src/tools/sss_override.c
index 212bf9a..addac2c 100644
--- a/src/tools/sss_override.c
+++ b/src/tools/sss_override.c
@@ -1917,18 +1917,18 @@ static int override_group_export(struct sss_cmdline *cmdline,
 int main(int argc, const char **argv)
 {
     struct sss_route_cmd commands[] = {
-        SSS_TOOL_COMMAND_NOMSG("user-add", 0, override_user_add),
-        SSS_TOOL_COMMAND_NOMSG("user-del", 0, override_user_del),
-        SSS_TOOL_COMMAND_NOMSG("user-find", 0, override_user_find),
-        SSS_TOOL_COMMAND_NOMSG("user-show", 0, override_user_show),
-        SSS_TOOL_COMMAND_NOMSG("user-import", 0, override_user_import),
-        SSS_TOOL_COMMAND_NOMSG("user-export", 0, override_user_export),
-        SSS_TOOL_COMMAND_NOMSG("group-add", 0, override_group_add),
-        SSS_TOOL_COMMAND_NOMSG("group-del", 0, override_group_del),
-        SSS_TOOL_COMMAND_NOMSG("group-find", 0, override_group_find),
-        SSS_TOOL_COMMAND_NOMSG("group-show", 0, override_group_show),
-        SSS_TOOL_COMMAND_NOMSG("group-import", 0, override_group_import),
-        SSS_TOOL_COMMAND_NOMSG("group-export", 0, override_group_export),
+        SSS_TOOL_COMMAND_NOMSG("user-add", 0, override_user_add, 0),
+        SSS_TOOL_COMMAND_NOMSG("user-del", 0, override_user_del, 0),
+        SSS_TOOL_COMMAND_NOMSG("user-find", 0, override_user_find, 0),
+        SSS_TOOL_COMMAND_NOMSG("user-show", 0, override_user_show, 0),
+        SSS_TOOL_COMMAND_NOMSG("user-import", 0, override_user_import, 0),
+        SSS_TOOL_COMMAND_NOMSG("user-export", 0, override_user_export, 0),
+        SSS_TOOL_COMMAND_NOMSG("group-add", 0, override_group_add, 0),
+        SSS_TOOL_COMMAND_NOMSG("group-del", 0, override_group_del, 0),
+        SSS_TOOL_COMMAND_NOMSG("group-find", 0, override_group_find, 0),
+        SSS_TOOL_COMMAND_NOMSG("group-show", 0, override_group_show, 0),
+        SSS_TOOL_COMMAND_NOMSG("group-import", 0, override_group_import, 0),
+        SSS_TOOL_COMMAND_NOMSG("group-export", 0, override_group_export, 0),
         SSS_TOOL_LAST
     };
 
diff --git a/src/tools/sssctl/sssctl.c b/src/tools/sssctl/sssctl.c
index 1035a73..bdec11e 100644
--- a/src/tools/sssctl/sssctl.c
+++ b/src/tools/sssctl/sssctl.c
@@ -260,23 +260,23 @@ int main(int argc, const char **argv)
 {
     struct sss_route_cmd commands[] = {
         SSS_TOOL_DELIMITER("SSSD Status:"),
-        SSS_TOOL_COMMAND("domain-list", "List available domains", 0, sssctl_domain_list),
-        SSS_TOOL_COMMAND("domain-status", "Print information about domain", 0, sssctl_domain_status),
+        SSS_TOOL_COMMAND("domain-list", "List available domains", 0, sssctl_domain_list, 0),
+        SSS_TOOL_COMMAND("domain-status", "Print information about domain", 0, sssctl_domain_status, 0),
         SSS_TOOL_DELIMITER("Information about cached content:"),
-        SSS_TOOL_COMMAND("user-show", "Information about cached user", 0, sssctl_user_show),
-        SSS_TOOL_COMMAND("group-show", "Information about cached group", 0, sssctl_group_show),
-        SSS_TOOL_COMMAND("netgroup-show", "Information about cached netgroup", 0, sssctl_netgroup_show),
+        SSS_TOOL_COMMAND("user-show", "Information about cached user", 0, sssctl_user_show, 0),
+        SSS_TOOL_COMMAND("group-show", "Information about cached group", 0, sssctl_group_show, 0),
+        SSS_TOOL_COMMAND("netgroup-show", "Information about cached netgroup", 0, sssctl_netgroup_show, 0),
         SSS_TOOL_DELIMITER("Local data tools:"),
-        SSS_TOOL_COMMAND("client-data-backup", "Backup local data", 0, sssctl_client_data_backup),
-        SSS_TOOL_COMMAND("client-data-restore", "Restore local data from backup", 0, sssctl_client_data_restore),
-        SSS_TOOL_COMMAND("cache-remove", "Backup local data and remove cached content", 0, sssctl_cache_remove),
-        SSS_TOOL_COMMAND("cache-upgrade", "Perform cache upgrade", ERR_SYSDB_VERSION_TOO_OLD, sssctl_cache_upgrade),
+        SSS_TOOL_COMMAND("client-data-backup", "Backup local data", 0, sssctl_client_data_backup, 0),
+        SSS_TOOL_COMMAND("client-data-restore", "Restore local data from backup", 0, sssctl_client_data_restore, 0),
+        SSS_TOOL_COMMAND("cache-remove", "Backup local data and remove cached content", 0, sssctl_cache_remove, 0),
+        SSS_TOOL_COMMAND("cache-upgrade", "Perform cache upgrade", ERR_SYSDB_VERSION_TOO_OLD, sssctl_cache_upgrade, 0),
         SSS_TOOL_DELIMITER("Log files tools:"),
-        SSS_TOOL_COMMAND("logs-remove", "Remove existing SSSD log files", 0, sssctl_logs_remove),
-        SSS_TOOL_COMMAND("logs-fetch", "Archive SSSD log files in tarball", 0, sssctl_logs_fetch),
+        SSS_TOOL_COMMAND("logs-remove", "Remove existing SSSD log files", 0, sssctl_logs_remove, 0),
+        SSS_TOOL_COMMAND("logs-fetch", "Archive SSSD log files in tarball", 0, sssctl_logs_fetch, 0),
 #ifdef HAVE_LIBINI_CONFIG_V1_3
         SSS_TOOL_DELIMITER("Configuration files tools:"),
-        SSS_TOOL_COMMAND("config-check", "Perform static analysis of SSSD configuration", 0, sssctl_config_check),
+        SSS_TOOL_COMMAND("config-check", "Perform static analysis of SSSD configuration", 0, sssctl_config_check, SSS_TOOL_CMD_FLAG_NO_CONFDB),
 #endif
         SSS_TOOL_LAST
     };
_______________________________________________
sssd-devel mailing list -- sssd-devel@lists.fedorahosted.org
To unsubscribe send an email to sssd-devel-le...@lists.fedorahosted.org

Reply via email to