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