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 1 commit
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
83 changes: 39 additions & 44 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,36 @@ get_ipc_socket_selection(bool *use_ipc_socket_ret)
return return_value;
}

static bool
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;
}

/**
* @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)
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 @@ -500,6 +528,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 +604,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 +709,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");
killenheladagen marked this conversation as resolved.
Show resolved Hide resolved
if (!read_settings_and_start_dockerd()) {
exit_code = -1;
goto end;
}
Expand Down
Loading