summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/main.c49
-rwxr-xr-xsrc/ukify/ukify.py6
2 files changed, 26 insertions, 29 deletions
diff --git a/src/core/main.c b/src/core/main.c
index f779410fda..899d211d1a 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1494,9 +1494,15 @@ static void redirect_telinit(int argc, char *argv[]) {
#endif
}
-static int become_shutdown(
- const char *shutdown_verb,
- int retval) {
+static int become_shutdown(int objective, int retval) {
+
+ static const char* const table[_MANAGER_OBJECTIVE_MAX] = {
+ [MANAGER_EXIT] = "exit",
+ [MANAGER_REBOOT] = "reboot",
+ [MANAGER_POWEROFF] = "poweroff",
+ [MANAGER_HALT] = "halt",
+ [MANAGER_KEXEC] = "kexec",
+ };
char log_level[DECIMAL_STR_MAX(int) + 1],
exit_code[DECIMAL_STR_MAX(uint8_t) + 1],
@@ -1504,7 +1510,7 @@ static int become_shutdown(
const char* command_line[13] = {
SYSTEMD_SHUTDOWN_BINARY_PATH,
- shutdown_verb,
+ table[objective],
"--timeout", timeout,
"--log-level", log_level,
"--log-target",
@@ -1515,7 +1521,8 @@ static int become_shutdown(
size_t pos = 7;
int r;
- assert(shutdown_verb);
+ assert(objective >= 0 && objective < _MANAGER_OBJECTIVE_MAX);
+ assert(table[objective]);
assert(!command_line[pos]);
env_block = strv_copy(environ);
@@ -1549,7 +1556,7 @@ static int become_shutdown(
if (log_get_show_time())
command_line[pos++] = "--log-time";
- if (streq(shutdown_verb, "exit")) {
+ if (objective == MANAGER_EXIT) {
command_line[pos++] = "--exit-code";
command_line[pos++] = exit_code;
xsprintf(exit_code, "%d", retval);
@@ -1557,9 +1564,9 @@ static int become_shutdown(
assert(pos < ELEMENTSOF(command_line));
- if (streq(shutdown_verb, "reboot"))
+ if (objective == MANAGER_REBOOT)
watchdog_timer = arg_reboot_watchdog;
- else if (streq(shutdown_verb, "kexec"))
+ else if (objective == MANAGER_KEXEC)
watchdog_timer = arg_kexec_watchdog;
/* If we reboot or kexec let's set the shutdown watchdog and tell the
@@ -1925,7 +1932,6 @@ static int invoke_main_loop(
const struct rlimit *saved_rlimit_nofile,
const struct rlimit *saved_rlimit_memlock,
int *ret_retval, /* Return parameters relevant for shutting down */
- const char **ret_shutdown_verb, /* … */
FDSet **ret_fds, /* Return parameters for reexecuting */
char **ret_switch_root_dir, /* … */
char **ret_switch_root_init, /* … */
@@ -1937,7 +1943,6 @@ static int invoke_main_loop(
assert(saved_rlimit_nofile);
assert(saved_rlimit_memlock);
assert(ret_retval);
- assert(ret_shutdown_verb);
assert(ret_fds);
assert(ret_switch_root_dir);
assert(ret_switch_root_init);
@@ -1995,7 +2000,6 @@ static int invoke_main_loop(
log_notice("Reexecuting.");
*ret_retval = EXIT_SUCCESS;
- *ret_shutdown_verb = NULL;
*ret_switch_root_dir = *ret_switch_root_init = NULL;
return objective;
@@ -2015,7 +2019,6 @@ static int invoke_main_loop(
log_notice("Switching root.");
*ret_retval = EXIT_SUCCESS;
- *ret_shutdown_verb = NULL;
/* Steal the switch root parameters */
*ret_switch_root_dir = TAKE_PTR(m->switch_root);
@@ -2028,7 +2031,6 @@ static int invoke_main_loop(
log_debug("Exit.");
*ret_retval = m->return_value;
- *ret_shutdown_verb = NULL;
*ret_fds = NULL;
*ret_switch_root_dir = *ret_switch_root_init = NULL;
@@ -2040,18 +2042,9 @@ static int invoke_main_loop(
case MANAGER_POWEROFF:
case MANAGER_HALT:
case MANAGER_KEXEC: {
- static const char* const table[_MANAGER_OBJECTIVE_MAX] = {
- [MANAGER_EXIT] = "exit",
- [MANAGER_REBOOT] = "reboot",
- [MANAGER_POWEROFF] = "poweroff",
- [MANAGER_HALT] = "halt",
- [MANAGER_KEXEC] = "kexec",
- };
-
log_notice("Shutting down.");
*ret_retval = m->return_value;
- assert_se(*ret_shutdown_verb = table[m->objective]);
*ret_fds = NULL;
*ret_switch_root_dir = *ret_switch_root_init = NULL;
@@ -2723,7 +2716,7 @@ int main(int argc, char *argv[]) {
char *switch_root_dir = NULL, *switch_root_init = NULL;
usec_t before_startup, after_startup;
static char systemd[] = "systemd";
- const char *shutdown_verb = NULL, *error_message = NULL;
+ const char *error_message = NULL;
int r, retval = EXIT_FAILURE;
Manager *m = NULL;
FDSet *fds = NULL;
@@ -3036,7 +3029,6 @@ int main(int argc, char *argv[]) {
&saved_rlimit_nofile,
&saved_rlimit_memlock,
&retval,
- &shutdown_verb,
&fds,
&switch_root_dir,
&switch_root_init,
@@ -3096,8 +3088,13 @@ finish:
/* Try to invoke the shutdown binary unless we already failed.
* If we failed above, we want to freeze after finishing cleanup. */
- if (r >= 0 && shutdown_verb) {
- r = become_shutdown(shutdown_verb, retval);
+ if (arg_system && IN_SET(r, MANAGER_EXIT,
+ MANAGER_REBOOT,
+ MANAGER_POWEROFF,
+ MANAGER_HALT,
+ MANAGER_KEXEC)) {
+
+ r = become_shutdown(r, retval);
log_error_errno(r, "Failed to execute shutdown binary, %s: %m", getpid_cached() == 1 ? "freezing" : "quitting");
error_message = "Failed to execute shutdown binary";
}
diff --git a/src/ukify/ukify.py b/src/ukify/ukify.py
index 2493de2a40..2827eadbb6 100755
--- a/src/ukify/ukify.py
+++ b/src/ukify/ukify.py
@@ -76,12 +76,12 @@ def path_is_readable(s: str | None) -> pathlib.Path | None:
return p
-def pe_executable_size(filename):
+def pe_next_section_offset(filename):
import pefile
pe = pefile.PE(filename)
section = pe.sections[-1]
- return section.VirtualAddress + section.Misc_VirtualSize
+ return pe.OPTIONAL_HEADER.ImageBase + section.VirtualAddress + section.Misc_VirtualSize
def round_up(x, blocksize=4096):
@@ -265,7 +265,7 @@ class UKI:
offset: int | None = dataclasses.field(default=None, init=False)
def __post_init__(self):
- self.offset = round_up(pe_executable_size(self.executable))
+ self.offset = round_up(pe_next_section_offset(self.executable))
def add_section(self, section):
assert self.offset