Skip to content

Commit

Permalink
[Pal] Enhance tty device checking
Browse files Browse the repository at this point in the history
Add a flag `is_tty` to `PAL_HANDLE` for device. Device oprations check it
instead of fd to determine whether the handle is tty device because fd is
changed after fork.

Signed-off-by: Li, Xun <xun.li@intel.com>
  • Loading branch information
llly committed Sep 1, 2023
1 parent 10c2668 commit 4fa0159
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
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

0 comments on commit 4fa0159

Please sign in to comment.