This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
/
shim_ipc_fs_lock.c
177 lines (147 loc) · 5.63 KB
/
shim_ipc_fs_lock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2021 Intel Corporation
* Paweł Marczewski <[email protected]>
*/
/*
* IPC glue code for filesystem locks.
*/
#include "shim_fs_lock.h"
#include "shim_ipc.h"
static int cancel_posix_lock(IDTYPE dest, void* arg) {
struct shim_ipc_msg* lock_msg = arg;
char* path = ((struct shim_ipc_posix_lock*)lock_msg->data)->path;
size_t path_len = strlen(path);
struct shim_ipc_posix_lock_cancel msgin = {
/* Sequence number of the original message (set by
* `ipc_send_msg_and_get_response_with_cancel` before sending) */
.cancel_seq = lock_msg->header.seq,
};
size_t total_msg_size = get_ipc_msg_size(sizeof(msgin) + path_len + 1);
struct shim_ipc_msg* msg = __alloca(total_msg_size);
init_ipc_msg(msg, IPC_MSG_POSIX_LOCK_CANCEL, total_msg_size);
memcpy(msg->data, &msgin, sizeof(msgin));
memcpy(((struct shim_ipc_posix_lock_cancel*)&msg->data)->path, path, path_len + 1);
return ipc_send_message(dest, msg);
}
int ipc_posix_lock_set(const char* path, struct posix_lock* pl, bool wait) {
assert(g_process_ipc_ids.leader_vmid);
struct shim_ipc_posix_lock msgin = {
.type = pl->type,
.start = pl->start,
.end = pl->end,
.pid = pl->pid,
.wait = wait,
};
size_t path_len = strlen(path);
size_t total_msg_size = get_ipc_msg_size(sizeof(msgin) + path_len + 1);
struct shim_ipc_msg* msg = __alloca(total_msg_size);
init_ipc_msg(msg, IPC_MSG_POSIX_LOCK_SET, total_msg_size);
memcpy(msg->data, &msgin, sizeof(msgin));
memcpy(((struct shim_ipc_posix_lock*)&msg->data)->path, path, path_len + 1);
void* data;
int ret = ipc_send_msg_and_get_response_with_cancel(
g_process_ipc_ids.leader_vmid, msg, &cancel_posix_lock, msg, &data);
if (ret < 0)
return ret;
int result = *(int*)data;
free(data);
return result;
}
int ipc_posix_lock_set_send_response(IDTYPE vmid, uint64_t seq, int result) {
assert(!g_process_ipc_ids.leader_vmid);
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(vmid, msg);
}
int ipc_posix_lock_get(const char* path, struct posix_lock* pl, struct posix_lock* out_pl) {
assert(g_process_ipc_ids.leader_vmid);
struct shim_ipc_posix_lock msgin = {
.type = pl->type,
.start = pl->start,
.end = pl->end,
.pid = pl->pid,
};
size_t path_len = strlen(path);
size_t total_msg_size = get_ipc_msg_size(sizeof(msgin) + path_len + 1);
struct shim_ipc_msg* msg = __alloca(total_msg_size);
init_ipc_msg(msg, IPC_MSG_POSIX_LOCK_GET, total_msg_size);
memcpy(msg->data, &msgin, sizeof(msgin));
memcpy(((struct shim_ipc_posix_lock*)&msg->data)->path, path, path_len + 1);
void* data;
int ret = ipc_send_msg_and_get_response(g_process_ipc_ids.leader_vmid, msg, &data);
if (ret < 0)
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;
}
free(data);
return result;
}
int ipc_posix_lock_clear_pid(IDTYPE pid) {
assert(g_process_ipc_ids.leader_vmid);
size_t total_msg_size = get_ipc_msg_size(sizeof(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));
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, uint64_t seq) {
struct shim_ipc_posix_lock* msgin = data;
struct posix_lock pl = {
.type = msgin->type,
.start = msgin->start,
.end = msgin->end,
.pid = msgin->pid,
};
return posix_lock_set_from_ipc(msgin->path, &pl, msgin->wait, src, seq);
}
int ipc_posix_lock_cancel_callback(IDTYPE src, void* data, uint64_t seq) {
__UNUSED(seq);
struct shim_ipc_posix_lock_cancel* msgin = data;
return posix_lock_cancel_from_ipc(msgin->path, src, msgin->cancel_seq);
}
int ipc_posix_lock_get_callback(IDTYPE src, void* data, uint64_t seq) {
struct shim_ipc_posix_lock* msgin = data;
struct posix_lock pl = {
.type = msgin->type,
.start = msgin->start,
.end = msgin->end,
.pid = msgin->pid,
};
struct posix_lock pl2 = {0};
int result = posix_lock_get_from_ipc(msgin->path, &pl, &pl2);
struct shim_ipc_posix_lock_resp msgout = {
.result = result,
.type = pl2.type,
.start = pl2.start,
.end = pl2.end,
.pid = pl2.pid,
};
size_t total_msg_size = get_ipc_msg_size(sizeof(msgout));
struct shim_ipc_msg* msg = __alloca(total_msg_size);
init_ipc_response(msg, seq, total_msg_size);
memcpy(msg->data, &msgout, sizeof(msgout));
return ipc_send_message(src, msg);
}
int ipc_posix_lock_clear_pid_callback(IDTYPE src, void* data, uint64_t seq) {
IDTYPE* pid = data;
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);
}