From 836fb00f2190811c2bf804860f5afe1160d10eac Mon Sep 17 00:00:00 2001 From: Yegor Alexeyev Date: Sun, 25 Jul 2021 05:20:27 +0300 Subject: units: added factory-reset.target --- catalog/systemd.catalog.in | 9 ++++++ man/logind.conf.xml | 5 ++-- man/systemd.special.xml | 7 +++++ src/basic/special.h | 1 + src/login/logind-action.c | 3 ++ src/login/logind-action.h | 1 + src/login/logind-dbus.c | 74 ++++++++++++++++++++++++++++------------------ src/systemd/sd-messages.h | 3 ++ units/factory-reset.target | 12 ++++++++ units/meson.build | 1 + 10 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 units/factory-reset.target diff --git a/catalog/systemd.catalog.in b/catalog/systemd.catalog.in index 3e08f564a6..a5d7dc6f78 100644 --- a/catalog/systemd.catalog.in +++ b/catalog/systemd.catalog.in @@ -188,6 +188,15 @@ Support: %SUPPORT_URL% System shutdown has been initiated. The shutdown has now begun and all system services are terminated and all file systems unmounted. +-- c14aaf76ec284a5fa1f105f88dfb061c +Subject: System factory reset initiated +Defined-By: systemd +Support: %SUPPORT_URL% + +System factory reset has been initiated. The precise operation this +executes is implementation-defined, but typically has the effect of +reverting the system's state and configuration to vendor defaults. + -- 7d4958e842da4a758f6c1cdc7b36dcc5 Subject: A start job for unit @UNIT@ has begun execution Defined-By: systemd diff --git a/man/logind.conf.xml b/man/logind.conf.xml index 5dc24a3f05..3045c1b9ba 100644 --- a/man/logind.conf.xml +++ b/man/logind.conf.xml @@ -216,8 +216,9 @@ suspend, hibernate, hybrid-sleep, - suspend-then-hibernate, and - lock. + suspend-then-hibernate, + lock, and + factory-reset. If ignore, logind will never handle these keys. If lock, all running sessions will be screen-locked; otherwise, the specified action will be taken diff --git a/man/systemd.special.xml b/man/systemd.special.xml index b09c4e9fa2..8755b523ae 100644 --- a/man/systemd.special.xml +++ b/man/systemd.special.xml @@ -33,6 +33,7 @@ default.target, emergency.target, exit.target, + factory-reset.target, final.target, first-boot-complete.target, getty.target, @@ -279,6 +280,12 @@ shutdown when the service manager starts to exit. + + factory-reset.target + + A special target to trigger a factory reset. + + final.target diff --git a/src/basic/special.h b/src/basic/special.h index 78f22f1ac9..5d1111fd71 100644 --- a/src/basic/special.h +++ b/src/basic/special.h @@ -20,6 +20,7 @@ #define SPECIAL_HIBERNATE_TARGET "hibernate.target" #define SPECIAL_HYBRID_SLEEP_TARGET "hybrid-sleep.target" #define SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET "suspend-then-hibernate.target" +#define SPECIAL_FACTORY_RESET_TARGET "factory-reset.target" /* Special boot targets */ #define SPECIAL_RESCUE_TARGET "rescue.target" diff --git a/src/login/logind-action.c b/src/login/logind-action.c index 8ed066c25e..e172910948 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -27,6 +27,7 @@ const char* manager_target_for_action(HandleAction handle) { [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET, [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET, [HANDLE_SUSPEND_THEN_HIBERNATE] = SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, + [HANDLE_FACTORY_RESET] = SPECIAL_FACTORY_RESET_TARGET, }; assert(handle >= 0); @@ -51,6 +52,7 @@ int manager_handle_action( [HANDLE_HIBERNATE] = "Hibernating...", [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending...", [HANDLE_SUSPEND_THEN_HIBERNATE] = "Suspending, then hibernating...", + [HANDLE_FACTORY_RESET] = "Performing factory reset...", }; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; @@ -179,6 +181,7 @@ static const char* const handle_action_table[_HANDLE_ACTION_MAX] = { [HANDLE_HIBERNATE] = "hibernate", [HANDLE_HYBRID_SLEEP] = "hybrid-sleep", [HANDLE_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate", + [HANDLE_FACTORY_RESET] = "factory-reset", [HANDLE_LOCK] = "lock", }; diff --git a/src/login/logind-action.h b/src/login/logind-action.h index 0baad37a84..ec2fece2b7 100644 --- a/src/login/logind-action.h +++ b/src/login/logind-action.h @@ -14,6 +14,7 @@ typedef enum HandleAction { HANDLE_HYBRID_SLEEP, HANDLE_SUSPEND_THEN_HIBERNATE, HANDLE_LOCK, + HANDLE_FACTORY_RESET, _HANDLE_ACTION_MAX, _HANDLE_ACTION_INVALID = -EINVAL, } HandleAction; diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index ccda0bdec4..6e9dde1c15 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1491,41 +1491,59 @@ static int have_multiple_sessions( return false; } -static int bus_manager_log_shutdown( - Manager *m, - const char *unit_name) { - - const char *p, *q; - +_printf_(2, 0) +static int log_with_wall_message(Manager *m, const char *d, const char *p, const char *q) { assert(m); - assert(unit_name); - - if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) { - p = "MESSAGE=System is powering down"; - q = "SHUTDOWN=power-off"; - } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) { - p = "MESSAGE=System is rebooting"; - q = "SHUTDOWN=reboot"; - } else if (streq(unit_name, SPECIAL_HALT_TARGET)) { - p = "MESSAGE=System is halting"; - q = "SHUTDOWN=halt"; - } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) { - p = "MESSAGE=System is rebooting with kexec"; - q = "SHUTDOWN=kexec"; - } else { - p = "MESSAGE=System is shutting down"; - q = NULL; - } if (isempty(m->wall_message)) p = strjoina(p, "."); else p = strjoina(p, " (", m->wall_message, ")."); - return log_struct(LOG_NOTICE, - "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR, - p, - q); + return log_struct(LOG_NOTICE, d, p, q); +} + +static int bus_manager_log_shutdown( + Manager *m, + const char *unit_name) { + + assert(m); + assert(unit_name); + + if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) + return log_with_wall_message(m, + "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR, + "MESSAGE=System is powering down", + "SHUTDOWN=power-off"); + + if (streq(unit_name, SPECIAL_REBOOT_TARGET)) + return log_with_wall_message(m, + "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR, + "MESSAGE=System is rebooting", + "SHUTDOWN=reboot"); + + if (streq(unit_name, SPECIAL_HALT_TARGET)) + return log_with_wall_message(m, + "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR, + "MESSAGE=System is halting", + "SHUTDOWN=halt"); + + if (streq(unit_name, SPECIAL_KEXEC_TARGET)) + return log_with_wall_message(m, + "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR, + "MESSAGE=System is rebooting with kexec", + "SHUTDOWN=kexec"); + + if (streq(unit_name, SPECIAL_FACTORY_RESET_TARGET)) + return log_with_wall_message(m, + "MESSAGE_ID=" SD_MESSAGE_FACTORY_RESET_STR, + "MESSAGE=System is performing factory reset", + NULL); + + return log_with_wall_message(m, + "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR, + "MESSAGE=System is shutting down", + NULL); } static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) { diff --git a/src/systemd/sd-messages.h b/src/systemd/sd-messages.h index eb285efe94..b9445cf0a9 100644 --- a/src/systemd/sd-messages.h +++ b/src/systemd/sd-messages.h @@ -81,6 +81,9 @@ _SD_BEGIN_DECLARATIONS; #define SD_MESSAGE_SHUTDOWN SD_ID128_MAKE(98,26,88,66,d1,d5,4a,49,9c,4e,98,92,1d,93,bc,40) #define SD_MESSAGE_SHUTDOWN_STR SD_ID128_MAKE_STR(98,26,88,66,d1,d5,4a,49,9c,4e,98,92,1d,93,bc,40) +#define SD_MESSAGE_FACTORY_RESET SD_ID128_MAKE(c1,4a,af,76,ec,28,4a,5f,a1,f1,05,f8,8d,fb,06,1c) +#define SD_MESSAGE_FACTORY_RESET_STR SD_ID128_MAKE_STR(c1,4a,af,76,ec,28,4a,5f,a1,f1,05,f8,8d,fb,06,1c) + /* The messages below are actually about jobs, not really about units, the macros are misleadingly named. Moreover * SD_MESSAGE_UNIT_FAILED is not actually about a failing unit but about a failed start job. A job either finishes with * SD_MESSAGE_UNIT_STARTED or with SD_MESSAGE_UNIT_FAILED hence. */ diff --git a/units/factory-reset.target b/units/factory-reset.target new file mode 100644 index 0000000000..99f383d6b1 --- /dev/null +++ b/units/factory-reset.target @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of systemd. +# +# systemd is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. + +[Unit] +Description=Target that triggers factory reset. Does nothing by default. +Documentation=man:systemd.special(7) diff --git a/units/meson.build b/units/meson.build index 64da9efaaf..9398df721e 100644 --- a/units/meson.build +++ b/units/meson.build @@ -19,6 +19,7 @@ units = [ 'sysinit.target.wants/'], ['emergency.target', ''], ['exit.target', ''], + ['factory-reset.target', ''], ['final.target', ''], ['first-boot-complete.target', ''], ['getty.target', '', -- cgit v1.2.1