From 6f28c033ec506847b3bfa4efaf52478120c36946 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 13 Jul 2010 20:20:36 +0200 Subject: [PATCH] systemctl: introduce try-restart and reload-or-restart commands --- man/systemctl.xml | 34 +++++++++++++++++++++++++++------- src/dbus-manager.c | 26 +++++++++++++++++++++++++- src/dbus-unit.c | 16 +++++++++++++++- src/dbus-unit.h | 10 +++++++++- src/systemctl.c | 35 ++++++++++++++++++++++++----------- 5 files changed, 100 insertions(+), 21 deletions(-) diff --git a/man/systemctl.xml b/man/systemctl.xml index 22d3f6ee..d73e974e 100644 --- a/man/systemctl.xml +++ b/man/systemctl.xml @@ -193,13 +193,6 @@ specified on the command line. - - restart [NAME...] - - Restart one or more - units specified on the command - line. - reload [NAME...] @@ -226,6 +219,33 @@ commands. + + restart [NAME...] + + Restart one or more + units specified on the command + line. If the units are not running yet + they will be + started. + + + try-restart [NAME...] + + Restart one or more + units specified on the command + line. If the units are not running yet + the operation will + fail. + + + reload-or-restart [NAME...] + reload-or-try-restart [NAME...] + + Reload one or more + units if they support it. If not + restart them + instead. + isolate [NAME] diff --git a/src/dbus-manager.c b/src/dbus-manager.c index f52b06e5..c717ccd8 100644 --- a/src/dbus-manager.c +++ b/src/dbus-manager.c @@ -62,6 +62,16 @@ " \n" \ " \n" \ " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -239,6 +249,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *reply = NULL; char * path = NULL; JobType job_type = _JOB_TYPE_INVALID; + bool reload_if_possible = false; assert(connection); assert(message); @@ -310,7 +321,13 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, job_type = JOB_RESTART; else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit")) job_type = JOB_TRY_RESTART; - else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) { + else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) { + reload_if_possible = true; + job_type = JOB_RESTART; + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) { + reload_if_possible = true; + job_type = JOB_TRY_RESTART; + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) { uint32_t id; Job *j; @@ -739,6 +756,13 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0) return bus_send_error_reply(m, connection, message, &error, r); + if (reload_if_possible && unit_can_reload(u)) { + if (job_type == JOB_RESTART) + job_type = JOB_RELOAD_OR_START; + else if (job_type == JOB_TRY_RESTART) + job_type = JOB_RELOAD; + } + if (job_type == JOB_START && u->meta.only_by_dependency) { dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only."); return bus_send_error_reply(m, connection, message, &error, -EPERM); diff --git a/src/dbus-unit.c b/src/dbus-unit.c index 735e5a53..da7d1bd5 100644 --- a/src/dbus-unit.c +++ b/src/dbus-unit.c @@ -260,6 +260,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn DBusError error; JobType job_type = _JOB_TYPE_INVALID; char *path = NULL; + bool reload_if_possible = false; dbus_error_init(&error); @@ -273,7 +274,13 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn job_type = JOB_RESTART; else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart")) job_type = JOB_TRY_RESTART; - else if (UNIT_VTABLE(u)->bus_message_handler) + else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) { + reload_if_possible = true; + job_type = JOB_RESTART; + } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) { + reload_if_possible = true; + job_type = JOB_TRY_RESTART; + } else if (UNIT_VTABLE(u)->bus_message_handler) return UNIT_VTABLE(u)->bus_message_handler(u, connection, message); else return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; @@ -296,6 +303,13 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn DBUS_TYPE_INVALID)) return bus_send_error_reply(m, connection, message, &error, -EINVAL); + if (reload_if_possible && unit_can_reload(u)) { + if (job_type == JOB_RESTART) + job_type = JOB_RELOAD_OR_START; + else if (job_type == JOB_TRY_RESTART) + job_type = JOB_RELOAD; + } + if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) { dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode); return bus_send_error_reply(m, connection, message, &error, -EINVAL); diff --git a/src/dbus-unit.h b/src/dbus-unit.h index 17965b61..1d11af3a 100644 --- a/src/dbus-unit.h +++ b/src/dbus-unit.h @@ -36,6 +36,10 @@ " \n" \ " \n" \ " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -44,7 +48,11 @@ " \n" \ " \n" \ " \n" \ - " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ diff --git a/src/systemctl.c b/src/systemctl.c index 4218a22b..bdb294ed 100644 --- a/src/systemctl.c +++ b/src/systemctl.c @@ -733,10 +733,13 @@ static int start_unit(DBusConnection *bus, char **args, unsigned n) { if (arg_action == ACTION_SYSTEMCTL) { method = - streq(args[0], "stop") ? "StopUnit" : - streq(args[0], "reload") ? "ReloadUnit" : - streq(args[0], "restart") ? "RestartUnit" : - "StartUnit"; + streq(args[0], "stop") ? "StopUnit" : + streq(args[0], "reload") ? "ReloadUnit" : + streq(args[0], "restart") ? "RestartUnit" : + streq(args[0], "try-restart") ? "TryRestartUnit" : + streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" : + streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" : + "StartUnit"; mode = (streq(args[0], "isolate") || @@ -2497,7 +2500,8 @@ static int systemctl_help(void) { " -t --type=TYPE List only units of a particular type\n" " -p --property=NAME Show only properties by this name\n" " -a --all Show all units/properties, including dead/empty ones\n" - " --fail When installing a new job, fail if conflicting jobs are pending\n" + " --fail When installing a new job, fail if conflicting jobs are\n" + " pending\n" " --system Connect to system bus\n" " --session Connect to session bus\n" " -q --quiet Suppress output\n" @@ -2507,12 +2511,18 @@ static int systemctl_help(void) { " list-units List units\n" " start [NAME...] Start one or more units\n" " stop [NAME...] Stop one or more units\n" - " restart [NAME...] Restart one or more units\n" " reload [NAME...] Reload one or more units\n" + " restart [NAME...] Start or restart one or more units\n" + " try-restart [NAME...] Restart one or more units if active\n" + " reload-or-restart [NAME...] Reload one or more units is possible,\n" + " otherwise start or restart\n" + " reload-or-try-restart [NAME...] Reload one or more units is possible,\n" + " otherwise restart if active\n" " isolate [NAME] Start one unit and stop all others\n" - " check [NAME...] Check whether any of the passed units are active\n" + " check [NAME...] Check whether units are active\n" " status [NAME...] Show status of one or more units\n" - " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n" + " show [NAME...|JOB...] Show properties of one or more\n" + " units/jobs/manager\n" " load [NAME...] Load one or more units\n" " list-jobs List jobs\n" " cancel [JOB...] Cancel one or more jobs\n" @@ -2530,9 +2540,9 @@ static int systemctl_help(void) { " halt Shut down and halt the system\n" " poweroff Shut down and power-off the system\n" " reboot Shut down and reboot the system\n" - " default Enter default mode\n" - " rescue Enter rescue mode\n" - " emergency Enter emergency mode\n", + " rescue Enter system rescue mode\n" + " emergency Enter system emergency mode\n" + " default Enter system default mode\n", program_invocation_short_name); return 0; @@ -3193,6 +3203,9 @@ static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) { { "stop", MORE, 2, start_unit }, { "reload", MORE, 2, start_unit }, { "restart", MORE, 2, start_unit }, + { "try-restart", MORE, 2, start_unit }, + { "reload-or-restart", MORE, 2, start_unit }, + { "reload-or-try-restart", MORE, 2, start_unit }, { "isolate", EQUAL, 2, start_unit }, { "check", MORE, 2, check_unit }, { "show", MORE, 1, show }, -- 2.39.5