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

[Pal] Enhance device "dev:tty" checking #1096

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 5 additions & 4 deletions pal/src/host/linux-sgx/pal_devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, enum

if (!strcmp(uri, "tty")) {
/* special case of "dev:tty" device which is the standard input + standard output */
hdl->dev.is_tty = true;
hdl->dev.nonblocking = false;

if (access == PAL_ACCESS_RDONLY) {
Expand All @@ -57,6 +58,7 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, enum
}
} else {
/* other devices must be opened through the host */
hdl->dev.is_tty = false;
hdl->dev.nonblocking = !!(options & PAL_OPTION_NONBLOCK);

ret = ocall_open(uri, PAL_ACCESS_TO_LINUX_OPEN(access) |
Expand Down Expand Up @@ -119,9 +121,8 @@ static int dev_close(PAL_HANDLE handle) {
if (handle->hdr.type != PAL_TYPE_DEV)
return -PAL_ERROR_INVAL;

/* currently we just assign `0`/`1` FDs without duplicating, so close is a no-op for them */
int ret = 0;
if (handle->dev.fd != PAL_IDX_POISON && handle->dev.fd != 0 && handle->dev.fd != 1) {
if (handle->dev.fd != PAL_IDX_POISON && !handle->dev.is_tty) {
ret = ocall_close(handle->dev.fd);
}
handle->dev.fd = PAL_IDX_POISON;
Expand Down Expand Up @@ -178,7 +179,7 @@ static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
if (handle->hdr.type != PAL_TYPE_DEV)
return -PAL_ERROR_INVAL;

if (handle->dev.fd == 0 || handle->dev.fd == 1) {
if (handle->dev.is_tty) {
/* special case of "dev:tty" device which is the standard input + standard output */
attr->share_flags = 0;
attr->pending_size = 0;
Expand All @@ -203,7 +204,7 @@ static int64_t dev_setlength(PAL_HANDLE handle, uint64_t length) {
if (handle->hdr.type != PAL_TYPE_DEV)
return -PAL_ERROR_INVAL;

if (!(handle->dev.fd == 0 || handle->dev.fd == 1))
if (!handle->dev.is_tty)
return -PAL_ERROR_NOTSUPPORT;

if (length != 0)
Expand Down
1 change: 1 addition & 0 deletions pal/src/host/linux-sgx/pal_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef struct {

struct {
PAL_IDX fd;
bool is_tty;
bool nonblocking;
} dev;

Expand Down
9 changes: 5 additions & 4 deletions pal/src/host/linux/pal_devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, enum

if (!strcmp(uri, "tty")) {
/* special case of "dev:tty" device which is the standard input + standard output */
hdl->dev.is_tty = true;
hdl->dev.nonblocking = false;

if (access == PAL_ACCESS_RDONLY) {
Expand All @@ -53,6 +54,7 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, enum
}
} else {
/* other devices must be opened through the host */
hdl->dev.is_tty = false;
hdl->dev.nonblocking = !!(options & PAL_OPTION_NONBLOCK);

ret = DO_SYSCALL(open, uri, PAL_ACCESS_TO_LINUX_OPEN(access) |
Expand Down Expand Up @@ -115,9 +117,8 @@ static int dev_close(PAL_HANDLE handle) {
if (handle->hdr.type != PAL_TYPE_DEV)
return -PAL_ERROR_INVAL;

/* currently we just assign `0`/`1` FDs without duplicating, so close is a no-op for them */
int ret = 0;
if (handle->dev.fd != PAL_IDX_POISON && handle->dev.fd != 0 && handle->dev.fd != 1) {
if (handle->dev.fd != PAL_IDX_POISON && !handle->dev.is_tty) {
ret = DO_SYSCALL(close, handle->dev.fd);
}
handle->dev.fd = PAL_IDX_POISON;
Expand Down Expand Up @@ -166,7 +167,7 @@ static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
if (handle->hdr.type != PAL_TYPE_DEV)
return -PAL_ERROR_INVAL;

if (handle->dev.fd == 0 || handle->dev.fd == 1) {
if (handle->dev.is_tty) {
/* special case of "dev:tty" device which is the standard input + standard output */
attr->share_flags = 0;
attr->pending_size = 0;
Expand All @@ -191,7 +192,7 @@ static int64_t dev_setlength(PAL_HANDLE handle, uint64_t length) {
if (handle->hdr.type != PAL_TYPE_DEV)
return -PAL_ERROR_INVAL;

if (!(handle->dev.fd == 0 || handle->dev.fd == 1))
if (!handle->dev.is_tty)
return -PAL_ERROR_NOTSUPPORT;

if (length != 0)
Expand Down
1 change: 1 addition & 0 deletions pal/src/host/linux/pal_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef struct {

struct {
PAL_IDX fd;
bool is_tty;
bool nonblocking;
} dev;

Expand Down