| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; only |
| 8 | * version 2.1 of the License. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/un.h> |
| 29 | #include <unistd.h> |
| 30 | #include <assert.h> |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | |
| 34 | #include <lttng/ust-ctl.h> |
| 35 | #include <ust-comm.h> |
| 36 | #include <helper.h> |
| 37 | #include <lttng/ust-error.h> |
| 38 | #include <lttng/ust-events.h> |
| 39 | #include <lttng/ust-dynamic-type.h> |
| 40 | #include <usterr-signal-safe.h> |
| 41 | |
| 42 | #include "../liblttng-ust/compat.h" |
| 43 | |
| 44 | #define USTCOMM_CODE_OFFSET(code) \ |
| 45 | (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1)) |
| 46 | |
| 47 | #define USTCOMM_MAX_SEND_FDS 4 |
| 48 | |
| 49 | static |
| 50 | ssize_t count_fields_recursive(size_t nr_fields, |
| 51 | const struct lttng_event_field *lttng_fields); |
| 52 | static |
| 53 | int serialize_one_field(struct lttng_session *session, |
| 54 | struct ustctl_field *fields, size_t *iter_output, |
| 55 | const struct lttng_event_field *lf); |
| 56 | |
| 57 | /* |
| 58 | * Human readable error message. |
| 59 | */ |
| 60 | static const char *ustcomm_readable_code[] = { |
| 61 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK) ] = "Success", |
| 62 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR) ] = "Unknown error", |
| 63 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT) ] = "No entry", |
| 64 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST) ] = "Object already exists", |
| 65 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL) ] = "Invalid argument", |
| 66 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM) ] = "Permission denied", |
| 67 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS) ] = "Not implemented", |
| 68 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING) ] = "Process is exiting", |
| 69 | |
| 70 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number", |
| 71 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type", |
| 72 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version", |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * lttng_ust_strerror |
| 77 | * |
| 78 | * Receives positive error value. |
| 79 | * Return ptr to string representing a human readable |
| 80 | * error code from the ustcomm_return_code enum. |
| 81 | */ |
| 82 | const char *lttng_ust_strerror(int code) |
| 83 | { |
| 84 | if (code == LTTNG_UST_OK) |
| 85 | return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)]; |
| 86 | if (code < LTTNG_UST_ERR) |
| 87 | return strerror(code); |
| 88 | if (code >= LTTNG_UST_ERR_NR) |
| 89 | code = LTTNG_UST_ERR; |
| 90 | return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)]; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * ustcomm_connect_unix_sock |
| 95 | * |
| 96 | * Connect to unix socket using the path name. |
| 97 | */ |
| 98 | int ustcomm_connect_unix_sock(const char *pathname) |
| 99 | { |
| 100 | struct sockaddr_un sun; |
| 101 | int fd, ret; |
| 102 | |
| 103 | /* |
| 104 | * libust threads require the close-on-exec flag for all |
| 105 | * resources so it does not leak file descriptors upon exec. |
| 106 | */ |
| 107 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 108 | if (fd < 0) { |
| 109 | PERROR("socket"); |
| 110 | ret = -errno; |
| 111 | goto error; |
| 112 | } |
| 113 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 114 | if (ret < 0) { |
| 115 | PERROR("fcntl"); |
| 116 | ret = -errno; |
| 117 | goto error_fcntl; |
| 118 | } |
| 119 | |
| 120 | memset(&sun, 0, sizeof(sun)); |
| 121 | sun.sun_family = AF_UNIX; |
| 122 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 123 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
| 124 | |
| 125 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 126 | if (ret < 0) { |
| 127 | /* |
| 128 | * Don't print message on connect ENOENT error, because |
| 129 | * connect is used in normal execution to detect if |
| 130 | * sessiond is alive. ENOENT is when the unix socket |
| 131 | * file does not exist, and ECONNREFUSED is when the |
| 132 | * file exists but no sessiond is listening. |
| 133 | */ |
| 134 | if (errno != ECONNREFUSED && errno != ECONNRESET |
| 135 | && errno != ENOENT && errno != EACCES) |
| 136 | PERROR("connect"); |
| 137 | ret = -errno; |
| 138 | if (ret == -ECONNREFUSED || ret == -ECONNRESET) |
| 139 | ret = -EPIPE; |
| 140 | goto error_connect; |
| 141 | } |
| 142 | |
| 143 | return fd; |
| 144 | |
| 145 | error_connect: |
| 146 | error_fcntl: |
| 147 | { |
| 148 | int closeret; |
| 149 | |
| 150 | closeret = close(fd); |
| 151 | if (closeret) |
| 152 | PERROR("close"); |
| 153 | } |
| 154 | error: |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * ustcomm_accept_unix_sock |
| 160 | * |
| 161 | * Do an accept(2) on the sock and return the |
| 162 | * new file descriptor. The socket MUST be bind(2) before. |
| 163 | */ |
| 164 | int ustcomm_accept_unix_sock(int sock) |
| 165 | { |
| 166 | int new_fd; |
| 167 | struct sockaddr_un sun; |
| 168 | socklen_t len = 0; |
| 169 | |
| 170 | /* Blocking call */ |
| 171 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); |
| 172 | if (new_fd < 0) { |
| 173 | if (errno != ECONNABORTED) |
| 174 | PERROR("accept"); |
| 175 | new_fd = -errno; |
| 176 | if (new_fd == -ECONNABORTED) |
| 177 | new_fd = -EPIPE; |
| 178 | } |
| 179 | return new_fd; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * ustcomm_create_unix_sock |
| 184 | * |
| 185 | * Creates a AF_UNIX local socket using pathname |
| 186 | * bind the socket upon creation and return the fd. |
| 187 | */ |
| 188 | int ustcomm_create_unix_sock(const char *pathname) |
| 189 | { |
| 190 | struct sockaddr_un sun; |
| 191 | int fd, ret; |
| 192 | |
| 193 | /* Create server socket */ |
| 194 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 195 | PERROR("socket"); |
| 196 | ret = -errno; |
| 197 | goto error; |
| 198 | } |
| 199 | |
| 200 | memset(&sun, 0, sizeof(sun)); |
| 201 | sun.sun_family = AF_UNIX; |
| 202 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 203 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
| 204 | |
| 205 | /* Unlink the old file if present */ |
| 206 | (void) unlink(pathname); |
| 207 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 208 | if (ret < 0) { |
| 209 | PERROR("bind"); |
| 210 | ret = -errno; |
| 211 | goto error_close; |
| 212 | } |
| 213 | |
| 214 | return fd; |
| 215 | |
| 216 | error_close: |
| 217 | { |
| 218 | int closeret; |
| 219 | |
| 220 | closeret = close(fd); |
| 221 | if (closeret) { |
| 222 | PERROR("close"); |
| 223 | } |
| 224 | } |
| 225 | error: |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * ustcomm_listen_unix_sock |
| 231 | * |
| 232 | * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN. |
| 233 | */ |
| 234 | int ustcomm_listen_unix_sock(int sock) |
| 235 | { |
| 236 | int ret; |
| 237 | |
| 238 | ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN); |
| 239 | if (ret < 0) { |
| 240 | ret = -errno; |
| 241 | PERROR("listen"); |
| 242 | } |
| 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * ustcomm_close_unix_sock |
| 249 | * |
| 250 | * Shutdown cleanly a unix socket. |
| 251 | */ |
| 252 | int ustcomm_close_unix_sock(int sock) |
| 253 | { |
| 254 | int ret; |
| 255 | |
| 256 | ret = close(sock); |
| 257 | if (ret < 0) { |
| 258 | PERROR("close"); |
| 259 | ret = -errno; |
| 260 | } |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * ustcomm_recv_unix_sock |
| 267 | * |
| 268 | * Receive data of size len in put that data into |
| 269 | * the buf param. Using recvmsg API. |
| 270 | * Return the size of received data. |
| 271 | * Return 0 on orderly shutdown. |
| 272 | */ |
| 273 | ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len) |
| 274 | { |
| 275 | struct msghdr msg; |
| 276 | struct iovec iov[1]; |
| 277 | ssize_t ret = -1; |
| 278 | size_t len_last; |
| 279 | |
| 280 | memset(&msg, 0, sizeof(msg)); |
| 281 | |
| 282 | iov[0].iov_base = buf; |
| 283 | iov[0].iov_len = len; |
| 284 | msg.msg_iov = iov; |
| 285 | msg.msg_iovlen = 1; |
| 286 | |
| 287 | do { |
| 288 | len_last = iov[0].iov_len; |
| 289 | ret = recvmsg(sock, &msg, 0); |
| 290 | if (ret > 0) { |
| 291 | iov[0].iov_base += ret; |
| 292 | iov[0].iov_len -= ret; |
| 293 | assert(ret <= len_last); |
| 294 | } |
| 295 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); |
| 296 | |
| 297 | if (ret < 0) { |
| 298 | int shutret; |
| 299 | |
| 300 | if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED) |
| 301 | PERROR("recvmsg"); |
| 302 | ret = -errno; |
| 303 | if (ret == -ECONNRESET || ret == -ECONNREFUSED) |
| 304 | ret = -EPIPE; |
| 305 | |
| 306 | shutret = shutdown(sock, SHUT_RDWR); |
| 307 | if (shutret) |
| 308 | ERR("Socket shutdown error"); |
| 309 | } else if (ret > 0) { |
| 310 | ret = len; |
| 311 | } |
| 312 | /* ret = 0 means an orderly shutdown. */ |
| 313 | |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * ustcomm_send_unix_sock |
| 319 | * |
| 320 | * Send buf data of size len. Using sendmsg API. |
| 321 | * Return the size of sent data. |
| 322 | */ |
| 323 | ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len) |
| 324 | { |
| 325 | struct msghdr msg; |
| 326 | struct iovec iov[1]; |
| 327 | ssize_t ret; |
| 328 | |
| 329 | memset(&msg, 0, sizeof(msg)); |
| 330 | |
| 331 | iov[0].iov_base = (void *) buf; |
| 332 | iov[0].iov_len = len; |
| 333 | msg.msg_iov = iov; |
| 334 | msg.msg_iovlen = 1; |
| 335 | |
| 336 | /* |
| 337 | * Using the MSG_NOSIGNAL when sending data from sessiond to |
| 338 | * libust, so libust does not receive an unhandled SIGPIPE or |
| 339 | * SIGURG. The sessiond receiver side can be made more resilient |
| 340 | * by ignoring SIGPIPE, but we don't have this luxury on the |
| 341 | * libust side. |
| 342 | */ |
| 343 | do { |
| 344 | ret = sendmsg(sock, &msg, MSG_NOSIGNAL); |
| 345 | } while (ret < 0 && errno == EINTR); |
| 346 | |
| 347 | if (ret < 0) { |
| 348 | int shutret; |
| 349 | |
| 350 | if (errno != EPIPE && errno != ECONNRESET) |
| 351 | PERROR("sendmsg"); |
| 352 | ret = -errno; |
| 353 | if (ret == -ECONNRESET) |
| 354 | ret = -EPIPE; |
| 355 | |
| 356 | shutret = shutdown(sock, SHUT_RDWR); |
| 357 | if (shutret) |
| 358 | ERR("Socket shutdown error"); |
| 359 | } |
| 360 | |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Send a message accompanied by fd(s) over a unix socket. |
| 366 | * |
| 367 | * Returns the size of data sent, or negative error value. |
| 368 | */ |
| 369 | ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 370 | { |
| 371 | struct msghdr msg; |
| 372 | struct cmsghdr *cmptr; |
| 373 | struct iovec iov[1]; |
| 374 | ssize_t ret = -1; |
| 375 | unsigned int sizeof_fds = nb_fd * sizeof(int); |
| 376 | char tmp[CMSG_SPACE(sizeof_fds)]; |
| 377 | char dummy = 0; |
| 378 | |
| 379 | memset(&msg, 0, sizeof(msg)); |
| 380 | memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char)); |
| 381 | |
| 382 | if (nb_fd > USTCOMM_MAX_SEND_FDS) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | msg.msg_control = (caddr_t)tmp; |
| 386 | msg.msg_controllen = CMSG_LEN(sizeof_fds); |
| 387 | |
| 388 | cmptr = CMSG_FIRSTHDR(&msg); |
| 389 | if (!cmptr) |
| 390 | return -EINVAL; |
| 391 | cmptr->cmsg_level = SOL_SOCKET; |
| 392 | cmptr->cmsg_type = SCM_RIGHTS; |
| 393 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
| 394 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
| 395 | /* Sum of the length of all control messages in the buffer: */ |
| 396 | msg.msg_controllen = cmptr->cmsg_len; |
| 397 | |
| 398 | iov[0].iov_base = &dummy; |
| 399 | iov[0].iov_len = 1; |
| 400 | msg.msg_iov = iov; |
| 401 | msg.msg_iovlen = 1; |
| 402 | |
| 403 | do { |
| 404 | ret = sendmsg(sock, &msg, MSG_NOSIGNAL); |
| 405 | } while (ret < 0 && errno == EINTR); |
| 406 | if (ret < 0) { |
| 407 | /* |
| 408 | * We consider EPIPE and ECONNRESET as expected. |
| 409 | */ |
| 410 | if (errno != EPIPE && errno != ECONNRESET) { |
| 411 | PERROR("sendmsg"); |
| 412 | } |
| 413 | ret = -errno; |
| 414 | if (ret == -ECONNRESET) |
| 415 | ret = -EPIPE; |
| 416 | } |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Recv a message accompanied by fd(s) from a unix socket. |
| 422 | * |
| 423 | * Returns the size of received data, or negative error value. |
| 424 | * |
| 425 | * Expect at most "nb_fd" file descriptors. Returns the number of fd |
| 426 | * actually received in nb_fd. |
| 427 | * Returns -EPIPE on orderly shutdown. |
| 428 | */ |
| 429 | ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 430 | { |
| 431 | struct iovec iov[1]; |
| 432 | ssize_t ret = 0; |
| 433 | struct cmsghdr *cmsg; |
| 434 | size_t sizeof_fds = nb_fd * sizeof(int); |
| 435 | char recv_fd[CMSG_SPACE(sizeof_fds)]; |
| 436 | struct msghdr msg; |
| 437 | char dummy; |
| 438 | |
| 439 | memset(&msg, 0, sizeof(msg)); |
| 440 | |
| 441 | /* Prepare to receive the structures */ |
| 442 | iov[0].iov_base = &dummy; |
| 443 | iov[0].iov_len = 1; |
| 444 | msg.msg_iov = iov; |
| 445 | msg.msg_iovlen = 1; |
| 446 | msg.msg_control = recv_fd; |
| 447 | msg.msg_controllen = sizeof(recv_fd); |
| 448 | |
| 449 | do { |
| 450 | ret = recvmsg(sock, &msg, 0); |
| 451 | } while (ret < 0 && errno == EINTR); |
| 452 | if (ret < 0) { |
| 453 | if (errno != EPIPE && errno != ECONNRESET) { |
| 454 | PERROR("recvmsg fds"); |
| 455 | } |
| 456 | ret = -errno; |
| 457 | if (ret == -ECONNRESET) |
| 458 | ret = -EPIPE; |
| 459 | goto end; |
| 460 | } |
| 461 | if (ret == 0) { |
| 462 | /* orderly shutdown */ |
| 463 | ret = -EPIPE; |
| 464 | goto end; |
| 465 | } |
| 466 | if (ret != 1) { |
| 467 | ERR("Error: Received %zd bytes, expected %d\n", |
| 468 | ret, 1); |
| 469 | goto end; |
| 470 | } |
| 471 | if (msg.msg_flags & MSG_CTRUNC) { |
| 472 | ERR("Error: Control message truncated.\n"); |
| 473 | ret = -1; |
| 474 | goto end; |
| 475 | } |
| 476 | cmsg = CMSG_FIRSTHDR(&msg); |
| 477 | if (!cmsg) { |
| 478 | ERR("Error: Invalid control message header\n"); |
| 479 | ret = -1; |
| 480 | goto end; |
| 481 | } |
| 482 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
| 483 | ERR("Didn't received any fd\n"); |
| 484 | ret = -1; |
| 485 | goto end; |
| 486 | } |
| 487 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
| 488 | ERR("Error: Received %zu bytes of ancillary data, expected %zu\n", |
| 489 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); |
| 490 | ret = -1; |
| 491 | goto end; |
| 492 | } |
| 493 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
| 494 | ret = sizeof_fds; |
| 495 | end: |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum) |
| 500 | { |
| 501 | ssize_t len; |
| 502 | |
| 503 | len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum)); |
| 504 | switch (len) { |
| 505 | case sizeof(*lum): |
| 506 | break; |
| 507 | default: |
| 508 | if (len < 0) { |
| 509 | return len; |
| 510 | } else { |
| 511 | ERR("incorrect message size: %zd\n", len); |
| 512 | return -EINVAL; |
| 513 | } |
| 514 | } |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur, |
| 519 | uint32_t expected_handle, uint32_t expected_cmd) |
| 520 | { |
| 521 | ssize_t len; |
| 522 | |
| 523 | memset(lur, 0, sizeof(*lur)); |
| 524 | len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur)); |
| 525 | switch (len) { |
| 526 | case 0: /* orderly shutdown */ |
| 527 | return -EPIPE; |
| 528 | case sizeof(*lur): |
| 529 | { |
| 530 | int err = 0; |
| 531 | |
| 532 | if (lur->handle != expected_handle) { |
| 533 | ERR("Unexpected result message handle: " |
| 534 | "expected: %u vs received: %u\n", |
| 535 | expected_handle, lur->handle); |
| 536 | err = 1; |
| 537 | } |
| 538 | if (lur->cmd != expected_cmd) { |
| 539 | ERR("Unexpected result message command " |
| 540 | "expected: %u vs received: %u\n", |
| 541 | expected_cmd, lur->cmd); |
| 542 | err = 1; |
| 543 | } |
| 544 | if (err) { |
| 545 | return -EINVAL; |
| 546 | } else { |
| 547 | return lur->ret_code; |
| 548 | } |
| 549 | } |
| 550 | default: |
| 551 | if (len >= 0) { |
| 552 | ERR("incorrect message size: %zd\n", len); |
| 553 | } |
| 554 | return len; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | int ustcomm_send_app_cmd(int sock, |
| 559 | struct ustcomm_ust_msg *lum, |
| 560 | struct ustcomm_ust_reply *lur) |
| 561 | { |
| 562 | int ret; |
| 563 | |
| 564 | ret = ustcomm_send_app_msg(sock, lum); |
| 565 | if (ret) |
| 566 | return ret; |
| 567 | ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd); |
| 568 | if (ret > 0) |
| 569 | return -EIO; |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * chan_data is allocated internally if this function returns the |
| 575 | * expected var_len. |
| 576 | */ |
| 577 | ssize_t ustcomm_recv_channel_from_sessiond(int sock, |
| 578 | void **_chan_data, uint64_t var_len, |
| 579 | int *_wakeup_fd) |
| 580 | { |
| 581 | void *chan_data; |
| 582 | ssize_t len, nr_fd; |
| 583 | int wakeup_fd; |
| 584 | |
| 585 | if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) { |
| 586 | len = -EINVAL; |
| 587 | goto error_check; |
| 588 | } |
| 589 | /* Receive variable length data */ |
| 590 | chan_data = zmalloc(var_len); |
| 591 | if (!chan_data) { |
| 592 | len = -ENOMEM; |
| 593 | goto error_alloc; |
| 594 | } |
| 595 | len = ustcomm_recv_unix_sock(sock, chan_data, var_len); |
| 596 | if (len != var_len) { |
| 597 | goto error_recv; |
| 598 | } |
| 599 | /* recv wakeup fd */ |
| 600 | nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1); |
| 601 | if (nr_fd <= 0) { |
| 602 | if (nr_fd < 0) { |
| 603 | len = nr_fd; |
| 604 | goto error_recv; |
| 605 | } else { |
| 606 | len = -EIO; |
| 607 | goto error_recv; |
| 608 | } |
| 609 | } |
| 610 | *_wakeup_fd = wakeup_fd; |
| 611 | *_chan_data = chan_data; |
| 612 | return len; |
| 613 | |
| 614 | error_recv: |
| 615 | free(chan_data); |
| 616 | error_alloc: |
| 617 | error_check: |
| 618 | return len; |
| 619 | } |
| 620 | |
| 621 | int ustcomm_recv_stream_from_sessiond(int sock, |
| 622 | uint64_t *memory_map_size, |
| 623 | int *shm_fd, int *wakeup_fd) |
| 624 | { |
| 625 | ssize_t len; |
| 626 | int ret; |
| 627 | int fds[2]; |
| 628 | |
| 629 | /* recv shm fd and wakeup fd */ |
| 630 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); |
| 631 | if (len <= 0) { |
| 632 | if (len < 0) { |
| 633 | ret = len; |
| 634 | goto error; |
| 635 | } else { |
| 636 | ret = -EIO; |
| 637 | goto error; |
| 638 | } |
| 639 | } |
| 640 | *shm_fd = fds[0]; |
| 641 | *wakeup_fd = fds[1]; |
| 642 | return 0; |
| 643 | |
| 644 | error: |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Returns 0 on success, negative error value on error. |
| 650 | */ |
| 651 | int ustcomm_send_reg_msg(int sock, |
| 652 | enum ustctl_socket_type type, |
| 653 | uint32_t bits_per_long, |
| 654 | uint32_t uint8_t_alignment, |
| 655 | uint32_t uint16_t_alignment, |
| 656 | uint32_t uint32_t_alignment, |
| 657 | uint32_t uint64_t_alignment, |
| 658 | uint32_t long_alignment) |
| 659 | { |
| 660 | ssize_t len; |
| 661 | struct ustctl_reg_msg reg_msg; |
| 662 | |
| 663 | reg_msg.magic = LTTNG_UST_COMM_MAGIC; |
| 664 | reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION; |
| 665 | reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION; |
| 666 | reg_msg.pid = getpid(); |
| 667 | reg_msg.ppid = getppid(); |
| 668 | reg_msg.uid = getuid(); |
| 669 | reg_msg.gid = getgid(); |
| 670 | reg_msg.bits_per_long = bits_per_long; |
| 671 | reg_msg.uint8_t_alignment = uint8_t_alignment; |
| 672 | reg_msg.uint16_t_alignment = uint16_t_alignment; |
| 673 | reg_msg.uint32_t_alignment = uint32_t_alignment; |
| 674 | reg_msg.uint64_t_alignment = uint64_t_alignment; |
| 675 | reg_msg.long_alignment = long_alignment; |
| 676 | reg_msg.socket_type = type; |
| 677 | lttng_ust_getprocname(reg_msg.name); |
| 678 | memset(reg_msg.padding, 0, sizeof(reg_msg.padding)); |
| 679 | |
| 680 | len = ustcomm_send_unix_sock(sock, ®_msg, sizeof(reg_msg)); |
| 681 | if (len > 0 && len != sizeof(reg_msg)) |
| 682 | return -EIO; |
| 683 | if (len < 0) |
| 684 | return len; |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | static |
| 689 | ssize_t count_one_type(const struct lttng_type *lt) |
| 690 | { |
| 691 | switch (lt->atype) { |
| 692 | case atype_integer: |
| 693 | case atype_float: |
| 694 | case atype_string: |
| 695 | case atype_enum: |
| 696 | case atype_array: |
| 697 | case atype_sequence: |
| 698 | return 1; |
| 699 | case atype_struct: |
| 700 | //TODO: implement non-empty struct. |
| 701 | return 1; |
| 702 | case atype_dynamic: |
| 703 | { |
| 704 | const struct lttng_event_field *choices; |
| 705 | size_t nr_choices; |
| 706 | int ret; |
| 707 | |
| 708 | ret = lttng_ust_dynamic_type_choices(&nr_choices, |
| 709 | &choices); |
| 710 | if (ret) |
| 711 | return ret; |
| 712 | /* |
| 713 | * One field for enum, one field for variant, and |
| 714 | * one field per choice. |
| 715 | */ |
| 716 | return count_fields_recursive(nr_choices, choices) + 2; |
| 717 | } |
| 718 | default: |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static |
| 725 | ssize_t count_fields_recursive(size_t nr_fields, |
| 726 | const struct lttng_event_field *lttng_fields) |
| 727 | { |
| 728 | int i; |
| 729 | ssize_t ret, count = 0; |
| 730 | |
| 731 | for (i = 0; i < nr_fields; i++) { |
| 732 | const struct lttng_event_field *lf; |
| 733 | |
| 734 | lf = <tng_fields[i]; |
| 735 | /* skip 'nowrite' fields */ |
| 736 | if (lf->nowrite) |
| 737 | continue; |
| 738 | ret = count_one_type(&lf->type); |
| 739 | if (ret < 0) |
| 740 | return ret; /* error */ |
| 741 | count += ret; |
| 742 | } |
| 743 | return count; |
| 744 | } |
| 745 | |
| 746 | static |
| 747 | ssize_t count_ctx_fields_recursive(size_t nr_fields, |
| 748 | const struct lttng_ctx_field *lttng_fields) |
| 749 | { |
| 750 | int i; |
| 751 | ssize_t ret, count = 0; |
| 752 | |
| 753 | for (i = 0; i < nr_fields; i++) { |
| 754 | const struct lttng_event_field *lf; |
| 755 | |
| 756 | lf = <tng_fields[i].event_field; |
| 757 | /* skip 'nowrite' fields */ |
| 758 | if (lf->nowrite) |
| 759 | continue; |
| 760 | ret = count_one_type(&lf->type); |
| 761 | if (ret < 0) |
| 762 | return ret; /* error */ |
| 763 | count += ret; |
| 764 | } |
| 765 | return count; |
| 766 | } |
| 767 | |
| 768 | static |
| 769 | int serialize_string_encoding(enum ustctl_string_encodings *ue, |
| 770 | enum lttng_string_encodings le) |
| 771 | { |
| 772 | switch (le) { |
| 773 | case lttng_encode_none: |
| 774 | *ue = ustctl_encode_none; |
| 775 | break; |
| 776 | case lttng_encode_UTF8: |
| 777 | *ue = ustctl_encode_UTF8; |
| 778 | break; |
| 779 | case lttng_encode_ASCII: |
| 780 | *ue = ustctl_encode_ASCII; |
| 781 | break; |
| 782 | default: |
| 783 | return -EINVAL; |
| 784 | } |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | static |
| 789 | int serialize_integer_type(struct ustctl_integer_type *uit, |
| 790 | const struct lttng_integer_type *lit) |
| 791 | { |
| 792 | uit->size = lit->size; |
| 793 | uit->signedness = lit->signedness; |
| 794 | uit->reverse_byte_order = lit->reverse_byte_order; |
| 795 | uit->base = lit->base; |
| 796 | if (serialize_string_encoding(&uit->encoding, lit->encoding)) |
| 797 | return -EINVAL; |
| 798 | uit->alignment = lit->alignment; |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static |
| 803 | int serialize_basic_type(struct lttng_session *session, |
| 804 | enum ustctl_abstract_types *uatype, |
| 805 | enum lttng_abstract_types atype, |
| 806 | union _ustctl_basic_type *ubt, |
| 807 | const union _lttng_basic_type *lbt) |
| 808 | { |
| 809 | switch (atype) { |
| 810 | case atype_integer: |
| 811 | { |
| 812 | if (serialize_integer_type(&ubt->integer, &lbt->integer)) |
| 813 | return -EINVAL; |
| 814 | *uatype = ustctl_atype_integer; |
| 815 | break; |
| 816 | } |
| 817 | case atype_string: |
| 818 | { |
| 819 | if (serialize_string_encoding(&ubt->string.encoding, |
| 820 | lbt->string.encoding)) |
| 821 | return -EINVAL; |
| 822 | *uatype = ustctl_atype_string; |
| 823 | break; |
| 824 | } |
| 825 | case atype_float: |
| 826 | { |
| 827 | struct ustctl_float_type *uft; |
| 828 | const struct lttng_float_type *lft; |
| 829 | |
| 830 | uft = &ubt->_float; |
| 831 | lft = &lbt->_float; |
| 832 | uft->exp_dig = lft->exp_dig; |
| 833 | uft->mant_dig = lft->mant_dig; |
| 834 | uft->alignment = lft->alignment; |
| 835 | uft->reverse_byte_order = lft->reverse_byte_order; |
| 836 | *uatype = ustctl_atype_float; |
| 837 | break; |
| 838 | } |
| 839 | case atype_enum: |
| 840 | { |
| 841 | strncpy(ubt->enumeration.name, lbt->enumeration.desc->name, |
| 842 | LTTNG_UST_SYM_NAME_LEN); |
| 843 | ubt->enumeration.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 844 | if (serialize_integer_type(&ubt->enumeration.container_type, |
| 845 | &lbt->enumeration.container_type)) |
| 846 | return -EINVAL; |
| 847 | if (session) { |
| 848 | const struct lttng_enum *_enum; |
| 849 | |
| 850 | _enum = lttng_ust_enum_get(session, |
| 851 | lbt->enumeration.desc->name); |
| 852 | if (!_enum) |
| 853 | return -EINVAL; |
| 854 | ubt->enumeration.id = _enum->id; |
| 855 | } else { |
| 856 | ubt->enumeration.id = -1ULL; |
| 857 | } |
| 858 | *uatype = ustctl_atype_enum; |
| 859 | break; |
| 860 | } |
| 861 | case atype_array: |
| 862 | case atype_sequence: |
| 863 | default: |
| 864 | return -EINVAL; |
| 865 | } |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static |
| 870 | int serialize_dynamic_type(struct lttng_session *session, |
| 871 | struct ustctl_field *fields, size_t *iter_output, |
| 872 | const struct lttng_event_field *lf) |
| 873 | { |
| 874 | const struct lttng_event_field *choices; |
| 875 | char tag_field_name[LTTNG_UST_SYM_NAME_LEN]; |
| 876 | const struct lttng_type *tag_type; |
| 877 | const struct lttng_event_field *tag_field_generic; |
| 878 | struct lttng_event_field tag_field = { |
| 879 | .name = tag_field_name, |
| 880 | .nowrite = 0, |
| 881 | }; |
| 882 | struct ustctl_field *uf; |
| 883 | size_t nr_choices, i; |
| 884 | int ret; |
| 885 | |
| 886 | tag_field_generic = lttng_ust_dynamic_type_tag_field(); |
| 887 | tag_type = &tag_field_generic->type; |
| 888 | |
| 889 | /* Serialize enum field. */ |
| 890 | strncpy(tag_field_name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
| 891 | tag_field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 892 | strncat(tag_field_name, |
| 893 | "_tag", |
| 894 | LTTNG_UST_SYM_NAME_LEN - strlen(tag_field_name) - 1); |
| 895 | tag_field.type = *tag_type; |
| 896 | ret = serialize_one_field(session, fields, iter_output, |
| 897 | &tag_field); |
| 898 | if (ret) |
| 899 | return ret; |
| 900 | |
| 901 | /* Serialize variant field. */ |
| 902 | uf = &fields[*iter_output]; |
| 903 | ret = lttng_ust_dynamic_type_choices(&nr_choices, &choices); |
| 904 | if (ret) |
| 905 | return ret; |
| 906 | |
| 907 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
| 908 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 909 | uf->type.atype = ustctl_atype_variant; |
| 910 | uf->type.u.variant.nr_choices = nr_choices; |
| 911 | strncpy(uf->type.u.variant.tag_name, |
| 912 | tag_field_name, |
| 913 | LTTNG_UST_SYM_NAME_LEN); |
| 914 | uf->type.u.variant.tag_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 915 | (*iter_output)++; |
| 916 | |
| 917 | /* Serialize choice fields after variant. */ |
| 918 | for (i = 0; i < nr_choices; i++) { |
| 919 | ret = serialize_one_field(session, fields, |
| 920 | iter_output, &choices[i]); |
| 921 | if (ret) |
| 922 | return ret; |
| 923 | } |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | static |
| 928 | int serialize_one_field(struct lttng_session *session, |
| 929 | struct ustctl_field *fields, size_t *iter_output, |
| 930 | const struct lttng_event_field *lf) |
| 931 | { |
| 932 | const struct lttng_type *lt = &lf->type; |
| 933 | int ret; |
| 934 | |
| 935 | /* skip 'nowrite' fields */ |
| 936 | if (lf->nowrite) |
| 937 | return 0; |
| 938 | |
| 939 | switch (lt->atype) { |
| 940 | case atype_integer: |
| 941 | case atype_float: |
| 942 | case atype_string: |
| 943 | case atype_enum: |
| 944 | { |
| 945 | struct ustctl_field *uf = &fields[*iter_output]; |
| 946 | struct ustctl_type *ut = &uf->type; |
| 947 | |
| 948 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
| 949 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 950 | ret = serialize_basic_type(session, &ut->atype, lt->atype, |
| 951 | &ut->u.basic, <->u.basic); |
| 952 | if (ret) |
| 953 | return ret; |
| 954 | (*iter_output)++; |
| 955 | break; |
| 956 | } |
| 957 | case atype_array: |
| 958 | { |
| 959 | struct ustctl_field *uf = &fields[*iter_output]; |
| 960 | struct ustctl_type *ut = &uf->type; |
| 961 | struct ustctl_basic_type *ubt; |
| 962 | const struct lttng_basic_type *lbt; |
| 963 | |
| 964 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
| 965 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 966 | uf->type.atype = ustctl_atype_array; |
| 967 | ubt = &ut->u.array.elem_type; |
| 968 | lbt = <->u.array.elem_type; |
| 969 | ut->u.array.length = lt->u.array.length; |
| 970 | ret = serialize_basic_type(session, &ubt->atype, lbt->atype, |
| 971 | &ubt->u.basic, &lbt->u.basic); |
| 972 | if (ret) |
| 973 | return -EINVAL; |
| 974 | ut->atype = ustctl_atype_array; |
| 975 | (*iter_output)++; |
| 976 | break; |
| 977 | } |
| 978 | case atype_sequence: |
| 979 | { |
| 980 | struct ustctl_field *uf = &fields[*iter_output]; |
| 981 | struct ustctl_type *ut = &uf->type; |
| 982 | struct ustctl_basic_type *ubt; |
| 983 | const struct lttng_basic_type *lbt; |
| 984 | int ret; |
| 985 | |
| 986 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
| 987 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 988 | uf->type.atype = ustctl_atype_sequence; |
| 989 | ubt = &ut->u.sequence.length_type; |
| 990 | lbt = <->u.sequence.length_type; |
| 991 | ret = serialize_basic_type(session, &ubt->atype, lbt->atype, |
| 992 | &ubt->u.basic, &lbt->u.basic); |
| 993 | if (ret) |
| 994 | return -EINVAL; |
| 995 | ubt = &ut->u.sequence.elem_type; |
| 996 | lbt = <->u.sequence.elem_type; |
| 997 | ret = serialize_basic_type(session, &ubt->atype, lbt->atype, |
| 998 | &ubt->u.basic, &lbt->u.basic); |
| 999 | if (ret) |
| 1000 | return -EINVAL; |
| 1001 | ut->atype = ustctl_atype_sequence; |
| 1002 | (*iter_output)++; |
| 1003 | break; |
| 1004 | } |
| 1005 | case atype_dynamic: |
| 1006 | { |
| 1007 | ret = serialize_dynamic_type(session, fields, iter_output, lf); |
| 1008 | if (ret) |
| 1009 | return -EINVAL; |
| 1010 | break; |
| 1011 | } |
| 1012 | case atype_struct: |
| 1013 | { |
| 1014 | struct ustctl_field *uf = &fields[*iter_output]; |
| 1015 | |
| 1016 | /* |
| 1017 | * TODO: add support for non-empty struct. |
| 1018 | */ |
| 1019 | if (lf->type.u._struct.nr_fields != 0) { |
| 1020 | return -EINVAL; |
| 1021 | } |
| 1022 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
| 1023 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 1024 | uf->type.atype = ustctl_atype_struct; |
| 1025 | uf->type.u._struct.nr_fields = 0; |
| 1026 | (*iter_output)++; |
| 1027 | break; |
| 1028 | } |
| 1029 | default: |
| 1030 | return -EINVAL; |
| 1031 | } |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | static |
| 1036 | int serialize_fields(struct lttng_session *session, |
| 1037 | size_t *_nr_write_fields, |
| 1038 | struct ustctl_field **ustctl_fields, |
| 1039 | size_t nr_fields, |
| 1040 | const struct lttng_event_field *lttng_fields) |
| 1041 | { |
| 1042 | struct ustctl_field *fields; |
| 1043 | int ret; |
| 1044 | size_t i, iter_output = 0; |
| 1045 | ssize_t nr_write_fields; |
| 1046 | |
| 1047 | nr_write_fields = count_fields_recursive(nr_fields, lttng_fields); |
| 1048 | if (nr_write_fields < 0) { |
| 1049 | return (int) nr_write_fields; |
| 1050 | } |
| 1051 | |
| 1052 | fields = zmalloc(nr_write_fields * sizeof(*fields)); |
| 1053 | if (!fields) |
| 1054 | return -ENOMEM; |
| 1055 | |
| 1056 | for (i = 0; i < nr_fields; i++) { |
| 1057 | ret = serialize_one_field(session, fields, &iter_output, |
| 1058 | <tng_fields[i]); |
| 1059 | if (ret) |
| 1060 | goto error_type; |
| 1061 | } |
| 1062 | |
| 1063 | *_nr_write_fields = nr_write_fields; |
| 1064 | *ustctl_fields = fields; |
| 1065 | return 0; |
| 1066 | |
| 1067 | error_type: |
| 1068 | free(fields); |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | static |
| 1073 | int serialize_entries(struct ustctl_enum_entry **_entries, |
| 1074 | size_t nr_entries, |
| 1075 | const struct lttng_enum_entry *lttng_entries) |
| 1076 | { |
| 1077 | struct ustctl_enum_entry *entries; |
| 1078 | int i; |
| 1079 | |
| 1080 | /* Serialize the entries */ |
| 1081 | entries = zmalloc(nr_entries * sizeof(*entries)); |
| 1082 | if (!entries) |
| 1083 | return -ENOMEM; |
| 1084 | for (i = 0; i < nr_entries; i++) { |
| 1085 | struct ustctl_enum_entry *uentry; |
| 1086 | const struct lttng_enum_entry *lentry; |
| 1087 | |
| 1088 | uentry = &entries[i]; |
| 1089 | lentry = <tng_entries[i]; |
| 1090 | |
| 1091 | uentry->start.value = lentry->start.value; |
| 1092 | uentry->start.signedness = lentry->start.signedness; |
| 1093 | uentry->end.value = lentry->end.value; |
| 1094 | uentry->end.signedness = lentry->end.signedness; |
| 1095 | strncpy(uentry->string, lentry->string, LTTNG_UST_SYM_NAME_LEN); |
| 1096 | uentry->string[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 1097 | } |
| 1098 | *_entries = entries; |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static |
| 1103 | int serialize_ctx_fields(struct lttng_session *session, |
| 1104 | size_t *_nr_write_fields, |
| 1105 | struct ustctl_field **ustctl_fields, |
| 1106 | size_t nr_fields, |
| 1107 | const struct lttng_ctx_field *lttng_fields) |
| 1108 | { |
| 1109 | struct ustctl_field *fields; |
| 1110 | int ret; |
| 1111 | size_t i, iter_output = 0; |
| 1112 | ssize_t nr_write_fields; |
| 1113 | |
| 1114 | nr_write_fields = count_ctx_fields_recursive(nr_fields, |
| 1115 | lttng_fields); |
| 1116 | if (nr_write_fields < 0) { |
| 1117 | return (int) nr_write_fields; |
| 1118 | } |
| 1119 | |
| 1120 | fields = zmalloc(nr_write_fields * sizeof(*fields)); |
| 1121 | if (!fields) |
| 1122 | return -ENOMEM; |
| 1123 | |
| 1124 | for (i = 0; i < nr_fields; i++) { |
| 1125 | ret = serialize_one_field(session, fields, &iter_output, |
| 1126 | <tng_fields[i].event_field); |
| 1127 | if (ret) |
| 1128 | goto error_type; |
| 1129 | } |
| 1130 | |
| 1131 | *_nr_write_fields = nr_write_fields; |
| 1132 | *ustctl_fields = fields; |
| 1133 | return 0; |
| 1134 | |
| 1135 | error_type: |
| 1136 | free(fields); |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * Returns 0 on success, negative error value on error. |
| 1142 | */ |
| 1143 | int ustcomm_register_event(int sock, |
| 1144 | struct lttng_session *session, |
| 1145 | int session_objd, /* session descriptor */ |
| 1146 | int channel_objd, /* channel descriptor */ |
| 1147 | const char *event_name, /* event name (input) */ |
| 1148 | int loglevel, |
| 1149 | const char *signature, /* event signature (input) */ |
| 1150 | size_t nr_fields, /* fields */ |
| 1151 | const struct lttng_event_field *lttng_fields, |
| 1152 | const char *model_emf_uri, |
| 1153 | uint32_t *id) /* event id (output) */ |
| 1154 | { |
| 1155 | ssize_t len; |
| 1156 | struct { |
| 1157 | struct ustcomm_notify_hdr header; |
| 1158 | struct ustcomm_notify_event_msg m; |
| 1159 | } msg; |
| 1160 | struct { |
| 1161 | struct ustcomm_notify_hdr header; |
| 1162 | struct ustcomm_notify_event_reply r; |
| 1163 | } reply; |
| 1164 | size_t signature_len, fields_len, model_emf_uri_len; |
| 1165 | struct ustctl_field *fields = NULL; |
| 1166 | size_t nr_write_fields = 0; |
| 1167 | int ret; |
| 1168 | |
| 1169 | memset(&msg, 0, sizeof(msg)); |
| 1170 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; |
| 1171 | msg.m.session_objd = session_objd; |
| 1172 | msg.m.channel_objd = channel_objd; |
| 1173 | strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN); |
| 1174 | msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 1175 | msg.m.loglevel = loglevel; |
| 1176 | signature_len = strlen(signature) + 1; |
| 1177 | msg.m.signature_len = signature_len; |
| 1178 | |
| 1179 | /* Calculate fields len, serialize fields. */ |
| 1180 | if (nr_fields > 0) { |
| 1181 | ret = serialize_fields(session, &nr_write_fields, &fields, |
| 1182 | nr_fields, lttng_fields); |
| 1183 | if (ret) |
| 1184 | return ret; |
| 1185 | } |
| 1186 | |
| 1187 | fields_len = sizeof(*fields) * nr_write_fields; |
| 1188 | msg.m.fields_len = fields_len; |
| 1189 | if (model_emf_uri) { |
| 1190 | model_emf_uri_len = strlen(model_emf_uri) + 1; |
| 1191 | } else { |
| 1192 | model_emf_uri_len = 0; |
| 1193 | } |
| 1194 | msg.m.model_emf_uri_len = model_emf_uri_len; |
| 1195 | |
| 1196 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 1197 | if (len > 0 && len != sizeof(msg)) { |
| 1198 | ret = -EIO; |
| 1199 | goto error_fields; |
| 1200 | } |
| 1201 | if (len < 0) { |
| 1202 | ret = len; |
| 1203 | goto error_fields; |
| 1204 | } |
| 1205 | |
| 1206 | /* send signature */ |
| 1207 | len = ustcomm_send_unix_sock(sock, signature, signature_len); |
| 1208 | if (len > 0 && len != signature_len) { |
| 1209 | ret = -EIO; |
| 1210 | goto error_fields; |
| 1211 | } |
| 1212 | if (len < 0) { |
| 1213 | ret = len; |
| 1214 | goto error_fields; |
| 1215 | } |
| 1216 | |
| 1217 | /* send fields */ |
| 1218 | if (fields_len > 0) { |
| 1219 | len = ustcomm_send_unix_sock(sock, fields, fields_len); |
| 1220 | if (len > 0 && len != fields_len) { |
| 1221 | ret = -EIO; |
| 1222 | goto error_fields; |
| 1223 | } |
| 1224 | if (len < 0) { |
| 1225 | ret = len; |
| 1226 | goto error_fields; |
| 1227 | } |
| 1228 | } |
| 1229 | free(fields); |
| 1230 | |
| 1231 | if (model_emf_uri_len) { |
| 1232 | /* send model_emf_uri */ |
| 1233 | len = ustcomm_send_unix_sock(sock, model_emf_uri, |
| 1234 | model_emf_uri_len); |
| 1235 | if (len > 0 && len != model_emf_uri_len) { |
| 1236 | return -EIO; |
| 1237 | } |
| 1238 | if (len < 0) { |
| 1239 | return len; |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | /* receive reply */ |
| 1244 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); |
| 1245 | switch (len) { |
| 1246 | case 0: /* orderly shutdown */ |
| 1247 | return -EPIPE; |
| 1248 | case sizeof(reply): |
| 1249 | if (reply.header.notify_cmd != msg.header.notify_cmd) { |
| 1250 | ERR("Unexpected result message command " |
| 1251 | "expected: %u vs received: %u\n", |
| 1252 | msg.header.notify_cmd, reply.header.notify_cmd); |
| 1253 | return -EINVAL; |
| 1254 | } |
| 1255 | if (reply.r.ret_code > 0) |
| 1256 | return -EINVAL; |
| 1257 | if (reply.r.ret_code < 0) |
| 1258 | return reply.r.ret_code; |
| 1259 | *id = reply.r.event_id; |
| 1260 | DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n", |
| 1261 | event_name, reply.r.ret_code, reply.r.event_id); |
| 1262 | return 0; |
| 1263 | default: |
| 1264 | if (len < 0) { |
| 1265 | /* Transport level error */ |
| 1266 | if (errno == EPIPE || errno == ECONNRESET) |
| 1267 | len = -errno; |
| 1268 | return len; |
| 1269 | } else { |
| 1270 | ERR("incorrect message size: %zd\n", len); |
| 1271 | return len; |
| 1272 | } |
| 1273 | } |
| 1274 | /* Unreached. */ |
| 1275 | |
| 1276 | /* Error path only. */ |
| 1277 | error_fields: |
| 1278 | free(fields); |
| 1279 | return ret; |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * Returns 0 on success, negative error value on error. |
| 1284 | * Returns -EPIPE or -ECONNRESET if other end has hung up. |
| 1285 | */ |
| 1286 | int ustcomm_register_enum(int sock, |
| 1287 | int session_objd, /* session descriptor */ |
| 1288 | const char *enum_name, /* enum name (input) */ |
| 1289 | size_t nr_entries, /* entries */ |
| 1290 | const struct lttng_enum_entry *lttng_entries, |
| 1291 | uint64_t *id) |
| 1292 | { |
| 1293 | ssize_t len; |
| 1294 | struct { |
| 1295 | struct ustcomm_notify_hdr header; |
| 1296 | struct ustcomm_notify_enum_msg m; |
| 1297 | } msg; |
| 1298 | struct { |
| 1299 | struct ustcomm_notify_hdr header; |
| 1300 | struct ustcomm_notify_enum_reply r; |
| 1301 | } reply; |
| 1302 | size_t entries_len; |
| 1303 | struct ustctl_enum_entry *entries = NULL; |
| 1304 | int ret; |
| 1305 | |
| 1306 | memset(&msg, 0, sizeof(msg)); |
| 1307 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM; |
| 1308 | msg.m.session_objd = session_objd; |
| 1309 | strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN); |
| 1310 | msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 1311 | |
| 1312 | /* Calculate entries len, serialize entries. */ |
| 1313 | if (nr_entries > 0) { |
| 1314 | ret = serialize_entries(&entries, |
| 1315 | nr_entries, lttng_entries); |
| 1316 | if (ret) |
| 1317 | return ret; |
| 1318 | } |
| 1319 | |
| 1320 | entries_len = sizeof(*entries) * nr_entries; |
| 1321 | msg.m.entries_len = entries_len; |
| 1322 | |
| 1323 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 1324 | if (len > 0 && len != sizeof(msg)) { |
| 1325 | ret = -EIO; |
| 1326 | goto error_entries; |
| 1327 | } |
| 1328 | if (len < 0) { |
| 1329 | ret = len; |
| 1330 | goto error_entries; |
| 1331 | } |
| 1332 | |
| 1333 | /* send entries */ |
| 1334 | if (entries_len > 0) { |
| 1335 | len = ustcomm_send_unix_sock(sock, entries, entries_len); |
| 1336 | if (len > 0 && len != entries_len) { |
| 1337 | ret = -EIO; |
| 1338 | goto error_entries; |
| 1339 | } |
| 1340 | if (len < 0) { |
| 1341 | ret = len; |
| 1342 | goto error_entries; |
| 1343 | } |
| 1344 | } |
| 1345 | free(entries); |
| 1346 | entries = NULL; |
| 1347 | |
| 1348 | /* receive reply */ |
| 1349 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); |
| 1350 | switch (len) { |
| 1351 | case 0: /* orderly shutdown */ |
| 1352 | return -EPIPE; |
| 1353 | case sizeof(reply): |
| 1354 | if (reply.header.notify_cmd != msg.header.notify_cmd) { |
| 1355 | ERR("Unexpected result message command " |
| 1356 | "expected: %u vs received: %u\n", |
| 1357 | msg.header.notify_cmd, reply.header.notify_cmd); |
| 1358 | return -EINVAL; |
| 1359 | } |
| 1360 | if (reply.r.ret_code > 0) |
| 1361 | return -EINVAL; |
| 1362 | if (reply.r.ret_code < 0) |
| 1363 | return reply.r.ret_code; |
| 1364 | *id = reply.r.enum_id; |
| 1365 | DBG("Sent register enum notification for name \"%s\": ret_code %d\n", |
| 1366 | enum_name, reply.r.ret_code); |
| 1367 | return 0; |
| 1368 | default: |
| 1369 | if (len < 0) { |
| 1370 | /* Transport level error */ |
| 1371 | if (errno == EPIPE || errno == ECONNRESET) |
| 1372 | len = -errno; |
| 1373 | return len; |
| 1374 | } else { |
| 1375 | ERR("incorrect message size: %zd\n", len); |
| 1376 | return len; |
| 1377 | } |
| 1378 | } |
| 1379 | return ret; |
| 1380 | |
| 1381 | error_entries: |
| 1382 | free(entries); |
| 1383 | return ret; |
| 1384 | } |
| 1385 | |
| 1386 | /* |
| 1387 | * Returns 0 on success, negative error value on error. |
| 1388 | * Returns -EPIPE or -ECONNRESET if other end has hung up. |
| 1389 | */ |
| 1390 | int ustcomm_register_channel(int sock, |
| 1391 | struct lttng_session *session, |
| 1392 | int session_objd, /* session descriptor */ |
| 1393 | int channel_objd, /* channel descriptor */ |
| 1394 | size_t nr_ctx_fields, |
| 1395 | const struct lttng_ctx_field *ctx_fields, |
| 1396 | uint32_t *chan_id, /* channel id (output) */ |
| 1397 | int *header_type) /* header type (output) */ |
| 1398 | { |
| 1399 | ssize_t len; |
| 1400 | struct { |
| 1401 | struct ustcomm_notify_hdr header; |
| 1402 | struct ustcomm_notify_channel_msg m; |
| 1403 | } msg; |
| 1404 | struct { |
| 1405 | struct ustcomm_notify_hdr header; |
| 1406 | struct ustcomm_notify_channel_reply r; |
| 1407 | } reply; |
| 1408 | size_t fields_len; |
| 1409 | struct ustctl_field *fields = NULL; |
| 1410 | int ret; |
| 1411 | size_t nr_write_fields = 0; |
| 1412 | |
| 1413 | memset(&msg, 0, sizeof(msg)); |
| 1414 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; |
| 1415 | msg.m.session_objd = session_objd; |
| 1416 | msg.m.channel_objd = channel_objd; |
| 1417 | |
| 1418 | /* Calculate fields len, serialize fields. */ |
| 1419 | if (nr_ctx_fields > 0) { |
| 1420 | ret = serialize_ctx_fields(session, &nr_write_fields, &fields, |
| 1421 | nr_ctx_fields, ctx_fields); |
| 1422 | if (ret) |
| 1423 | return ret; |
| 1424 | } |
| 1425 | |
| 1426 | fields_len = sizeof(*fields) * nr_write_fields; |
| 1427 | msg.m.ctx_fields_len = fields_len; |
| 1428 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 1429 | if (len > 0 && len != sizeof(msg)) { |
| 1430 | free(fields); |
| 1431 | return -EIO; |
| 1432 | } |
| 1433 | if (len < 0) { |
| 1434 | free(fields); |
| 1435 | return len; |
| 1436 | } |
| 1437 | |
| 1438 | /* send fields */ |
| 1439 | if (fields_len > 0) { |
| 1440 | len = ustcomm_send_unix_sock(sock, fields, fields_len); |
| 1441 | free(fields); |
| 1442 | if (len > 0 && len != fields_len) { |
| 1443 | return -EIO; |
| 1444 | } |
| 1445 | if (len < 0) { |
| 1446 | return len; |
| 1447 | } |
| 1448 | } else { |
| 1449 | free(fields); |
| 1450 | } |
| 1451 | |
| 1452 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); |
| 1453 | switch (len) { |
| 1454 | case 0: /* orderly shutdown */ |
| 1455 | return -EPIPE; |
| 1456 | case sizeof(reply): |
| 1457 | if (reply.header.notify_cmd != msg.header.notify_cmd) { |
| 1458 | ERR("Unexpected result message command " |
| 1459 | "expected: %u vs received: %u\n", |
| 1460 | msg.header.notify_cmd, reply.header.notify_cmd); |
| 1461 | return -EINVAL; |
| 1462 | } |
| 1463 | if (reply.r.ret_code > 0) |
| 1464 | return -EINVAL; |
| 1465 | if (reply.r.ret_code < 0) |
| 1466 | return reply.r.ret_code; |
| 1467 | *chan_id = reply.r.chan_id; |
| 1468 | switch (reply.r.header_type) { |
| 1469 | case 1: |
| 1470 | case 2: |
| 1471 | *header_type = reply.r.header_type; |
| 1472 | break; |
| 1473 | default: |
| 1474 | ERR("Unexpected channel header type %u\n", |
| 1475 | reply.r.header_type); |
| 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | DBG("Sent register channel notification: chan_id %d, header_type %d\n", |
| 1479 | reply.r.chan_id, reply.r.header_type); |
| 1480 | return 0; |
| 1481 | default: |
| 1482 | if (len < 0) { |
| 1483 | /* Transport level error */ |
| 1484 | if (errno == EPIPE || errno == ECONNRESET) |
| 1485 | len = -errno; |
| 1486 | return len; |
| 1487 | } else { |
| 1488 | ERR("incorrect message size: %zd\n", len); |
| 1489 | return len; |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Set socket reciving timeout. |
| 1496 | */ |
| 1497 | int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec) |
| 1498 | { |
| 1499 | int ret; |
| 1500 | struct timeval tv; |
| 1501 | |
| 1502 | tv.tv_sec = msec / 1000; |
| 1503 | tv.tv_usec = (msec * 1000 % 1000000); |
| 1504 | |
| 1505 | ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 1506 | if (ret < 0) { |
| 1507 | PERROR("setsockopt SO_RCVTIMEO"); |
| 1508 | ret = -errno; |
| 1509 | } |
| 1510 | |
| 1511 | return ret; |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * Set socket sending timeout. |
| 1516 | */ |
| 1517 | int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec) |
| 1518 | { |
| 1519 | int ret; |
| 1520 | struct timeval tv; |
| 1521 | |
| 1522 | tv.tv_sec = msec / 1000; |
| 1523 | tv.tv_usec = (msec * 1000) % 1000000; |
| 1524 | |
| 1525 | ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); |
| 1526 | if (ret < 0) { |
| 1527 | PERROR("setsockopt SO_SNDTIMEO"); |
| 1528 | ret = -errno; |
| 1529 | } |
| 1530 | |
| 1531 | return ret; |
| 1532 | } |