forked from a-darwish/memfd-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
217 lines (168 loc) · 5.41 KB
/
server.cpp
File metadata and controls
217 lines (168 loc) · 5.41 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Example memfd_create(2) server application.
*
* Copyright (C) 2015 Ahmed S. Darwish <darwish.07@gmail.com>
*
* SPDX-License-Identifier: Unlicense
*
* Kindly check attached README.md file for further details.
*/
#include <linux/memfd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <linux/un.h>
#include <fcntl.h>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <cstdio>
#include <cerrno>
#include <ctime>
#include <vector>
#include <set>
#include <fstream>
#include <signal.h>
#include <sys/time.h>
#include "memfd.hpp"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
int new_memfd_region(char *unique_str) {
char *shm;
const int shm_size = 1024;
int fd, ret;
fd = memfd_create("Server memfd", MFD_ALLOW_SEALING);
if (fd == -1) error("memfd_create()");
ret = ftruncate(fd, shm_size);
if (ret == -1) error("ftruncate()");
ret = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK);
if (ret == -1) error("fcntl(F_SEAL_SHRINK)");
shm = static_cast<char *>(mmap(nullptr, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (shm == MAP_FAILED) error("mmap()");
sprintf(shm, "Connection accepted timestamp from server: %s", unique_str);
ret = munmap(shm, shm_size);
if (ret == -1) error("munmap()");
ret = fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL);
if (ret == -1) error("fcntl(F_SEAL_SEAL)");
return fd;
}
void send_fd(int conn, int fd) {
struct msghdr msgh{};
struct iovec iov{};
union {
struct cmsghdr cmsgh;
/* Space large enough to hold an 'int' */
char control[CMSG_SPACE(sizeof(int))];
} control_un{};
if (fd == -1) {
fprintf(stderr, "Cannot pass an invalid fd equaling -1\n");
exit(EXIT_FAILURE);
}
/* We must transmit at least 1 byte of real data in order
* to send some other ancillary data. */
char placeholder = 'A';
iov.iov_base = &placeholder;
iov.iov_len = sizeof(char);
msgh.msg_name = nullptr;
msgh.msg_namelen = 0;
msgh.msg_iov = &iov;
msgh.msg_iovlen = 1;
msgh.msg_control = control_un.control;
msgh.msg_controllen = sizeof(control_un.control);
/* Write the fd as ancillary data */
control_un.cmsgh.cmsg_len = CMSG_LEN(sizeof(int));
control_un.cmsgh.cmsg_level = SOL_SOCKET;
control_un.cmsgh.cmsg_type = SCM_RIGHTS;
*((int *) CMSG_DATA(CMSG_FIRSTHDR(&msgh))) = fd;
int size = sendmsg(conn, &msgh, 0);
if (size < 0) error("sendmsg()");
}
#define LOCAL_SOCKET_NAME "/tmp/unix_socket"
#define MAX_CONNECT_BACKLOG 128
#define SIG_INTERVAL_NS 50000
void timespec_diff(const struct timespec *start, const struct timespec *stop,
struct timespec *result) {
if ((stop->tv_nsec - start->tv_nsec) < 0) {
result->tv_sec = stop->tv_sec - start->tv_sec - 1;
result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000;
} else {
result->tv_sec = stop->tv_sec - start->tv_sec;
result->tv_nsec = stop->tv_nsec - start->tv_nsec;
}
}
static const char *const delim = " ";
timespec &readTimeFromSharedMemory(char *shm, timespec &temp) {
auto string = strtok(shm, delim);
if (string != nullptr) {
temp.tv_sec = atol(string);
string = strtok(nullptr, delim);
if (string != nullptr) {
temp.tv_nsec = atol(string);
}
}
return temp;
}
static void start_server_and_send_memfd_to_clients() {
int sock, conn, fd, ret;
struct sockaddr_un address;
socklen_t addrlen;
sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (sock == -1) error("socket()");
memset(&address, 0, sizeof(address));
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, LOCAL_SOCKET_NAME);
ret = unlink(LOCAL_SOCKET_NAME);
if (ret != 0 && ret != -ENOENT && ret != -EPERM) error("unlink()");
ret = bind(sock, (struct sockaddr *) &address, sizeof(address));
if (ret != 0) error("bind()");
ret = listen(sock, MAX_CONNECT_BACKLOG);
if (ret != 0) error("listen()");
conn = accept(sock, (struct sockaddr *) &address, &addrlen);
time_t now = time(nullptr);
char *nowbuf = ctime(&now);
nowbuf[strlen(nowbuf) - 1] = '\0';
printf("[%s] New client connection!\n", nowbuf);
fd = new_memfd_region(nowbuf);
send_fd(conn, fd);
sleep(1);
int pid;
int res = read(conn, &pid, sizeof(int));
if (res < 0) {
printf("Didn't get pid from domain socket!");
}
printf("%d\n", pid);
char *shm;
const int shm_size = 1024;
shm = static_cast<char *>(mmap(nullptr, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (shm == MAP_FAILED) errorp("mmap");
// Wait until the client has memory mapped the shared memory and is writing to the memory
// TODO: Think of better synchronization mechanism
sleep(1);
printf("READY!\n");
timespec current{}, newVal{}, diff{};
timespec sleep{};
while (true) {
current = readTimeFromSharedMemory(shm, current);
kill(pid, SIGALRM);
while (true) {
if (newVal.tv_nsec != current.tv_nsec || newVal.tv_sec != current.tv_sec) break;
kill(pid, SIGALRM);
newVal = readTimeFromSharedMemory(shm, newVal);
}
timespec_diff(¤t, &newVal, &diff);
if (diff.tv_nsec >= SIG_INTERVAL_NS) {
kill(pid, SIGPROF);
} else {
long sleepTime = SIG_INTERVAL_NS - diff.tv_nsec - 20;
if (sleepTime > 0) {
sleep.tv_nsec = sleepTime;
nanosleep(&sleep, nullptr);
}
}
current = newVal;
}
}
int main(int argc, char **argv) {
start_server_and_send_memfd_to_clients();
return 0;
}
#pragma clang diagnostic pop