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

Pass around SD card area instead of using global constant #157

Merged
merged 6 commits into from
Mar 26, 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
Prev Previous commit
Next Next commit
Centralize restart of dockerd
By running g_main_loop_run() in a while-loop, other parts of the
program can retry starting dockerd by simply calling
g_main_loop_quit(). To exit this loop, call quit_program(), which will
also set the global acap_exit_code variable.

This is needed then e.g. axstorage is used to wait for SD card to
become available.
  • Loading branch information
killenheladagen committed Mar 22, 2024
commit 76a2276d455f7a3ff3af602c96c47102c92011ff
76 changes: 27 additions & 49 deletions app/dockerdwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdio.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <syslog.h>
#include <unistd.h>

Expand All @@ -42,8 +43,9 @@ static void dockerd_process_exited_callback(__attribute__((unused)) GPid pid,
// Loop run on the main process
static GMainLoop *loop = NULL;

// Exit code
static int exit_code = 0;
// Exit code of this program. Set using 'quit_program()'.
#define EX_KEEP_RUNNING -1
static int application_exit_code = EX_KEEP_RUNNING;

// Pid of the running dockerd process
static pid_t dockerd_process_pid = -1;
Expand All @@ -52,9 +54,6 @@ static pid_t dockerd_process_pid = -1;
static const char *dockerd_path_on_sd_card =
"/var/spool/storage/SD_DISK/dockerd";

// True if the dockerd_exited_callback should restart dockerd
static bool restart_dockerd = false;

// All ax_parameters the acap has
static const char *ax_parameters[] = {"IPCSocket", "SDCardSupport", "UseTLS"};

Expand All @@ -64,6 +63,13 @@ static const char *tls_certs[] = {"ca.pem",
"server-cert.pem",
"server-key.pem"};

static void
quit_program(int exit_code)
{
application_exit_code = exit_code;
g_main_loop_quit(loop);
}

/**
* @brief Signals handling
*
Expand All @@ -76,7 +82,7 @@ handle_signals(__attribute__((unused)) int signal_num)
case SIGINT:
case SIGTERM:
case SIGQUIT:
g_main_loop_quit(loop);
quit_program(EX_OK);
}
}

Expand Down Expand Up @@ -478,8 +484,7 @@ start_dockerd(const struct settings *settings)
if (!is_process_alive(dockerd_process_pid)) {
syslog(LOG_ERR,
"Starting dockerd failed: Process died unexpectedly during startup");
exit_code = -1;
g_main_loop_quit(loop);
quit_program(EX_SOFTWARE);
goto end;
}
return_value = true;
Expand Down Expand Up @@ -514,7 +519,7 @@ stop_dockerd(void)
goto end;
}

// Send SIGTERM to the process
syslog(LOG_INFO, "Sending SIGTERM to dockerd.");
bool sigterm_successfully_sent = kill(dockerd_process_pid, SIGTERM) == 0;
if (!sigterm_successfully_sent) {
syslog(
Expand Down Expand Up @@ -555,8 +560,6 @@ dockerd_process_exited_callback(__attribute__((unused)) GPid pid,
if (!g_spawn_check_exit_status(status, &error)) {
syslog(LOG_ERR, "Dockerd process exited with error: %d", status);
g_clear_error(&error);

exit_code = -1;
}

dockerd_process_pid = -1;
Expand All @@ -566,16 +569,7 @@ dockerd_process_exited_callback(__attribute__((unused)) GPid pid,
// manner. Remove it manually.
remove("/var/run/docker.pid");

if (restart_dockerd) {
restart_dockerd = false;
if (!read_settings_and_start_dockerd()) {
exit_code = -1;
g_main_loop_quit(loop);
}
} else {
// We shouldn't restart, stop instead.
g_main_loop_quit(loop);
}
g_main_loop_quit(loop); // Trigger a restart of dockerd from main()
}

/**
Expand All @@ -596,17 +590,9 @@ parameter_changed_callback(const gchar *name,
++i) {
if (strcmp(parname, ax_parameters[i]) == 0) {
syslog(LOG_INFO, "%s changed to: %s", ax_parameters[i], value);
restart_dockerd = true;
g_main_loop_quit(loop); // Trigger a restart of dockerd from main()
}
}

// Stop the currently running process.
if (!stop_dockerd()) {
syslog(LOG_ERR,
"Failed to stop dockerd process. Please restart the acap "
"manually.");
exit_code = -1;
}
}

static AXParameter *
Expand Down Expand Up @@ -651,42 +637,34 @@ int
main(void)
{
AXParameter *ax_parameter = NULL;
exit_code = 0;

openlog(NULL, LOG_PID, LOG_USER);
syslog(LOG_INFO, "Started logging.");

loop = g_main_loop_new(NULL, FALSE);

// Setup signal handling.
init_signals();

// Setup ax_parameter
ax_parameter = setup_axparameter();
if (ax_parameter == NULL) {
syslog(LOG_ERR, "Error in setup_axparameter");
exit_code = -1;
goto end;
quit_program(EX_SOFTWARE);
}

/* Create the GLib event loop. */
loop = g_main_loop_new(NULL, FALSE);
loop = g_main_loop_ref(loop);
while (application_exit_code == EX_KEEP_RUNNING) {
if (dockerd_process_pid == -1 && !read_settings_and_start_dockerd())
quit_program(EX_SOFTWARE);

if (!read_settings_and_start_dockerd()) {
exit_code = -1;
goto end;
g_main_loop_run(loop);

if (!stop_dockerd())
syslog(LOG_WARNING, "Failed to shut down dockerd.");
}

/* Run the GLib event loop. */
g_main_loop_run(loop);
g_main_loop_unref(loop);

end:
if (stop_dockerd()) {
syslog(LOG_INFO, "Shutting down. dockerd shut down successfully.");
} else {
syslog(LOG_WARNING, "Shutting down. Failed to shut down dockerd.");
}

if (ax_parameter != NULL) {
for (size_t i = 0; i < sizeof(ax_parameters) / sizeof(ax_parameters[0]);
++i) {
Expand All @@ -698,5 +676,5 @@ main(void)
ax_parameter_free(ax_parameter);
}

return exit_code;
return application_exit_code;
}