From 505ca5e27a136bd51e22ff90d9ce2b425d7f71a3 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Jul 08 2011 19:44:34 +0000 Subject: - output hints when we encounter errors setting up our bus connection in the daemon (#712075) --- diff --git a/src/getcert.c b/src/getcert.c index 036d820..3aee3dc 100644 --- a/src/getcert.c +++ b/src/getcert.c @@ -268,34 +268,19 @@ prep_req(enum cm_tdbus_type which, static enum { hint_unknown, hint_found } print_hint(const char *error, const char *message) { - char *buf = NULL; - const char *text = NULL; - if (strcmp(error, DBUS_ERROR_ACCESS_DENIED) == 0) { - text = _("Insufficient access. Please retry operation as root.\n"); - } else - if ((strcmp(error, DBUS_ERROR_NAME_HAS_NO_OWNER) == 0) || - (strcmp(error, DBUS_ERROR_SERVICE_UNKNOWN) == 0)) { - text = _("Please verify that the certmonger service has been started.\n"); - } else - if (strcmp(error, DBUS_ERROR_NO_REPLY) == 0) { - text = _("Please verify that the certmonger service is still running.\n"); - } else - if (strcmp(error, DBUS_ERROR_NO_SERVER) == 0) { - text = _("Please verify that the message bus (D-Bus) service is running.\n"); - } else - if (strncmp(error, CM_DBUS_ERROR_BASE, - strlen(CM_DBUS_ERROR_BASE)) == 0) { - text = _(message); - buf = malloc(strlen(text) + 2); - if (buf != NULL) { - sprintf(buf, "%s\n", text); - text = buf; - } + char *text = NULL; + void *ctx; + ctx = talloc_new(NULL); + text = cm_tdbusm_hint(ctx, error, message); + if ((text == NULL) && + (strncmp(error, CM_DBUS_ERROR_BASE, + strlen(CM_DBUS_ERROR_BASE)) == 0)) { + text = talloc_asprintf("%s\n", _(message)); } if (text != NULL) { - printf("%s", text); + printf("%s", _(text)); } - free(buf); + talloc_free(ctx); return text ? hint_found : hint_unknown; } diff --git a/src/main.c b/src/main.c index fdb7dfc..6f5a367 100644 --- a/src/main.c +++ b/src/main.c @@ -38,6 +38,7 @@ #include "env.h" #include "log.h" #include "tdbus.h" +#include "tdbusm.h" #ifdef ENABLE_NLS #include @@ -56,8 +57,9 @@ main(int argc, char **argv) pid_t pid; FILE *pfp; const char *pidfile = NULL; - char *tmpdir; + char *tmpdir, *hint; dbus_bool_t dofork; + DBusError error; bus = cm_env_default_bus(); dofork = cm_env_default_fork(); @@ -169,8 +171,12 @@ main(int argc, char **argv) exit(1); } - if (cm_tdbus_setup(ec, bus, ctx) != 0) { + if (cm_tdbus_setup(ec, bus, ctx, &error) != 0) { fprintf(stderr, "Error connecting to D-Bus.\n"); + hint = cm_tdbusm_hint(ec, error.name, error.message); + if (hint != NULL) { + fprintf(stderr, "%s", hint); + } talloc_free(ec); exit(1); } diff --git a/src/tdbus.c b/src/tdbus.c index 5dc1e95..db44ab1 100644 --- a/src/tdbus.c +++ b/src/tdbus.c @@ -57,7 +57,7 @@ struct tdbus_connection { void *data; }; -static int cm_tdbus_setup_connection(struct tdbus_connection *tdb); +static int cm_tdbus_setup_connection(struct tdbus_connection *tdb, DBusError *); static void cm_tdbus_dispatch_status(DBusConnection *conn, DBusDispatchStatus new_status, @@ -460,7 +460,7 @@ cm_tdbus_reconnect(struct tevent_context *ec, struct tevent_timer *timer, if (dbus_connection_get_is_connected(tdb->conn)) { /* We're reconnected; reset our handlers. */ cm_log(1, "Reconnected to %s bus.\n", bus_desc); - cm_tdbus_setup_connection(tdb); + cm_tdbus_setup_connection(tdb, NULL); } else { /* Try reconnecting again later. */ later = tevent_timeval_current_ofs(CM_DBUS_RECONNECT_TIMEOUT, 0), @@ -514,7 +514,7 @@ cm_tdbus_filter(DBusConnection *conn, DBusMessage *dmessage, void *data) } static int -cm_tdbus_setup_connection(struct tdbus_connection *tdb) +cm_tdbus_setup_connection(struct tdbus_connection *tdb, DBusError *error) { DBusError err; const char *bus_desc; @@ -562,6 +562,9 @@ cm_tdbus_setup_connection(struct tdbus_connection *tdb) CM_DBUS_NAME, err.message ? err.message : (err.name ? err.name : ""), i); + if (error != NULL) { + dbus_move_error(&err, error); + } return -1; } /* Handle any messages that are already pending. */ @@ -586,7 +589,7 @@ cm_tdbus_setup_connection(struct tdbus_connection *tdb) int cm_tdbus_setup(struct tevent_context *ec, enum cm_tdbus_type bus_type, - void *data) + void *data, DBusError *error) { DBusConnection *conn; const char *bus_desc; @@ -602,15 +605,18 @@ cm_tdbus_setup(struct tevent_context *ec, enum cm_tdbus_type bus_type, bus_desc = NULL; conn = NULL; exit_on_disconnect = TRUE; + if (error != NULL) { + dbus_error_init(error); + } switch (bus_type) { case cm_tdbus_system: - conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL); + conn = dbus_bus_get(DBUS_BUS_SYSTEM, error); /* Don't exit if we get disconnected. */ exit_on_disconnect = FALSE; bus_desc = "system"; break; case cm_tdbus_session: - conn = dbus_bus_get(DBUS_BUS_SESSION, NULL); + conn = dbus_bus_get(DBUS_BUS_SESSION, error); /* Exit if we get disconnected. */ exit_on_disconnect = TRUE; bus_desc = "session"; @@ -625,5 +631,5 @@ cm_tdbus_setup(struct tevent_context *ec, enum cm_tdbus_type bus_type, tdb->conn = conn; tdb->conn_type = bus_type; tdb->data = data; - return cm_tdbus_setup_connection(tdb); + return cm_tdbus_setup_connection(tdb, error); } diff --git a/src/tdbus.h b/src/tdbus.h index 9b069ed..91390c1 100644 --- a/src/tdbus.h +++ b/src/tdbus.h @@ -38,6 +38,6 @@ enum cm_tdbus_type { cm_tdbus_system, cm_tdbus_session }; int cm_tdbus_setup(struct tevent_context *ec, enum cm_tdbus_type bus_type, - void *data); + void *data, DBusError *error); #endif diff --git a/src/tdbusm.c b/src/tdbusm.c index 61f7f80..c4ec201 100644 --- a/src/tdbusm.c +++ b/src/tdbusm.c @@ -27,6 +27,8 @@ #include "tdbusm.h" +#define N_(_text) _text + static char empty_string[] = ""; static const char *empty_string_array[] = {NULL}; @@ -1272,3 +1274,23 @@ cm_tdbusm_find_dict_entry(struct cm_tdbusm_dict **d, } return ret; } + +char * +cm_tdbusm_hint(void *parent, const char *error, const char *message) +{ + char *text = NULL; + if (strcmp(error, DBUS_ERROR_ACCESS_DENIED) == 0) { + text = N_("Insufficient access. Please retry operation as root.\n"); + } else + if ((strcmp(error, DBUS_ERROR_NAME_HAS_NO_OWNER) == 0) || + (strcmp(error, DBUS_ERROR_SERVICE_UNKNOWN) == 0)) { + text = N_("Please verify that the certmonger service has been started.\n"); + } else + if (strcmp(error, DBUS_ERROR_NO_REPLY) == 0) { + text = N_("Please verify that the certmonger service is still running.\n"); + } else + if (strcmp(error, DBUS_ERROR_NO_SERVER) == 0) { + text = N_("Please verify that the message bus (D-Bus) service is running.\n"); + } + return text; +} diff --git a/src/tdbusm.h b/src/tdbusm.h index 8ee865e..64ffbd3 100644 --- a/src/tdbusm.h +++ b/src/tdbusm.h @@ -100,5 +100,6 @@ int cm_tdbusm_set_d(DBusMessage *msg, const struct cm_tdbusm_dict **d); struct cm_tdbusm_dict *cm_tdbusm_find_dict_entry(struct cm_tdbusm_dict **d, const char *key, enum cm_tdbusm_dict_value_type value_type); +char *cm_tdbusm_hint(void *parent, const char *error, const char *message); #endif