Skip to content

Commit

Permalink
commands: handle EINTR
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Jul 8, 2017
1 parent c01c2be commit ee8377b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
22 changes: 14 additions & 8 deletions src/lxc/commands_utils.c
Expand Up @@ -45,22 +45,28 @@ int lxc_cmd_sock_rcv_state(int state_client_fd, int timeout)
struct lxc_msg msg;
struct timeval out;

memset(&out, 0, sizeof(out));
out.tv_sec = timeout;
ret = setsockopt(state_client_fd, SOL_SOCKET, SO_RCVTIMEO,
(const void *)&out, sizeof(out));
if (ret < 0) {
SYSERROR("Failed to set %ds timeout on containter state socket", timeout);
return -1;
if (timeout >= 0) {
memset(&out, 0, sizeof(out));
out.tv_sec = timeout;
ret = setsockopt(state_client_fd, SOL_SOCKET, SO_RCVTIMEO,
(const void *)&out, sizeof(out));
if (ret < 0) {
SYSERROR("Failed to set %ds timeout on containter "
"state socket",
timeout);
return -1;
}
}

memset(&msg, 0, sizeof(msg));

again:
ret = recv(state_client_fd, &msg, sizeof(msg), 0);
if (ret < 0) {
if (errno == EINTR)
if (errno == EINTR) {
TRACE("Caught EINTR; retrying");
goto again;
}

ERROR("failed to receive message: %s", strerror(errno));
return -1;
Expand Down
16 changes: 9 additions & 7 deletions src/lxc/lxccontainer.c
Expand Up @@ -702,7 +702,7 @@ static void free_init_cmd(char **argv)
static int lxc_rcv_status(int state_socket)
{
int ret;
lxc_state_t state = -1;
int state = -1;
struct timeval timeout = {0};

/* Set 5 second timeout to prevent hanging forever in case something
Expand All @@ -716,14 +716,16 @@ static int lxc_rcv_status(int state_socket)
return -1;
}

again:
/* Receive container state. */
ret = lxc_abstract_unix_rcv_credential(state_socket, &state,
sizeof(lxc_state_t));
/* Close container state client. */
close(state_socket);

if (ret <= 0)
return -1;
sizeof(int));
if (ret <= 0) {
if (errno != EINTR)
return -1;
TRACE("Caught EINTR; retrying");
goto again;
}

return state;
}
Expand Down
13 changes: 10 additions & 3 deletions src/lxc/start.c
Expand Up @@ -384,9 +384,11 @@ static int lxc_serve_state_clients(const char *name,

again:
ret = send(client->clientfd, &msg, sizeof(msg), 0);
if (ret < 0) {
if (errno == EINTR)
if (ret <= 0) {
if (errno == EINTR) {
TRACE("Caught EINTR; retrying");
goto again;
}

ERROR("failed to send message to client");
}
Expand Down Expand Up @@ -417,11 +419,16 @@ static int lxc_serve_state_socket_pair(const char *name,
close(handler->state_socket_pair[0]);
handler->state_socket_pair[0] = -1;

again:
ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1],
&(int){state}, sizeof(int));
if (ret != sizeof(int))
if (ret != sizeof(int)) {
if (errno == EINTR)
goto again;
SYSERROR("Failed to send state to %d",
handler->state_socket_pair[1]);
return -1;
}

TRACE("Sent container state \"%s\" to %d", lxc_state2str(state),
handler->state_socket_pair[1]);
Expand Down

0 comments on commit ee8377b

Please sign in to comment.