]>
Commit | Line | Data |
---|---|---|
649dbf42 | 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
4c17b2e3 | 2 | /* |
649dbf42 | 3 | Copyright (C) 2018 - 2021 Proxmox Server Solutions GmbH |
4c17b2e3 DC |
4 | |
5 | Author: Dominik Csapak <d.csapak@proxmox.com> | |
aedf8208 | 6 | Author: Stefan Reiter <s.reiter@proxmox.com> |
4c17b2e3 | 7 | |
aedf8208 | 8 | Description: |
4c17b2e3 | 9 | |
aedf8208 TL |
10 | qmeventd listens on a given socket, and waits for qemu processes to |
11 | connect. After accepting a connection qmeventd waits for shutdown events | |
12 | followed by the closing of the socket. Once that happens `qm cleanup` will | |
13 | be executed with following three arguments: | |
14 | VMID <graceful> <guest> | |
15 | Where `graceful` can be `1` or `0` depending if shutdown event was observed | |
16 | before the socket got closed. The second parameter `guest` is also boolean | |
17 | `1` or `0` depending if the shutdown was requested from the guest OS | |
18 | (i.e., the "inside"). | |
4c17b2e3 DC |
19 | */ |
20 | ||
21 | #ifndef _GNU_SOURCE | |
22 | #define _GNU_SOURCE | |
23 | #endif | |
24 | ||
25 | #include <errno.h> | |
26 | #include <fcntl.h> | |
aedf8208 | 27 | #include <gmodule.h> |
4c17b2e3 DC |
28 | #include <json.h> |
29 | #include <signal.h> | |
30 | #include <stdbool.h> | |
31 | #include <stdio.h> | |
0a1641ae | 32 | #include <stdlib.h> |
4c17b2e3 DC |
33 | #include <string.h> |
34 | #include <sys/epoll.h> | |
35 | #include <sys/socket.h> | |
36 | #include <sys/types.h> | |
37 | #include <sys/un.h> | |
38 | #include <sys/wait.h> | |
39 | #include <unistd.h> | |
0a1641ae | 40 | #include <time.h> |
4c17b2e3 DC |
41 | |
42 | #include "qmeventd.h" | |
43 | ||
0a1641ae DC |
44 | #define DEFAULT_KILL_TIMEOUT 60 |
45 | ||
4c17b2e3 | 46 | static int verbose = 0; |
0a1641ae | 47 | static int kill_timeout = DEFAULT_KILL_TIMEOUT; |
4c17b2e3 DC |
48 | static int epoll_fd = 0; |
49 | static const char *progname; | |
3ff85001 | 50 | GHashTable *vm_clients; // key=vmid (freed on remove), value=*Client (free manually) |
4c500f16 | 51 | GSList *forced_cleanups; |
0a1641ae | 52 | static int needs_cleanup = 0; |
3ff85001 | 53 | |
4c17b2e3 DC |
54 | /* |
55 | * Helper functions | |
56 | */ | |
57 | ||
58 | static void | |
59 | usage() | |
60 | { | |
61 | fprintf(stderr, "Usage: %s [-f] [-v] PATH\n", progname); | |
62 | fprintf(stderr, " -f run in foreground (default: false)\n"); | |
63 | fprintf(stderr, " -v verbose (default: false)\n"); | |
0a1641ae | 64 | fprintf(stderr, " -t <s> kill timeout (default: %ds)\n", DEFAULT_KILL_TIMEOUT); |
4c17b2e3 DC |
65 | fprintf(stderr, " PATH use PATH for socket\n"); |
66 | } | |
67 | ||
68 | static pid_t | |
69 | get_pid_from_fd(int fd) | |
70 | { | |
71 | struct ucred credentials = { .pid = 0, .uid = 0, .gid = 0 }; | |
72 | socklen_t len = sizeof(struct ucred); | |
73 | log_neg(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &credentials, &len), "getsockopt"); | |
74 | return credentials.pid; | |
75 | } | |
76 | ||
77 | /* | |
502870a0 | 78 | * parses the vmid from the qemu.slice entry of /proc/<pid>/cgroup |
4c17b2e3 DC |
79 | */ |
80 | static unsigned long | |
81 | get_vmid_from_pid(pid_t pid) | |
82 | { | |
83 | char filename[32] = { 0 }; | |
502870a0 | 84 | int len = snprintf(filename, sizeof(filename), "/proc/%d/cgroup", pid); |
4c17b2e3 DC |
85 | if (len < 0) { |
86 | fprintf(stderr, "error during snprintf for %d: %s\n", pid, | |
87 | strerror(errno)); | |
88 | return 0; | |
89 | } | |
90 | if ((size_t)len >= sizeof(filename)) { | |
91 | fprintf(stderr, "error: pid %d too long\n", pid); | |
92 | return 0; | |
93 | } | |
94 | FILE *fp = fopen(filename, "re"); | |
95 | if (fp == NULL) { | |
96 | fprintf(stderr, "error opening %s: %s\n", filename, strerror(errno)); | |
97 | return 0; | |
98 | } | |
99 | ||
100 | unsigned long vmid = 0; | |
4c17b2e3 DC |
101 | char *buf = NULL; |
102 | size_t buflen = 0; | |
502870a0 FE |
103 | |
104 | while (getline(&buf, &buflen, fp) >= 0) { | |
105 | char *cgroup_path = strrchr(buf, ':'); | |
106 | if (!cgroup_path) { | |
107 | fprintf(stderr, "unexpected cgroup entry %s\n", buf); | |
aaa77c9f | 108 | continue; |
4c17b2e3 | 109 | } |
502870a0 | 110 | cgroup_path++; |
4c17b2e3 | 111 | |
d0b58753 | 112 | if (strncmp(cgroup_path, "/qemu.slice/", 12)) { |
502870a0 FE |
113 | continue; |
114 | } | |
4c17b2e3 | 115 | |
502870a0 FE |
116 | char *vmid_start = strrchr(buf, '/'); |
117 | if (!vmid_start) { | |
118 | fprintf(stderr, "unexpected cgroup entry %s\n", buf); | |
aaa77c9f | 119 | continue; |
502870a0 FE |
120 | } |
121 | vmid_start++; | |
122 | ||
123 | if (vmid_start[0] == '-' || vmid_start[0] == '\0') { | |
124 | fprintf(stderr, "invalid vmid in cgroup entry %s\n", buf); | |
aaa77c9f | 125 | continue; |
4c17b2e3 DC |
126 | } |
127 | ||
128 | errno = 0; | |
129 | char *endptr = NULL; | |
502870a0 | 130 | vmid = strtoul(vmid_start, &endptr, 10); |
a9f2e2d6 FE |
131 | if (!endptr || strncmp(endptr, ".scope", 6)) { |
132 | fprintf(stderr, "unexpected cgroup entry %s\n", buf); | |
133 | vmid = 0; | |
134 | continue; | |
135 | } | |
4c17b2e3 DC |
136 | if (errno != 0) { |
137 | vmid = 0; | |
4c17b2e3 DC |
138 | } |
139 | ||
742da46a | 140 | break; |
4c17b2e3 DC |
141 | } |
142 | ||
502870a0 FE |
143 | if (errno) { |
144 | fprintf(stderr, "error parsing vmid for %d: %s\n", pid, strerror(errno)); | |
742da46a | 145 | } else if (!vmid) { |
d0b58753 | 146 | fprintf(stderr, "error parsing vmid for %d: no matching qemu.slice cgroup entry\n", pid); |
502870a0 | 147 | } |
4c17b2e3 | 148 | |
4c17b2e3 DC |
149 | free(buf); |
150 | fclose(fp); | |
151 | return vmid; | |
152 | } | |
153 | ||
154 | static bool | |
155 | must_write(int fd, const char *buf, size_t len) | |
156 | { | |
157 | ssize_t wlen; | |
158 | do { | |
159 | wlen = write(fd, buf, len); | |
160 | } while (wlen < 0 && errno == EINTR); | |
161 | ||
162 | return (wlen == (ssize_t)len); | |
163 | } | |
164 | ||
165 | /* | |
166 | * qmp handling functions | |
167 | */ | |
168 | ||
3ff85001 SR |
169 | static void |
170 | send_qmp_cmd(struct Client *client, const char *buf, size_t len) | |
171 | { | |
172 | if (!must_write(client->fd, buf, len - 1)) { | |
173 | fprintf(stderr, "%s: cannot send QMP message\n", client->qemu.vmid); | |
174 | cleanup_client(client); | |
175 | } | |
176 | } | |
177 | ||
4c17b2e3 DC |
178 | void |
179 | handle_qmp_handshake(struct Client *client) | |
180 | { | |
3ff85001 SR |
181 | VERBOSE_PRINT("pid%d: got QMP handshake, assuming QEMU client\n", client->pid); |
182 | ||
183 | // extract vmid from cmdline, now that we know it's a QEMU process | |
184 | unsigned long vmid = get_vmid_from_pid(client->pid); | |
185 | int res = snprintf(client->qemu.vmid, sizeof(client->qemu.vmid), "%lu", vmid); | |
186 | if (vmid == 0 || res < 0 || res >= (int)sizeof(client->qemu.vmid)) { | |
187 | fprintf(stderr, "could not get vmid from pid %d\n", client->pid); | |
4c17b2e3 | 188 | cleanup_client(client); |
3ff85001 SR |
189 | return; |
190 | } | |
191 | ||
192 | VERBOSE_PRINT("pid%d: assigned VMID: %s\n", client->pid, client->qemu.vmid); | |
193 | client->type = CLIENT_QEMU; | |
194 | if(!g_hash_table_insert(vm_clients, strdup(client->qemu.vmid), client)) { | |
195 | // not fatal, just means backup handling won't work | |
196 | fprintf(stderr, "%s: could not insert client into VMID->client table\n", | |
197 | client->qemu.vmid); | |
4c17b2e3 | 198 | } |
3ff85001 SR |
199 | |
200 | static const char qmp_answer[] = "{\"execute\":\"qmp_capabilities\"}\n"; | |
201 | send_qmp_cmd(client, qmp_answer, sizeof(qmp_answer)); | |
4c17b2e3 DC |
202 | } |
203 | ||
204 | void | |
205 | handle_qmp_event(struct Client *client, struct json_object *obj) | |
206 | { | |
207 | struct json_object *event; | |
208 | if (!json_object_object_get_ex(obj, "event", &event)) { | |
209 | return; | |
210 | } | |
a2488e4c | 211 | VERBOSE_PRINT("%s: got QMP event: %s\n", client->qemu.vmid, json_object_get_string(event)); |
3ff85001 SR |
212 | |
213 | if (client->state == STATE_TERMINATING) { | |
214 | // QEMU sometimes sends a second SHUTDOWN after SIGTERM, ignore | |
a2488e4c | 215 | VERBOSE_PRINT("%s: event was after termination, ignoring\n", client->qemu.vmid); |
3ff85001 SR |
216 | return; |
217 | } | |
218 | ||
4c17b2e3 DC |
219 | // event, check if shutdown and get guest parameter |
220 | if (!strcmp(json_object_get_string(event), "SHUTDOWN")) { | |
3ff85001 | 221 | client->qemu.graceful = 1; |
4c17b2e3 DC |
222 | struct json_object *data; |
223 | struct json_object *guest; | |
224 | if (json_object_object_get_ex(obj, "data", &data) && | |
225 | json_object_object_get_ex(data, "guest", &guest)) | |
226 | { | |
3ff85001 | 227 | client->qemu.guest = (unsigned short)json_object_get_boolean(guest); |
4c17b2e3 | 228 | } |
3ff85001 SR |
229 | |
230 | // check if a backup is running and kill QEMU process if not | |
231 | terminate_check(client); | |
232 | } | |
233 | } | |
234 | ||
235 | void | |
236 | terminate_check(struct Client *client) | |
237 | { | |
238 | if (client->state != STATE_IDLE) { | |
239 | // if we're already in a request, queue this one until after | |
240 | VERBOSE_PRINT("%s: terminate_check queued\n", client->qemu.vmid); | |
241 | client->qemu.term_check_queued = true; | |
242 | return; | |
243 | } | |
244 | ||
245 | client->qemu.term_check_queued = false; | |
246 | ||
247 | VERBOSE_PRINT("%s: query-status\n", client->qemu.vmid); | |
248 | client->state = STATE_EXPECT_STATUS_RESP; | |
249 | static const char qmp_req[] = "{\"execute\":\"query-status\"}\n"; | |
250 | send_qmp_cmd(client, qmp_req, sizeof(qmp_req)); | |
251 | } | |
252 | ||
253 | void | |
254 | handle_qmp_return(struct Client *client, struct json_object *data, bool error) | |
255 | { | |
256 | if (error) { | |
257 | const char *msg = "n/a"; | |
258 | struct json_object *desc; | |
259 | if (json_object_object_get_ex(data, "desc", &desc)) { | |
260 | msg = json_object_get_string(desc); | |
261 | } | |
262 | fprintf(stderr, "%s: received error from QMP: %s\n", | |
263 | client->qemu.vmid, msg); | |
264 | client->state = STATE_IDLE; | |
265 | goto out; | |
266 | } | |
267 | ||
268 | struct json_object *status; | |
269 | json_bool has_status = data && | |
270 | json_object_object_get_ex(data, "status", &status); | |
271 | ||
272 | bool active = false; | |
273 | if (has_status) { | |
274 | const char *status_str = json_object_get_string(status); | |
275 | active = status_str && | |
276 | (!strcmp(status_str, "running") || !strcmp(status_str, "paused")); | |
277 | } | |
278 | ||
279 | switch (client->state) { | |
280 | case STATE_EXPECT_STATUS_RESP: | |
281 | client->state = STATE_IDLE; | |
282 | if (active) { | |
283 | VERBOSE_PRINT("%s: got status: VM is active\n", client->qemu.vmid); | |
284 | } else if (!client->qemu.backup) { | |
285 | terminate_client(client); | |
286 | } else { | |
287 | // if we're in a backup, don't do anything, vzdump will notify | |
288 | // us when the backup finishes | |
289 | VERBOSE_PRINT("%s: not active, but running backup - keep alive\n", | |
290 | client->qemu.vmid); | |
291 | } | |
292 | break; | |
293 | ||
294 | // this means we received the empty return from our handshake answer | |
295 | case STATE_HANDSHAKE: | |
296 | client->state = STATE_IDLE; | |
297 | VERBOSE_PRINT("%s: QMP handshake complete\n", client->qemu.vmid); | |
298 | break; | |
299 | ||
b5c39b22 | 300 | // we expect an empty return object after sending quit |
3ff85001 | 301 | case STATE_TERMINATING: |
b5c39b22 DC |
302 | break; |
303 | case STATE_IDLE: | |
3ff85001 SR |
304 | VERBOSE_PRINT("%s: spurious return value received\n", |
305 | client->qemu.vmid); | |
306 | break; | |
307 | } | |
308 | ||
309 | out: | |
310 | if (client->qemu.term_check_queued) { | |
311 | terminate_check(client); | |
312 | } | |
313 | } | |
314 | ||
315 | /* | |
316 | * VZDump specific client functions | |
317 | */ | |
318 | ||
319 | void | |
320 | handle_vzdump_handshake(struct Client *client, struct json_object *data) | |
321 | { | |
322 | client->state = STATE_IDLE; | |
323 | ||
324 | struct json_object *vmid_obj; | |
325 | json_bool has_vmid = data && json_object_object_get_ex(data, "vmid", &vmid_obj); | |
326 | ||
327 | if (!has_vmid) { | |
42edf948 | 328 | VERBOSE_PRINT("pid%d: invalid vzdump handshake: no vmid\n", client->pid); |
3ff85001 SR |
329 | return; |
330 | } | |
331 | ||
332 | const char *vmid_str = json_object_get_string(vmid_obj); | |
333 | ||
334 | if (!vmid_str) { | |
42edf948 | 335 | VERBOSE_PRINT("pid%d: invalid vzdump handshake: vmid is not a string\n", client->pid); |
3ff85001 SR |
336 | return; |
337 | } | |
338 | ||
339 | int res = snprintf(client->vzdump.vmid, sizeof(client->vzdump.vmid), "%s", vmid_str); | |
340 | if (res < 0 || res >= (int)sizeof(client->vzdump.vmid)) { | |
42edf948 | 341 | VERBOSE_PRINT("pid%d: invalid vzdump handshake: vmid too long or invalid\n", client->pid); |
3ff85001 SR |
342 | return; |
343 | } | |
344 | ||
42edf948 | 345 | struct Client *vmc = (struct Client*) g_hash_table_lookup(vm_clients, client->vzdump.vmid); |
3ff85001 SR |
346 | if (vmc) { |
347 | vmc->qemu.backup = true; | |
348 | ||
349 | // only mark as VZDUMP once we have set everything up, otherwise 'cleanup' | |
350 | // might try to access an invalid value | |
351 | client->type = CLIENT_VZDUMP; | |
42edf948 | 352 | VERBOSE_PRINT("%s: vzdump backup started\n", client->vzdump.vmid); |
3ff85001 | 353 | } else { |
42edf948 | 354 | VERBOSE_PRINT("%s: vzdump requested backup start for unregistered VM\n", client->vzdump.vmid); |
4c17b2e3 DC |
355 | } |
356 | } | |
357 | ||
358 | /* | |
359 | * client management functions | |
360 | */ | |
361 | ||
362 | void | |
363 | add_new_client(int client_fd) | |
364 | { | |
365 | struct Client *client = calloc(sizeof(struct Client), 1); | |
6d4f89b6 TL |
366 | if (client == NULL) { |
367 | fprintf(stderr, "could not add new client - allocation failed!\n"); | |
368 | fflush(stderr); | |
369 | return; | |
370 | } | |
3ff85001 SR |
371 | client->state = STATE_HANDSHAKE; |
372 | client->type = CLIENT_NONE; | |
4c17b2e3 DC |
373 | client->fd = client_fd; |
374 | client->pid = get_pid_from_fd(client_fd); | |
375 | if (client->pid == 0) { | |
376 | fprintf(stderr, "could not get pid from client\n"); | |
377 | goto err; | |
378 | } | |
4c17b2e3 DC |
379 | |
380 | struct epoll_event ev; | |
381 | ev.events = EPOLLIN; | |
382 | ev.data.ptr = client; | |
3ff85001 | 383 | int res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev); |
4c17b2e3 DC |
384 | if (res < 0) { |
385 | perror("epoll_ctl client add"); | |
386 | goto err; | |
387 | } | |
388 | ||
3ff85001 | 389 | VERBOSE_PRINT("added new client, pid: %d\n", client->pid); |
4c17b2e3 DC |
390 | |
391 | return; | |
392 | err: | |
393 | (void)close(client_fd); | |
394 | free(client); | |
395 | } | |
396 | ||
3ff85001 SR |
397 | static void |
398 | cleanup_qemu_client(struct Client *client) | |
4c17b2e3 | 399 | { |
3ff85001 SR |
400 | unsigned short graceful = client->qemu.graceful; |
401 | unsigned short guest = client->qemu.guest; | |
402 | char vmid[sizeof(client->qemu.vmid)]; | |
403 | strncpy(vmid, client->qemu.vmid, sizeof(vmid)); | |
404 | g_hash_table_remove(vm_clients, &vmid); // frees key, ignore errors | |
405 | VERBOSE_PRINT("%s: executing cleanup (graceful: %d, guest: %d)\n", | |
406 | vmid, graceful, guest); | |
4c17b2e3 DC |
407 | |
408 | int pid = fork(); | |
409 | if (pid < 0) { | |
410 | fprintf(stderr, "fork failed: %s\n", strerror(errno)); | |
411 | return; | |
412 | } | |
413 | if (pid == 0) { | |
414 | char *script = "/usr/sbin/qm"; | |
415 | ||
416 | char *args[] = { | |
417 | script, | |
418 | "cleanup", | |
419 | vmid, | |
420 | graceful ? "1" : "0", | |
421 | guest ? "1" : "0", | |
422 | NULL | |
423 | }; | |
424 | ||
425 | execvp(script, args); | |
426 | perror("execvp"); | |
427 | _exit(1); | |
428 | } | |
429 | } | |
430 | ||
3ff85001 SR |
431 | void |
432 | cleanup_client(struct Client *client) | |
433 | { | |
434 | log_neg(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->fd, NULL), "epoll del"); | |
435 | (void)close(client->fd); | |
436 | ||
437 | struct Client *vmc; | |
438 | switch (client->type) { | |
439 | case CLIENT_QEMU: | |
440 | cleanup_qemu_client(client); | |
441 | break; | |
442 | ||
443 | case CLIENT_VZDUMP: | |
444 | vmc = (struct Client*) g_hash_table_lookup(vm_clients, client->vzdump.vmid); | |
445 | if (vmc) { | |
446 | VERBOSE_PRINT("%s: backup ended\n", client->vzdump.vmid); | |
447 | vmc->qemu.backup = false; | |
448 | terminate_check(vmc); | |
449 | } | |
450 | break; | |
451 | ||
452 | case CLIENT_NONE: | |
453 | // do nothing, only close socket | |
454 | break; | |
455 | } | |
456 | ||
0a1641ae DC |
457 | if (client->pidfd > 0) { |
458 | (void)close(client->pidfd); | |
459 | } | |
460 | VERBOSE_PRINT("removing %s from forced cleanups\n", client->qemu.vmid); | |
461 | forced_cleanups = g_slist_remove(forced_cleanups, client); | |
3ff85001 SR |
462 | free(client); |
463 | } | |
464 | ||
465 | void | |
466 | terminate_client(struct Client *client) | |
467 | { | |
42edf948 | 468 | VERBOSE_PRINT("%s: terminating client (pid %d)\n", client->qemu.vmid, client->pid); |
3ff85001 SR |
469 | |
470 | client->state = STATE_TERMINATING; | |
471 | ||
4c500f16 SR |
472 | // open a pidfd before kill for later cleanup |
473 | int pidfd = pidfd_open(client->pid, 0); | |
474 | if (pidfd < 0) { | |
475 | switch (errno) { | |
476 | case ESRCH: | |
477 | // process already dead for some reason, cleanup done | |
478 | VERBOSE_PRINT("%s: failed to open pidfd, process already dead (pid %d)\n", | |
479 | client->qemu.vmid, client->pid); | |
480 | return; | |
481 | ||
482 | // otherwise fall back to just using the PID directly, but don't | |
483 | // print if we only failed because we're running on an older kernel | |
484 | case ENOSYS: | |
485 | break; | |
486 | default: | |
487 | perror("failed to open QEMU pidfd for cleanup"); | |
488 | break; | |
489 | } | |
490 | } | |
491 | ||
b5c39b22 DC |
492 | // try to send a 'quit' command first, fallback to SIGTERM of the pid |
493 | static const char qmp_quit_command[] = "{\"execute\":\"quit\"}\n"; | |
494 | VERBOSE_PRINT("%s: sending 'quit' via QMP\n", client->qemu.vmid); | |
495 | if (!must_write(client->fd, qmp_quit_command, sizeof(qmp_quit_command) - 1)) { | |
496 | VERBOSE_PRINT("%s: sending 'SIGTERM' to pid %d\n", client->qemu.vmid, client->pid); | |
497 | int err = kill(client->pid, SIGTERM); | |
498 | log_neg(err, "kill"); | |
499 | } | |
4c500f16 | 500 | |
0a1641ae DC |
501 | time_t timeout = time(NULL) + kill_timeout; |
502 | ||
503 | client->pidfd = pidfd; | |
504 | client->timeout = timeout; | |
4c500f16 | 505 | |
0a1641ae DC |
506 | forced_cleanups = g_slist_prepend(forced_cleanups, (void *)client); |
507 | needs_cleanup = 1; | |
3ff85001 SR |
508 | } |
509 | ||
4c17b2e3 DC |
510 | void |
511 | handle_client(struct Client *client) | |
512 | { | |
3ff85001 | 513 | VERBOSE_PRINT("pid%d: entering handle\n", client->pid); |
4c17b2e3 DC |
514 | ssize_t len; |
515 | do { | |
516 | len = read(client->fd, (client->buf+client->buflen), | |
517 | sizeof(client->buf) - client->buflen); | |
518 | } while (len < 0 && errno == EINTR); | |
519 | ||
520 | if (len < 0) { | |
521 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { | |
522 | log_neg((int)len, "read"); | |
523 | cleanup_client(client); | |
524 | } | |
525 | return; | |
526 | } else if (len == 0) { | |
3ff85001 | 527 | VERBOSE_PRINT("pid%d: got EOF\n", client->pid); |
4c17b2e3 DC |
528 | cleanup_client(client); |
529 | return; | |
530 | } | |
531 | ||
3ff85001 | 532 | VERBOSE_PRINT("pid%d: read %ld bytes\n", client->pid, len); |
4c17b2e3 DC |
533 | client->buflen += len; |
534 | ||
535 | struct json_tokener *tok = json_tokener_new(); | |
536 | struct json_object *jobj = NULL; | |
537 | enum json_tokener_error jerr = json_tokener_success; | |
538 | while (jerr == json_tokener_success && client->buflen != 0) { | |
539 | jobj = json_tokener_parse_ex(tok, client->buf, (int)client->buflen); | |
540 | jerr = json_tokener_get_error(tok); | |
541 | unsigned int offset = (unsigned int)tok->char_offset; | |
542 | switch (jerr) { | |
543 | case json_tokener_success: | |
544 | // move rest from buffer to front | |
545 | memmove(client->buf, client->buf + offset, client->buflen - offset); | |
546 | client->buflen -= offset; | |
547 | if (json_object_is_type(jobj, json_type_object)) { | |
548 | struct json_object *obj; | |
549 | if (json_object_object_get_ex(jobj, "QMP", &obj)) { | |
550 | handle_qmp_handshake(client); | |
551 | } else if (json_object_object_get_ex(jobj, "event", &obj)) { | |
552 | handle_qmp_event(client, jobj); | |
3ff85001 SR |
553 | } else if (json_object_object_get_ex(jobj, "return", &obj)) { |
554 | handle_qmp_return(client, obj, false); | |
555 | } else if (json_object_object_get_ex(jobj, "error", &obj)) { | |
556 | handle_qmp_return(client, obj, true); | |
557 | } else if (json_object_object_get_ex(jobj, "vzdump", &obj)) { | |
558 | handle_vzdump_handshake(client, obj); | |
4c17b2e3 DC |
559 | } // else ignore message |
560 | } | |
561 | break; | |
562 | case json_tokener_continue: | |
563 | if (client->buflen >= sizeof(client->buf)) { | |
a2488e4c | 564 | VERBOSE_PRINT("pid%d: msg too large, discarding buffer\n", client->pid); |
4c17b2e3 DC |
565 | memset(client->buf, 0, sizeof(client->buf)); |
566 | client->buflen = 0; | |
567 | } // else we have enough space try again after next read | |
568 | break; | |
569 | default: | |
a2488e4c | 570 | VERBOSE_PRINT("pid%d: parse error: %d, discarding buffer\n", client->pid, jerr); |
4c17b2e3 DC |
571 | memset(client->buf, 0, client->buflen); |
572 | client->buflen = 0; | |
573 | break; | |
574 | } | |
575 | json_object_put(jobj); | |
576 | } | |
577 | json_tokener_free(tok); | |
578 | } | |
579 | ||
4c500f16 | 580 | static void |
0a1641ae | 581 | sigkill(void *ptr, void *time_ptr) |
4c500f16 | 582 | { |
0a1641ae | 583 | struct Client *data = ptr; |
4c500f16 SR |
584 | int err; |
585 | ||
0a1641ae DC |
586 | if (data->timeout != 0 && data->timeout > *(time_t *)time_ptr) { |
587 | return; | |
588 | } | |
589 | ||
590 | if (data->pidfd > 0) { | |
591 | err = pidfd_send_signal(data->pidfd, SIGKILL, NULL, 0); | |
592 | (void)close(data->pidfd); | |
593 | data->pidfd = -1; | |
4c500f16 | 594 | } else { |
0a1641ae | 595 | err = kill(data->pid, SIGKILL); |
4c500f16 SR |
596 | } |
597 | ||
598 | if (err < 0) { | |
599 | if (errno != ESRCH) { | |
600 | fprintf(stderr, "SIGKILL cleanup of pid '%d' failed - %s\n", | |
0a1641ae | 601 | data->pid, strerror(errno)); |
4c500f16 SR |
602 | } |
603 | } else { | |
604 | fprintf(stderr, "cleanup failed, terminating pid '%d' with SIGKILL\n", | |
0a1641ae | 605 | data->pid); |
4c500f16 | 606 | } |
0a1641ae DC |
607 | |
608 | data->timeout = 0; | |
609 | ||
610 | // remove ourselves from the list | |
611 | forced_cleanups = g_slist_remove(forced_cleanups, ptr); | |
4c500f16 SR |
612 | } |
613 | ||
614 | static void | |
615 | handle_forced_cleanup() | |
616 | { | |
0a1641ae | 617 | if (g_slist_length(forced_cleanups) > 0) { |
0a279963 | 618 | VERBOSE_PRINT("clearing forced cleanup backlog\n"); |
0a1641ae DC |
619 | time_t cur_time = time(NULL); |
620 | g_slist_foreach(forced_cleanups, sigkill, &cur_time); | |
4c500f16 | 621 | } |
0a1641ae | 622 | needs_cleanup = g_slist_length(forced_cleanups) > 0; |
4c500f16 SR |
623 | } |
624 | ||
4c17b2e3 DC |
625 | int |
626 | main(int argc, char *argv[]) | |
627 | { | |
628 | int opt; | |
629 | int daemonize = 1; | |
630 | char *socket_path = NULL; | |
631 | progname = argv[0]; | |
632 | ||
0a1641ae | 633 | while ((opt = getopt(argc, argv, "hfvt:")) != -1) { |
4c17b2e3 DC |
634 | switch (opt) { |
635 | case 'f': | |
636 | daemonize = 0; | |
637 | break; | |
638 | case 'v': | |
639 | verbose = 1; | |
640 | break; | |
0a1641ae DC |
641 | case 't': |
642 | errno = 0; | |
643 | char *endptr = NULL; | |
644 | kill_timeout = strtoul(optarg, &endptr, 10); | |
645 | if (errno != 0 || *endptr != '\0' || kill_timeout == 0) { | |
646 | usage(); | |
647 | exit(EXIT_FAILURE); | |
648 | } | |
649 | break; | |
4c17b2e3 DC |
650 | case 'h': |
651 | usage(); | |
652 | exit(EXIT_SUCCESS); | |
653 | break; | |
654 | default: | |
655 | usage(); | |
656 | exit(EXIT_FAILURE); | |
657 | } | |
658 | } | |
659 | ||
660 | if (optind >= argc) { | |
661 | usage(); | |
662 | exit(EXIT_FAILURE); | |
663 | } | |
664 | ||
665 | signal(SIGCHLD, SIG_IGN); | |
666 | ||
667 | socket_path = argv[optind]; | |
668 | ||
669 | int sock = socket(AF_UNIX, SOCK_STREAM, 0); | |
670 | bail_neg(sock, "socket"); | |
671 | ||
672 | struct sockaddr_un addr; | |
673 | memset(&addr, 0, sizeof(addr)); | |
674 | addr.sun_family = AF_UNIX; | |
675 | strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); | |
676 | ||
677 | unlink(socket_path); | |
678 | bail_neg(bind(sock, (struct sockaddr*)&addr, sizeof(addr)), "bind"); | |
679 | ||
680 | struct epoll_event ev, events[1]; | |
681 | epoll_fd = epoll_create1(EPOLL_CLOEXEC); | |
682 | bail_neg(epoll_fd, "epoll_create1"); | |
683 | ||
684 | ev.events = EPOLLIN; | |
685 | ev.data.fd = sock; | |
686 | bail_neg(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev), "epoll_ctl"); | |
687 | ||
688 | bail_neg(listen(sock, 10), "listen"); | |
689 | ||
690 | if (daemonize) { | |
691 | bail_neg(daemon(0, 1), "daemon"); | |
692 | } | |
693 | ||
3ff85001 SR |
694 | vm_clients = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); |
695 | ||
4c17b2e3 DC |
696 | int nevents; |
697 | ||
698 | for(;;) { | |
0a1641ae | 699 | nevents = epoll_wait(epoll_fd, events, 1, needs_cleanup ? 10*1000 : -1); |
2d7a026e FE |
700 | if (nevents < 0 && errno == EINTR) { |
701 | continue; | |
702 | } | |
4c17b2e3 DC |
703 | bail_neg(nevents, "epoll_wait"); |
704 | ||
705 | for (int n = 0; n < nevents; n++) { | |
706 | if (events[n].data.fd == sock) { | |
707 | ||
42edf948 | 708 | int conn_sock = accept4(sock, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC); |
4c17b2e3 DC |
709 | log_neg(conn_sock, "accept"); |
710 | if (conn_sock > -1) { | |
711 | add_new_client(conn_sock); | |
712 | } | |
713 | } else { | |
714 | handle_client((struct Client *)events[n].data.ptr); | |
715 | } | |
716 | } | |
4c500f16 | 717 | handle_forced_cleanup(); |
4c17b2e3 DC |
718 | } |
719 | } |