Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 80ff863

Browse files
committed
handle channel and tty read on closed socket
1. when read returns 0, it means remotes has closed socket, for normal TCP connections. 2. when read returns -ENOTCONN for vsock, it means remote has closed the socket. We should be able to just handle case 1) if we patch vsock kernel implementation to match normal TCP connection behavior. Signed-off-by: Peng Tao <bergwolf@gmail.com>
1 parent 5f4e90c commit 80ff863

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/init.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,18 @@ static int hyper_ttyfd_read(struct hyper_event *he, int efd, int events)
10441044
if (buf->get < STREAM_HEADER_SIZE) {
10451045
size = hyper_channel_read(he, efd, STREAM_HEADER_SIZE - buf->get, events);
10461046
if (size < 0) {
1047+
/* vsock returns -ENOTCONN upon reading closed socket... */
1048+
if (size == -ENOTCONN) {
1049+
fprintf(stderr, "ttyfd read failed on %p fd %d with err %d\n", he, he->fd, size);
1050+
hyper_modify_event(efd, he, he->flag & ~EPOLLIN);
1051+
size = 0;
1052+
}
10471053
return size;
1054+
} else if (size == 0) {
1055+
/* remote closes connection */
1056+
fprintf(stderr, "remote closes connection on %p fd %d\n", he, he->fd);
1057+
hyper_modify_event(efd, he, he->flag & ~EPOLLIN);
1058+
return 0;
10481059
}
10491060

10501061
buf->get += size;
@@ -1192,7 +1203,18 @@ static int hyper_ctlfd_read(struct hyper_event *he, int efd, int events)
11921203
if (buf->get < CONTROL_HEADER_SIZE) {
11931204
size = hyper_channel_read(he, efd, CONTROL_HEADER_SIZE - buf->get, events);
11941205
if (size < 0) {
1206+
/* vsock returns -ENOTCONN upon reading closed socket... */
1207+
if (size == -ENOTCONN) {
1208+
fprintf(stderr, "ttyfd read failed on %p fd %d with err %d\n", he, he->fd, size);
1209+
hyper_modify_event(efd, he, he->flag & ~EPOLLIN);
1210+
size = 0;
1211+
}
11951212
return size;
1213+
} else if (size == 0) {
1214+
/* remote closes connection */
1215+
fprintf(stderr, "remote closes connection on %p fd %d\n", he, he->fd);
1216+
hyper_modify_event(efd, he, he->flag & ~EPOLLIN);
1217+
return 0;
11961218
}
11971219
if (size > 0) {
11981220
uint8_t *data = malloc(4);

src/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ ssize_t nonblock_read(int fd, void *buf, size_t count)
714714
len += ret;
715715
}
716716

717-
return len > 0 ? len : ret;
717+
return len > 0 ? len : -errno;
718718
}
719719

720720
int hyper_mount_nfs(char *server, char *mountpoint)

0 commit comments

Comments
 (0)