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

Avoid CivetWeb crash if no optional port could be bound #2313

Merged
merged 2 commits into from
Mar 3, 2025
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
2 changes: 1 addition & 1 deletion src/webserver/civetweb/civetweb.c
Original file line number Diff line number Diff line change
Expand Up @@ -20599,7 +20599,7 @@ master_thread_run(struct mg_context *ctx)
unsigned int i;
unsigned int workerthreadcount;

if (!ctx) {
if (!ctx || !ctx->listening_socket_fds) {
return;
}

Expand Down
15 changes: 7 additions & 8 deletions src/webserver/webserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ static in_port_t https_port = 0;
* @note If no ports are configured, a warning is logged and the function returns.
*
* @param void This function does not take any parameters.
* @return void This function does not return any value.
* @return bool Returns whether the server ports were successfully retrieved
*/
static void get_server_ports(void)
static bool get_server_ports(void)
{
if(ctx == NULL)
return;
return false;

// Loop over all listening ports
struct mg_server_port mgports[MAXPORTS] = { 0 };
Expand All @@ -239,7 +239,7 @@ static void get_server_ports(void)
if(ports < 1)
{
log_warn("No web server ports configured!");
return;
return false;
}

// Loop over all ports
Expand Down Expand Up @@ -289,6 +289,8 @@ static void get_server_ports(void)
server_ports[i].is_bound ? "OK" : "NOT bound");

}

return true;
}

in_port_t __attribute__((pure)) get_https_port(void)
Expand Down Expand Up @@ -564,17 +566,14 @@ void http_init(void)
init.configuration_options = options;

/* Start the server */
if((ctx = mg_start2(&init, &error)) == NULL)
if((ctx = mg_start2(&init, &error)) == NULL || !get_server_ports())
{
log_err("Start of webserver failed!. Web interface will not be available!");
log_err(" Error: %s (error code %u.%u)", error.text, error.code, error.code_sub);
log_err(" Hint: Check the webserver log at %s", config.files.log.webserver.v.s);
return;
}

// Get server ports
get_server_ports();

// Register API handler
mg_set_request_handler(ctx, "/api", api_handler, NULL);

Expand Down
Loading