Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
fixup! [LibOS] Implement POSIX locks (fcntl)
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Marczewski <[email protected]>
  • Loading branch information
pwmarcz committed Jun 29, 2021
1 parent 109eb24 commit e0709b4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion LibOS/shim/include/shim_fs_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int posix_lock_set(struct shim_dentry* dent, struct posix_lock* pl, bool wait);
int posix_lock_get(struct shim_dentry* dent, struct posix_lock* pl, struct posix_lock* out_pl);

/* Removes all locks for a given PID. Should be called before process exit. */
void posix_lock_clear_pid(IDTYPE pid);
int posix_lock_clear_pid(IDTYPE pid);

/* Version of `posix_lock_set` called from IPC callback. */
int posix_lock_set_from_ipc(const char* path, struct posix_lock* pl, bool wait, IDTYPE vmid,
Expand Down
2 changes: 1 addition & 1 deletion LibOS/shim/include/shim_ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ int ipc_sync_confirm_close_callback(IDTYPE src, void* data, unsigned long seq);
/*
* POSIX_LOCK_SET: `struct shim_ipc_posix_lock` -> `int`
* POSIX_LOCK_GET: `struct shim_ipc_posix_lock` -> `struct shim_ipc_posix_lock_resp`
* POSIX_LOCK_CLEAR_PID: `IDTYPE` -> (no response)
* POSIX_LOCK_CLEAR_PID: `IDTYPE` -> `int`
*/

struct shim_ipc_posix_lock {
Expand Down
8 changes: 3 additions & 5 deletions LibOS/shim/src/fs/shim_fs_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,9 @@ int posix_lock_get_from_ipc(const char* path, struct posix_lock* pl, struct posi
return ret;
}

void posix_lock_clear_pid(IDTYPE pid) {
int posix_lock_clear_pid(IDTYPE pid) {
if (g_process_ipc_ids.leader_vmid) {
int ret = ipc_posix_lock_clear_pid(pid);
if (ret < 0)
log_warning("failed to notify leader about clearing locks: %d\n", ret);
return;
return ipc_posix_lock_clear_pid(pid);
}

log_debug("clearing POSIX locks for pid %d\n", pid);
Expand Down Expand Up @@ -609,4 +606,5 @@ void posix_lock_clear_pid(IDTYPE pid) {
}

unlock(&g_fs_lock_lock);
return 0;
}
31 changes: 21 additions & 10 deletions LibOS/shim/src/ipc/shim_ipc_fs_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ int ipc_posix_lock_set(const char* path, struct posix_lock* pl, bool wait) {
int ret = ipc_send_msg_and_get_response(g_process_ipc_ids.leader_vmid, msg, &data);
if (ret < 0)
return ret;

int* resp = data;
return *resp;
int result = *(int*)data;
free(data);
return result;
}

int ipc_posix_lock_set_send_response(IDTYPE vmid, unsigned long seq, int result) {
Expand Down Expand Up @@ -71,13 +71,15 @@ int ipc_posix_lock_get(const char* path, struct posix_lock* pl, struct posix_loc
return ret;

struct shim_ipc_posix_lock_resp* resp = data;
int result = resp->result;
if (resp->result == 0) {
out_pl->type = resp->type;
out_pl->start = resp->start;
out_pl->end = resp->end;
out_pl->pid = resp->pid;
}
return resp->result;
free(data);
return result;
}

int ipc_posix_lock_clear_pid(IDTYPE pid) {
Expand All @@ -87,7 +89,14 @@ int ipc_posix_lock_clear_pid(IDTYPE pid) {
struct shim_ipc_msg* msg = __alloca(total_msg_size);
init_ipc_msg(msg, IPC_MSG_POSIX_LOCK_CLEAR_PID, total_msg_size);
memcpy(msg->data, &pid, sizeof(pid));
return ipc_send_message(g_process_ipc_ids.leader_vmid, msg);

void* data;
int ret = ipc_send_msg_and_get_response(g_process_ipc_ids.leader_vmid, msg, &data);
if (ret < 0)
return ret;
int result = *(int*)data;
free(data);
return result;
}

int ipc_posix_lock_set_callback(IDTYPE src, void* data, unsigned long seq) {
Expand Down Expand Up @@ -135,10 +144,12 @@ int ipc_posix_lock_get_callback(IDTYPE src, void* data, unsigned long seq) {
}

int ipc_posix_lock_clear_pid_callback(IDTYPE src, void* data, unsigned long seq) {
__UNUSED(src);
__UNUSED(seq);

IDTYPE* pid = data;
posix_lock_clear_pid(*pid);
return 0;
int result = posix_lock_clear_pid(*pid);

size_t total_msg_size = get_ipc_msg_size(sizeof(result));
struct shim_ipc_msg* msg = __alloca(total_msg_size);
init_ipc_response(msg, seq, total_msg_size);
memcpy(msg->data, &result, sizeof(result));
return ipc_send_message(src, msg);
}
4 changes: 3 additions & 1 deletion LibOS/shim/src/sys/shim_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ static noreturn void libos_clean_and_exit(int exit_code) {
* 2) wait for them to exit here, before we terminate the IPC helper
*/

posix_lock_clear_pid(g_process.pid);
int ret = posix_lock_clear_pid(g_process.pid);
if (ret < 0)
log_warning("error clearing POSIX locks: %d\n", ret);

shutdown_sync_client();

Expand Down

0 comments on commit e0709b4

Please sign in to comment.