-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.c
98 lines (82 loc) · 2.11 KB
/
main.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
/*
* server.c
*
* Copyright (c) 2014 Virtual Open Systems Sarl.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
*/
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "vhost_client.h"
#include "vhost_server.h"
static struct sigaction sigact;
int app_running = 0;
static void signal_handler(int);
static void init_signals(void);
static void cleanup(void);
int main(int argc, char* argv[])
{
int opt = 0;
VhostClient *vhost_master = 0;
VhostServer *vhost_slave = 0;
atexit(cleanup);
init_signals();
while ((opt = getopt(argc, argv, "q:s:c:")) != -1) {
switch (opt) {
case 'q':
vhost_master = new_vhost_client(optarg);
break;
case 's':
vhost_slave = new_vhost_server(optarg, 1 /*is_listen*/);
break;
case 'c':
vhost_slave = new_vhost_server(optarg, 0 /*is_listen*/);
break;
default:
break;
}
if (vhost_master || vhost_slave)
break;
}
if (vhost_slave) {
run_vhost_server(vhost_slave);
end_vhost_server(vhost_slave);
free(vhost_slave);
} else if (vhost_master) {
run_vhost_client(vhost_master);
free(vhost_master);
} else {
fprintf(stderr, "Usage: %s [-q path | -s path | -c path]\n", argv[0]);
fprintf(stderr, "\t-q - act as master\n");
fprintf(stderr, "\t-s - act as slave server\n");
fprintf(stderr, "\t-c - act as slave client\n");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
static void signal_handler(int sig){
switch(sig)
{
case SIGINT:
case SIGKILL:
case SIGTERM:
app_running = 0;
break;
default:
break;
}
}
static void init_signals(void){
sigact.sa_handler = signal_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, (struct sigaction *)NULL);
}
static void cleanup(void){
sigemptyset(&sigact.sa_mask);
app_running = 0;
}