Skip to content

Commit

Permalink
Convert PalStreamOpen to return pal_error_t
Browse files Browse the repository at this point in the history
Signed-off-by: Mariusz Zaborski <oshogbo@invisiblethingslab.com>
  • Loading branch information
oshogbo committed May 16, 2024
1 parent d7a0b9c commit f4477e3
Show file tree
Hide file tree
Showing 40 changed files with 346 additions and 301 deletions.
4 changes: 2 additions & 2 deletions Documentation/Doxyfile-pal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@INCLUDE = Doxyfile
PROJECT_NAME = "PAL"
OUTPUT_DIRECTORY = _build/doxygen-pal
INPUT = ../pal/include/arch/x86_64 ../pal/include/pal ../pal/include ../common/include/iovec.h
INPUT = ../pal/include/arch/x86_64 ../pal/include/pal ../pal/include ../common/include/pal_error.h ../common/include/iovec.h
MACRO_EXPANSION = YES
EXPAND_ONLY_PREDEF = YES
PREDEFINED = noreturn= __x86_64__=1
PREDEFINED = noreturn= __x86_64__=1 NODISCARD=
11 changes: 11 additions & 0 deletions Documentation/pal/host-abi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ Data types and variables
Data types
^^^^^^^^^^

Return type
"""""""""""

All new PAL functions should return an error of :type:`pal_error_t`.

.. doxygentypedef:: pal_error_t
:project: pal

.. doxygenenum:: _pal_error_t
:project: pal

PAL handles
"""""""""""

Expand Down
27 changes: 15 additions & 12 deletions libos/src/fs/chroot/encrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,11 @@ static int chroot_encrypted_mkdir(struct libos_dentry* dent, mode_t perm) {

/* This opens a "dir:..." URI */
PAL_HANDLE palhdl;
ret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, HOST_PERM(perm), PAL_CREATE_ALWAYS,
PAL_OPTION_PASSTHROUGH, &palhdl);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
pal_error_t pret;
pret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, HOST_PERM(perm), PAL_CREATE_ALWAYS,
PAL_OPTION_PASSTHROUGH, &palhdl);
if (pret != PAL_ERROR_SUCCESS) {
ret = -pal_to_unix_errno(pret);
goto out;
}
PalObjectDestroy(palhdl);
Expand Down Expand Up @@ -304,10 +305,11 @@ static int chroot_encrypted_unlink(struct libos_dentry* dent) {
return ret;

PAL_HANDLE palhdl;
ret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
PAL_OPTION_PASSTHROUGH, &palhdl);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
pal_error_t pret;
pret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
PAL_OPTION_PASSTHROUGH, &palhdl);
if (pret != PAL_ERROR_SUCCESS) {
ret = -pal_to_unix_errno(pret);
goto out;
}

Expand Down Expand Up @@ -369,10 +371,11 @@ static int chroot_encrypted_chmod(struct libos_dentry* dent, mode_t perm) {
goto out;

PAL_HANDLE palhdl;
ret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
PAL_OPTION_PASSTHROUGH, &palhdl);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
pal_error_t pret;
pret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
PAL_OPTION_PASSTHROUGH, &palhdl);
if (pret != PAL_ERROR_SUCCESS) {
ret = -pal_to_unix_errno(pret);
goto out;
}
mode_t host_perm = HOST_PERM(perm);
Expand Down
18 changes: 11 additions & 7 deletions libos/src/fs/chroot/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ static int chroot_lookup(struct libos_dentry* dent) {
/* Open a temporary read-only PAL handle for a file (used by `unlink` etc.) */
static int chroot_temp_open(struct libos_dentry* dent, PAL_HANDLE* out_palhdl) {
char* uri;
pal_error_t pret;

int ret = dentry_uri(dent, dent->inode->type, &uri);
if (ret < 0)
return ret;

ret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
/*options=*/0, out_palhdl);
pret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
/*options=*/0, out_palhdl);
free(uri);
return pal_to_unix_errno(ret);
return -pal_to_unix_errno(pret);
}

