| 1 | /* |
| 2 | * QTest |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2012 |
| 5 | * Copyright Red Hat, Inc. 2012 |
| 6 | * Copyright SUSE LINUX Products GmbH 2013 |
| 7 | * |
| 8 | * Authors: |
| 9 | * Anthony Liguori <aliguori@us.ibm.com> |
| 10 | * Paolo Bonzini <pbonzini@redhat.com> |
| 11 | * Andreas Färber <afaerber@suse.de> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 14 | * See the COPYING file in the top-level directory. |
| 15 | */ |
| 16 | |
| 17 | #include "qemu/osdep.h" |
| 18 | |
| 19 | #include "libqmp.h" |
| 20 | #include "libqtest.h" |
| 21 | |
| 22 | #ifndef _WIN32 |
| 23 | #include <sys/socket.h> |
| 24 | #endif |
| 25 | |
| 26 | #include "qemu/cutils.h" |
| 27 | #include "qemu/sockets.h" |
| 28 | #include "qapi/error.h" |
| 29 | #include "qobject/json-parser.h" |
| 30 | #include "qobject/qjson.h" |
| 31 | |
| 32 | #define SOCKET_MAX_FDS 16 |
| 33 | |
| 34 | typedef struct { |
| 35 | JSONMessageParser parser; |
| 36 | QDict *response; |
| 37 | } QMPResponseParser; |
| 38 | |
| 39 | static void socket_send(int fd, const char *buf, size_t size) |
| 40 | { |
| 41 | ssize_t res = qemu_send_full(fd, buf, size); |
| 42 | |
| 43 | assert(res == size); |
| 44 | } |
| 45 | |
| 46 | static void qmp_response(void *opaque, QObject *obj, Error *err) |
| 47 | { |
| 48 | QMPResponseParser *qmp = opaque; |
| 49 | |
| 50 | assert(!obj != !err); |
| 51 | |
| 52 | if (err) { |
| 53 | error_prepend(&err, "QMP JSON response parsing failed: "); |
| 54 | error_report_err(err); |
| 55 | abort(); |
| 56 | } |
| 57 | |
| 58 | g_assert(!qmp->response); |
| 59 | qmp->response = qobject_to(QDict, obj); |
| 60 | g_assert(qmp->response); |
| 61 | } |
| 62 | |
| 63 | QDict *qmp_fd_receive(int fd) |
| 64 | { |
| 65 | QMPResponseParser qmp; |
| 66 | bool log = qtest_verbose("qmp"); |
| 67 | |
| 68 | qmp.response = NULL; |
| 69 | json_message_parser_init(&qmp.parser, qmp_response, &qmp, NULL); |
| 70 | while (!qmp.response) { |
| 71 | ssize_t len; |
| 72 | char c; |
| 73 | |
| 74 | len = recv(fd, &c, 1, 0); |
| 75 | if (len == -1 && errno == EINTR) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | if (len == -1 || len == 0) { |
| 80 | fprintf(stderr, "Broken pipe\n"); |
| 81 | abort(); |
| 82 | } |
| 83 | |
| 84 | if (log) { |
| 85 | g_assert(write(2, &c, 1) == 1); |
| 86 | } |
| 87 | json_message_parser_feed(&qmp.parser, &c, 1); |
| 88 | } |
| 89 | if (log) { |
| 90 | g_assert(write(2, "\n", 1) == 1); |
| 91 | } |
| 92 | json_message_parser_destroy(&qmp.parser); |
| 93 | |
| 94 | return qmp.response; |
| 95 | } |
| 96 | |
| 97 | #ifndef _WIN32 |
| 98 | /* Sends a message and file descriptors to the socket. |
| 99 | * It's needed for qmp-commands like getfd/add-fd */ |
| 100 | static void socket_send_fds(int socket_fd, int *fds, size_t fds_num, |
| 101 | const char *buf, size_t buf_size) |
| 102 | { |
| 103 | ssize_t ret; |
| 104 | struct msghdr msg = { 0 }; |
| 105 | char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)] = { 0 }; |
| 106 | size_t fdsize = sizeof(int) * fds_num; |
| 107 | struct cmsghdr *cmsg; |
| 108 | struct iovec iov = { .iov_base = (char *)buf, .iov_len = buf_size }; |
| 109 | |
| 110 | msg.msg_iov = &iov; |
| 111 | msg.msg_iovlen = 1; |
| 112 | |
| 113 | if (fds && fds_num > 0) { |
| 114 | g_assert_cmpuint(fds_num, <, SOCKET_MAX_FDS); |
| 115 | |
| 116 | msg.msg_control = control; |
| 117 | msg.msg_controllen = CMSG_SPACE(fdsize); |
| 118 | |
| 119 | cmsg = CMSG_FIRSTHDR(&msg); |
| 120 | cmsg->cmsg_len = CMSG_LEN(fdsize); |
| 121 | cmsg->cmsg_level = SOL_SOCKET; |
| 122 | cmsg->cmsg_type = SCM_RIGHTS; |
| 123 | memcpy(CMSG_DATA(cmsg), fds, fdsize); |
| 124 | } |
| 125 | |
| 126 | do { |
| 127 | ret = sendmsg(socket_fd, &msg, 0); |
| 128 | } while (ret < 0 && errno == EINTR); |
| 129 | g_assert_cmpint(ret, >, 0); |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | /** |
| 134 | * Allow users to send a message without waiting for the reply, |
| 135 | * in the case that they choose to discard all replies up until |
| 136 | * a particular EVENT is received. |
| 137 | */ |
| 138 | static G_GNUC_PRINTF(4, 0) void |
| 139 | _qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num, |
| 140 | const char *fmt, va_list ap) |
| 141 | { |
| 142 | QObject *qobj; |
| 143 | |
| 144 | #ifdef _WIN32 |
| 145 | assert(fds_num == 0); |
| 146 | #endif |
| 147 | |
| 148 | /* Going through qobject ensures we escape strings properly */ |
| 149 | qobj = qobject_from_vjsonf_nofail(fmt, ap); |
| 150 | |
| 151 | /* No need to send anything for an empty QObject. */ |
| 152 | if (qobj) { |
| 153 | bool log = qtest_verbose("qmp"); |
| 154 | GString *str = qobject_to_json(qobj); |
| 155 | |
| 156 | /* |
| 157 | * BUG: QMP doesn't react to input until it sees a newline, an |
| 158 | * object, or an array. Work-around: give it a newline. |
| 159 | */ |
| 160 | g_string_append_c(str, '\n'); |
| 161 | |
| 162 | if (log) { |
| 163 | fprintf(stderr, "%s", str->str); |
| 164 | } |
| 165 | |
| 166 | #ifndef _WIN32 |
| 167 | /* Send QMP request */ |
| 168 | if (fds && fds_num > 0) { |
| 169 | socket_send_fds(fd, fds, fds_num, str->str, str->len); |
| 170 | } else |
| 171 | #endif |
| 172 | { |
| 173 | socket_send(fd, str->str, str->len); |
| 174 | } |
| 175 | |
| 176 | g_string_free(str, true); |
| 177 | qobject_unref(qobj); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | #ifndef _WIN32 |
| 182 | void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num, |
| 183 | const char *fmt, va_list ap) |
| 184 | { |
| 185 | _qmp_fd_vsend_fds(fd, fds, fds_num, fmt, ap); |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | void qmp_fd_vsend(int fd, const char *fmt, va_list ap) |
| 190 | { |
| 191 | _qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | QDict *qmp_fdv(int fd, const char *fmt, va_list ap) |
| 196 | { |
| 197 | _qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap); |
| 198 | |
| 199 | return qmp_fd_receive(fd); |
| 200 | } |
| 201 | |
| 202 | QDict *qmp_fd(int fd, const char *fmt, ...) |
| 203 | { |
| 204 | va_list ap; |
| 205 | QDict *response; |
| 206 | |
| 207 | va_start(ap, fmt); |
| 208 | response = qmp_fdv(fd, fmt, ap); |
| 209 | va_end(ap); |
| 210 | return response; |
| 211 | } |
| 212 | |
| 213 | void qmp_fd_send(int fd, const char *fmt, ...) |
| 214 | { |
| 215 | va_list ap; |
| 216 | |
| 217 | va_start(ap, fmt); |
| 218 | qmp_fd_vsend(fd, fmt, ap); |
| 219 | va_end(ap); |
| 220 | } |
| 221 | |
| 222 | void qmp_fd_vsend_raw(int fd, const char *fmt, va_list ap) |
| 223 | { |
| 224 | bool log = qtest_verbose("qmp"); |
| 225 | char *str = g_strdup_vprintf(fmt, ap); |
| 226 | |
| 227 | if (log) { |
| 228 | fprintf(stderr, "%s", str); |
| 229 | } |
| 230 | socket_send(fd, str, strlen(str)); |
| 231 | g_free(str); |
| 232 | } |
| 233 | |
| 234 | void qmp_fd_send_raw(int fd, const char *fmt, ...) |
| 235 | { |
| 236 | va_list ap; |
| 237 | |
| 238 | va_start(ap, fmt); |
| 239 | qmp_fd_vsend_raw(fd, fmt, ap); |
| 240 | va_end(ap); |
| 241 | } |
| 242 | |
| 243 | bool qmp_rsp_is_err(QDict *rsp) |
| 244 | { |
| 245 | QDict *error = qdict_get_qdict(rsp, "error"); |
| 246 | qobject_unref(rsp); |
| 247 | return !!error; |
| 248 | } |
| 249 | |
| 250 | void qmp_expect_error_and_unref(QDict *rsp, const char *class) |
| 251 | { |
| 252 | QDict *error = qdict_get_qdict(rsp, "error"); |
| 253 | |
| 254 | g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, class); |
| 255 | g_assert_nonnull(qdict_get_try_str(error, "desc")); |
| 256 | g_assert(!qdict_haskey(rsp, "return")); |
| 257 | |
| 258 | qobject_unref(rsp); |
| 259 | } |