]> git.proxmox.com Git - qemu-server.git/blob - qmeventd/qmeventd.h
2cf1947fd25d28798ad255d0d6577e611676b2b2
[qemu-server.git] / qmeventd / qmeventd.h
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2 /*
3 Copyright (C) 2018 - 2021 Proxmox Server Solutions GmbH
4
5 Author: Dominik Csapak <d.csapak@proxmox.com>
6 Author: Stefan Reiter <s.reiter@proxmox.com>
7 */
8
9 #include <sys/syscall.h>
10
11 #ifndef __NR_pidfd_open
12 #define __NR_pidfd_open 434
13 #endif
14 #ifndef __NR_pidfd_send_signal
15 #define __NR_pidfd_send_signal 424
16 #endif
17
18 #define VERBOSE_PRINT(...) do { if (verbose) { printf(__VA_ARGS__); fflush(stdout); } } while (0)
19
20 static inline void log_neg(int errval, const char *msg)
21 {
22 if (errval < 0) {
23 perror(msg);
24 }
25 }
26
27 static inline void bail_neg(int errval, const char *msg)
28 {
29 if (errval < 0) {
30 perror(msg);
31 exit(EXIT_FAILURE);
32 }
33 }
34
35 static inline int
36 pidfd_open(pid_t pid, unsigned int flags)
37 {
38 return syscall(__NR_pidfd_open, pid, flags);
39 }
40
41 static inline int
42 pidfd_send_signal(int pidfd, int sig, siginfo_t *info, unsigned int flags)
43 {
44 return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
45 }
46
47 typedef enum {
48 CLIENT_NONE,
49 CLIENT_QEMU,
50 CLIENT_VZDUMP
51 } ClientType;
52
53 typedef enum {
54 STATE_HANDSHAKE,
55 STATE_IDLE,
56 STATE_EXPECT_STATUS_RESP,
57 STATE_TERMINATING
58 } ClientState;
59
60 struct Client {
61 char buf[4096];
62 unsigned int buflen;
63
64 int fd;
65 pid_t pid;
66
67 ClientType type;
68 ClientState state;
69
70 // only relevant for type=CLIENT_QEMU
71 struct {
72 char vmid[16];
73 unsigned short graceful;
74 unsigned short guest;
75 bool term_check_queued;
76 bool backup;
77 } qemu;
78
79 // only relevant for type=CLIENT_VZDUMP
80 struct {
81 // vmid of referenced backup
82 char vmid[16];
83 } vzdump;
84 };
85
86 struct CleanupData {
87 pid_t pid;
88 int pidfd;
89 };
90
91 void handle_qmp_handshake(struct Client *client);
92 void handle_qmp_event(struct Client *client, struct json_object *obj);
93 void handle_qmp_return(struct Client *client, struct json_object *data, bool error);
94 void handle_vzdump_handshake(struct Client *client, struct json_object *data);
95 void handle_client(struct Client *client);
96 void add_new_client(int client_fd);
97 void cleanup_client(struct Client *client);
98 void terminate_client(struct Client *client);
99 void terminate_check(struct Client *client);