Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package settings in a struct and reduce code duplication #139

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 44 additions & 51 deletions app/dockerdwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
#include <syslog.h>
#include <unistd.h>

struct settings {
bool use_sdcard;
bool use_tls;
bool use_ipc_socket;
};

/**
* @brief Callback called when the dockerd process exits.
*/
Expand Down Expand Up @@ -382,14 +388,33 @@ get_ipc_socket_selection(bool *use_ipc_socket_ret)
return return_value;
}

/**
* @brief Start a new dockerd process.
*
* @return True if successful, false otherwise
*/
static bool
start_dockerd(bool use_sdcard, bool use_tls, bool use_ipc_socket)
read_settings(struct settings *settings)
{
if (!get_and_verify_sd_card_selection(&settings->use_sdcard)) {
syslog(LOG_ERR, "Failed to setup sd_card");
return false;
}
if (!get_and_verify_tls_selection(&settings->use_tls)) {
syslog(LOG_ERR, "Failed to verify tls selection");
return false;
}
if (!get_ipc_socket_selection(&settings->use_ipc_socket)) {
syslog(LOG_ERR, "Failed to get ipc socket selection");
return false;
}
return true;
}

// Return true if dockerd was successfully started.
// Log an error and return false if it failed to start properly.
static bool
start_dockerd(const struct settings *settings)
{
const bool use_sdcard = settings->use_sdcard;
const bool use_tls = settings->use_tls;
const bool use_ipc_socket = settings->use_ipc_socket;

GError *error = NULL;

bool return_value = false;
Expand Down Expand Up @@ -477,7 +502,7 @@ start_dockerd(bool use_sdcard, bool use_tls, bool use_ipc_socket)
&error);
if (!result) {
syslog(LOG_ERR,
"Could not execv the dockerd process. Return value: %d, error: %s",
"Starting dockerd failed: execv returned: %d, error: %s",
result,
error->message);
goto end;
Expand All @@ -487,7 +512,8 @@ start_dockerd(bool use_sdcard, bool use_tls, bool use_ipc_socket)
g_child_watch_add(dockerd_process_pid, dockerd_process_exited_callback, NULL);

if (!is_process_alive(dockerd_process_pid)) {
// The process died during adding of callback, tell loop to quit.
syslog(LOG_ERR,
"Starting dockerd failed: Process died unexpectedly during startup");
exit_code = -1;
g_main_loop_quit(loop);
goto end;
Expand All @@ -500,6 +526,13 @@ start_dockerd(bool use_sdcard, bool use_tls, bool use_ipc_socket)
return return_value;
}

static bool
read_settings_and_start_dockerd(void)
{
struct settings settings = {0};
return read_settings(&settings) && start_dockerd(&settings);
}

/**
* @brief Stop the currently running dockerd process.
*
Expand Down Expand Up @@ -569,27 +602,7 @@ dockerd_process_exited_callback(__attribute__((unused)) GPid pid,

if (restart_dockerd) {
restart_dockerd = false;
bool use_sdcard = false;
bool use_tls = false;
bool use_ipc_socket = false;

if (!get_and_verify_sd_card_selection(&use_sdcard)) {
syslog(LOG_ERR, "Failed to setup sd_card");
exit_code = -1;
g_main_loop_quit(loop);
}
if (!get_and_verify_tls_selection(&use_tls)) {
syslog(LOG_ERR, "Failed to verify tls selection");
exit_code = -1;
g_main_loop_quit(loop);
}
if (!get_ipc_socket_selection(&use_ipc_socket)) {
syslog(LOG_ERR, "Failed to get ipc socket selection");
exit_code = -1;
g_main_loop_quit(loop);
}
if (!start_dockerd(use_sdcard, use_tls, use_ipc_socket)) {
syslog(LOG_ERR, "Failed to restart dockerd, exiting.");
if (!read_settings_and_start_dockerd()) {
exit_code = -1;
g_main_loop_quit(loop);
}
Expand Down Expand Up @@ -694,35 +707,15 @@ main(void)
ax_parameter = setup_axparameter();
if (ax_parameter == NULL) {
syslog(LOG_ERR, "Error in setup_axparameter: %s", error->message);
exit_code = -1;
goto end;
}

/* Create the GLib event loop. */
loop = g_main_loop_new(NULL, FALSE);
loop = g_main_loop_ref(loop);

bool use_sdcard = false;
bool use_tls = false;
bool use_ipc_socket = false;

if (!get_and_verify_sd_card_selection(&use_sdcard)) {
syslog(LOG_INFO, "Failed to setup sd_card");
exit_code = -1;
goto end;
}
if (!get_and_verify_tls_selection(&use_tls)) {
syslog(LOG_INFO, "Failed to verify tls selection");
exit_code = -1;
goto end;
}
if (!get_ipc_socket_selection(&use_ipc_socket)) {
syslog(LOG_INFO, "Failed to get ipc socket selection");
exit_code = -1;
goto end;
}

if (!start_dockerd(use_sdcard, use_tls, use_ipc_socket)) {
syslog(LOG_ERR, "Starting dockerd failed");
if (!read_settings_and_start_dockerd()) {
exit_code = -1;
goto end;
}
Expand Down