/* Open a PAL handle, and associate it with a LibOS handle (if provided). */
Expand All @@ -130,8 +132,9 @@ static int chroot_do_open(struct libos_handle* hdl, struct libos_dentry* dent, m
assert(locked(&g_dcache_lock));

int ret;

pal_error_t pret;
char* uri;

ret = dentry_uri(dent, type, &uri);
if (ret < 0)
return ret;
Expand All @@ -141,9 +144,10 @@ static int chroot_do_open(struct libos_handle* hdl, struct libos_dentry* dent, m
enum pal_create_mode create = LINUX_OPEN_FLAGS_TO_PAL_CREATE(flags);
pal_stream_options_t options = LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(flags);
mode_t host_perm = HOST_PERM(perm);
ret = PalStreamOpen(uri, access, host_perm, create, options, &palhdl);
if (ret < 0) {
ret = pal_to_unix_errno(ret);

pret = PalStreamOpen(uri, access, host_perm, create, options, &palhdl);
if (pret != PAL_ERROR_SUCCESS) {
ret = -pal_to_unix_errno(pret);
goto out;
}

Expand Down
9 changes: 5 additions & 4 deletions libos/src/fs/dev/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ static int dev_tty_open(struct libos_handle* hdl, struct libos_dentry* dent, int
return -ENOMEM;

PAL_HANDLE palhdl;
int ret = PalStreamOpen(uri, LINUX_OPEN_FLAGS_TO_PAL_ACCESS(flags), PSEUDO_PERM_FILE_RW,
PAL_CREATE_NEVER, /*options=*/0, &palhdl);
if (ret < 0) {
pal_error_t pret = PalStreamOpen(uri, LINUX_OPEN_FLAGS_TO_PAL_ACCESS(flags),
PSEUDO_PERM_FILE_RW, PAL_CREATE_NEVER, /*options=*/0,
&palhdl);
if (pret != PAL_ERROR_SUCCESS) {
free(uri);
return pal_to_unix_errno(ret);
return -pal_to_unix_errno(pret);
}

assert(hdl);
Expand Down
10 changes: 5 additions & 5 deletions libos/src/fs/libos_fs_encrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA

if (!pal_handle) {
enum pal_create_mode create_mode = create ? PAL_CREATE_ALWAYS : PAL_CREATE_NEVER;
ret = PalStreamOpen(enc->uri, PAL_ACCESS_RDWR, share_flags, create_mode,
PAL_OPTION_PASSTHROUGH, &pal_handle);
if (ret < 0) {
log_warning("PalStreamOpen failed: %s", pal_strerror(ret));
return pal_to_unix_errno(ret);
pal_error_t pret = PalStreamOpen(enc->uri, PAL_ACCESS_RDWR, share_flags, create_mode,
PAL_OPTION_PASSTHROUGH, &pal_handle);
if (pret != PAL_ERROR_SUCCESS) {
log_warning("PalStreamOpen failed: %s", pal_strerror(pret));
return -pal_to_unix_errno(pret);
}
}

Expand Down
7 changes: 4 additions & 3 deletions libos/src/fs/shm/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static int shm_do_open(struct libos_handle* hdl, struct libos_dentry* dent, mode
assert(locked(&g_dcache_lock));

char* uri;
pal_error_t pret;
int ret = dentry_uri(dent, type, &uri);
if (ret < 0)
return ret;
Expand All @@ -66,9 +67,9 @@ static int shm_do_open(struct libos_handle* hdl, struct libos_dentry* dent, mode
enum pal_create_mode create = LINUX_OPEN_FLAGS_TO_PAL_CREATE(flags);
pal_stream_options_t options = LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(flags);
mode_t host_perm = HOST_PERM(perm);
ret = PalStreamOpen(uri, access, host_perm, create, options, &palhdl);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
pret = PalStreamOpen(uri, access, host_perm, create, options, &palhdl);
if (pret != PAL_ERROR_SUCCESS) {
ret = -pal_to_unix_errno(pret);
goto out;
}

Expand Down
11 changes: 6 additions & 5 deletions libos/src/ipc/libos_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ static int vmid_to_uri(IDTYPE vmid, char* uri, size_t uri_size) {
static int ipc_connect(IDTYPE dest, struct libos_ipc_connection** conn_ptr) {
struct libos_ipc_connection dummy = { .vmid = dest };
int ret = 0;
pal_error_t pret;

lock(&g_ipc_connections_lock);
struct libos_ipc_connection* conn = node2conn(avl_tree_find(&g_ipc_connections, &dummy.node));
Expand All @@ -128,11 +129,11 @@ static int ipc_connect(IDTYPE dest, struct libos_ipc_connection** conn_ptr) {
BUG();
}
do {
ret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_IGNORED,
/*options=*/0, &conn->handle);
} while (ret == -PAL_ERROR_INTERRUPTED);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
pret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_IGNORED,
/*options=*/0, &conn->handle);
} while (pret == PAL_ERROR_INTERRUPTED);
if (pret != PAL_ERROR_SUCCESS) {
ret = -pal_to_unix_errno(pret);
goto out;
}
ret = write_exact(conn->handle, &g_process_ipc_ids.self_vmid,
Expand Down
11 changes: 6 additions & 5 deletions libos/src/libos_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ int create_pipe(char* name, char* uri, size_t size, PAL_HANDLE* hdl, bool use_vm
int ret;
size_t len;
char pipename[PIPE_URI_SIZE];
pal_error_t pret;
PAL_HANDLE pipe = NULL;

assert(hdl);
Expand All @@ -566,14 +567,14 @@ int create_pipe(char* name, char* uri, size_t size, PAL_HANDLE* hdl, bool use_vm
if (len >= size)
return -ERANGE;

ret = PalStreamOpen(uri, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED,
/*options=*/0, &pipe);
if (ret < 0) {
if (!use_vmid_for_name && ret == -PAL_ERROR_STREAMEXIST) {
pret = PalStreamOpen(uri, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED,
/*options=*/0, &pipe);
if (pret != PAL_ERROR_SUCCESS) {
if (!use_vmid_for_name && pret == PAL_ERROR_STREAMEXIST) {
/* tried to create a pipe with random name but it already exists */
continue;
}
return pal_to_unix_errno(ret);
return -pal_to_unix_errno(pret);
}

break; /* succeeded in creating the pipe with random/vmid name */
Expand Down
13 changes: 7 additions & 6 deletions libos/src/libos_pollable_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
int create_pollable_event(struct libos_pollable_event* event) {
char uri[PIPE_URI_SIZE];
PAL_HANDLE srv_handle;
pal_error_t pret;
int ret = create_pipe(/*name=*/NULL, uri, sizeof(uri), &srv_handle,
/*use_vmid_for_name=*/false);
if (ret < 0) {
Expand All @@ -22,12 +23,12 @@ int create_pollable_event(struct libos_pollable_event* event) {

PAL_HANDLE write_handle;
do {
ret = PalStreamOpen(uri, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED,
PAL_OPTION_NONBLOCK, &write_handle);
} while (ret == -PAL_ERROR_INTERRUPTED);
if (ret < 0) {
log_error("PalStreamOpen failed: %s", pal_strerror(ret));
ret = pal_to_unix_errno(ret);
pret = PalStreamOpen(uri, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED,
PAL_OPTION_NONBLOCK, &write_handle);
} while (pret == PAL_ERROR_INTERRUPTED);
if (pret != PAL_ERROR_SUCCESS) {
log_error("PalStreamOpen failed: %s", pal_strerror(pret));
ret = -pal_to_unix_errno(pret);
goto out;
}

Expand Down
18 changes: 10 additions & 8 deletions libos/src/net/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ static int bind(struct libos_handle* handle, void* addr, size_t addrlen) {
unlock(&handle->lock);

PAL_HANDLE pal_handle = NULL;
ret = PalStreamOpen(pipe_name, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED, options,
&pal_handle);
if (ret < 0) {
return (ret == -PAL_ERROR_STREAMEXIST) ? -EADDRINUSE : pal_to_unix_errno(ret);
pal_error_t pret;
pret = PalStreamOpen(pipe_name, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED, options,
&pal_handle);
if (pret != PAL_ERROR_SUCCESS) {
return (pret == PAL_ERROR_STREAMEXIST) ? -EADDRINUSE : -pal_to_unix_errno(pret);
}

__atomic_store_n(&handle->info.sock.pal_handle, pal_handle, __ATOMIC_RELEASE);
Expand Down Expand Up @@ -243,10 +244,11 @@ static int connect(struct libos_handle* handle, void* addr, size_t addrlen, bool
unlock(&handle->lock);

PAL_HANDLE pal_handle = NULL;
ret = PalStreamOpen(pipe_name, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED, options,
&pal_handle);
if (ret < 0) {
return (ret == -PAL_ERROR_CONNFAILED) ? -ENOENT : pal_to_unix_errno(ret);
pal_error_t pret;
pret = PalStreamOpen(pipe_name, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED,
options, &pal_handle);
if (pret != PAL_ERROR_SUCCESS) {
return (pret == PAL_ERROR_CONNFAILED) ? -ENOENT : -pal_to_unix_errno(pret);
}

assert(sock->pal_handle == NULL);
Expand Down
10 changes: 5 additions & 5 deletions libos/src/sys/libos_eventfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ int init_eventfd_mode(void) {
static int create_eventfd_pal_handle(uint64_t initial_count, int flags,
PAL_HANDLE* out_pal_handle) {
int ret;

pal_error_t pret;
PAL_HANDLE hdl = NULL;

int pal_flags = 0;
pal_flags |= flags & EFD_NONBLOCK ? PAL_OPTION_NONBLOCK : 0;
pal_flags |= flags & EFD_SEMAPHORE ? PAL_OPTION_EFD_SEMAPHORE : 0;

ret = PalStreamOpen(URI_PREFIX_EVENTFD, PAL_ACCESS_RDWR, /*share_flags=*/0,
PAL_CREATE_IGNORED, pal_flags, &hdl);
if (ret < 0) {
pret = PalStreamOpen(URI_PREFIX_EVENTFD, PAL_ACCESS_RDWR, /*share_flags=*/0,
PAL_CREATE_IGNORED, pal_flags, &hdl);
if (pret != PAL_ERROR_SUCCESS) {
log_error("eventfd: creation failure");
return pal_to_unix_errno(ret);
return -pal_to_unix_errno(pret);
}

/* set the initial count */
Expand Down
9 changes: 5 additions & 4 deletions libos/src/sys/libos_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

static int create_pipes(struct libos_handle* srv, struct libos_handle* cli, int flags, char* name) {
int ret = 0;
pal_error_t pret;
char uri[PIPE_URI_SIZE];

PAL_HANDLE hdl0 = NULL; /* server pipe (temporary, waits for connect from hdl2) */
Expand All @@ -34,10 +35,10 @@ static int create_pipes(struct libos_handle* srv, struct libos_handle* cli, int
return ret;
}

ret = PalStreamOpen(uri, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED,
LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(flags), &hdl2);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
pret = PalStreamOpen(uri, PAL_ACCESS_RDWR, /*share_flags=*/0, PAL_CREATE_IGNORED,
LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(flags), &hdl2);
if (pret != PAL_ERROR_SUCCESS) {
ret = -pal_to_unix_errno(pret);
log_error("pipe connection failure");
goto out;
}
Expand Down
7 changes: 4 additions & 3 deletions pal/include/pal/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ typedef uint32_t pal_stream_options_t; /* bitfield */
* \param handle[out] If the resource is successfully opened or created, a PAL handle is returned
* in `*handle` for further access such as reading or writing.
*
* \returns 0 on success, negative error code on failure.
* \returns PAL_ERROR_SUCCESS on success, error code on failure.
*
* Supported URI types:
* * `%file:...`, `dir:...`: Files or directories on the host file system. If #PAL_CREATE_TRY or
Expand All @@ -351,8 +351,9 @@ typedef uint32_t pal_stream_options_t; /* bitfield */
* processes. The server side of a pipe can accept any number of connections. If `pipe:` is given
* as the URI (i.e., without a name), it will open an anonymous bidirectional pipe.
*/
int PalStreamOpen(const char* typed_uri, enum pal_access access, pal_share_flags_t share_flags,
enum pal_create_mode create, pal_stream_options_t options, PAL_HANDLE* handle);
pal_error_t PalStreamOpen(const char* typed_uri, enum pal_access access,
pal_share_flags_t share_flags, enum pal_create_mode create,
pal_stream_options_t options, PAL_HANDLE* handle);

/*!
* \brief Block until a new connection is accepted and return the PAL handle for the connection.
Expand Down
11 changes: 6 additions & 5 deletions pal/include/pal_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ struct handle_ops {
/* 'open' is used by PalStreamOpen. 'handle' is a preallocated handle, 'type' will be a
* normalized prefix, 'uri' is the remaining string of uri. access, share, create, and options
* follow the same flags defined for PalStreamOpen in pal.h. */
int (*open)(PAL_HANDLE* handle, const char* type, const char* uri, enum pal_access access,
pal_share_flags_t share, enum pal_create_mode create, pal_stream_options_t options);
pal_error_t (*open)(PAL_HANDLE* handle, const char* type, const char* uri,
enum pal_access access, pal_share_flags_t share,
enum pal_create_mode create, pal_stream_options_t options);

/* 'read' and 'write' is used by PalStreamRead and PalStreamWrite, so they have exactly same
* prototype as them. */
Expand Down Expand Up @@ -167,9 +168,9 @@ int _PalDeviceIoControl(PAL_HANDLE handle, uint32_t cmd, unsigned long arg, int*
int _PalGetCPUInfo(struct pal_cpu_info* info);

/* PalStream calls */
int _PalStreamOpen(PAL_HANDLE* handle, const char* uri, enum pal_access access,
pal_share_flags_t share, enum pal_create_mode create,
pal_stream_options_t options);
pal_error_t _PalStreamOpen(PAL_HANDLE* handle, const char* uri, enum pal_access access,
pal_share_flags_t share, enum pal_create_mode create,
pal_stream_options_t options);
int _PalStreamDelete(PAL_HANDLE handle, enum pal_delete_mode delete_mode);
int64_t _PalStreamRead(PAL_HANDLE handle, uint64_t offset, uint64_t count, void* buf);
int64_t _PalStreamWrite(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buf);
Expand Down
Loading

0 comments on commit f4477e3

Please sign in to comment.