Skip to content

Commit

Permalink
[PAL] Remove redundant PAL APIs
Browse files Browse the repository at this point in the history
This commit removes:
- `source` and `size` arguments from `PalStreamRead`,
- `dest` argument from `PalStreamWrite`,
- `PalStreamGetName`.
It also removes PAL internal function `_PalStreamRealpath`.
All of the above were either redundant or not used at all.

Signed-off-by: Borys Popławski <borysp@invisiblethingslab.com>
  • Loading branch information
boryspoplawski committed Aug 22, 2022
1 parent 3d23397 commit f7995b6
Show file tree
Hide file tree
Showing 35 changed files with 85 additions and 458 deletions.
3 changes: 0 additions & 3 deletions Documentation/pal/host-abi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,6 @@ applications.
.. doxygenfunction:: PalStreamAttributesSetByHandle
:project: pal

.. doxygenfunction:: PalStreamGetName
:project: pal

.. doxygenfunction:: PalStreamChangeName
:project: pal

Expand Down
6 changes: 3 additions & 3 deletions libos/src/fs/chroot/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ static ssize_t chroot_read(struct libos_handle* hdl, void* buf, size_t count, fi
assert(hdl->type == TYPE_CHROOT);

size_t actual_count = count;
int ret = PalStreamRead(hdl->pal_handle, *pos, &actual_count, buf, /*source=*/NULL, /*size=*/0);
int ret = PalStreamRead(hdl->pal_handle, *pos, &actual_count, buf);
if (ret < 0) {
return pal_to_unix_errno(ret);
}
Expand All @@ -306,7 +306,7 @@ static ssize_t chroot_write(struct libos_handle* hdl, const void* buf, size_t co
assert(hdl->type == TYPE_CHROOT);

size_t actual_count = count;
int ret = PalStreamWrite(hdl->pal_handle, *pos, &actual_count, (void*)buf, /*dest=*/NULL);
int ret = PalStreamWrite(hdl->pal_handle, *pos, &actual_count, (void*)buf);
if (ret < 0) {
return pal_to_unix_errno(ret);
}
Expand Down Expand Up @@ -375,7 +375,7 @@ int chroot_readdir(struct libos_dentry* dent, readdir_callback_t callback, void*

while (true) {
size_t read_size = buf_size;
ret = PalStreamRead(palhdl, /*offset=*/0, &read_size, buf, /*source=*/NULL, /*size=*/0);
ret = PalStreamRead(palhdl, /*offset=*/0, &read_size, buf);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
goto out;
Expand Down
4 changes: 2 additions & 2 deletions libos/src/fs/eventfd/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static ssize_t eventfd_read(struct libos_handle* hdl, void* buf, size_t count, f
return -EINVAL;

size_t orig_count = count;
int ret = PalStreamRead(hdl->pal_handle, 0, &count, buf, NULL, 0);
int ret = PalStreamRead(hdl->pal_handle, 0, &count, buf);
ret = pal_to_unix_errno(ret);
maybe_epoll_et_trigger(hdl, ret, /*in=*/true, ret == 0 ? count < orig_count : false);
if (ret < 0) {
Expand All @@ -38,7 +38,7 @@ static ssize_t eventfd_write(struct libos_handle* hdl, const void* buf, size_t c
return -EINVAL;

size_t orig_count = count;
int ret = PalStreamWrite(hdl->pal_handle, 0, &count, (void*)buf, NULL);
int ret = PalStreamWrite(hdl->pal_handle, 0, &count, (void*)buf);
ret = pal_to_unix_errno(ret);
maybe_epoll_et_trigger(hdl, ret, /*in=*/false, ret == 0 ? count < orig_count : false);
if (ret < 0) {
Expand Down
5 changes: 2 additions & 3 deletions libos/src/fs/libos_fs_encrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ static pf_status_t cb_read(pf_handle_t handle, void* buffer, uint64_t offset, si

while (remaining > 0) {
size_t count = remaining;
int ret = PalStreamRead(pal_handle, offset + buffer_offset, &count, buffer + buffer_offset,
/*source=*/NULL, /*size=*/0);
int ret = PalStreamRead(pal_handle, offset + buffer_offset, &count, buffer + buffer_offset);
if (ret == -PAL_ERROR_INTERRUPTED)
continue;

Expand Down Expand Up @@ -58,7 +57,7 @@ static pf_status_t cb_write(pf_handle_t handle, const void* buffer, uint64_t off
while (remaining > 0) {
size_t count = remaining;
int ret = PalStreamWrite(pal_handle, offset + buffer_offset, &count,
(void*)(buffer + buffer_offset), /*dest=*/NULL);
(void*)(buffer + buffer_offset));
if (ret == -PAL_ERROR_INTERRUPTED)
continue;

Expand Down
4 changes: 2 additions & 2 deletions libos/src/fs/pipe/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static ssize_t pipe_read(struct libos_handle* hdl, void* buf, size_t count, file
return -EACCES;

size_t orig_count = count;
int ret = PalStreamRead(hdl->pal_handle, 0, &count, buf, NULL, 0);
int ret = PalStreamRead(hdl->pal_handle, 0, &count, buf);
ret = pal_to_unix_errno(ret);
maybe_epoll_et_trigger(hdl, ret, /*in=*/true, ret == 0 ? count < orig_count : false);
if (ret < 0) {
Expand All @@ -111,7 +111,7 @@ static ssize_t pipe_write(struct libos_handle* hdl, const void* buf, size_t coun
return -EACCES;

size_t orig_count = count;
int ret = PalStreamWrite(hdl->pal_handle, 0, &count, (void*)buf, NULL);
int ret = PalStreamWrite(hdl->pal_handle, 0, &count, (void*)buf);
ret = pal_to_unix_errno(ret);
maybe_epoll_et_trigger(hdl, ret, /*in=*/false, ret == 0 ? count < orig_count : false);
if (ret < 0) {
Expand Down
2 changes: 1 addition & 1 deletion libos/src/ipc/libos_ipc_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static int receive_ipc_messages(struct libos_ipc_connection* conn) {
/* Receive at least the message header. */
while (size < sizeof(buf.msg_header)) {
size_t tmp_size = sizeof(buf) - size;
int ret = PalStreamRead(conn->handle, /*offset=*/0, &tmp_size, buf.buf + size, NULL, 0);
int ret = PalStreamRead(conn->handle, /*offset=*/0, &tmp_size, buf.buf + size);
if (ret < 0) {
if (ret == -PAL_ERROR_INTERRUPTED || ret == -PAL_ERROR_TRYAGAIN) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions libos/src/libos_pollable_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int set_pollable_event(struct libos_pollable_event* event) {
do {
char c = 0;
size_t size = sizeof(c);
ret = PalStreamWrite(event->write_handle, /*offset=*/0, &size, &c, /*dest=*/NULL);
ret = PalStreamWrite(event->write_handle, /*offset=*/0, &size, &c);
ret = pal_to_unix_errno(ret);
if (ret == 0 && size == 0) {
ret = -EINVAL;
Expand All @@ -97,7 +97,7 @@ int clear_pollable_event(struct libos_pollable_event* event) {
do {
char buf[0x100];
size_t size = sizeof(buf);
int ret = PalStreamRead(event->read_handle, /*offset=*/0, &size, buf, NULL, 0);
int ret = PalStreamRead(event->read_handle, /*offset=*/0, &size, buf);
ret = pal_to_unix_errno(ret);
if (ret == 0 && size == 0) {
ret = -EINVAL;
Expand Down
4 changes: 2 additions & 2 deletions libos/src/libos_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int read_exact(PAL_HANDLE handle, void* buf, size_t size) {
size_t read = 0;
while (read < size) {
size_t tmp_read = size - read;
int ret = PalStreamRead(handle, /*offset=*/0, &tmp_read, (char*)buf + read, NULL, 0);
int ret = PalStreamRead(handle, /*offset=*/0, &tmp_read, (char*)buf + read);
if (ret < 0) {
if (ret == -PAL_ERROR_INTERRUPTED || ret == -PAL_ERROR_TRYAGAIN) {
continue;
Expand All @@ -29,7 +29,7 @@ int write_exact(PAL_HANDLE handle, void* buf, size_t size) {
size_t written = 0;
while (written < size) {
size_t tmp_written = size - written;
int ret = PalStreamWrite(handle, /*offset=*/0, &tmp_written, (char*)buf + written, NULL);
int ret = PalStreamWrite(handle, /*offset=*/0, &tmp_written, (char*)buf + written);
if (ret < 0) {
if (ret == -PAL_ERROR_INTERRUPTED || ret == -PAL_ERROR_TRYAGAIN) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions libos/src/net/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ static int send(struct libos_handle* handle, struct iovec* iov, size_t iov_len,
/* `size` is already correct. */
}

int ret = PalStreamWrite(pal_handle, /*offset=*/0, &size, buf, NULL);
int ret = PalStreamWrite(pal_handle, /*offset=*/0, &size, buf);
free(backing_buf);
if (ret < 0) {
return (ret == -PAL_ERROR_TOOLONG) ? -EMSGSIZE : pal_to_unix_errno(ret);
Expand Down Expand Up @@ -415,7 +415,7 @@ static int recv(struct libos_handle* handle, struct iovec* iov, size_t iov_len,
/* `size` is already correct. */
}

int ret = PalStreamRead(pal_handle, /*offset=*/0, &size, buf, NULL, 0);
int ret = PalStreamRead(pal_handle, /*offset=*/0, &size, buf);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
} else {
Expand Down
2 changes: 1 addition & 1 deletion libos/src/sys/libos_eventfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static int create_eventfd(PAL_HANDLE* efd, uint64_t initial_count, int flags) {

/* set the initial count */
size_t write_size = sizeof(initial_count);
ret = PalStreamWrite(hdl, /*offset=*/0, &write_size, &initial_count, /*dest=*/NULL);
ret = PalStreamWrite(hdl, /*offset=*/0, &write_size, &initial_count);
if (ret < 0) {
log_error("eventfd: failed to set initial count");
return pal_to_unix_errno(ret);
Expand Down
18 changes: 2 additions & 16 deletions pal/include/pal/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ typedef struct toml_table_t toml_table_t;

typedef uint32_t PAL_IDX; /*!< an index */

/* maximum length of pipe/FIFO name (should be less than Linux sockaddr_un.sun_path = 108) */
#define PIPE_NAME_MAX 96

/* maximum length of URIs */
#define URI_MAX 4096

Expand Down Expand Up @@ -312,17 +309,13 @@ int PalStreamWaitForClient(PAL_HANDLE handle, PAL_HANDLE* client, pal_stream_opt
* \param[in,out] count Contains size of \p buffer. On success, will be set to the number of bytes
* read.
* \param buffer Pointer to the buffer to read into.
* \param[out] source If \p handle is a UDP socket, \p size is not zero and \p source is not
* NULL, the remote socket address is returned in it.
* \param size Size of the \p source buffer.
*
* \returns 0 on success, negative error code on failure.
*
* If \p handle is a directory, PalStreamRead fills the buffer with the null-terminated names of the
* directory entries.
*/
int PalStreamRead(PAL_HANDLE handle, uint64_t offset, size_t* count, void* buffer, char* source,
size_t size);
int PalStreamRead(PAL_HANDLE handle, uint64_t offset, size_t* count, void* buffer);

/*!
* \brief Write data to an open stream.
Expand All @@ -333,12 +326,10 @@ int PalStreamRead(PAL_HANDLE handle, uint64_t offset, size_t* count, void* buffe
* \param[in,out] count Contains size of \p buffer. On success, will be set to the number of bytes
* written.
* \param buffer Pointer to the buffer to write from.
* \param dest If the handle is a UDP socket, specifies the remote socket address.
*
* \returns 0 on success, negative error code on failure.
*/
int PalStreamWrite(PAL_HANDLE handle, uint64_t offset, size_t* count, void* buffer,
const char* dest);
int PalStreamWrite(PAL_HANDLE handle, uint64_t offset, size_t* count, void* buffer);

enum pal_delete_mode {
PAL_DELETE_ALL, /*!< delete the whole resource / shut down both directions */
Expand Down Expand Up @@ -456,11 +447,6 @@ int PalStreamAttributesQueryByHandle(PAL_HANDLE handle, PAL_STREAM_ATTR* attr);
*/
int PalStreamAttributesSetByHandle(PAL_HANDLE handle, PAL_STREAM_ATTR* attr);

/*!
* \brief Query the name of an open stream. On success `buffer` contains a null-terminated string.
*/
int PalStreamGetName(PAL_HANDLE handle, char* buffer, size_t size);

/*!
* \brief This API changes the name of an open stream.
*/
Expand Down
22 changes: 2 additions & 20 deletions pal/include/pal_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,9 @@ struct pal_common_state {
extern struct pal_common_state g_pal_common_state;
extern struct pal_public_state g_pal_public_state;

// TODO: clean unused stuff
/* handle_ops is the operators provided for each handler type. They are mostly used by
* stream-related PAL calls, but can also be used by some others in special ways. */
struct handle_ops {
/* 'getrealpath' return the real path that represent the handle */
const char* (*getrealpath)(PAL_HANDLE handle);

/* 'getname' is used by PalStreamGetName. It's different from 'getrealpath' */
int (*getname)(PAL_HANDLE handle, char* buffer, size_t count);

/* '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. */
Expand All @@ -56,13 +49,6 @@ struct handle_ops {
int64_t (*read)(PAL_HANDLE handle, uint64_t offset, uint64_t count, void* buffer);
int64_t (*write)(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buffer);

/* 'readbyaddr' and 'writebyaddr' are the same as read and write, but with extra field to
* specify address */
int64_t (*readbyaddr)(PAL_HANDLE handle, uint64_t offset, uint64_t count, void* buffer,
char* addr, size_t addrlen);
int64_t (*writebyaddr)(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buffer,
const char* addr, size_t addrlen);

/* 'close' and 'delete' is used by PalObjectClose and PalStreamDelete, 'close' will close the
* stream, while 'delete' actually destroy the stream, such as deleting a file or shutting
* down a socket */
Expand Down Expand Up @@ -184,19 +170,15 @@ 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);
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, char* addr,
int addrlen);
int64_t _PalStreamWrite(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buf,
const char* addr, int addrlen);
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);
int _PalStreamAttributesQuery(const char* uri, PAL_STREAM_ATTR* attr);
int _PalStreamAttributesQueryByHandle(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr);
int _PalStreamMap(PAL_HANDLE handle, void** addr_ptr, pal_prot_flags_t prot, uint64_t offset,
uint64_t size);
int _PalStreamUnmap(void* addr, uint64_t size);
int64_t _PalStreamSetLength(PAL_HANDLE handle, uint64_t length);
int _PalStreamFlush(PAL_HANDLE handle);
int _PalStreamGetName(PAL_HANDLE handle, char* buf, size_t size);
const char* _PalStreamRealpath(PAL_HANDLE hdl);
int _PalSendHandle(PAL_HANDLE target_process, PAL_HANDLE cargo);
int _PalReceiveHandle(PAL_HANDLE source_process, PAL_HANDLE* out_cargo);

Expand Down
2 changes: 1 addition & 1 deletion pal/regression/Directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char** argv, char** envp) {
}

size_t bytes = sizeof(buffer);
ret = PalStreamRead(dir1, 0, &bytes, buffer, NULL, 0);
ret = PalStreamRead(dir1, 0, &bytes, buffer);
if (ret >= 0 && bytes) {
for (char* c = buffer; c < buffer + bytes; c += strlen(c) + 1)
if (strlen(c))
Expand Down
12 changes: 6 additions & 6 deletions pal/regression/File.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ int main(int argc, char** argv, char** envp) {

/* test file read */
size_t size = sizeof(buffer1);
ret = PalStreamRead(file1, 0, &size, buffer1, NULL, 0);
ret = PalStreamRead(file1, 0, &size, buffer1);
if (ret == 0 && size == sizeof(buffer1)) {
print_hex("Read Test 1 (0th - 40th): %s\n", buffer1, size);
}

size = sizeof(buffer1);
ret = PalStreamRead(file1, 0, &size, buffer1, NULL, 0);
ret = PalStreamRead(file1, 0, &size, buffer1);
if (ret == 0 && size == sizeof(buffer1)) {
print_hex("Read Test 2 (0th - 40th): %s\n", buffer1, size);
}

size = sizeof(buffer2);
ret = PalStreamRead(file1, 200, &size, buffer2, NULL, 0);
ret = PalStreamRead(file1, 200, &size, buffer2);
if (ret == 0 && size == sizeof(buffer2)) {
print_hex("Read Test 3 (200th - 240th): %s\n", buffer2, size);
}
Expand Down Expand Up @@ -135,17 +135,17 @@ int main(int argc, char** argv, char** envp) {
/* test file writing */

size_t size = sizeof(buffer1);
ret = PalStreamWrite(file4, 0, &size, buffer1, NULL);
ret = PalStreamWrite(file4, 0, &size, buffer1);
if (ret < 0)
goto fail_writing;

size = sizeof(buffer2);
ret = PalStreamWrite(file4, 0, &size, buffer2, NULL);
ret = PalStreamWrite(file4, 0, &size, buffer2);
if (ret < 0)
goto fail_writing;

size = sizeof(buffer1);
ret = PalStreamWrite(file4, 200, &size, buffer1, NULL);
ret = PalStreamWrite(file4, 200, &size, buffer1);
if (ret < 0)
goto fail_writing;

Expand Down
4 changes: 2 additions & 2 deletions pal/regression/File2.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char** argv, char** envp) {
}

size_t bytes = sizeof(str) - 1;
ret = PalStreamWrite(out, 0, &bytes, str, NULL);
ret = PalStreamWrite(out, 0, &bytes, str);
if (ret < 0 || bytes != sizeof(str) - 1) {
pal_printf("second PalStreamWrite failed\n");
return 1;
Expand All @@ -36,7 +36,7 @@ int main(int argc, char** argv, char** envp) {

bytes = sizeof(str);
memset(str, 0, bytes);
ret = PalStreamRead(in, 0, &bytes, str, NULL, 0);
ret = PalStreamRead(in, 0, &bytes, str);
if (ret < 0) {
pal_printf("PalStreamRead failed\n");
return 1;
Expand Down
2 changes: 1 addition & 1 deletion pal/regression/HelloWorld.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, char** argv, char** envp) {
}

size_t bytes = sizeof(str) - 1;
ret = PalStreamWrite(out, 0, &bytes, str, NULL);
ret = PalStreamWrite(out, 0, &bytes, str);

if (ret < 0 || bytes != sizeof(str) - 1) {
pal_printf("PalStreamWrite failed\n");
Expand Down
2 changes: 1 addition & 1 deletion pal/regression/Pie.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, char** argv, char** envp) {
}

size_t bytes = sizeof(str) - 1;
ret = PalStreamWrite(out, 0, &bytes, str, NULL);
ret = PalStreamWrite(out, 0, &bytes, str);

if (ret < 0 || bytes != sizeof(str) - 1) {
pal_printf("PalStreamWrite failed\n");
Expand Down
8 changes: 4 additions & 4 deletions pal/regression/Pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ int main(int argc, char** argv, char** envp) {
pal_printf("Pipe Connection 1 OK\n");

size_t size = sizeof(buffer1);
ret = PalStreamWrite(pipe3, 0, &size, buffer1, NULL);
ret = PalStreamWrite(pipe3, 0, &size, buffer1);
if (ret == 0 && size > 0)
pal_printf("Pipe Write 1 OK\n");

size = sizeof(buffer3);
ret = PalStreamRead(pipe2, 0, &size, buffer3, NULL, 0);
ret = PalStreamRead(pipe2, 0, &size, buffer3);
if (ret == 0 && size > 0)
pal_printf("Pipe Read 1: %s\n", buffer3);

size = sizeof(buffer2);
ret = PalStreamWrite(pipe2, 0, &size, buffer2, NULL);
ret = PalStreamWrite(pipe2, 0, &size, buffer2);
if (ret == 0 && size > 0)
pal_printf("Pipe Write 2 OK\n");

size = sizeof(buffer4);
ret = PalStreamRead(pipe3, 0, &size, buffer4, NULL, 0);
ret = PalStreamRead(pipe3, 0, &size, buffer4);
if (ret == 0 && size > 0)
pal_printf("Pipe Read 2: %s\n", buffer4);
}
Expand Down
Loading

0 comments on commit f7995b6

Please sign in to comment.