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