]>
Commit | Line | Data |
---|---|---|
4c17b2e3 DC |
1 | /* |
2 | ||
3 | Copyright (C) 2018 Proxmox Server Solutions GmbH | |
4 | ||
5 | Copyright: qemumonitor is under GNU GPL, the GNU General Public License. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; version 2 dated June, 1991. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. | |
20 | ||
21 | Author: Dominik Csapak <d.csapak@proxmox.com> | |
22 | */ | |
23 | ||
4c500f16 SR |
24 | #include <sys/syscall.h> |
25 | ||
26 | #ifndef __NR_pidfd_open | |
27 | #define __NR_pidfd_open 434 | |
28 | #endif | |
29 | #ifndef __NR_pidfd_send_signal | |
30 | #define __NR_pidfd_send_signal 424 | |
31 | #endif | |
32 | ||
e8e0fd93 | 33 | #define VERBOSE_PRINT(...) do { if (verbose) { printf(__VA_ARGS__); fflush(stdout); } } while (0) |
4c17b2e3 DC |
34 | |
35 | static inline void log_neg(int errval, const char *msg) | |
36 | { | |
37 | if (errval < 0) { | |
38 | perror(msg); | |
39 | } | |
40 | } | |
41 | ||
42 | static inline void bail_neg(int errval, const char *msg) | |
43 | { | |
44 | if (errval < 0) { | |
45 | perror(msg); | |
46 | exit(EXIT_FAILURE); | |
47 | } | |
48 | } | |
49 | ||
4c500f16 SR |
50 | static inline int |
51 | pidfd_open(pid_t pid, unsigned int flags) | |
52 | { | |
53 | return syscall(__NR_pidfd_open, pid, flags); | |
54 | } | |
55 | ||
56 | static inline int | |
57 | pidfd_send_signal(int pidfd, int sig, siginfo_t *info, unsigned int flags) | |
58 | { | |
59 | return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags); | |
60 | } | |
61 | ||
3ff85001 SR |
62 | typedef enum { |
63 | CLIENT_NONE, | |
64 | CLIENT_QEMU, | |
65 | CLIENT_VZDUMP | |
66 | } ClientType; | |
67 | ||
68 | typedef enum { | |
69 | STATE_HANDSHAKE, | |
70 | STATE_IDLE, | |
71 | STATE_EXPECT_STATUS_RESP, | |
72 | STATE_TERMINATING | |
73 | } ClientState; | |
74 | ||
4c17b2e3 DC |
75 | struct Client { |
76 | char buf[4096]; | |
3ff85001 SR |
77 | unsigned int buflen; |
78 | ||
4c17b2e3 DC |
79 | int fd; |
80 | pid_t pid; | |
3ff85001 SR |
81 | |
82 | ClientType type; | |
83 | ClientState state; | |
84 | ||
85 | // only relevant for type=CLIENT_QEMU | |
86 | struct { | |
87 | char vmid[16]; | |
88 | unsigned short graceful; | |
89 | unsigned short guest; | |
90 | bool term_check_queued; | |
91 | bool backup; | |
92 | } qemu; | |
93 | ||
94 | // only relevant for type=CLIENT_VZDUMP | |
95 | struct { | |
96 | // vmid of referenced backup | |
97 | char vmid[16]; | |
98 | } vzdump; | |
4c17b2e3 DC |
99 | }; |
100 | ||
4c500f16 SR |
101 | struct CleanupData { |
102 | pid_t pid; | |
103 | int pidfd; | |
104 | }; | |
105 | ||
4c17b2e3 DC |
106 | void handle_qmp_handshake(struct Client *client); |
107 | void handle_qmp_event(struct Client *client, struct json_object *obj); | |
3ff85001 SR |
108 | void handle_qmp_return(struct Client *client, struct json_object *data, bool error); |
109 | void handle_vzdump_handshake(struct Client *client, struct json_object *data); | |
4c17b2e3 DC |
110 | void handle_client(struct Client *client); |
111 | void add_new_client(int client_fd); | |
112 | void cleanup_client(struct Client *client); | |
3ff85001 SR |
113 | void terminate_client(struct Client *client); |
114 | void terminate_check(struct Client *client); |