Skip to content

Commit

Permalink
[LibOS] Close open fds on process exit
Browse files Browse the repository at this point in the history
Signed-off-by: Kailun Qin <[email protected]>
  • Loading branch information
kailun-qin authored and Dmitrii Kuvaiskii committed Aug 16, 2022
1 parent 6a46851 commit 2e700c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions libos/include/libos_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ int set_new_fd_handle_above_fd(uint32_t fd, struct libos_handle* hdl, int fd_fla
struct libos_handle* __detach_fd_handle(struct libos_fd_handle* fd, int* flags,
struct libos_handle_map* map);
struct libos_handle* detach_fd_handle(uint32_t fd, int* flags, struct libos_handle_map* map);
void detach_all_fds(void);

/* manage handle mapping */
int dup_handle_map(struct libos_handle_map** new_map, struct libos_handle_map* old_map);
Expand Down
19 changes: 14 additions & 5 deletions libos/src/bookkeep/libos_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,19 @@ struct libos_handle* detach_fd_handle(uint32_t fd, int* flags,
return handle;
}

static int detach_fd(struct libos_fd_handle* fd_hdl, struct libos_handle_map* map) {
struct libos_handle* hdl = __detach_fd_handle(fd_hdl, NULL, map);
put_handle(hdl);
return 0;
}

void detach_all_fds(void) {
struct libos_handle_map* handle_map = get_thread_handle_map(NULL);
assert(handle_map);

walk_handle_map(&detach_fd, handle_map);
}

struct libos_handle* get_new_handle(void) {
struct libos_handle* new_handle =
get_mem_obj_from_mgr_enlarge(handle_mgr, size_align_up(HANDLE_MGR_ALLOC));
Expand Down Expand Up @@ -651,18 +664,14 @@ int walk_handle_map(int (*callback)(struct libos_fd_handle*, struct libos_handle
int ret = 0;
lock(&map->lock);

if (map->fd_top == FD_NULL)
goto done;

for (uint32_t i = 0; i <= map->fd_top; i++) {
for (uint32_t i = 0; map->fd_top != FD_NULL && i <= map->fd_top; i++) {
if (!HANDLE_ALLOCATED(map->map[i]))
continue;

if ((ret = (*callback)(map->map[i], map)) < 0)
break;
}

done:
unlock(&map->lock);
return ret;
}
Expand Down
3 changes: 3 additions & 0 deletions libos/src/sys/libos_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "libos_fs_lock.h"
#include "libos_handle.h"
#include "libos_ipc.h"
#include "libos_lock.h"
#include "libos_process.h"
Expand Down Expand Up @@ -99,6 +100,8 @@ noreturn void thread_exit(int error_code, int term_signal) {
if (ret < 0)
log_warning("error clearing POSIX locks: %d", ret);

detach_all_fds();

/* This is the last thread of the process. Let parent know we exited. */
ret = ipc_cld_exit_send(error_code, term_signal);
if (ret < 0) {
Expand Down

0 comments on commit 2e700c1

Please sign in to comment.