From eda855d1efde7dad9ac779a058e21efcfed1b9c2 Mon Sep 17 00:00:00 2001 From: Otavio Pontes Date: Thu, 15 Aug 2019 13:14:47 -0700 Subject: [PATCH] update: Save swupd binary path before executing any update process On updates swupd binary can be replaced so readlink will fail to get the swudp binary path. In this case we should run that before running any update process. And besides that hardcode /usr/bin/swupd as the default swupd path in the case there's something wrong with readlink Fixes #1067 Fixes #1068 Signed-off-by: Otavio Pontes --- src/update.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/update.c b/src/update.c index 59af720d..165a5438 100644 --- a/src/update.c +++ b/src/update.c @@ -45,9 +45,24 @@ static int requested_version = -1; static bool download_only = false; static bool update_search_file_index = false; static bool keepcache = false; +static char swupd_binary[LINE_MAX] = { 0 }; int nonpack; +static void save_swupd_binary_path() +{ + /* we need to resolve the whole path to swupd first, proc/self/exe + * is a symbolic link to the executable that is running the current process */ + int path_length; + path_length = readlink("/proc/self/exe", swupd_binary, sizeof(swupd_binary)); + if (path_length <= 0 || path_length >= LINE_MAX) { + // On errors fallback to default location + strncpy(swupd_binary, "/usr/bin/swupd", sizeof(swupd_binary)); + } else { + swupd_binary[path_length] = '\0'; + } +} + /* This loads the upstream Clear Manifest.Full and local * Manifest.full, and then checks that there are no conflicts between * the files they both include */ @@ -243,6 +258,8 @@ static enum swupd_code main_update() /* start the timer used to report the total time to telemetry */ clock_gettime(CLOCK_MONOTONIC_RAW, &ts_start); + save_swupd_binary_path(); + /* Step 1: Preparation steps */ timelist_timer_start(globals.global_times, "Prepare for update"); progress_set_step(1, "prepare_for_update"); @@ -572,17 +589,6 @@ clean_curl: return SWUPD_INVALID_BINARY; } - /* we need to resolve the whole path to swupd first, proc/self/exe - * is a symbolic link to the executable that is running the current process */ - char swupd_binary[LINE_MAX]; - int path_length; - path_length = readlink("/proc/self/exe", swupd_binary, sizeof(swupd_binary)); - if (path_length <= 0 || path_length >= LINE_MAX) { - error("Could not determine the swupd path\n"); - return -1; - } - swupd_binary[path_length] = '\0'; - /* Run the swupd_argv saved from main */ return execv(swupd_binary, globals.swupd_argv); }