xref: /qemu/contrib/ivshmem-server/main.c (revision 7a4e543d)
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/osdep.h"
10 #include "qemu-common.h"
11 
12 #include "ivshmem-server.h"
13 
14 #define IVSHMEM_SERVER_DEFAULT_VERBOSE        0
15 #define IVSHMEM_SERVER_DEFAULT_FOREGROUND     0
16 #define IVSHMEM_SERVER_DEFAULT_PID_FILE       "/var/run/ivshmem-server.pid"
17 #define IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket"
18 #define IVSHMEM_SERVER_DEFAULT_SHM_PATH       "ivshmem"
19 #define IVSHMEM_SERVER_DEFAULT_SHM_SIZE       (4*1024*1024)
20 #define IVSHMEM_SERVER_DEFAULT_N_VECTORS      1
21 
22 /* used to quit on signal SIGTERM */
23 static int ivshmem_server_quit;
24 
25 /* arguments given by the user */
26 typedef struct IvshmemServerArgs {
27     bool verbose;
28     bool foreground;
29     const char *pid_file;
30     const char *unix_socket_path;
31     const char *shm_path;
32     uint64_t shm_size;
33     unsigned n_vectors;
34 } IvshmemServerArgs;
35 
36 /* show ivshmem_server_usage and exit with given error code */
37 static void
38 ivshmem_server_usage(const char *name, int code)
39 {
40     fprintf(stderr, "%s [opts]\n", name);
41     fprintf(stderr, "  -h: show this help\n");
42     fprintf(stderr, "  -v: verbose mode\n");
43     fprintf(stderr, "  -F: foreground mode (default is to daemonize)\n");
44     fprintf(stderr, "  -p <pid_file>: path to the PID file (used in daemon\n"
45                     "     mode only).\n"
46                     "     Default=%s\n", IVSHMEM_SERVER_DEFAULT_SHM_PATH);
47     fprintf(stderr, "  -S <unix_socket_path>: path to the unix socket\n"
48                     "     to listen to.\n"
49                     "     Default=%s\n", IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH);
50     fprintf(stderr, "  -m <shm_path>: path to the shared memory.\n"
51                     "     The path corresponds to a POSIX shm name or a\n"
52                     "     hugetlbfs mount point.\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 *err = 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 = optarg;
97             break;
98 
99         case 'S': /* unix_socket_path */
100             args->unix_socket_path = optarg;
101             break;
102 
103         case 'm': /* shm_path */
104             args->shm_path = optarg;
105             break;
106 
107         case 'l': /* shm_size */
108             parse_option_size("shm_size", optarg, &args->shm_size, &err);
109             if (err) {
110                 error_report_err(err);
111                 ivshmem_server_usage(argv[0], 1);
112             }
113             break;
114 
115         case 'n': /* n_vectors */
116             if (parse_uint_full(optarg, &v, 0) < 0) {
117                 fprintf(stderr, "cannot parse n_vectors\n");
118                 ivshmem_server_usage(argv[0], 1);
119             }
120             args->n_vectors = v;
121             break;
122 
123         default:
124             ivshmem_server_usage(argv[0], 1);
125             break;
126         }
127     }
128 
129     if (args->n_vectors > IVSHMEM_SERVER_MAX_VECTORS) {
130         fprintf(stderr, "too many requested vectors (max is %d)\n",
131                 IVSHMEM_SERVER_MAX_VECTORS);
132         ivshmem_server_usage(argv[0], 1);
133     }
134 
135     if (args->verbose == 1 && args->foreground == 0) {
136         fprintf(stderr, "cannot use verbose in daemon mode\n");
137         ivshmem_server_usage(argv[0], 1);
138     }
139 }
140 
141 /* wait for events on listening server unix socket and connected client
142  * sockets */
143 static int
144 ivshmem_server_poll_events(IvshmemServer *server)
145 {
146     fd_set fds;
147     int ret = 0, maxfd;
148 
149     while (!ivshmem_server_quit) {
150 
151         FD_ZERO(&fds);
152         maxfd = 0;
153         ivshmem_server_get_fds(server, &fds, &maxfd);
154 
155         ret = select(maxfd, &fds, NULL, NULL, NULL);
156 
157         if (ret < 0) {
158             if (errno == EINTR) {
159                 continue;
160             }
161 
162             fprintf(stderr, "select error: %s\n", strerror(errno));
163             break;
164         }
165         if (ret == 0) {
166             continue;
167         }
168 
169         if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
170             fprintf(stderr, "ivshmem_server_handle_fds() failed\n");
171             break;
172         }
173     }
174 
175     return ret;
176 }
177 
178 static void
179 ivshmem_server_quit_cb(int signum)
180 {
181     ivshmem_server_quit = 1;
182 }
183 
184 int
185 main(int argc, char *argv[])
186 {
187     IvshmemServer server;
188     struct sigaction sa, sa_quit;
189     IvshmemServerArgs args = {
190         .verbose = IVSHMEM_SERVER_DEFAULT_VERBOSE,
191         .foreground = IVSHMEM_SERVER_DEFAULT_FOREGROUND,
192         .pid_file = IVSHMEM_SERVER_DEFAULT_PID_FILE,
193         .unix_socket_path = IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH,
194         .shm_path = IVSHMEM_SERVER_DEFAULT_SHM_PATH,
195         .shm_size = IVSHMEM_SERVER_DEFAULT_SHM_SIZE,
196         .n_vectors = IVSHMEM_SERVER_DEFAULT_N_VECTORS,
197     };
198     int ret = 1;
199 
200     /* parse arguments, will exit on error */
201     ivshmem_server_parse_args(&args, argc, argv);
202 
203     /* Ignore SIGPIPE, see this link for more info:
204      * http://www.mail-archive.com/libevent-users@monkey.org/msg01606.html */
205     sa.sa_handler = SIG_IGN;
206     sa.sa_flags = 0;
207     if (sigemptyset(&sa.sa_mask) == -1 ||
208         sigaction(SIGPIPE, &sa, 0) == -1) {
209         perror("failed to ignore SIGPIPE; sigaction");
210         goto err;
211     }
212 
213     sa_quit.sa_handler = ivshmem_server_quit_cb;
214     sa_quit.sa_flags = 0;
215     if (sigemptyset(&sa_quit.sa_mask) == -1 ||
216         sigaction(SIGTERM, &sa_quit, 0) == -1) {
217         perror("failed to add SIGTERM handler; sigaction");
218         goto err;
219     }
220 
221     /* init the ivshms structure */
222     if (ivshmem_server_init(&server, args.unix_socket_path, args.shm_path,
223                             args.shm_size, args.n_vectors, args.verbose) < 0) {
224         fprintf(stderr, "cannot init server\n");
225         goto err;
226     }
227 
228     /* start the ivshmem server (open shm & unix socket) */
229     if (ivshmem_server_start(&server) < 0) {
230         fprintf(stderr, "cannot bind\n");
231         goto err;
232     }
233 
234     /* daemonize if asked to */
235     if (!args.foreground) {
236         FILE *fp;
237 
238         if (qemu_daemon(1, 1) < 0) {
239             fprintf(stderr, "cannot daemonize: %s\n", strerror(errno));
240             goto err_close;
241         }
242 
243         /* write pid file */
244         fp = fopen(args.pid_file, "w");
245         if (fp == NULL) {
246             fprintf(stderr, "cannot write pid file: %s\n", strerror(errno));
247             goto err_close;
248         }
249 
250         fprintf(fp, "%d\n", (int) getpid());
251         fclose(fp);
252     }
253 
254     ivshmem_server_poll_events(&server);
255     fprintf(stdout, "server disconnected\n");
256     ret = 0;
257 
258 err_close:
259     ivshmem_server_close(&server);
260 err:
261     return ret;
262 }
263