fix: Sécuriser l’écriture socket et initialiser APR dans les tests
Co-authored-by: aider (openrouter/openai/gpt-5.3-codex) <aider@aider.chat>
This commit is contained in:
@ -28,6 +28,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
#ifndef MSG_NOSIGNAL
|
||||
#define MSG_NOSIGNAL 0
|
||||
#endif
|
||||
|
||||
/* Maximum Unix socket path length (sun_path is typically 108 bytes) */
|
||||
#define MAX_SOCKET_PATH_LEN (sizeof(((struct sockaddr_un *)0)->sun_path) - 1)
|
||||
|
||||
@ -513,8 +517,14 @@ static int write_to_socket(const char *data, apr_size_t len, server_rec *s,
|
||||
reqin_log_config_t *cfg, reqin_log_child_state_t *state)
|
||||
{
|
||||
int fd;
|
||||
apr_size_t total_written = 0;
|
||||
apr_time_t error_interval = apr_time_from_sec(cfg->error_report_interval);
|
||||
ssize_t n;
|
||||
apr_time_t error_interval;
|
||||
|
||||
if (!cfg || !state || !s || !data || len == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
error_interval = apr_time_from_sec(cfg->error_report_interval);
|
||||
|
||||
FD_MUTEX_LOCK(state);
|
||||
|
||||
@ -524,42 +534,46 @@ static int write_to_socket(const char *data, apr_size_t len, server_rec *s,
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (total_written < len) {
|
||||
ssize_t n = write(fd, data + total_written, len - total_written);
|
||||
if (n < 0) {
|
||||
int err = errno;
|
||||
apr_time_t now = apr_time_now();
|
||||
int should_report = 0;
|
||||
int conn_lost = (err == EPIPE || err == ECONNRESET || err == ENOTCONN);
|
||||
n = send(fd, data, len, MSG_DONTWAIT | MSG_NOSIGNAL);
|
||||
if (n < 0) {
|
||||
int err = errno;
|
||||
apr_time_t now = apr_time_now();
|
||||
int should_report = 0;
|
||||
int conn_lost = (err == EPIPE || err == ECONNRESET || err == ENOTCONN);
|
||||
|
||||
if (conn_lost) {
|
||||
close(fd);
|
||||
state->socket_fd = -1;
|
||||
state->connect_failed = 1;
|
||||
}
|
||||
|
||||
if (err != EAGAIN && err != EWOULDBLOCK &&
|
||||
(now - state->last_error_report) >= error_interval) {
|
||||
state->last_error_report = now;
|
||||
should_report = 1;
|
||||
}
|
||||
|
||||
FD_MUTEX_UNLOCK(state);
|
||||
|
||||
if (should_report) {
|
||||
if (conn_lost) {
|
||||
ap_log_error(APLOG_MARK, APLOG_ERR, err, s,
|
||||
MOD_REQIN_LOG_NAME ": Unix socket write failed: connection lost");
|
||||
} else {
|
||||
ap_log_error(APLOG_MARK, APLOG_ERR, err, s,
|
||||
MOD_REQIN_LOG_NAME ": Unix socket write failed");
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
if (conn_lost) {
|
||||
close(fd);
|
||||
state->socket_fd = -1;
|
||||
state->connect_failed = 1;
|
||||
}
|
||||
|
||||
total_written += (apr_size_t)n;
|
||||
if (err != EAGAIN && err != EWOULDBLOCK &&
|
||||
(now - state->last_error_report) >= error_interval) {
|
||||
state->last_error_report = now;
|
||||
should_report = 1;
|
||||
}
|
||||
|
||||
FD_MUTEX_UNLOCK(state);
|
||||
|
||||
if (should_report) {
|
||||
if (conn_lost) {
|
||||
ap_log_error(APLOG_MARK, APLOG_ERR, err, s,
|
||||
MOD_REQIN_LOG_NAME ": Unix socket write failed: connection lost");
|
||||
} else {
|
||||
ap_log_error(APLOG_MARK, APLOG_ERR, err, s,
|
||||
MOD_REQIN_LOG_NAME ": Unix socket write failed");
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((apr_size_t)n < len) {
|
||||
close(fd);
|
||||
state->socket_fd = -1;
|
||||
state->connect_failed = 1;
|
||||
FD_MUTEX_UNLOCK(state);
|
||||
return -1;
|
||||
}
|
||||
|
||||
FD_MUTEX_UNLOCK(state);
|
||||
|
||||
Reference in New Issue
Block a user