Skip to content

Commit

Permalink
linux: do not write twice errors
Browse files Browse the repository at this point in the history
if the error was sent correctly to the sync socket, then do not
attempt to print it again from the container process.

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Jul 28, 2023
1 parent e6f4e4c commit 164c55e
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/libcrun/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -3535,11 +3535,13 @@ read_error_from_sync_socket (int sync_socket_fd, int *error, char **str)
int code;
int ret;

ret = TEMP_FAILURE_RETRY (read (sync_socket_fd, &code, sizeof (code)));
if (UNLIKELY (ret < 0))
return false;

*error = code;
if (*error == 0)
{
ret = TEMP_FAILURE_RETRY (read (sync_socket_fd, &code, sizeof (code)));
if (UNLIKELY (ret < 0))
return false;
*error = code;
}

ret = TEMP_FAILURE_RETRY (read (sync_socket_fd, &size, sizeof (size)));
if (UNLIKELY (ret < 0))
Expand All @@ -3566,9 +3568,9 @@ read_error_from_sync_socket (int sync_socket_fd, int *error, char **str)
static bool
send_error_to_sync_socket (int sync_socket_fd, bool has_fd, libcrun_error_t *err)
{
size_t size;
int ret;
int code;
size_t size;
char *msg;

if (err == NULL || *err == NULL)
Expand Down Expand Up @@ -3604,10 +3606,10 @@ send_error_to_sync_socket (int sync_socket_fd, bool has_fd, libcrun_error_t *err
static inline int
send_success_to_sync_socket (int sync_socket, libcrun_error_t *err)
{
const char success = 0;
const int success = 0;
int ret;

ret = TEMP_FAILURE_RETRY (write (sync_socket, &success, 1));
ret = TEMP_FAILURE_RETRY (write (sync_socket, &success, sizeof (success)));
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "write to sync socket");

Expand All @@ -3622,32 +3624,31 @@ send_error_to_sync_socket_and_die (int sync_socket_fd, bool has_terminal, libcru
if (err == NULL || *err == NULL)
_exit (EXIT_FAILURE);

if (send_error_to_sync_socket (sync_socket_fd, has_terminal, err))
_exit (EXIT_FAILURE);

errno = crun_error_get_errno (err);
msg = (*err)->msg;
libcrun_fail_with_error (errno, "%s", msg);
if (! send_error_to_sync_socket (sync_socket_fd, has_terminal, err))
{
errno = crun_error_get_errno (err);
msg = (*err)->msg;
libcrun_fail_with_error (errno, "%s", msg);
}
_exit (EXIT_FAILURE);
}

static int
expect_success_from_sync_socket (int sync_fd, libcrun_error_t *err)
{
int err_code;
cleanup_free char *err_str = NULL;
char res = 1;
int res = 1;
int ret;

ret = TEMP_FAILURE_RETRY (read (sync_fd, &res, 1));
if (UNLIKELY (ret != 1))
ret = TEMP_FAILURE_RETRY (read (sync_fd, &res, sizeof (res)));
if (UNLIKELY (ret != sizeof (res)))
return crun_make_error (err, errno, "read status from sync socket");

if (res == 0)
return 0;

if (read_error_from_sync_socket (sync_fd, &err_code, &err_str))
return crun_make_error (err, err_code, "%s", err_str);
if (read_error_from_sync_socket (sync_fd, &res, &err_str))
return crun_make_error (err, res, "%s", err_str);

return crun_make_error (err, 0, "read from sync socket");
}
Expand Down Expand Up @@ -4806,7 +4807,7 @@ join_process_parent_helper (runtime_spec_schema_config_schema_process *process,
ret = receive_fd_from_socket (sync_fd, err);
if (UNLIKELY (ret < 0))
{
int err_code;
int err_code = 0;
cleanup_free char *err_str = NULL;

if (read_error_from_sync_socket (sync_fd, &err_code, &err_str))
Expand Down

0 comments on commit 164c55e

Please sign in to comment.