-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.c
165 lines (131 loc) · 4.08 KB
/
server.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
/*
* Example memfd_create(2) server application.
*
* Copyright (C) 2015 Ahmed S. Darwish <[email protected]>
*
* SPDX-License-Identifier: Unlicense
*
* Kindly check attached README.md file for further details.
*/
#include <sys/syscall.h>
#include <linux/memfd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <linux/un.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include "memfd.h"
static void error(char *msg) {
perror(msg);
exit(EXIT_FAILURE);
}
static 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 = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (shm == MAP_FAILED)
error("mmap()");
sprintf(shm, "Secure zero-copy message from server: %s", unique_str);
/* Seal writes too, but unmap our shared mappings beforehand */
ret = munmap(shm, shm_size);
if (ret == -1)
error("munmap()");
ret = fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE);
if (ret == -1)
error("fcntl(F_SEAL_WRITE)");
ret = fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL);
if (ret == -1)
error("fcntl(F_SEAL_SEAL)");
return fd;
}
/* Pass file descriptor @fd to the client, which is
* connected to us through the Unix domain socket @conn */
static 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 = NULL;
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 "./unix_socket"
#define MAX_CONNECT_BACKLOG 128
#define CTIME_BUFFER_LEN 30
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()");
while (true) {
conn = accept(sock, (struct sockaddr *) &address, &addrlen);
if (conn == -1)
break;
/* Remove useless ctime(3) trailing newline */
time_t now = time(NULL);
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);
close(conn);
close(fd);
}
}
int main(int argc, char **argv) {
start_server_and_send_memfd_to_clients();
return 0;
}