From 33a68bc4302891e8c591570942bc39394acefd23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borys=20Pop=C5=82awski?= Date: Fri, 3 Sep 2021 02:08:54 +0200 Subject: [PATCH] [PAL] Make `pal_type_*` enum values uppercase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also remove SET_HANDLE_TYPE and IS_HANDLE_TYPE macros for grepability. Signed-off-by: Borys Popławski --- LibOS/shim/src/fs/chroot/fs.c | 8 ++-- Pal/include/pal/pal.h | 34 ++++++------- Pal/regression/SendHandle.c | 6 +-- Pal/src/db_events.c | 6 +-- Pal/src/db_streams.c | 30 ++++++------ Pal/src/db_threading.c | 2 +- Pal/src/host/Linux-SGX/db_devices.c | 16 +++---- Pal/src/host/Linux-SGX/db_eventfd.c | 8 ++-- Pal/src/host/Linux-SGX/db_events.c | 2 +- Pal/src/host/Linux-SGX/db_files.c | 18 +++---- Pal/src/host/Linux-SGX/db_main.c | 2 +- Pal/src/host/Linux-SGX/db_pipes.c | 50 +++++++++---------- Pal/src/host/Linux-SGX/db_process.c | 4 +- Pal/src/host/Linux-SGX/db_sockets.c | 44 ++++++++--------- Pal/src/host/Linux-SGX/db_streams.c | 56 +++++++++++----------- Pal/src/host/Linux-SGX/db_threading.c | 2 +- Pal/src/host/Linux-SGX/enclave_framework.c | 10 ++-- Pal/src/host/Linux/db_devices.c | 16 +++---- Pal/src/host/Linux/db_eventfd.c | 8 ++-- Pal/src/host/Linux/db_events.c | 2 +- Pal/src/host/Linux/db_files.c | 14 +++--- Pal/src/host/Linux/db_main.c | 2 +- Pal/src/host/Linux/db_pipes.c | 46 +++++++++--------- Pal/src/host/Linux/db_process.c | 4 +- Pal/src/host/Linux/db_sockets.c | 46 +++++++++--------- Pal/src/host/Linux/db_streams.c | 56 +++++++++++----------- Pal/src/host/Linux/db_threading.c | 2 +- 27 files changed, 245 insertions(+), 249 deletions(-) diff --git a/LibOS/shim/src/fs/chroot/fs.c b/LibOS/shim/src/fs/chroot/fs.c index d459ffa13f..8fc0c4ae72 100644 --- a/LibOS/shim/src/fs/chroot/fs.c +++ b/LibOS/shim/src/fs/chroot/fs.c @@ -238,15 +238,15 @@ static int __query_attr(struct shim_dentry* dent, struct shim_file_data* data, mode_t type; /* need to correct the data type */ switch (pal_attr.handle_type) { - case pal_type_file: + case PAL_TYPE_FILE: data->type = FILE_REGULAR; type = S_IFREG; break; - case pal_type_dir: + case PAL_TYPE_DIR: data->type = FILE_DIR; type = S_IFDIR; break; - case pal_type_dev: + case PAL_TYPE_DEV: if (strstartswith(qstrgetstr(&data->host_uri) + static_strlen(URI_PREFIX_DEV), "tty")) { data->type = FILE_TTY; } else { @@ -254,7 +254,7 @@ static int __query_attr(struct shim_dentry* dent, struct shim_file_data* data, } type = S_IFCHR; break; - case pal_type_pipe: + case PAL_TYPE_PIPE: log_warning("trying to access '%s' which is a host-level FIFO (named pipe); " "Graphene supports only named pipes created by Graphene processes", qstrgetstr(&data->host_uri)); diff --git a/Pal/include/pal/pal.h b/Pal/include/pal/pal.h index 23509ec3dc..f4a8d176c5 100644 --- a/Pal/include/pal/pal.h +++ b/Pal/include/pal/pal.h @@ -72,9 +72,6 @@ static inline void init_handle_hdr(PAL_HDR* hdr, int pal_type) { hdr->flags = 0; } -#define SET_HANDLE_TYPE(handle, t) init_handle_hdr(HANDLE_HDR(handle), pal_type_##t) -#define IS_HANDLE_TYPE(handle, t) (HANDLE_HDR(handle)->type == pal_type_##t) - #else typedef union pal_handle { struct { @@ -93,27 +90,26 @@ typedef union pal_handle { /********** PAL TYPE DEFINITIONS **********/ enum { - pal_type_file, - pal_type_pipe, - pal_type_pipesrv, - pal_type_pipecli, - pal_type_pipeprv, - pal_type_dev, - pal_type_dir, - pal_type_tcp, - pal_type_tcpsrv, - pal_type_udp, - pal_type_udpsrv, - pal_type_process, - pal_type_thread, - pal_type_event, - pal_type_eventfd, + PAL_TYPE_FILE, + PAL_TYPE_PIPE, + PAL_TYPE_PIPESRV, + PAL_TYPE_PIPECLI, + PAL_TYPE_PIPEPRV, + PAL_TYPE_DEV, + PAL_TYPE_DIR, + PAL_TYPE_TCP, + PAL_TYPE_TCPSRV, + PAL_TYPE_UDP, + PAL_TYPE_UDPSRV, + PAL_TYPE_PROCESS, + PAL_TYPE_THREAD, + PAL_TYPE_EVENT, + PAL_TYPE_EVENTFD, PAL_HANDLE_TYPE_BOUND, }; #define PAL_IDX_POISON ((PAL_IDX)-1) /* PAL identifier poison value */ #define PAL_GET_TYPE(h) (HANDLE_HDR(h)->type) -#define PAL_CHECK_TYPE(h, t) (PAL_GET_TYPE(h) == pal_type_##t) #define UNKNOWN_HANDLE(handle) (PAL_GET_TYPE(handle) >= PAL_HANDLE_TYPE_BOUND) typedef struct PAL_PTR_RANGE_ { diff --git a/Pal/regression/SendHandle.c b/Pal/regression/SendHandle.c index 561d49cc47..d9676c13fb 100644 --- a/Pal/regression/SendHandle.c +++ b/Pal/regression/SendHandle.c @@ -22,7 +22,7 @@ int main(int argc, char** argv) { memset(buffer, 0, 20); switch (PAL_GET_TYPE(handles[i])) { - case pal_type_pipesrv: { + case PAL_TYPE_PIPESRV: { PAL_HANDLE pipe = NULL; ret = DkStreamWaitForClient(handles[i], &pipe); @@ -38,7 +38,7 @@ int main(int argc, char** argv) { break; } - case pal_type_udpsrv: { + case PAL_TYPE_UDPSRV: { char uri[20]; size = sizeof(buffer); @@ -49,7 +49,7 @@ int main(int argc, char** argv) { break; } - case pal_type_file: + case PAL_TYPE_FILE: size = sizeof(buffer); ret = DkStreamRead(handles[i], 0, &size, buffer, NULL, 0); if (ret == 0 && size > 0) diff --git a/Pal/src/db_events.c b/Pal/src/db_events.c index bb377cce28..33d7a8bee6 100644 --- a/Pal/src/db_events.c +++ b/Pal/src/db_events.c @@ -13,16 +13,16 @@ int DkEventCreate(PAL_HANDLE* handle, bool init_signaled, bool auto_clear) { } void DkEventSet(PAL_HANDLE handle) { - assert(handle && IS_HANDLE_TYPE(handle, event)); + assert(handle && HANDLE_HDR(handle)->type == PAL_TYPE_EVENT); _DkEventSet(handle); } void DkEventClear(PAL_HANDLE handle) { - assert(handle && IS_HANDLE_TYPE(handle, event)); + assert(handle && HANDLE_HDR(handle)->type == PAL_TYPE_EVENT); _DkEventClear(handle); } int DkEventWait(PAL_HANDLE handle, uint64_t* timeout_us) { - assert(handle && IS_HANDLE_TYPE(handle, event)); + assert(handle && HANDLE_HDR(handle)->type == PAL_TYPE_EVENT); return _DkEventWait(handle, timeout_us); } diff --git a/Pal/src/db_streams.c b/Pal/src/db_streams.c index f94395c367..57c800241d 100644 --- a/Pal/src/db_streams.c +++ b/Pal/src/db_streams.c @@ -27,21 +27,21 @@ extern struct handle_ops g_event_ops; extern struct handle_ops g_eventfd_ops; const struct handle_ops* g_pal_handle_ops[PAL_HANDLE_TYPE_BOUND] = { - [pal_type_file] = &g_file_ops, - [pal_type_pipe] = &g_pipe_ops, - [pal_type_pipesrv] = &g_pipe_ops, - [pal_type_pipecli] = &g_pipe_ops, - [pal_type_pipeprv] = &g_pipeprv_ops, - [pal_type_dev] = &g_dev_ops, - [pal_type_dir] = &g_dir_ops, - [pal_type_tcp] = &g_tcp_ops, - [pal_type_tcpsrv] = &g_tcp_ops, - [pal_type_udp] = &g_udp_ops, - [pal_type_udpsrv] = &g_udpsrv_ops, - [pal_type_process] = &g_proc_ops, - [pal_type_thread] = &g_thread_ops, - [pal_type_event] = &g_event_ops, - [pal_type_eventfd] = &g_eventfd_ops, + [PAL_TYPE_FILE] = &g_file_ops, + [PAL_TYPE_PIPE] = &g_pipe_ops, + [PAL_TYPE_PIPESRV] = &g_pipe_ops, + [PAL_TYPE_PIPECLI] = &g_pipe_ops, + [PAL_TYPE_PIPEPRV] = &g_pipeprv_ops, + [PAL_TYPE_DEV] = &g_dev_ops, + [PAL_TYPE_DIR] = &g_dir_ops, + [PAL_TYPE_TCP] = &g_tcp_ops, + [PAL_TYPE_TCPSRV] = &g_tcp_ops, + [PAL_TYPE_UDP] = &g_udp_ops, + [PAL_TYPE_UDPSRV] = &g_udpsrv_ops, + [PAL_TYPE_PROCESS] = &g_proc_ops, + [PAL_TYPE_THREAD] = &g_thread_ops, + [PAL_TYPE_EVENT] = &g_event_ops, + [PAL_TYPE_EVENTFD] = &g_eventfd_ops, }; /* parse_stream_uri scan the uri, seperate prefix and search for diff --git a/Pal/src/db_threading.c b/Pal/src/db_threading.c index 1346e4cde5..9ea1be10a0 100644 --- a/Pal/src/db_threading.c +++ b/Pal/src/db_threading.c @@ -30,7 +30,7 @@ noreturn void DkThreadExit(PAL_PTR clear_child_tid) { /* PAL call DkThreadResume: resume the execution of a thread which is delayed before */ int DkThreadResume(PAL_HANDLE threadHandle) { - if (!threadHandle || !IS_HANDLE_TYPE(threadHandle, thread)) { + if (!threadHandle || HANDLE_HDR(threadHandle)->type != PAL_TYPE_THREAD) { return -PAL_ERROR_INVAL; } diff --git a/Pal/src/host/Linux-SGX/db_devices.c b/Pal/src/host/Linux-SGX/db_devices.c index 9c042d8cb3..7c4c7b2c4f 100644 --- a/Pal/src/host/Linux-SGX/db_devices.c +++ b/Pal/src/host/Linux-SGX/db_devices.c @@ -37,7 +37,7 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int a if (!hdl) return -PAL_ERROR_NOMEM; - SET_HANDLE_TYPE(hdl, dev); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_DEV); if (!strcmp(uri, "tty")) { /* special case of "dev:tty" device which is the standard input + standard output */ @@ -86,7 +86,7 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int a } static int64_t dev_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* buffer) { - if (offset || !IS_HANDLE_TYPE(handle, dev)) + if (offset || HANDLE_HDR(handle)->type != PAL_TYPE_DEV) return -PAL_ERROR_INVAL; if (!(HANDLE_HDR(handle)->flags & RFD(0))) @@ -100,7 +100,7 @@ static int64_t dev_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* } static int64_t dev_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, const void* buffer) { - if (offset || !IS_HANDLE_TYPE(handle, dev)) + if (offset || HANDLE_HDR(handle)->type != PAL_TYPE_DEV) return -PAL_ERROR_INVAL; if (!(HANDLE_HDR(handle)->flags & WFD(0))) @@ -114,7 +114,7 @@ static int64_t dev_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, cons } static int dev_close(PAL_HANDLE handle) { - if (!IS_HANDLE_TYPE(handle, dev)) + if (HANDLE_HDR(handle)->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 */ @@ -127,7 +127,7 @@ static int dev_close(PAL_HANDLE handle) { } static int dev_flush(PAL_HANDLE handle) { - if (!IS_HANDLE_TYPE(handle, dev)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_DEV) return -PAL_ERROR_INVAL; if (handle->dev.fd != PAL_IDX_POISON) { @@ -173,13 +173,13 @@ static int dev_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* att ocall_close(fd); } - attr->handle_type = pal_type_dev; + attr->handle_type = PAL_TYPE_DEV; attr->nonblocking = PAL_FALSE; return 0; } static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { - if (!IS_HANDLE_TYPE(handle, dev)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_DEV) return -PAL_ERROR_INVAL; if (handle->dev.fd == 0 || handle->dev.fd == 1) { @@ -203,7 +203,7 @@ static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { attr->pending_size = stat_buf.st_size; } - attr->handle_type = pal_type_dev; + attr->handle_type = PAL_TYPE_DEV; attr->nonblocking = handle->dev.nonblocking; return 0; } diff --git a/Pal/src/host/Linux-SGX/db_eventfd.c b/Pal/src/host/Linux-SGX/db_eventfd.c index eb9161faac..979afabc8d 100644 --- a/Pal/src/host/Linux-SGX/db_eventfd.c +++ b/Pal/src/host/Linux-SGX/db_eventfd.c @@ -57,7 +57,7 @@ static int eventfd_pal_open(PAL_HANDLE* handle, const char* type, const char* ur ocall_close(fd); return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, eventfd); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_EVENTFD); /* Note: using index 0, given that there is only 1 eventfd FD per pal-handle. */ HANDLE_HDR(hdl)->flags = RFD(0) | WFD(0); @@ -73,7 +73,7 @@ static int64_t eventfd_pal_read(PAL_HANDLE handle, uint64_t offset, uint64_t len if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, eventfd)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_EVENTFD) return -PAL_ERROR_NOTCONNECTION; if (len < sizeof(uint64_t)) @@ -94,7 +94,7 @@ static int64_t eventfd_pal_write(PAL_HANDLE handle, uint64_t offset, uint64_t le if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, eventfd)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_EVENTFD) return -PAL_ERROR_NOTCONNECTION; if (len < sizeof(uint64_t)) @@ -144,7 +144,7 @@ static int eventfd_pal_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) } static int eventfd_pal_close(PAL_HANDLE handle) { - if (IS_HANDLE_TYPE(handle, eventfd)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_EVENTFD) { if (handle->eventfd.fd != PAL_IDX_POISON) { ocall_close(handle->eventfd.fd); handle->eventfd.fd = PAL_IDX_POISON; diff --git a/Pal/src/host/Linux-SGX/db_events.c b/Pal/src/host/Linux-SGX/db_events.c index fc9318a4d9..dd58376177 100644 --- a/Pal/src/host/Linux-SGX/db_events.c +++ b/Pal/src/host/Linux-SGX/db_events.c @@ -22,7 +22,7 @@ int _DkEventCreate(PAL_HANDLE* handle_ptr, bool init_signaled, bool auto_clear) return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(handle, event); + init_handle_hdr(HANDLE_HDR(handle), PAL_TYPE_EVENT); handle->event.signaled_untrusted = malloc_untrusted(sizeof(*handle->event.signaled_untrusted)); if (!handle->event.signaled_untrusted) { free(handle); diff --git a/Pal/src/host/Linux-SGX/db_files.c b/Pal/src/host/Linux-SGX/db_files.c index 461dd6e85e..dc7c881986 100644 --- a/Pal/src/host/Linux-SGX/db_files.c +++ b/Pal/src/host/Linux-SGX/db_files.c @@ -66,7 +66,7 @@ static int file_open(PAL_HANDLE* handle, const char* type, const char* uri, int return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, file); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_FILE); HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0); memcpy((char*)hdl + HANDLE_SIZE(file), normpath, normpath_size); @@ -625,15 +625,15 @@ static int file_flush(PAL_HANDLE handle) { static inline int file_stat_type(struct stat* stat) { if (S_ISREG(stat->st_mode)) - return pal_type_file; + return PAL_TYPE_FILE; if (S_ISDIR(stat->st_mode)) - return pal_type_dir; + return PAL_TYPE_DIR; if (S_ISCHR(stat->st_mode)) - return pal_type_dev; + return PAL_TYPE_DEV; if (S_ISFIFO(stat->st_mode)) - return pal_type_pipe; + return PAL_TYPE_PIPE; if (S_ISSOCK(stat->st_mode)) - return pal_type_dev; + return PAL_TYPE_DEV; return 0; } @@ -720,7 +720,7 @@ static int file_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* at /* For protected files return the data size, not real FS size */ struct protected_file* pf = get_protected_file(path); - if (pf && attr->handle_type != pal_type_dir) { + if (pf && attr->handle_type != PAL_TYPE_DIR) { /* protected files should be regular files */ if (S_ISFIFO(stat_buf.st_mode)) { ret = -PAL_ERROR_DENIED; @@ -757,7 +757,7 @@ static int file_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { file_attrcopy(attr, &stat_buf); - if (attr->handle_type != pal_type_dir) { + if (attr->handle_type != PAL_TYPE_DIR) { /* For protected files return the data size, not real FS size */ struct protected_file* pf = find_protected_file_handle(handle); if (pf) { @@ -874,7 +874,7 @@ static int dir_open(PAL_HANDLE* handle, const char* type, const char* uri, int a ocall_close(fd); return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, dir); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_DIR); HANDLE_HDR(hdl)->flags |= RFD(0); hdl->dir.fd = fd; char* path = (void*)hdl + HANDLE_SIZE(dir); diff --git a/Pal/src/host/Linux-SGX/db_main.c b/Pal/src/host/Linux-SGX/db_main.c index d9b9df4e20..6026359fa9 100644 --- a/Pal/src/host/Linux-SGX/db_main.c +++ b/Pal/src/host/Linux-SGX/db_main.c @@ -733,7 +733,7 @@ noreturn void pal_linux_main(char* uptr_libpal_uri, size_t libpal_uri_len, char* log_error("Out of memory"); ocall_exit(1, true); } - SET_HANDLE_TYPE(first_thread, thread); + init_handle_hdr(HANDLE_HDR(first_thread), PAL_TYPE_THREAD); first_thread->thread.tcs = g_enclave_base + GET_ENCLAVE_TLS(tcs_offset); /* child threads are assigned TIDs 2,3,...; see pal_start_thread() */ first_thread->thread.tid = 1; diff --git a/Pal/src/host/Linux-SGX/db_pipes.c b/Pal/src/host/Linux-SGX/db_pipes.c index d168b8a992..fe56900e99 100644 --- a/Pal/src/host/Linux-SGX/db_pipes.c +++ b/Pal/src/host/Linux-SGX/db_pipes.c @@ -33,7 +33,7 @@ static int thread_handshake_func(void* param) { PAL_HANDLE handle = (PAL_HANDLE)param; assert(handle); - assert(IS_HANDLE_TYPE(handle, pipe)); + assert(HANDLE_HDR(handle)->type == PAL_TYPE_PIPE); assert(!handle->pipe.ssl_ctx); assert(!handle->pipe.handshake_done); @@ -85,7 +85,7 @@ static int pipe_listen(PAL_HANDLE* handle, const char* name, int options) { return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, pipesrv); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_PIPESRV); HANDLE_HDR(hdl)->flags |= RFD(0); /* cannot write to a listening socket */ hdl->pipe.fd = ret; hdl->pipe.nonblocking = options & PAL_OPTION_NONBLOCK ? PAL_TRUE : PAL_FALSE; @@ -118,7 +118,7 @@ static int pipe_listen(PAL_HANDLE* handle, const char* name, int options) { * \return 0 on success, negative PAL error code otherwise. */ static int pipe_waitforclient(PAL_HANDLE handle, PAL_HANDLE* client) { - if (!IS_HANDLE_TYPE(handle, pipesrv)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPESRV) return -PAL_ERROR_NOTSERVER; if (handle->pipe.fd == PAL_IDX_POISON) @@ -135,7 +135,7 @@ static int pipe_waitforclient(PAL_HANDLE handle, PAL_HANDLE* client) { return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(clnt, pipecli); + init_handle_hdr(HANDLE_HDR(clnt), PAL_TYPE_PIPECLI); HANDLE_HDR(clnt)->flags |= RFD(0) | WFD(0); clnt->pipe.fd = ret; clnt->pipe.name = handle->pipe.name; @@ -203,7 +203,7 @@ static int pipe_connect(PAL_HANDLE* handle, const char* name, int options) { return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, pipe); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_PIPE); HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0); hdl->pipe.fd = ret; hdl->pipe.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE; @@ -266,7 +266,7 @@ static int pipe_private(PAL_HANDLE* handle, int options) { return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, pipeprv); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_PIPEPRV); HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(1); /* first FD for reads, second FD for writes */ hdl->pipeprv.fds[0] = fds[0]; hdl->pipeprv.fds[1] = fds[1]; @@ -337,12 +337,12 @@ static int64_t pipe_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) && - !IS_HANDLE_TYPE(handle, pipe)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPECLI && HANDLE_HDR(handle)->type != PAL_TYPE_PIPEPRV && + HANDLE_HDR(handle)->type != PAL_TYPE_PIPE) return -PAL_ERROR_NOTCONNECTION; ssize_t bytes; - if (IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) { /* pipeprv are currently not encrypted, see pipe_private() */ bytes = ocall_recv(handle->pipeprv.fds[0], buffer, len, NULL, NULL, NULL, NULL); if (bytes < 0) @@ -375,12 +375,12 @@ static int64_t pipe_write(PAL_HANDLE handle, uint64_t offset, uint64_t len, cons if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) && - !IS_HANDLE_TYPE(handle, pipe)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPECLI && HANDLE_HDR(handle)->type != PAL_TYPE_PIPEPRV && + HANDLE_HDR(handle)->type != PAL_TYPE_PIPE) return -PAL_ERROR_NOTCONNECTION; ssize_t bytes; - if (IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) { /* pipeprv are currently not encrypted, see pipe_private() */ bytes = ocall_send(handle->pipeprv.fds[1], buffer, len, NULL, 0, NULL, 0); if (bytes < 0) @@ -407,7 +407,7 @@ static int64_t pipe_write(PAL_HANDLE handle, uint64_t offset, uint64_t len, cons * \return 0 on success, negative PAL error code otherwise. */ static int pipe_close(PAL_HANDLE handle) { - if (IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) { if (handle->pipeprv.fds[0] != PAL_IDX_POISON) { ocall_close(handle->pipeprv.fds[0]); handle->pipeprv.fds[0] = PAL_IDX_POISON; @@ -454,7 +454,7 @@ static int pipe_delete(PAL_HANDLE handle, int access) { return -PAL_ERROR_INVAL; } - if (IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) { /* pipeprv has two underlying FDs, shut down the requested one(s) */ if (handle->pipeprv.fds[0] != PAL_IDX_POISON && (shutdown == SHUT_RD || shutdown == SHUT_RDWR)) { @@ -494,13 +494,13 @@ static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { return -PAL_ERROR_BADHANDLE; attr->handle_type = HANDLE_HDR(handle)->type; - attr->nonblocking = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.nonblocking - : handle->pipe.nonblocking; + attr->nonblocking = HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV ? handle->pipeprv.nonblocking + : handle->pipe.nonblocking; attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0); /* get number of bytes available for reading (doesn't make sense for "listening" pipes) */ attr->pending_size = 0; - if (!IS_HANDLE_TYPE(handle, pipesrv)) { + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPESRV) { ret = ocall_fionread(handle->pipe.fd); if (ret < 0) return unix_to_pal_error(ret); @@ -509,7 +509,7 @@ static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { } /* query if there is data available for reading/writing */ - if (IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) { /* for private pipe, readable and writable are queried on different fds */ struct pollfd pfd[2] = {{.fd = handle->pipeprv.fds[0], .events = POLLIN, .revents = 0}, {.fd = handle->pipeprv.fds[1], .events = POLLOUT, .revents = 0}}; @@ -527,7 +527,7 @@ static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { /* for non-private pipes, both readable and writable are queried on the same fd */ short pfd_events = POLLIN; - if (!IS_HANDLE_TYPE(handle, pipesrv)) { + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPESRV) { /* querying for writing doesn't make sense for "listening" pipes */ pfd_events |= POLLOUT; } @@ -557,14 +557,14 @@ static int pipe_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { if (handle->generic.fds[0] == PAL_IDX_POISON) return -PAL_ERROR_BADHANDLE; - if (!IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPEPRV) { /* This pipe might use a secure session, make sure all initial work is done. */ while (!__atomic_load_n(&handle->pipe.handshake_done, __ATOMIC_ACQUIRE)) { CPU_RELAX(); } } - PAL_BOL* nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv) + PAL_BOL* nonblocking = (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) ? &handle->pipeprv.nonblocking : &handle->pipe.nonblocking; @@ -597,16 +597,16 @@ static int pipe_getname(PAL_HANDLE handle, char* buffer, size_t count) { size_t prefix_len = 0; switch (HANDLE_TYPE(handle)) { - case pal_type_pipesrv: - case pal_type_pipecli: + case PAL_TYPE_PIPESRV: + case PAL_TYPE_PIPECLI: prefix_len = static_strlen(URI_TYPE_PIPE_SRV); prefix = URI_TYPE_PIPE_SRV; break; - case pal_type_pipe: + case PAL_TYPE_PIPE: prefix_len = static_strlen(URI_TYPE_PIPE); prefix = URI_TYPE_PIPE; break; - case pal_type_pipeprv: + case PAL_TYPE_PIPEPRV: default: return -PAL_ERROR_INVAL; } diff --git a/Pal/src/host/Linux-SGX/db_process.c b/Pal/src/host/Linux-SGX/db_process.c index 05add06aad..7e519d4019 100644 --- a/Pal/src/host/Linux-SGX/db_process.c +++ b/Pal/src/host/Linux-SGX/db_process.c @@ -146,7 +146,7 @@ int _DkProcessCreate(PAL_HANDLE* handle, const char** args) { if (!child) return -PAL_ERROR_NOMEM; - SET_HANDLE_TYPE(child, process); + init_handle_hdr(HANDLE_HDR(child), PAL_TYPE_PROCESS); HANDLE_HDR(child)->flags |= RFD(0) | WFD(0); child->process.stream = stream_fd; child->process.nonblocking = PAL_FALSE; @@ -217,7 +217,7 @@ int init_child_process(PAL_HANDLE* parent_handle, uint64_t* instance_id_ptr) { if (!parent) return -PAL_ERROR_NOMEM; - SET_HANDLE_TYPE(parent, process); + init_handle_hdr(HANDLE_HDR(parent), PAL_TYPE_PROCESS); HANDLE_HDR(parent)->flags |= RFD(0) | WFD(0); parent->process.stream = g_pal_sec.stream_fd; diff --git a/Pal/src/host/Linux-SGX/db_sockets.c b/Pal/src/host/Linux-SGX/db_sockets.c index 34bee594f9..e367d7eaf0 100644 --- a/Pal/src/host/Linux-SGX/db_sockets.c +++ b/Pal/src/host/Linux-SGX/db_sockets.c @@ -229,7 +229,7 @@ static inline PAL_HANDLE socket_create_handle(int type, int fd, int options, memset(hdl, 0, sizeof(struct pal_handle)); init_handle_hdr(HANDLE_HDR(hdl), type); - HANDLE_HDR(hdl)->flags |= RFD(0) | (type != pal_type_tcpsrv ? WFD(0) : 0); + HANDLE_HDR(hdl)->flags |= RFD(0) | (type != PAL_TYPE_TCPSRV ? WFD(0) : 0); hdl->sock.fd = fd; void* addr = (void*)hdl + HANDLE_SIZE(sock); if (bind_addr) { @@ -289,7 +289,7 @@ static int tcp_listen(PAL_HANDLE* handle, char* uri, int create, int options) { if (ret < 0) return unix_to_pal_error(ret); - *handle = socket_create_handle(pal_type_tcpsrv, ret, options, bind_addr, bind_addrlen, NULL, 0, + *handle = socket_create_handle(PAL_TYPE_TCPSRV, ret, options, bind_addr, bind_addrlen, NULL, 0, &sock_options); if (!(*handle)) { ocall_close(ret); @@ -301,7 +301,7 @@ static int tcp_listen(PAL_HANDLE* handle, char* uri, int create, int options) { /* accept a tcp connection */ static int tcp_accept(PAL_HANDLE handle, PAL_HANDLE* client) { - if (!IS_HANDLE_TYPE(handle, tcpsrv) || !handle->sock.bind || handle->sock.conn) + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCPSRV || !handle->sock.bind || handle->sock.conn) return -PAL_ERROR_NOTSERVER; if (handle->sock.fd == PAL_IDX_POISON) @@ -322,7 +322,7 @@ static int tcp_accept(PAL_HANDLE handle, PAL_HANDLE* client) { if (ret < 0) return unix_to_pal_error(ret); - *client = socket_create_handle(pal_type_tcp, ret, 0, bind_addr, bind_addrlen, + *client = socket_create_handle(PAL_TYPE_TCP, ret, 0, bind_addr, bind_addrlen, (struct sockaddr*)&dest_addr, dest_addrlen, &sock_options); if (!(*client)) { @@ -363,7 +363,7 @@ static int tcp_connect(PAL_HANDLE* handle, char* uri, int options) { if (ret < 0) return unix_to_pal_error(ret); - *handle = socket_create_handle(pal_type_tcp, ret, options, bind_addr, bind_addrlen, dest_addr, + *handle = socket_create_handle(PAL_TYPE_TCP, ret, options, bind_addr, bind_addrlen, dest_addr, dest_addrlen, &sock_options); if (!(*handle)) { @@ -407,7 +407,7 @@ static int64_t tcp_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, tcp) || !handle->sock.conn) + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCP || !handle->sock.conn) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -426,7 +426,7 @@ static int64_t tcp_write(PAL_HANDLE handle, uint64_t offset, uint64_t len, const if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, tcp) || !handle->sock.conn) + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCP || !handle->sock.conn) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -464,7 +464,7 @@ static int udp_bind(PAL_HANDLE* handle, char* uri, int create, int options) { if (ret < 0) return unix_to_pal_error(ret); - *handle = socket_create_handle(pal_type_udpsrv, ret, options, bind_addr, bind_addrlen, NULL, 0, + *handle = socket_create_handle(PAL_TYPE_UDPSRV, ret, options, bind_addr, bind_addrlen, NULL, 0, &sock_options); if (!(*handle)) { @@ -500,7 +500,7 @@ static int udp_connect(PAL_HANDLE* handle, char* uri, int create, int options) { if (ret < 0) return unix_to_pal_error(ret); - *handle = socket_create_handle(dest_addr ? pal_type_udp : pal_type_udpsrv, ret, options, + *handle = socket_create_handle(dest_addr ? PAL_TYPE_UDP : PAL_TYPE_UDPSRV, ret, options, bind_addr, bind_addrlen, dest_addr, dest_addrlen, &sock_options); if (!(*handle)) { @@ -542,7 +542,7 @@ static int64_t udp_receive(PAL_HANDLE handle, uint64_t offset, uint64_t len, voi if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, udp)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_UDP) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -557,7 +557,7 @@ static int64_t udp_receivebyaddr(PAL_HANDLE handle, uint64_t offset, uint64_t le if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, udpsrv)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_UDPSRV) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -588,7 +588,7 @@ static int64_t udp_send(PAL_HANDLE handle, uint64_t offset, uint64_t len, const if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, udp)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_UDP) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -606,7 +606,7 @@ static int64_t udp_sendbyaddr(PAL_HANDLE handle, uint64_t offset, uint64_t len, if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, udpsrv)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_UDPSRV) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -640,10 +640,10 @@ static int socket_delete(PAL_HANDLE handle, int access) { if (handle->sock.fd == PAL_IDX_POISON) return 0; - if (!IS_HANDLE_TYPE(handle, tcp) && access) + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCP && access) return -PAL_ERROR_INVAL; - if (IS_HANDLE_TYPE(handle, tcp) || IS_HANDLE_TYPE(handle, tcpsrv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_TCP || HANDLE_HDR(handle)->type == PAL_TYPE_TCPSRV) { int shutdown; switch (access) { case 0: @@ -701,7 +701,7 @@ static int socket_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { /* get number of bytes available for reading (doesn't make sense for listening sockets) */ attr->pending_size = 0; - if (!IS_HANDLE_TYPE(handle, tcpsrv)) { + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCPSRV) { ret = ocall_fionread(handle->sock.fd); if (ret < 0) return unix_to_pal_error(ret); @@ -735,7 +735,7 @@ static int socket_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { handle->sock.nonblocking = attr->nonblocking; } - if (HANDLE_TYPE(handle) != pal_type_tcpsrv) { + if (HANDLE_TYPE(handle) != PAL_TYPE_TCPSRV) { struct __kernel_linger { int l_onoff; int l_linger; @@ -789,7 +789,7 @@ static int socket_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { } } - if (HANDLE_TYPE(handle) == pal_type_tcp || HANDLE_TYPE(handle) == pal_type_tcpsrv) { + if (HANDLE_TYPE(handle) == PAL_TYPE_TCP || HANDLE_TYPE(handle) == PAL_TYPE_TCPSRV) { if (attr->socket.tcp_cork != handle->sock.tcp_cork) { val = attr->socket.tcp_cork ? 1 : 0; ret = ocall_setsockopt(fd, SOL_TCP, TCP_CORK, &val, sizeof(int)); @@ -831,23 +831,23 @@ static int socket_getname(PAL_HANDLE handle, char* buffer, size_t count) { struct sockaddr* dest_addr = NULL; switch (PAL_GET_TYPE(handle)) { - case pal_type_tcpsrv: + case PAL_TYPE_TCPSRV: prefix_len = static_strlen(URI_PREFIX_TCP_SRV); prefix = URI_PREFIX_TCP_SRV; bind_addr = (struct sockaddr*)handle->sock.bind; break; - case pal_type_tcp: + case PAL_TYPE_TCP: prefix_len = static_strlen(URI_PREFIX_TCP); prefix = URI_PREFIX_TCP; bind_addr = (struct sockaddr*)handle->sock.bind; dest_addr = (struct sockaddr*)handle->sock.conn; break; - case pal_type_udpsrv: + case PAL_TYPE_UDPSRV: prefix_len = static_strlen(URI_PREFIX_UDP_SRV); prefix = URI_PREFIX_UDP_SRV; bind_addr = (struct sockaddr*)handle->sock.bind; break; - case pal_type_udp: + case PAL_TYPE_UDP: prefix_len = static_strlen(URI_PREFIX_UDP); prefix = URI_PREFIX_UDP; bind_addr = (struct sockaddr*)handle->sock.bind; diff --git a/Pal/src/host/Linux-SGX/db_streams.c b/Pal/src/host/Linux-SGX/db_streams.c index 186771502e..433648bdc7 100644 --- a/Pal/src/host/Linux-SGX/db_streams.c +++ b/Pal/src/host/Linux-SGX/db_streams.c @@ -94,12 +94,12 @@ static ssize_t handle_serialize(PAL_HANDLE handle, void** data) { /* find fields to serialize (depends on the handle type) and assign them to d1/d2; note that * no handle type has more than two such fields, and some have none at all */ switch (PAL_GET_TYPE(handle)) { - case pal_type_file: + case PAL_TYPE_FILE: d1 = handle->file.realpath; dsz1 = strlen(handle->file.realpath) + 1; break; - case pal_type_pipe: - case pal_type_pipecli: + case PAL_TYPE_PIPE: + case PAL_TYPE_PIPECLI: /* session key is part of handle but need to serialize SSL context */ if (handle->pipe.ssl_ctx) { free_d1 = true; @@ -108,22 +108,22 @@ static ssize_t handle_serialize(PAL_HANDLE handle, void** data) { return -PAL_ERROR_DENIED; } break; - case pal_type_pipesrv: - case pal_type_pipeprv: + case PAL_TYPE_PIPESRV: + case PAL_TYPE_PIPEPRV: break; - case pal_type_dev: + case PAL_TYPE_DEV: /* devices have no fields to serialize */ break; - case pal_type_dir: + case PAL_TYPE_DIR: if (handle->dir.realpath) { d1 = handle->dir.realpath; dsz1 = strlen(handle->dir.realpath) + 1; } break; - case pal_type_tcp: - case pal_type_tcpsrv: - case pal_type_udp: - case pal_type_udpsrv: + case PAL_TYPE_TCP: + case PAL_TYPE_TCPSRV: + case PAL_TYPE_UDP: + case PAL_TYPE_UDPSRV: if (handle->sock.bind) { d1 = (const void*)handle->sock.bind; dsz1 = addr_size(handle->sock.bind); @@ -133,7 +133,7 @@ static ssize_t handle_serialize(PAL_HANDLE handle, void** data) { dsz2 = addr_size(handle->sock.conn); } break; - case pal_type_process: + case PAL_TYPE_PROCESS: /* session key is part of handle but need to serialize SSL context */ if (handle->process.ssl_ctx) { free_d1 = true; @@ -142,7 +142,7 @@ static ssize_t handle_serialize(PAL_HANDLE handle, void** data) { return -PAL_ERROR_DENIED; } break; - case pal_type_eventfd: + case PAL_TYPE_EVENTFD: break; default: return -PAL_ERROR_INVAL; @@ -185,12 +185,12 @@ static int handle_deserialize(PAL_HANDLE* handle, const void* data, size_t size, /* update handle fields to point to correct contents (located right after handle itself) */ switch (PAL_GET_TYPE(hdl)) { - case pal_type_file: + case PAL_TYPE_FILE: hdl->file.realpath = hdl->file.realpath ? (PAL_STR)hdl + hdlsz : NULL; hdl->file.chunk_hashes = (PAL_PTR)NULL; break; - case pal_type_pipe: - case pal_type_pipecli: + case PAL_TYPE_PIPE: + case PAL_TYPE_PIPECLI: /* session key is part of handle but need to deserialize SSL context */ hdl->pipe.fd = fds[0]; /* correct host FD must be passed to SSL context */ ret = _DkStreamSecureInit(hdl, hdl->pipe.is_server, &hdl->pipe.session_key, @@ -201,18 +201,18 @@ static int handle_deserialize(PAL_HANDLE* handle, const void* data, size_t size, return -PAL_ERROR_DENIED; } break; - case pal_type_pipesrv: - case pal_type_pipeprv: + case PAL_TYPE_PIPESRV: + case PAL_TYPE_PIPEPRV: break; - case pal_type_dev: + case PAL_TYPE_DEV: break; - case pal_type_dir: + case PAL_TYPE_DIR: hdl->dir.realpath = hdl->dir.realpath ? (PAL_STR)hdl + hdlsz : NULL; break; - case pal_type_tcp: - case pal_type_tcpsrv: - case pal_type_udp: - case pal_type_udpsrv: { + case PAL_TYPE_TCP: + case PAL_TYPE_TCPSRV: + case PAL_TYPE_UDP: + case PAL_TYPE_UDPSRV: { size_t s1 = hdl->sock.bind ? addr_size((PAL_PTR)hdl + hdlsz) : 0; size_t s2 = hdl->sock.conn ? addr_size((PAL_PTR)hdl + hdlsz + s1) : 0; if (s1) @@ -221,7 +221,7 @@ static int handle_deserialize(PAL_HANDLE* handle, const void* data, size_t size, hdl->sock.conn = (PAL_PTR)hdl + hdlsz + s2; break; } - case pal_type_process: + case PAL_TYPE_PROCESS: /* session key is part of handle but need to deserialize SSL context */ hdl->process.stream = fds[0]; /* correct host FD must be passed to SSL context */ ret = _DkStreamSecureInit(hdl, hdl->process.is_server, &hdl->process.session_key, @@ -232,7 +232,7 @@ static int handle_deserialize(PAL_HANDLE* handle, const void* data, size_t size, return -PAL_ERROR_DENIED; } break; - case pal_type_eventfd: + case PAL_TYPE_EVENTFD: break; default: free(hdl); @@ -253,7 +253,7 @@ static int handle_deserialize(PAL_HANDLE* handle, const void* data, size_t size, * \return 0 on success, negative PAL error code otherwise. */ int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) { - if (!IS_HANDLE_TYPE(hdl, process)) + if (HANDLE_HDR(hdl)->type != PAL_TYPE_PROCESS) return -PAL_ERROR_BADHANDLE; /* serialize cargo handle into a blob hdl_data */ @@ -323,7 +323,7 @@ int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) { * \return 0 on success, negative PAL error code otherwise. */ int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE* cargo) { - if (!IS_HANDLE_TYPE(hdl, process)) + if (HANDLE_HDR(hdl)->type != PAL_TYPE_PROCESS) return -PAL_ERROR_BADHANDLE; ssize_t ret; diff --git a/Pal/src/host/Linux-SGX/db_threading.c b/Pal/src/host/Linux-SGX/db_threading.c index 496ae7a9c5..fb8f86da93 100644 --- a/Pal/src/host/Linux-SGX/db_threading.c +++ b/Pal/src/host/Linux-SGX/db_threading.c @@ -98,7 +98,7 @@ int _DkThreadCreate(PAL_HANDLE* handle, int (*callback)(void*), const void* para PAL_HANDLE new_thread = malloc(HANDLE_SIZE(thread)); if (!new_thread) return -PAL_ERROR_NOMEM; - SET_HANDLE_TYPE(new_thread, thread); + init_handle_hdr(HANDLE_HDR(new_thread), PAL_TYPE_THREAD); /* * tid will be filled later by pal_start_thread() * tid is cleared to avoid random value here. diff --git a/Pal/src/host/Linux-SGX/enclave_framework.c b/Pal/src/host/Linux-SGX/enclave_framework.c index b28994bb8d..787adc383b 100644 --- a/Pal/src/host/Linux-SGX/enclave_framework.c +++ b/Pal/src/host/Linux-SGX/enclave_framework.c @@ -994,7 +994,7 @@ int _DkStreamKeyExchange(PAL_HANDLE stream, PAL_SESSION_KEY* out_key, size_t secret_size; uint8_t secret[DH_SIZE]; - assert(IS_HANDLE_TYPE(stream, process)); + assert(HANDLE_HDR(stream)->type == PAL_TYPE_PROCESS); /* perform unauthenticated DH key exchange to produce two collaterals: the session key K_e and * the assymetric SHA256 hashes over K_e (for later use in SGX report's reportdata) */ @@ -1087,7 +1087,7 @@ int _DkStreamReportRequest(PAL_HANDLE stream, sgx_report_data_t* my_report_data, uint64_t bytes; int64_t ret; - assert(IS_HANDLE_TYPE(stream, process)); + assert(HANDLE_HDR(stream)->type == PAL_TYPE_PROCESS); /* A -> B: targetinfo[A] */ memset(&target_info, 0, sizeof(target_info)); @@ -1181,7 +1181,7 @@ int _DkStreamReportRespond(PAL_HANDLE stream, sgx_report_data_t* my_report_data, uint64_t bytes; int64_t ret; - assert(IS_HANDLE_TYPE(stream, process)); + assert(HANDLE_HDR(stream)->type == PAL_TYPE_PROCESS); memset(&target_info, 0, sizeof(target_info)); @@ -1261,9 +1261,9 @@ int _DkStreamSecureInit(PAL_HANDLE stream, bool is_server, PAL_SESSION_KEY* sess size_t buf_size) { int stream_fd; - if (IS_HANDLE_TYPE(stream, process)) + if (HANDLE_HDR(stream)->type == PAL_TYPE_PROCESS) stream_fd = stream->process.stream; - else if (IS_HANDLE_TYPE(stream, pipe) || IS_HANDLE_TYPE(stream, pipecli)) + else if (HANDLE_HDR(stream)->type == PAL_TYPE_PIPE || HANDLE_HDR(stream)->type == PAL_TYPE_PIPECLI) stream_fd = stream->pipe.fd; else return -PAL_ERROR_BADHANDLE; diff --git a/Pal/src/host/Linux/db_devices.c b/Pal/src/host/Linux/db_devices.c index 80ee3d35cd..bd40f1c529 100644 --- a/Pal/src/host/Linux/db_devices.c +++ b/Pal/src/host/Linux/db_devices.c @@ -36,7 +36,7 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int a if (!hdl) return -PAL_ERROR_NOMEM; - SET_HANDLE_TYPE(hdl, dev); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_DEV); if (!strcmp(uri, "tty")) { /* special case of "dev:tty" device which is the standard input + standard output */ @@ -85,7 +85,7 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int a } static int64_t dev_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* buffer) { - if (offset || !IS_HANDLE_TYPE(handle, dev)) + if (offset || HANDLE_HDR(handle)->type != PAL_TYPE_DEV) return -PAL_ERROR_INVAL; if (!(HANDLE_HDR(handle)->flags & RFD(0))) @@ -99,7 +99,7 @@ static int64_t dev_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* } static int64_t dev_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, const void* buffer) { - if (offset || !IS_HANDLE_TYPE(handle, dev)) + if (offset || HANDLE_HDR(handle)->type != PAL_TYPE_DEV) return -PAL_ERROR_INVAL; if (!(HANDLE_HDR(handle)->flags & WFD(0))) @@ -113,7 +113,7 @@ static int64_t dev_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, cons } static int dev_close(PAL_HANDLE handle) { - if (!IS_HANDLE_TYPE(handle, dev)) + if (HANDLE_HDR(handle)->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 */ @@ -126,7 +126,7 @@ static int dev_close(PAL_HANDLE handle) { } static int dev_flush(PAL_HANDLE handle) { - if (!IS_HANDLE_TYPE(handle, dev)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_DEV) return -PAL_ERROR_INVAL; if (handle->dev.fd != PAL_IDX_POISON) { @@ -164,13 +164,13 @@ static int dev_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* att attr->pending_size = stat_buf.st_size; } - attr->handle_type = pal_type_dev; + attr->handle_type = PAL_TYPE_DEV; attr->nonblocking = PAL_FALSE; return 0; } static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { - if (!IS_HANDLE_TYPE(handle, dev)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_DEV) return -PAL_ERROR_INVAL; if (handle->dev.fd == 0 || handle->dev.fd == 1) { @@ -194,7 +194,7 @@ static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { attr->pending_size = stat_buf.st_size; } - attr->handle_type = pal_type_dev; + attr->handle_type = PAL_TYPE_DEV; attr->nonblocking = handle->dev.nonblocking; return 0; } diff --git a/Pal/src/host/Linux/db_eventfd.c b/Pal/src/host/Linux/db_eventfd.c index d4e8282d0c..460d36bb92 100644 --- a/Pal/src/host/Linux/db_eventfd.c +++ b/Pal/src/host/Linux/db_eventfd.c @@ -59,7 +59,7 @@ static int eventfd_pal_open(PAL_HANDLE* handle, const char* type, const char* ur DO_SYSCALL(close, fd); return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, eventfd); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_EVENTFD); /* Note: using index 0, given that there is only 1 eventfd FD per pal-handle. */ HANDLE_HDR(hdl)->flags = RFD(0) | WFD(0); @@ -75,7 +75,7 @@ static int64_t eventfd_pal_read(PAL_HANDLE handle, uint64_t offset, uint64_t len if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, eventfd)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_EVENTFD) return -PAL_ERROR_NOTCONNECTION; if (len < sizeof(uint64_t)) @@ -94,7 +94,7 @@ static int64_t eventfd_pal_write(PAL_HANDLE handle, uint64_t offset, uint64_t le if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, eventfd)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_EVENTFD) return -PAL_ERROR_NOTCONNECTION; if (len < sizeof(uint64_t)) @@ -145,7 +145,7 @@ static int eventfd_pal_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) } static int eventfd_pal_close(PAL_HANDLE handle) { - if (IS_HANDLE_TYPE(handle, eventfd)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_EVENTFD) { if (handle->eventfd.fd != PAL_IDX_POISON) { DO_SYSCALL(close, handle->eventfd.fd); handle->eventfd.fd = PAL_IDX_POISON; diff --git a/Pal/src/host/Linux/db_events.c b/Pal/src/host/Linux/db_events.c index 77dc6b25d3..fe887008a4 100644 --- a/Pal/src/host/Linux/db_events.c +++ b/Pal/src/host/Linux/db_events.c @@ -23,7 +23,7 @@ int _DkEventCreate(PAL_HANDLE* handle_ptr, bool init_signaled, bool auto_clear) return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(handle, event); + init_handle_hdr(HANDLE_HDR(handle), PAL_TYPE_EVENT); handle->event.auto_clear = auto_clear; __atomic_store_n(&handle->event.signaled, init_signaled ? 1 : 0, __ATOMIC_RELEASE); diff --git a/Pal/src/host/Linux/db_files.c b/Pal/src/host/Linux/db_files.c index 85fb49401a..dec35abb24 100644 --- a/Pal/src/host/Linux/db_files.c +++ b/Pal/src/host/Linux/db_files.c @@ -50,7 +50,7 @@ static int file_open(PAL_HANDLE* handle, const char* type, const char* uri, int return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, file); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_FILE); HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0); hdl->file.fd = ret; hdl->file.map_start = NULL; @@ -189,15 +189,15 @@ static int file_flush(PAL_HANDLE handle) { static inline int file_stat_type(struct stat* stat) { if (S_ISREG(stat->st_mode)) - return pal_type_file; + return PAL_TYPE_FILE; if (S_ISDIR(stat->st_mode)) - return pal_type_dir; + return PAL_TYPE_DIR; if (S_ISCHR(stat->st_mode)) - return pal_type_dev; + return PAL_TYPE_DEV; if (S_ISFIFO(stat->st_mode)) - return pal_type_pipe; + return PAL_TYPE_PIPE; if (S_ISSOCK(stat->st_mode)) - return pal_type_dev; + return PAL_TYPE_DEV; return 0; } @@ -345,7 +345,7 @@ static int dir_open(PAL_HANDLE* handle, const char* type, const char* uri, int a DO_SYSCALL(close, fd); return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, dir); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_DIR); HANDLE_HDR(hdl)->flags |= RFD(0); hdl->dir.fd = fd; char* path = (void*)hdl + HANDLE_SIZE(dir); diff --git a/Pal/src/host/Linux/db_main.c b/Pal/src/host/Linux/db_main.c index 3e3289b1c1..c9d81d7342 100644 --- a/Pal/src/host/Linux/db_main.c +++ b/Pal/src/host/Linux/db_main.c @@ -208,7 +208,7 @@ noreturn void pal_linux_main(void* initial_rsp, void* fini_callback) { PAL_HANDLE first_thread = malloc(HANDLE_SIZE(thread)); if (!first_thread) INIT_FAIL(PAL_ERROR_NOMEM, "Out of memory"); - SET_HANDLE_TYPE(first_thread, thread); + init_handle_hdr(HANDLE_HDR(first_thread), PAL_TYPE_THREAD); first_thread->thread.tid = DO_SYSCALL(gettid); void* alt_stack = calloc(1, ALT_STACK_SIZE); diff --git a/Pal/src/host/Linux/db_pipes.c b/Pal/src/host/Linux/db_pipes.c index 86b72ee9f9..f6f26bc3bf 100644 --- a/Pal/src/host/Linux/db_pipes.c +++ b/Pal/src/host/Linux/db_pipes.c @@ -70,7 +70,7 @@ static int pipe_listen(PAL_HANDLE* handle, const char* name, int options) { return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, pipesrv); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_PIPESRV); HANDLE_HDR(hdl)->flags |= RFD(0); /* cannot write to a listening socket */ hdl->pipe.fd = fd; hdl->pipe.nonblocking = options & PAL_OPTION_NONBLOCK ? PAL_TRUE : PAL_FALSE; @@ -97,7 +97,7 @@ static int pipe_listen(PAL_HANDLE* handle, const char* name, int options) { * \return 0 on success, negative PAL error code otherwise. */ static int pipe_waitforclient(PAL_HANDLE handle, PAL_HANDLE* client) { - if (!IS_HANDLE_TYPE(handle, pipesrv)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPESRV) return -PAL_ERROR_NOTSERVER; if (handle->pipe.fd == PAL_IDX_POISON) @@ -113,7 +113,7 @@ static int pipe_waitforclient(PAL_HANDLE handle, PAL_HANDLE* client) { return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(clnt, pipecli); + init_handle_hdr(HANDLE_HDR(clnt), PAL_TYPE_PIPECLI); HANDLE_HDR(clnt)->flags |= RFD(0) | WFD(0); clnt->pipe.fd = newfd; clnt->pipe.name = handle->pipe.name; @@ -162,7 +162,7 @@ static int pipe_connect(PAL_HANDLE* handle, const char* name, int options) { return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, pipe); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_PIPE); HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0); hdl->pipe.fd = fd; hdl->pipe.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE; @@ -202,7 +202,7 @@ static int pipe_private(PAL_HANDLE* handle, int options) { return -PAL_ERROR_NOMEM; } - SET_HANDLE_TYPE(hdl, pipeprv); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_PIPEPRV); HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(1); /* first FD for reads, second FD for writes */ hdl->pipeprv.fds[0] = fds[0]; hdl->pipeprv.fds[1] = fds[1]; @@ -270,11 +270,11 @@ static int64_t pipe_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) && - !IS_HANDLE_TYPE(handle, pipe)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPECLI && HANDLE_HDR(handle)->type != PAL_TYPE_PIPEPRV && + HANDLE_HDR(handle)->type != PAL_TYPE_PIPE) return -PAL_ERROR_NOTCONNECTION; - int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] : handle->pipe.fd; + int fd = HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV ? handle->pipeprv.fds[0] : handle->pipe.fd; ssize_t bytes = DO_SYSCALL(read, fd, buffer, len); if (bytes < 0) @@ -296,11 +296,11 @@ static int64_t pipe_write(PAL_HANDLE handle, uint64_t offset, size_t len, const if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) && - !IS_HANDLE_TYPE(handle, pipe)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPECLI && HANDLE_HDR(handle)->type != PAL_TYPE_PIPEPRV && + HANDLE_HDR(handle)->type != PAL_TYPE_PIPE) return -PAL_ERROR_NOTCONNECTION; - int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] : handle->pipe.fd; + int fd = HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV ? handle->pipeprv.fds[1] : handle->pipe.fd; ssize_t bytes = DO_SYSCALL(write, fd, buffer, len); if (bytes < 0) @@ -316,7 +316,7 @@ static int64_t pipe_write(PAL_HANDLE handle, uint64_t offset, size_t len, const * \return 0 on success, negative PAL error code otherwise. */ static int pipe_close(PAL_HANDLE handle) { - if (IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) { if (handle->pipeprv.fds[0] != PAL_IDX_POISON) { DO_SYSCALL(close, handle->pipeprv.fds[0]); handle->pipeprv.fds[0] = PAL_IDX_POISON; @@ -356,7 +356,7 @@ static int pipe_delete(PAL_HANDLE handle, int access) { return -PAL_ERROR_INVAL; } - if (IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) { /* pipeprv has two underlying FDs, shut down the requested one(s) */ if (handle->pipeprv.fds[0] != PAL_IDX_POISON && (shutdown == SHUT_RD || shutdown == SHUT_RDWR)) { @@ -391,13 +391,13 @@ static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { return -PAL_ERROR_BADHANDLE; attr->handle_type = HANDLE_HDR(handle)->type; - attr->nonblocking = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.nonblocking - : handle->pipe.nonblocking; + attr->nonblocking = HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV ? handle->pipeprv.nonblocking + : handle->pipe.nonblocking; attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0); /* get number of bytes available for reading (doesn't make sense for "listening" pipes) */ attr->pending_size = 0; - if (!IS_HANDLE_TYPE(handle, pipesrv)) { + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPESRV) { int val; ret = DO_SYSCALL(ioctl, handle->pipe.fd, FIONREAD, &val); if (ret < 0) @@ -407,7 +407,7 @@ static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { } /* query if there is data available for reading/writing */ - if (IS_HANDLE_TYPE(handle, pipeprv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) { /* for private pipe, readable and writable are queried on different fds */ struct pollfd pfd[2] = {{.fd = handle->pipeprv.fds[0], .events = POLLIN, .revents = 0}, {.fd = handle->pipeprv.fds[1], .events = POLLOUT, .revents = 0}}; @@ -421,7 +421,7 @@ static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { } else { /* for non-private pipes, both readable and writable are queried on the same fd */ short pfd_events = POLLIN; - if (!IS_HANDLE_TYPE(handle, pipesrv)) { + if (HANDLE_HDR(handle)->type != PAL_TYPE_PIPESRV) { /* querying for writing doesn't make sense for "listening" pipes */ pfd_events |= POLLOUT; } @@ -452,7 +452,7 @@ static int pipe_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { if (handle->generic.fds[0] == PAL_IDX_POISON) return -PAL_ERROR_BADHANDLE; - PAL_BOL* nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv) + PAL_BOL* nonblocking = (HANDLE_HDR(handle)->type == PAL_TYPE_PIPEPRV) ? &handle->pipeprv.nonblocking : &handle->pipe.nonblocking; @@ -486,16 +486,16 @@ static int pipe_getname(PAL_HANDLE handle, char* buffer, size_t count) { size_t prefix_len = 0; switch (PAL_GET_TYPE(handle)) { - case pal_type_pipesrv: - case pal_type_pipecli: + case PAL_TYPE_PIPESRV: + case PAL_TYPE_PIPECLI: prefix_len = static_strlen(URI_TYPE_PIPE_SRV); prefix = URI_TYPE_PIPE_SRV; break; - case pal_type_pipe: + case PAL_TYPE_PIPE: prefix_len = static_strlen(URI_TYPE_PIPE); prefix = URI_TYPE_PIPE; break; - case pal_type_pipeprv: + case PAL_TYPE_PIPEPRV: default: return -PAL_ERROR_INVAL; } diff --git a/Pal/src/host/Linux/db_process.c b/Pal/src/host/Linux/db_process.c index d45beefd3a..68bdde7701 100644 --- a/Pal/src/host/Linux/db_process.c +++ b/Pal/src/host/Linux/db_process.c @@ -57,7 +57,7 @@ static inline int create_process_handle(PAL_HANDLE* parent, PAL_HANDLE* child) { goto out; } - SET_HANDLE_TYPE(phdl, process); + init_handle_hdr(HANDLE_HDR(phdl), PAL_TYPE_PROCESS); HANDLE_HDR(phdl)->flags |= RFD(0) | WFD(0); phdl->process.stream = fds[0]; phdl->process.nonblocking = PAL_FALSE; @@ -68,7 +68,7 @@ static inline int create_process_handle(PAL_HANDLE* parent, PAL_HANDLE* child) { goto out; } - SET_HANDLE_TYPE(chdl, process); + init_handle_hdr(HANDLE_HDR(chdl), PAL_TYPE_PROCESS); HANDLE_HDR(chdl)->flags |= RFD(0) | WFD(0); chdl->process.stream = fds[1]; chdl->process.nonblocking = PAL_FALSE; diff --git a/Pal/src/host/Linux/db_sockets.c b/Pal/src/host/Linux/db_sockets.c index edf50203d7..4b00876b08 100644 --- a/Pal/src/host/Linux/db_sockets.c +++ b/Pal/src/host/Linux/db_sockets.c @@ -231,7 +231,7 @@ static inline PAL_HANDLE socket_create_handle(int type, int fd, int options, memset(hdl, 0, sizeof(struct pal_handle)); init_handle_hdr(HANDLE_HDR(hdl), type); - HANDLE_HDR(hdl)->flags |= RFD(0) | (type != pal_type_tcpsrv ? WFD(0) : 0); + HANDLE_HDR(hdl)->flags |= RFD(0) | (type != PAL_TYPE_TCPSRV ? WFD(0) : 0); hdl->sock.fd = fd; void* addr = (void*)hdl + HANDLE_SIZE(sock); if (bind_addr) { @@ -252,7 +252,7 @@ static inline PAL_HANDLE socket_create_handle(int type, int fd, int options, hdl->sock.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE; hdl->sock.linger = 0; - if (type == pal_type_tcpsrv) { + if (type == PAL_TYPE_TCPSRV) { hdl->sock.receivebuf = 0; hdl->sock.sendbuf = 0; } else { @@ -339,7 +339,7 @@ static int tcp_listen(PAL_HANDLE* handle, char* uri, int create, int options) { goto failed; } - *handle = socket_create_handle(pal_type_tcpsrv, fd, options, bind_addr, bind_addrlen, NULL, 0); + *handle = socket_create_handle(PAL_TYPE_TCPSRV, fd, options, bind_addr, bind_addrlen, NULL, 0); if (!*handle) { ret = -PAL_ERROR_NOMEM; goto failed; @@ -354,7 +354,7 @@ static int tcp_listen(PAL_HANDLE* handle, char* uri, int create, int options) { /* accept a tcp connection */ static int tcp_accept(PAL_HANDLE handle, PAL_HANDLE* client) { - if (!IS_HANDLE_TYPE(handle, tcpsrv) || !handle->sock.bind || handle->sock.conn) + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCPSRV || !handle->sock.bind || handle->sock.conn) return -PAL_ERROR_NOTSERVER; if (handle->sock.fd == PAL_IDX_POISON) @@ -381,7 +381,7 @@ static int tcp_accept(PAL_HANDLE handle, PAL_HANDLE* client) { struct sockaddr* dest_addr = (struct sockaddr*)&buffer; size_t dest_addrlen = addrlen; - *client = socket_create_handle(pal_type_tcp, newfd, 0, bind_addr, bind_addrlen, dest_addr, + *client = socket_create_handle(PAL_TYPE_TCP, newfd, 0, bind_addr, bind_addrlen, dest_addr, dest_addrlen); if (!(*client)) { @@ -460,7 +460,7 @@ static int tcp_connect(PAL_HANDLE* handle, char* uri, int options) { bind_addr = NULL; } - *handle = socket_create_handle(pal_type_tcp, fd, options, bind_addr, bind_addrlen, dest_addr, + *handle = socket_create_handle(PAL_TYPE_TCP, fd, options, bind_addr, bind_addrlen, dest_addr, dest_addrlen); if (!(*handle)) { @@ -508,7 +508,7 @@ static int64_t tcp_read(PAL_HANDLE handle, uint64_t offset, size_t len, void* bu if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, tcp) || !handle->sock.conn) + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCP || !handle->sock.conn) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -539,7 +539,7 @@ static int64_t tcp_write(PAL_HANDLE handle, uint64_t offset, size_t len, const v if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, tcp) || !handle->sock.conn) + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCP || !handle->sock.conn) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -610,7 +610,7 @@ static int udp_bind(PAL_HANDLE* handle, char* uri, int create, int options) { } } - *handle = socket_create_handle(pal_type_udpsrv, fd, options, bind_addr, bind_addrlen, NULL, 0); + *handle = socket_create_handle(PAL_TYPE_UDPSRV, fd, options, bind_addr, bind_addrlen, NULL, 0); if (!(*handle)) { ret = -ENOMEM; @@ -670,7 +670,7 @@ static int udp_connect(PAL_HANDLE* handle, char* uri, int create, int options) { } } - *handle = socket_create_handle(dest_addr ? pal_type_udp : pal_type_udpsrv, fd, options, + *handle = socket_create_handle(dest_addr ? PAL_TYPE_UDP : PAL_TYPE_UDPSRV, fd, options, bind_addr, bind_addrlen, dest_addr, dest_addrlen); if (!(*handle)) { @@ -716,7 +716,7 @@ static int64_t udp_receive(PAL_HANDLE handle, uint64_t offset, size_t len, void* if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, udp)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_UDP) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -747,7 +747,7 @@ static int64_t udp_receivebyaddr(PAL_HANDLE handle, uint64_t offset, size_t len, if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, udpsrv)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_UDPSRV) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -788,7 +788,7 @@ static int64_t udp_send(PAL_HANDLE handle, uint64_t offset, size_t len, const vo if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, udp)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_UDP) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -818,7 +818,7 @@ static int64_t udp_sendbyaddr(PAL_HANDLE handle, uint64_t offset, size_t len, co if (offset) return -PAL_ERROR_INVAL; - if (!IS_HANDLE_TYPE(handle, udpsrv)) + if (HANDLE_HDR(handle)->type != PAL_TYPE_UDPSRV) return -PAL_ERROR_NOTCONNECTION; if (handle->sock.fd == PAL_IDX_POISON) @@ -863,10 +863,10 @@ static int socket_delete(PAL_HANDLE handle, int access) { if (handle->sock.fd == PAL_IDX_POISON) return 0; - if (!IS_HANDLE_TYPE(handle, tcp) && access) + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCP && access) return -PAL_ERROR_INVAL; - if (IS_HANDLE_TYPE(handle, tcp) || IS_HANDLE_TYPE(handle, tcpsrv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_TCP || HANDLE_HDR(handle)->type == PAL_TYPE_TCPSRV) { int shutdown; switch (access) { case 0: @@ -929,7 +929,7 @@ static int socket_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { /* get number of bytes available for reading (doesn't make sense for listening sockets) */ attr->pending_size = 0; - if (!IS_HANDLE_TYPE(handle, tcpsrv)) { + if (HANDLE_HDR(handle)->type != PAL_TYPE_TCPSRV) { int val; ret = DO_SYSCALL(ioctl, handle->sock.fd, FIONREAD, &val); if (ret < 0) @@ -965,7 +965,7 @@ static int socket_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { handle->sock.nonblocking = attr->nonblocking; } - if (IS_HANDLE_TYPE(handle, tcpsrv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_TCPSRV) { if (attr->socket.linger != handle->sock.linger) { struct __kernel_linger l; l.l_onoff = attr->socket.linger ? 1 : 0; @@ -1020,7 +1020,7 @@ static int socket_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) { } } - if (IS_HANDLE_TYPE(handle, tcp) || IS_HANDLE_TYPE(handle, tcpsrv)) { + if (HANDLE_HDR(handle)->type == PAL_TYPE_TCP || HANDLE_HDR(handle)->type == PAL_TYPE_TCPSRV) { if (attr->socket.tcp_cork != handle->sock.tcp_cork) { val = attr->socket.tcp_cork ? 1 : 0; ret = DO_SYSCALL(setsockopt, fd, SOL_TCP, TCP_CORK, &val, sizeof(int)); @@ -1065,23 +1065,23 @@ static int socket_getname(PAL_HANDLE handle, char* buffer, size_t count) { struct sockaddr* dest_addr = NULL; switch (PAL_GET_TYPE(handle)) { - case pal_type_tcpsrv: + case PAL_TYPE_TCPSRV: prefix_len = static_strlen(URI_PREFIX_TCP_SRV); prefix = URI_PREFIX_TCP_SRV; bind_addr = (struct sockaddr*)handle->sock.bind; break; - case pal_type_tcp: + case PAL_TYPE_TCP: prefix_len = static_strlen(URI_PREFIX_TCP); prefix = URI_PREFIX_TCP; bind_addr = (struct sockaddr*)handle->sock.bind; dest_addr = (struct sockaddr*)handle->sock.conn; break; - case pal_type_udpsrv: + case PAL_TYPE_UDPSRV: prefix_len = static_strlen(URI_PREFIX_UDP_SRV); prefix = URI_PREFIX_UDP_SRV; bind_addr = (struct sockaddr*)handle->sock.bind; break; - case pal_type_udp: + case PAL_TYPE_UDP: prefix_len = static_strlen(URI_PREFIX_UDP); prefix = URI_PREFIX_UDP; bind_addr = (struct sockaddr*)handle->sock.bind; diff --git a/Pal/src/host/Linux/db_streams.c b/Pal/src/host/Linux/db_streams.c index a14eb6f4e8..524c03f3da 100644 --- a/Pal/src/host/Linux/db_streams.c +++ b/Pal/src/host/Linux/db_streams.c @@ -100,29 +100,29 @@ int handle_serialize(PAL_HANDLE handle, void** data) { /* find fields to serialize (depends on the handle type) and assign them to d1/d2; note that * no handle type has more than two such fields, and some have none at all */ switch (PAL_GET_TYPE(handle)) { - case pal_type_file: + case PAL_TYPE_FILE: d1 = handle->file.realpath; dsz1 = strlen(handle->file.realpath) + 1; break; - case pal_type_pipe: - case pal_type_pipesrv: - case pal_type_pipecli: - case pal_type_pipeprv: + case PAL_TYPE_PIPE: + case PAL_TYPE_PIPESRV: + case PAL_TYPE_PIPECLI: + case PAL_TYPE_PIPEPRV: /* pipes have no fields to serialize */ break; - case pal_type_dev: + case PAL_TYPE_DEV: /* devices have no fields to serialize */ break; - case pal_type_dir: + case PAL_TYPE_DIR: if (handle->dir.realpath) { d1 = handle->dir.realpath; dsz1 = strlen(handle->dir.realpath) + 1; } break; - case pal_type_tcp: - case pal_type_tcpsrv: - case pal_type_udp: - case pal_type_udpsrv: + case PAL_TYPE_TCP: + case PAL_TYPE_TCPSRV: + case PAL_TYPE_UDP: + case PAL_TYPE_UDPSRV: if (handle->sock.bind) { d1 = (const void*)handle->sock.bind; dsz1 = addr_size(handle->sock.bind); @@ -132,8 +132,8 @@ int handle_serialize(PAL_HANDLE handle, void** data) { dsz2 = addr_size(handle->sock.conn); } break; - case pal_type_process: - case pal_type_eventfd: + case PAL_TYPE_PROCESS: + case PAL_TYPE_EVENTFD: break; default: return -PAL_ERROR_INVAL; @@ -165,23 +165,23 @@ int handle_deserialize(PAL_HANDLE* handle, const void* data, size_t size) { /* update handle fields to point to correct contents (located right after handle itself) */ switch (PAL_GET_TYPE(hdl)) { - case pal_type_file: + case PAL_TYPE_FILE: hdl->file.realpath = hdl->file.realpath ? (PAL_STR)hdl + hdlsz : NULL; break; - case pal_type_pipe: - case pal_type_pipesrv: - case pal_type_pipecli: - case pal_type_pipeprv: + case PAL_TYPE_PIPE: + case PAL_TYPE_PIPESRV: + case PAL_TYPE_PIPECLI: + case PAL_TYPE_PIPEPRV: break; - case pal_type_dev: + case PAL_TYPE_DEV: break; - case pal_type_dir: + case PAL_TYPE_DIR: hdl->dir.realpath = hdl->dir.realpath ? (PAL_STR)hdl + hdlsz : NULL; break; - case pal_type_tcp: - case pal_type_tcpsrv: - case pal_type_udp: - case pal_type_udpsrv: { + case PAL_TYPE_TCP: + case PAL_TYPE_TCPSRV: + case PAL_TYPE_UDP: + case PAL_TYPE_UDPSRV: { size_t s1 = hdl->sock.bind ? addr_size((PAL_PTR)hdl + hdlsz) : 0; size_t s2 = hdl->sock.conn ? addr_size((PAL_PTR)hdl + hdlsz + s1) : 0; if (s1) @@ -190,8 +190,8 @@ int handle_deserialize(PAL_HANDLE* handle, const void* data, size_t size) { hdl->sock.conn = (PAL_PTR)hdl + hdlsz + s2; break; } - case pal_type_process: - case pal_type_eventfd: + case PAL_TYPE_PROCESS: + case PAL_TYPE_EVENTFD: break; default: return -PAL_ERROR_BADHANDLE; @@ -209,7 +209,7 @@ int handle_deserialize(PAL_HANDLE* handle, const void* data, size_t size) { * \return 0 on success, negative PAL error code otherwise. */ int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) { - if (!IS_HANDLE_TYPE(hdl, process)) + if (HANDLE_HDR(hdl)->type != PAL_TYPE_PROCESS) return -PAL_ERROR_BADHANDLE; /* serialize cargo handle into a blob hdl_data */ @@ -283,7 +283,7 @@ int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) { * \return 0 on success, negative PAL error code otherwise. */ int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE* cargo) { - if (!IS_HANDLE_TYPE(hdl, process)) + if (HANDLE_HDR(hdl)->type != PAL_TYPE_PROCESS) return -PAL_ERROR_BADHANDLE; ssize_t ret; diff --git a/Pal/src/host/Linux/db_threading.c b/Pal/src/host/Linux/db_threading.c index 1af7ca7d59..b7732c0845 100644 --- a/Pal/src/host/Linux/db_threading.c +++ b/Pal/src/host/Linux/db_threading.c @@ -156,7 +156,7 @@ int _DkThreadCreate(PAL_HANDLE* handle, int (*callback)(void*), const void* para ret = -PAL_ERROR_NOMEM; goto err; } - SET_HANDLE_TYPE(hdl, thread); + init_handle_hdr(HANDLE_HDR(hdl), PAL_TYPE_THREAD); // Initialize TCB at the top of the alternative stack. PAL_TCB_LINUX* tcb = child_stack + ALT_STACK_SIZE - sizeof(PAL_TCB_LINUX);