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

Allow dockerd to recover when uploading a new file #210

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions app/dockerdwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,10 @@ static void sd_card_callback(const char* sd_card_area, void* app_state_void_ptr)
main_loop_quit(); // Trigger a restart of dockerd from main()
}

static void restart_dockerd_after_file_upload(void) {
static void restart_dockerd_after_file_upload(struct app_state* app_state) {
// If dockerd has failed before, this file upload may have resolved the problem.
allow_dockerd_to_start(app_state, true);

main_loop_quit();
}

Expand Down Expand Up @@ -831,7 +834,10 @@ int main(int argc, char** argv) {

init_signals();

int fcgi_error = fcgi_start(http_request_callback, restart_dockerd_after_file_upload);
struct restart_dockerd_context restart_dockerd_context;
restart_dockerd_context.restart_dockerd = restart_dockerd_after_file_upload;
restart_dockerd_context.app_state = &app_state;
int fcgi_error = fcgi_start(http_request_callback, &restart_dockerd_context);
if (fcgi_error)
return fcgi_error;

Expand Down
11 changes: 6 additions & 5 deletions app/http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ static void response_msg(FCGX_Request* request, const char* status, const char*
response(request, status, "text/plain", body);
}

static void
post_request(FCGX_Request* request, const char* filename, restart_dockerd_t restart_dockerd) {
static void post_request(FCGX_Request* request,
const char* filename,
struct restart_dockerd_context* restart_dockerd_context) {
g_autofree char* temp_file = fcgi_write_file_from_stream(*request);
if (!temp_file) {
response_msg(request, HTTP_422_UNPROCESSABLE_CONTENT, "Upload to temporary file failed.");
Expand All @@ -85,7 +86,7 @@ post_request(FCGX_Request* request, const char* filename, restart_dockerd_t rest
response_msg(request, HTTP_500_INTERNAL_SERVER_ERROR, "Failed to copy file to localdata");
else {
response_204_no_content(request);
restart_dockerd();
restart_dockerd_context->restart_dockerd(restart_dockerd_context->app_state);
}

if (unlink(temp_file) != 0)
Expand Down Expand Up @@ -113,7 +114,7 @@ static void malformed_request(FCGX_Request* request, const char* method, const c
response_msg(request, HTTP_400_BAD_REQUEST, "Malformed request");
}

void http_request_callback(void* request_void_ptr, void* restart_dockerd_void_ptr) {
void http_request_callback(void* request_void_ptr, void* restart_dockerd_context_void_ptr) {
FCGX_Request* request = (FCGX_Request*)request_void_ptr;

const char* method = FCGX_GetParam("REQUEST_METHOD", request->envp);
Expand All @@ -128,7 +129,7 @@ void http_request_callback(void* request_void_ptr, void* restart_dockerd_void_pt
filename++; // Strip leading '/'

if (strcmp(method, "POST") == 0)
post_request(request, filename, restart_dockerd_void_ptr);
post_request(request, filename, restart_dockerd_context_void_ptr);
else if (strcmp(method, "DELETE") == 0)
delete_request(request, filename);
else
Expand Down
13 changes: 10 additions & 3 deletions app/http_request.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#pragma once

// Callback function called from a thread by the FCGI server
void http_request_callback(void* request_void_ptr, void* restart_dockerd_void_ptr);
struct app_state;

typedef void (*restart_dockerd_t)(struct app_state*);

typedef void (*restart_dockerd_t)(void);
struct restart_dockerd_context {
restart_dockerd_t restart_dockerd;
struct app_state* app_state;
};

// Callback function called from a thread by the FCGI server
void http_request_callback(void* request_void_ptr, void* restart_dockerd_context_void_ptr);