xref: /qemu/contrib/ivshmem-server/main.c (revision a75eb03b9fca3af291ec2c433ddda06121ae927d)
1 /*
2  * Copyright 6WIND S.A., 2014
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or
5  * (at your option) any later version.  See the COPYING file in the
6  * top-level directory.
7  */
8 
9 #include "qemu-common.h"
10 
11 #include "ivshmem-server.h"
12 
13 #define IVSHMEM_SERVER_DEFAULT_VERBOSE        0
14 #define IVSHMEM_SERVER_DEFAULT_FOREGROUND     0
15 #define IVSHMEM_SERVER_DEFAULT_PID_FILE       "/var/run/ivshmem-server.pid"
16 #define IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket"
17 #define IVSHMEM_SERVER_DEFAULT_SHM_PATH       "ivshmem"
18 #define IVSHMEM_SERVER_DEFAULT_SHM_SIZE       (4*1024*1024)
19 #define IVSHMEM_SERVER_DEFAULT_N_VECTORS      1
20 
21 /* used to quit on signal SIGTERM */
22 static int ivshmem_server_quit;
23 
24 /* arguments given by the user */
25 typedef struct IvshmemServerArgs {
26     bool verbose;
27     bool foreground;
28     const char *pid_file;
29     const char *unix_socket_path;
30     const char *shm_path;
31     uint64_t shm_size;
32     unsigned n_vectors;
33 } IvshmemServerArgs;
34 
35 /* show ivshmem_server_usage and exit with given error code */
36 static void
37 ivshmem_server_usage(const char *name, int code)
38 {
39     fprintf(stderr, "%s [opts]\n", name);
40     fprintf(stderr, "  -h: show this help\n");
41     fprintf(stderr, "  -v: verbose mode\n");
42     fprintf(stderr, "  -F: foreground mode (default is to daemonize)\n");
43     fprintf(stderr, "  -p <pid_file>: path to the PID file (used in daemon\n"
44                     "     mode only).\n"
45                     "     Default=%s\n", IVSHMEM_SERVER_DEFAULT_SHM_PATH);
46     fprintf(stderr, "  -S <unix_socket_path>: path to the unix socket\n"
47                     "     to listen to.\n"
48                     "     Default=%s\n", IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH);
49     fprintf(stderr, "  -m <shm_path>: path to the shared memory.\n"
50                     "     The path corresponds to a POSIX shm name. To use a\n"
51                     "     real file, for instance in a hugetlbfs, use\n"
52                     "     /../../abspath/to/file.\n"
53                     "     default=%s\n", IVSHMEM_SERVER_DEFAULT_SHM_PATH);
54     fprintf(stderr, "  -l <size>: size of shared memory in bytes. The suffix\n"
55                     "     K, M and G can be used (ex: 1K means 1024).\n"
56                     "     default=%u\n", IVSHMEM_SERVER_DEFAULT_SHM_SIZE);
57     fprintf(stderr, "  -n <n_vects>: number of vectors.\n"
58                     "     default=%u\n", IVSHMEM_SERVER_DEFAULT_N_VECTORS);
59 
60     exit(code);
61 }
62 
63 /* parse the program arguments, exit on error */
64 static void
65 ivshmem_server_parse_args(IvshmemServerArgs *args, int argc, char *argv[])
66 {
67     int c;
68     unsigned long long v;
69     Error *errp = NULL;
70 
71     while ((c = getopt(argc, argv,
72                        "h"  /* help */
73                        "v"  /* verbose */
74                        "F"  /* foreground */
75                        "p:" /* pid_file */
76                        "S:" /* unix_socket_path */
77                        "m:" /* shm_path */
78                        "l:" /* shm_size */
79                        "n:" /* n_vectors */
80                       )) != -1) {
81 
82         switch (c) {
83         case 'h': /* help */
84             ivshmem_server_usage(argv[0], 0);
85             break;
86 
87         case 'v': /* verbose */
88             args->verbose = 1;
89             break;
90 
91         case 'F': /* foreground */
92             args->foreground = 1;
93             break;
94 
95         case 'p': /* pid_file */
96             args->pid_file = strdup(optarg);
97             break;
98 
99         case 'S': /* unix_socket_path */
100             args->unix_socket_path = strdup(optarg);
101             break;
102 
103         case 'm': /* shm_path */
104             args->shm_path = strdup(optarg);
105             break;
106 
107         case 'l': /* shm_size */
108             parse_option_size("shm_size", optarg, &args->shm_size, &errp);
109             if (errp) {
110                 fprintf(stderr, "cannot parse shm size: %s\n",
111                         error_get_pretty(errp));
112                 error_free(errp);
113                 ivshmem_server_usage(argv[0], 1);
114             }
115             break;
116 
117         case 'n': /* n_vectors */
118             if (parse_uint_full(optarg, &v, 0) < 0) {
119                 fprintf(stderr, "cannot parse n_vectors\n");
120                 ivshmem_server_usage(argv[0], 1);
121             }
122             args->n_vectors = v;
123             break;
124 
125         default:
126             ivshmem_server_usage(argv[0], 1);
127             break;
128         }
129     }
130 
131     if (args->n_vectors > IVSHMEM_SERVER_MAX_VECTORS) {
132         fprintf(stderr, "too many requested vectors (max is %d)\n",
133                 IVSHMEM_SERVER_MAX_VECTORS);
134         ivshmem_server_usage(argv[0], 1);
135     }
136 
137     if (args->verbose == 1 && args->foreground == 0) {
138         fprintf(stderr, "cannot use verbose in daemon mode\n");
139         ivshmem_server_usage(argv[0], 1);
140     }
141 }
142 
143 /* wait for events on listening server unix socket and connected client
144  * sockets */
145 static int
146 ivshmem_server_poll_events(IvshmemServer *server)
147 {
148     fd_set fds;
149     int ret = 0, maxfd;
150 
151     while (!ivshmem_server_quit) {
152 
153         FD_ZERO(&fds);
154         maxfd = 0;
155         ivshmem_server_get_fds(server, &fds, &maxfd);
156 
157         ret = select(maxfd, &fds, NULL, NULL, NULL);
158 
159         if (ret < 0) {
160             if (errno == EINTR) {
161                 continue;
162             }
163 
164             fprintf(stderr, "select error: %s\n", strerror(errno));
165             break;
166         }
167         if (ret == 0) {
168             continue;
169         }
170 
171         if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
172             fprintf(stderr, "ivshmem_server_handle_fds() failed\n");
173             break;
174         }
175     }
176 
177     return ret;
178 }
179 
180 static void
181 ivshmem_server_quit_cb(int signum)
182 {
183     ivshmem_server_quit = 1;
184 }
185 
186 int
187 main(int argc, char *argv[])
188 {
189     IvshmemServer server;
190     struct sigaction sa, sa_quit;
191     IvshmemServerArgs args = {
192         .verbose = IVSHMEM_SERVER_DEFAULT_VERBOSE,
193         .foreground = IVSHMEM_SERVER_DEFAULT_FOREGROUND,
194         .pid_file = IVSHMEM_SERVER_DEFAULT_PID_FILE,
195         .unix_socket_path = IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH,
196         .shm_path = IVSHMEM_SERVER_DEFAULT_SHM_PATH,
197         .shm_size = IVSHMEM_SERVER_DEFAULT_SHM_SIZE,
198         .n_vectors = IVSHMEM_SERVER_DEFAULT_N_VECTORS,
199     };
200     int ret = 1;
201 
202     /* parse arguments, will exit on error */
203     ivshmem_server_parse_args(&args, argc, argv);
204 
205     /* Ignore SIGPIPE, see this link for more info:
206      * http://www.mail-archive.com/libevent-users@monkey.org/msg01606.html */
207     sa.sa_handler = SIG_IGN;
208     sa.sa_flags = 0;
209     if (sigemptyset(&sa.sa_mask) == -1 ||
210         sigaction(SIGPIPE, &sa, 0) == -1) {
211         perror("failed to ignore SIGPIPE; sigaction");
212         goto err;
213     }
214 
215     sa_quit.sa_handler = ivshmem_server_quit_cb;
216     sa_quit.sa_flags = 0;
217     if (sigemptyset(&sa_quit.sa_mask) == -1 ||
218         sigaction(SIGTERM, &sa_quit, 0) == -1) {
219         perror("failed to add SIGTERM handler; sigaction");
220         goto err;
221     }
222 
223     /* init the ivshms structure */
224     if (ivshmem_server_init(&server, args.unix_socket_path, args.shm_path,
225                             args.shm_size, args.n_vectors, args.verbose) < 0) {
226         fprintf(stderr, "cannot init server\n");
227         goto err;
228     }
229 
230     /* start the ivshmem server (open shm & unix socket) */
231     if (ivshmem_server_start(&server) < 0) {
232         fprintf(stderr, "cannot bind\n");
233         goto err;
234     }
235 
236     /* daemonize if asked to */
237     if (!args.foreground) {
238         FILE *fp;
239 
240         if (qemu_daemon(1, 1) < 0) {
241             fprintf(stderr, "cannot daemonize: %s\n", strerror(errno));
242             goto err_close;
243         }
244 
245         /* write pid file */
246         fp = fopen(args.pid_file, "w");
247         if (fp == NULL) {
248             fprintf(stderr, "cannot write pid file: %s\n", strerror(errno));
249             goto err_close;
250         }
251 
252         fprintf(fp, "%d\n", (int) getpid());
253         fclose(fp);
254     }
255 
256     ivshmem_server_poll_events(&server);
257     fprintf(stdout, "server disconnected\n");
258     ret = 0;
259 
260 err_close:
261     ivshmem_server_close(&server);
262 err:
263     return ret;
264 }
265