aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_fdserver.c
blob: 0e9fb0e4dda09d4d242821663052bfa0432a9ee5 (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/* Copyright (c) 2016-2018, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include "config.h"

/*
 * This file implements a file descriptor sharing server enabling
 * sharing of file descriptors between processes, regardless of fork time.
 *
 * File descriptors are process scoped, but they can be "sent and converted
 * on the fly" between processes using special unix domain socket ancillary
 * data.
 * The receiving process gets a file descriptor "pointing" to the same thing
 * as the one sent (but the value of the file descriptor itself may be different
 * from the one sent).
 * Because ODP applications are responsible for creating ODP threads (i.e.
 * pthreads or linux processes), ODP has no control on the order things happen:
 * Nothing prevent a thread A to fork B and C, and then C creating a pktio
 * which will be used by A and B to send/receive packets.
 * Assuming this pktio uses a file descriptor, the latter will need to be
 * shared between the processes, despite the "non convenient" fork time.
 * The shared memory allocator is likely to use this as well to be able to
 * share memory regardless of fork() time.
 * This server handles a table of {(context,key)<-> fd} pair, and is
 * interfaced by the following functions:
 *
 * _odp_fdserver_register_fd(context, key, fd_to_send);
 * _odp_fdserver_deregister_fd(context, key);
 * _odp_fdserver_lookup_fd(context, key);
 *
 * which are used to register/deregister or querry for file descriptor based
 * on a context and key value couple, which has to be unique.
 *
 * Note again that the file descriptors stored here are local to this server
 * process and get converted both when registered or looked up.
 */

#include <odp_posix_extensions.h>
#include <odp/api/spinlock.h>
#include <odp_internal.h>
#include <odp_debug_internal.h>
#include <odp_fdserver_internal.h>
#include <sys/prctl.h>
#include <signal.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/wait.h>

#define FDSERVER_SOCKPATH_MAXLEN 255
#define FDSERVER_SOCK_FORMAT "%s/%s/odp-%d-fdserver"
#define FDSERVER_SOCKDIR_FORMAT "%s/%s"
#define FDSERVER_DEFAULT_DIR "/dev/shm"
#define FDSERVER_BACKLOG 5

#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif

#define FD_ODP_DEBUG_PRINT 0

#define FD_ODP_DBG(fmt, ...) \
	do { \
		if (FD_ODP_DEBUG_PRINT == 1) \
			ODP_DBG(fmt, ##__VA_ARGS__);\
	} while (0)

/* when accessing the client functions, clients should be mutexed: */
static odp_spinlock_t *client_lock;

/* define the tables of file descriptors handled by this server: */
#define FDSERVER_MAX_ENTRIES 256
typedef struct fdentry_s {
	fd_server_context_e context;
	uint64_t key;
	int  fd;
} fdentry_t;
static fdentry_t *fd_table;
static int fd_table_nb_entries;

/*
 * define the message struct used for communication between client and server
 * (this single message is used in both direction)
 * The file descriptors are sent out of band as ancillary data for conversion.
 */
typedef struct fd_server_msg {
	int command;
	fd_server_context_e context;
	uint64_t key;
} fdserver_msg_t;
/* possible commands are: */
#define FD_REGISTER_REQ		1  /* client -> server */
#define FD_REGISTER_ACK		2  /* server -> client */
#define FD_REGISTER_NACK	3  /* server -> client */
#define FD_LOOKUP_REQ		4  /* client -> server */
#define FD_LOOKUP_ACK		5  /* server -> client */
#define FD_LOOKUP_NACK		6  /* server -> client */
#define FD_DEREGISTER_REQ	7  /* client -> server */
#define FD_DEREGISTER_ACK	8  /* server -> client */
#define FD_DEREGISTER_NACK	9  /* server -> client */
#define FD_SERVERSTOP_REQ	10 /* client -> server (stops) */

/*
 * Client and server function:
 * Send a fdserver_msg, possibly including a file descriptor, on the socket
 * This function is used both by:
 * -the client (sending a FD_REGISTER_REQ with a file descriptor to be shared,
 *  or FD_LOOKUP_REQ/FD_DEREGISTER_REQ without a file descriptor)
 * -the server (sending FD_REGISTER_ACK/NACK, FD_LOOKUP_NACK,
 *  FD_DEREGISTER_ACK/NACK... without a fd or a
 *  FD_LOOKUP_ACK with a fd)
 * This function make use of the ancillary data (control data) to pass and
 * convert file descriptors over UNIX sockets
 * Return -1 on error, 0 on success.
 */
static int send_fdserver_msg(int sock, int command,
			     fd_server_context_e context, uint64_t key,
			     int fd_to_send)
{
	struct msghdr socket_message;
	struct iovec io_vector[1]; /* one msg frgmt only */
	struct cmsghdr *control_message = NULL;
	int *fd_location;
	fdserver_msg_t msg;
	int res;

	char ancillary_data[CMSG_SPACE(sizeof(int))];

	/* prepare the register request body (single framgent): */
	msg.command = command;
	msg.context = context;
	msg.key = key;
	io_vector[0].iov_base = &msg;
	io_vector[0].iov_len = sizeof(fdserver_msg_t);

	/* initialize socket message */
	memset(&socket_message, 0, sizeof(struct msghdr));
	socket_message.msg_iov = io_vector;
	socket_message.msg_iovlen = 1;

	if (fd_to_send >= 0) {
		/* provide space for the ancillary data */
		memset(ancillary_data, 0, CMSG_SPACE(sizeof(int)));
		socket_message.msg_control = ancillary_data;
		socket_message.msg_controllen = CMSG_SPACE(sizeof(int));

		/* initialize a single ancillary data element for fd passing */
		control_message = CMSG_FIRSTHDR(&socket_message);
		control_message->cmsg_level = SOL_SOCKET;
		control_message->cmsg_type = SCM_RIGHTS;
		control_message->cmsg_len = CMSG_LEN(sizeof(int));
		fd_location = (int *)(void *)CMSG_DATA(control_message);
		*fd_location = fd_to_send;
	}
	res = sendmsg(sock, &socket_message, 0);
	if (res < 0) {
		ODP_ERR("send_fdserver_msg: %s\n", strerror(errno));
		return -1;
	}

	return 0;
}

/*
 * Client and server function
 * Receive a fdserver_msg, possibly including a file descriptor, on the
 * given socket.
 * This function is used both by:
 * -the server (receiving a FD_REGISTER_REQ with a file descriptor to be shared,
 *  or FD_LOOKUP_REQ, FD_DEREGISTER_REQ without a file descriptor)
 * -the client (receiving FD_REGISTER_ACK...without a fd or a FD_LOOKUP_ACK with
 * a fd)
 * This function make use of the ancillary data (control data) to pass and
 * convert file descriptors over UNIX sockets.
 * Return -1 on error, 0 on success.
 */
static int recv_fdserver_msg(int sock, int *command,
			     fd_server_context_e *context, uint64_t *key,
			     int *recvd_fd)
{
	struct msghdr socket_message;
	struct iovec io_vector[1]; /* one msg frgmt only */
	struct cmsghdr *control_message = NULL;
	int *fd_location;
	fdserver_msg_t msg;
	char ancillary_data[CMSG_SPACE(sizeof(int))];

	memset(&socket_message, 0, sizeof(struct msghdr));
	memset(ancillary_data, 0, CMSG_SPACE(sizeof(int)));

	/* setup a place to fill in message contents */
	io_vector[0].iov_base = &msg;
	io_vector[0].iov_len = sizeof(fdserver_msg_t);
	socket_message.msg_iov = io_vector;
	socket_message.msg_iovlen = 1;

	/* provide space for the ancillary data */
	socket_message.msg_control = ancillary_data;
	socket_message.msg_controllen = CMSG_SPACE(sizeof(int));

	/* receive the message */
	if (recvmsg(sock, &socket_message, MSG_CMSG_CLOEXEC) < 0) {
		ODP_ERR("recv_fdserver_msg: %s\n", strerror(errno));
		return -1;
	}

	*command = msg.command;
	*context = msg.context;
	*key = msg.key;

	/* grab the converted file descriptor (if any) */
	*recvd_fd = -1;

	if ((socket_message.msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
		return 0;

	/* iterate ancillary elements to find the file descriptor: */
	for (control_message = CMSG_FIRSTHDR(&socket_message);
	     control_message != NULL;
	     control_message = CMSG_NXTHDR(&socket_message, control_message)) {
		if ((control_message->cmsg_level == SOL_SOCKET) &&
		    (control_message->cmsg_type == SCM_RIGHTS)) {
			fd_location = (int *)(void *)CMSG_DATA(control_message);
			*recvd_fd = *fd_location;
			break;
		}
	}

	return 0;
}

/* opens and returns a connected socket to the server */
static int get_socket(void)
{
	char sockpath[FDSERVER_SOCKPATH_MAXLEN];
	int s_sock; /* server socket */
	struct sockaddr_un remote;
	int len;

	/* construct the named socket path: */
	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCK_FORMAT,
		 odp_global_data.shm_dir,
		 odp_global_data.uid,
		 odp_global_data.main_pid);

	s_sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (s_sock == -1) {
		ODP_ERR("cannot connect to server: %s\n", strerror(errno));
		return -1;
	}

	remote.sun_family = AF_UNIX;
	strcpy(remote.sun_path, sockpath);
	len = strlen(remote.sun_path) + sizeof(remote.sun_family);
	while (connect(s_sock, (struct sockaddr *)&remote, len) == -1) {
		if (errno == EINTR)
			continue;
		ODP_ERR("cannot connect to server: %s\n", strerror(errno));
		close(s_sock);
		return -1;
	}

	return s_sock;
}

/*
 * Client function:
 * Register a file descriptor to the server. Return -1 on error.
 */
int _odp_fdserver_register_fd(fd_server_context_e context, uint64_t key,
			      int fd_to_send)
{
	int s_sock; /* server socket */
	int res;
	int command;
	int fd;

	odp_spinlock_lock(client_lock);

	FD_ODP_DBG("FD client register: pid=%d key=%" PRIu64 ", fd=%d\n",
		   getpid(), key, fd_to_send);

	s_sock = get_socket();
	if (s_sock < 0) {
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	res =  send_fdserver_msg(s_sock, FD_REGISTER_REQ, context, key,
				 fd_to_send);
	if (res < 0) {
		ODP_ERR("fd registration failure\n");
		close(s_sock);
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	res = recv_fdserver_msg(s_sock, &command, &context, &key, &fd);

	if ((res < 0) || (command != FD_REGISTER_ACK)) {
		ODP_ERR("fd registration failure\n");
		close(s_sock);
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	close(s_sock);

	odp_spinlock_unlock(client_lock);
	return 0;
}

/*
 * Client function:
 * Deregister a file descriptor from the server. Return -1 on error.
 */
int _odp_fdserver_deregister_fd(fd_server_context_e context, uint64_t key)
{
	int s_sock; /* server socket */
	int res;
	int command;
	int fd;

	odp_spinlock_lock(client_lock);

	FD_ODP_DBG("FD client deregister: pid=%d key=%" PRIu64 "\n",
		   getpid(), key);

	s_sock = get_socket();
	if (s_sock < 0) {
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	res =  send_fdserver_msg(s_sock, FD_DEREGISTER_REQ, context, key, -1);
	if (res < 0) {
		ODP_ERR("fd de-registration failure\n");
		close(s_sock);
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	res = recv_fdserver_msg(s_sock, &command, &context, &key, &fd);

	if ((res < 0) || (command != FD_DEREGISTER_ACK)) {
		ODP_ERR("fd de-registration failure\n");
		close(s_sock);
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	close(s_sock);

	odp_spinlock_unlock(client_lock);
	return 0;
}

/*
 * client function:
 * lookup a file descriptor from the server. return -1 on error,
 * or the file descriptor on success (>=0).
 */
int _odp_fdserver_lookup_fd(fd_server_context_e context, uint64_t key)
{
	int s_sock; /* server socket */
	int res;
	int command;
	int fd;

	odp_spinlock_lock(client_lock);

	s_sock = get_socket();
	if (s_sock < 0) {
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	res =  send_fdserver_msg(s_sock, FD_LOOKUP_REQ, context, key, -1);
	if (res < 0) {
		ODP_ERR("fd lookup failure\n");
		close(s_sock);
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	res = recv_fdserver_msg(s_sock, &command, &context, &key, &fd);

	if ((res < 0) || (command != FD_LOOKUP_ACK)) {
		ODP_ERR("fd lookup failure\n");
		close(s_sock);
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	close(s_sock);
	ODP_DBG("FD client lookup: pid=%d, key=%" PRIu64 ", fd=%d\n",
		getpid(), key, fd);

	odp_spinlock_unlock(client_lock);
	return fd;
}

/*
 * request server terminaison:
 */
static int stop_server(void)
{
	int s_sock; /* server socket */
	int res;

	odp_spinlock_lock(client_lock);

	FD_ODP_DBG("FD sending server stop request\n");

	s_sock = get_socket();
	if (s_sock < 0) {
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	res =  send_fdserver_msg(s_sock, FD_SERVERSTOP_REQ, 0, 0, -1);
	if (res < 0) {
		ODP_ERR("fd stop request failure\n");
		close(s_sock);
		odp_spinlock_unlock(client_lock);
		return -1;
	}

	close(s_sock);

	odp_spinlock_unlock(client_lock);
	return 0;
}

/*
 * server function
 * receive a client request and handle it.
 * Always returns 0 unless a stop request is received.
 */
static int handle_request(int client_sock)
{
	int command;
	fd_server_context_e context;
	uint64_t key;
	int fd;
	int i;

	/* get a client request: */
	recv_fdserver_msg(client_sock, &command, &context, &key, &fd);
	switch (command) {
	case FD_REGISTER_REQ:
		if ((fd < 0) || (context >= FD_SRV_CTX_END)) {
			ODP_ERR("Invalid register fd or context\n");
			send_fdserver_msg(client_sock, FD_REGISTER_NACK,
					  FD_SRV_CTX_NA, 0, -1);
			return 0;
		}

		/* store the file descriptor in table: */
		if (fd_table_nb_entries < FDSERVER_MAX_ENTRIES) {
			fd_table[fd_table_nb_entries].context = context;
			fd_table[fd_table_nb_entries].key     = key;
			fd_table[fd_table_nb_entries++].fd    = fd;
			FD_ODP_DBG("storing {ctx=%d, key=%" PRIu64 "}->fd=%d\n",
				   context, key, fd);
		} else {
			ODP_ERR("FD table full\n");
			send_fdserver_msg(client_sock, FD_REGISTER_NACK,
					  FD_SRV_CTX_NA, 0, -1);
			return 0;
		}

		send_fdserver_msg(client_sock, FD_REGISTER_ACK,
				  FD_SRV_CTX_NA, 0, -1);
		break;

	case FD_LOOKUP_REQ:
		if (context >= FD_SRV_CTX_END) {
			ODP_ERR("invalid lookup context\n");
			send_fdserver_msg(client_sock, FD_LOOKUP_NACK,
					  FD_SRV_CTX_NA, 0, -1);
			return 0;
		}

		/* search key in table and sent reply: */
		for (i = 0; i < fd_table_nb_entries; i++) {
			if ((fd_table[i].context == context) &&
			    (fd_table[i].key == key)) {
				fd = fd_table[i].fd;
				ODP_DBG("lookup {ctx=%d,"
					" key=%" PRIu64 "}->fd=%d\n",
					context, key, fd);
				send_fdserver_msg(client_sock,
						  FD_LOOKUP_ACK, context, key,
						  fd);
				return 0;
			}
		}

		/* context+key not found... send nack */
		send_fdserver_msg(client_sock, FD_LOOKUP_NACK, context, key,
				  -1);
		break;

	case FD_DEREGISTER_REQ:
		if (context >= FD_SRV_CTX_END) {
			ODP_ERR("invalid deregister context\n");
			send_fdserver_msg(client_sock, FD_DEREGISTER_NACK,
					  FD_SRV_CTX_NA, 0, -1);
			return 0;
		}

		/* search key in table and remove it if found, and reply: */
		for (i = 0; i < fd_table_nb_entries; i++) {
			if ((fd_table[i].context == context) &&
			    (fd_table[i].key == key)) {
				FD_ODP_DBG("drop {ctx=%d,"
					   " key=%" PRIu64 "}->fd=%d\n",
					   context, key, fd_table[i].fd);
				close(fd_table[i].fd);
				fd_table[i] = fd_table[--fd_table_nb_entries];
				send_fdserver_msg(client_sock,
						  FD_DEREGISTER_ACK,
						  context, key, -1);
				return 0;
			}
		}

		/* key not found... send nack */
		send_fdserver_msg(client_sock, FD_DEREGISTER_NACK,
				  context, key, -1);
		break;

	case FD_SERVERSTOP_REQ:
		FD_ODP_DBG("Stoping FD server\n");
		return 1;

	default:
		ODP_ERR("Unexpected request\n");
		break;
	}
	return 0;
}

/*
 * server function
 * loop forever, handling client requests one by one
 */
static void wait_requests(int sock)
{
	int c_socket; /* client connection */
	unsigned int addr_sz;
	struct sockaddr_un remote;

	for (;;) {
		addr_sz = sizeof(remote);
		c_socket = accept(sock, (struct sockaddr *)&remote, &addr_sz);
		if (c_socket == -1) {
			if (errno == EINTR)
				continue;

			ODP_ERR("wait_requests: %s\n", strerror(errno));
			return;
		}

		if (handle_request(c_socket))
			break;
		close(c_socket);
	}
	close(c_socket);
}

/*
 * Create a unix domain socket and fork a process to listen to incoming
 * requests.
 */
int _odp_fdserver_init_global(void)
{
	char sockpath[FDSERVER_SOCKPATH_MAXLEN];
	int sock;
	struct sockaddr_un local;
	pid_t server_pid;
	int res;

	/* create the client spinlock that any client can see: */
	client_lock = mmap(NULL, sizeof(odp_spinlock_t), PROT_READ | PROT_WRITE,
			   MAP_SHARED | MAP_ANONYMOUS, -1, 0);

	odp_spinlock_init(client_lock);

	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCKDIR_FORMAT,
		 odp_global_data.shm_dir,
		 odp_global_data.uid);

	mkdir(sockpath, 0744);

	/* construct the server named socket path: */
	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCK_FORMAT,
		 odp_global_data.shm_dir,
		 odp_global_data.uid,
		 odp_global_data.main_pid);

	/* create UNIX domain socket: */
	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock == -1) {
		ODP_ERR("_odp_fdserver_init_global: %s\n", strerror(errno));
		return -1;
	}

	/* remove previous named socket if it already exists: */
	unlink(sockpath);

	/* bind to new named socket: */
	local.sun_family = AF_UNIX;
	strncpy(local.sun_path, sockpath, sizeof(local.sun_path));
	res = bind(sock, (struct sockaddr *)&local, sizeof(struct sockaddr_un));
	if (res == -1) {
		ODP_ERR("_odp_fdserver_init_global: %s\n", strerror(errno));
		close(sock);
		return -1;
	}

	/* listen for incoming conections: */
	if (listen(sock, FDSERVER_BACKLOG) == -1) {
		ODP_ERR("_odp_fdserver_init_global: %s\n", strerror(errno));
		close(sock);
		return -1;
	}

	/* fork a server process: */
	server_pid = fork();
	if (server_pid == -1) {
		ODP_ERR("Could not fork!\n");
		close(sock);
		return -1;
	}

	if (server_pid == 0) { /*child */
		sigset_t sigset;
		struct sigaction action;

		sigfillset(&sigset);
		/* undefined if these are ignored, as per POSIX */
		sigdelset(&sigset, SIGFPE);
		sigdelset(&sigset, SIGILL);
		sigdelset(&sigset, SIGSEGV);
		/* can not be masked */
		sigdelset(&sigset, SIGKILL);
		sigdelset(&sigset, SIGSTOP);
		/* these we want to handle */
		sigdelset(&sigset, SIGTERM);
		if (sigprocmask(SIG_SETMASK, &sigset, NULL) == -1) {
			ODP_ERR("Could not set signal mask");
			exit(1);
		}

		/* set default handlers for those signals we can handle */
		memset(&action, 0, sizeof(action));
		action.sa_handler = SIG_DFL;
		sigemptyset(&action.sa_mask);
		action.sa_flags = 0;
		sigaction(SIGFPE, &action, NULL);
		sigaction(SIGILL, &action, NULL);
		sigaction(SIGSEGV, &action, NULL);
		sigaction(SIGTERM, &action, NULL);

		/* TODO: pin the server on appropriate service cpu mask */
		/* when (if) we can agree on the usage of service mask  */

		/* request to be killed if parent dies, hence avoiding  */
		/* orphans being "adopted" by the init process...	*/
		prctl(PR_SET_PDEATHSIG, SIGTERM);

		res = setsid();
		if (res == -1) {
			ODP_ERR("Could not setsid()");
			exit(1);
		}

		/* allocate the space for the file descriptor<->key table: */
		fd_table = malloc(FDSERVER_MAX_ENTRIES * sizeof(fdentry_t));
		if (!fd_table) {
			ODP_ERR("maloc failed!\n");
			exit(1);
		}

		/* wait for clients requests */
		wait_requests(sock); /* Returns when server is stopped  */
		close(sock);

		/* release the file descriptor table: */
		free(fd_table);

		exit(0);
	}

	/* parent */
	close(sock);
	return 0;
}

/*
 * Terminate the server
 */
int _odp_fdserver_term_global(void)
{
	int status;
	char sockpath[FDSERVER_SOCKPATH_MAXLEN];

	/* close the server and wait for child terminaison*/
	stop_server();
	wait(&status);

	/* construct the server named socket path: */
	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCK_FORMAT,
		 odp_global_data.shm_dir,
		 odp_global_data.uid,
		 odp_global_data.main_pid);

	/* delete the UNIX domain socket: */
	unlink(sockpath);

	/* delete shm files directory */
	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCKDIR_FORMAT,
		 odp_global_data.shm_dir,
		 odp_global_data.uid);
	rmdir(sockpath);

	return 0;
}