| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2 only, |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <assert.h> |
| 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include <common/defaults.h> |
| 31 | #include <common/error.h> |
| 32 | |
| 33 | #include "sessiond-comm.h" |
| 34 | |
| 35 | /* |
| 36 | * Human readable error message. |
| 37 | */ |
| 38 | static const char *lttcomm_readable_code[] = { |
| 39 | [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success", |
| 40 | [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error", |
| 41 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command", |
| 42 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented", |
| 43 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain", |
| 44 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found", |
| 45 | [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps", |
| 46 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found", |
| 47 | [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found", |
| 48 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found", |
| 49 | [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon", |
| 50 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_DIR_FAIL) ] = "Create directory failed", |
| 51 | [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed", |
| 52 | [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed", |
| 53 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable", |
| 54 | [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected", |
| 55 | [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist", |
| 56 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket", |
| 57 | [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found", |
| 58 | [ LTTCOMM_ERR_INDEX(LTTCOMM_EPERM) ] = "Permission denied", |
| 59 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available", |
| 60 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_VERSION) ] = "Kernel tracer version is not compatible", |
| 61 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists", |
| 62 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed", |
| 63 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_EXIST) ] = "Kernel channel already exists", |
| 64 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed", |
| 65 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found", |
| 66 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed", |
| 67 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed", |
| 68 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed", |
| 69 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed", |
| 70 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed", |
| 71 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed", |
| 72 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed", |
| 73 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed", |
| 74 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed", |
| 75 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed", |
| 76 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed", |
| 77 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist", |
| 78 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found", |
| 79 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed", |
| 80 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CALIBRATE_FAIL) ] = "UST calibration failed", |
| 81 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_VERSION) ] = "UST tracer version is not compatible", |
| 82 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed", |
| 83 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed", |
| 84 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST) ] = "UST channel already exist", |
| 85 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found", |
| 86 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed", |
| 87 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed", |
| 88 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_FAIL) ] = "Add UST context failed", |
| 89 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_ENABLE_FAIL) ] = "Enable UST event failed", |
| 90 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DISABLE_FAIL) ] = "Disable UST event failed", |
| 91 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_META_FAIL) ] = "Opening metadata failed", |
| 92 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_START_FAIL) ] = "Starting UST trace failed", |
| 93 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STOP_FAIL) ] = "Stoping UST trace failed", |
| 94 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER64_FAIL) ] = "64-bit UST consumer start failed", |
| 95 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER32_FAIL) ] = "32-bit UST consumer start failed", |
| 96 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STREAM_FAIL) ] = "UST create stream failed", |
| 97 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_FAIL) ] = "UST trace directory creation failed", |
| 98 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_EXIST) ] = "UST trace directory already exist", |
| 99 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_NO_SESSION) ] = "No UST session found", |
| 100 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_LIST_FAIL) ] = "Listing UST events failed", |
| 101 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_EXIST) ] = "UST event already exist", |
| 102 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND)] = "UST event not found", |
| 103 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST)] = "UST context already exist", |
| 104 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_INVAL)] = "UST invalid context", |
| 105 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NEED_ROOT_SESSIOND) ] = "Tracing the kernel requires a root lttng-sessiond daemon and \"tracing\" group user membership", |
| 106 | [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STARTED) ] = "Tracing already started", |
| 107 | [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STOPPED) ] = "Tracing already stopped", |
| 108 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_ENOSYS) ] = "Kernel event type not supported", |
| 109 | |
| 110 | [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready", |
| 111 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds", |
| 112 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds", |
| 113 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command", |
| 114 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread", |
| 115 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd", |
| 116 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up", |
| 117 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally", |
| 118 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error", |
| 119 | [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile", |
| 120 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF", |
| 121 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL", |
| 122 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM", |
| 123 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE", |
| 124 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found", |
| 125 | [ LTTCOMM_ERR_INDEX(LTTCOMM_INVALID) ] = "Invalid parameter", |
| 126 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_USTCONSUMERD) ] = "No UST consumer detected", |
| 127 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_KERNCONSUMERD) ] = "No kernel consumer detected", |
| 128 | [ LTTCOMM_ERR_INDEX(LTTCOMM_EVENT_EXIST_LOGLEVEL) ] = "Event already enabled with different loglevel", |
| 129 | }; |
| 130 | |
| 131 | /* |
| 132 | * Return ptr to string representing a human readable error code from the |
| 133 | * lttcomm_return_code enum. |
| 134 | * |
| 135 | * These code MUST be negative in other to treat that as an error value. |
| 136 | */ |
| 137 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) |
| 138 | { |
| 139 | int tmp_code = -code; |
| 140 | |
| 141 | if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) { |
| 142 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)]; |
| 143 | } |
| 144 | |
| 145 | return "Unknown error code"; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Connect to unix socket using the path name. |
| 150 | */ |
| 151 | int lttcomm_connect_unix_sock(const char *pathname) |
| 152 | { |
| 153 | struct sockaddr_un sun; |
| 154 | int fd, ret, closeret; |
| 155 | |
| 156 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 157 | if (fd < 0) { |
| 158 | PERROR("socket"); |
| 159 | ret = fd; |
| 160 | goto error; |
| 161 | } |
| 162 | |
| 163 | memset(&sun, 0, sizeof(sun)); |
| 164 | sun.sun_family = AF_UNIX; |
| 165 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 166 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
| 167 | |
| 168 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 169 | if (ret < 0) { |
| 170 | /* |
| 171 | * Don't print message on connect error, because connect is used in |
| 172 | * normal execution to detect if sessiond is alive. |
| 173 | */ |
| 174 | goto error_connect; |
| 175 | } |
| 176 | |
| 177 | return fd; |
| 178 | |
| 179 | error_connect: |
| 180 | closeret = close(fd); |
| 181 | if (closeret) { |
| 182 | PERROR("close"); |
| 183 | } |
| 184 | error: |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Do an accept(2) on the sock and return the new file descriptor. The socket |
| 190 | * MUST be bind(2) before. |
| 191 | */ |
| 192 | int lttcomm_accept_unix_sock(int sock) |
| 193 | { |
| 194 | int new_fd; |
| 195 | struct sockaddr_un sun; |
| 196 | socklen_t len = 0; |
| 197 | |
| 198 | /* Blocking call */ |
| 199 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); |
| 200 | if (new_fd < 0) { |
| 201 | PERROR("accept"); |
| 202 | } |
| 203 | |
| 204 | return new_fd; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation |
| 209 | * and return the fd. |
| 210 | */ |
| 211 | int lttcomm_create_unix_sock(const char *pathname) |
| 212 | { |
| 213 | struct sockaddr_un sun; |
| 214 | int fd; |
| 215 | int ret = -1; |
| 216 | |
| 217 | /* Create server socket */ |
| 218 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 219 | PERROR("socket"); |
| 220 | goto error; |
| 221 | } |
| 222 | |
| 223 | memset(&sun, 0, sizeof(sun)); |
| 224 | sun.sun_family = AF_UNIX; |
| 225 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 226 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
| 227 | |
| 228 | /* Unlink the old file if present */ |
| 229 | (void) unlink(pathname); |
| 230 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 231 | if (ret < 0) { |
| 232 | PERROR("bind"); |
| 233 | goto error; |
| 234 | } |
| 235 | |
| 236 | return fd; |
| 237 | |
| 238 | error: |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
| 244 | */ |
| 245 | int lttcomm_listen_unix_sock(int sock) |
| 246 | { |
| 247 | int ret; |
| 248 | |
| 249 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); |
| 250 | if (ret < 0) { |
| 251 | PERROR("listen"); |
| 252 | } |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Receive data of size len in put that data into the buf param. Using recvmsg |
| 259 | * API. |
| 260 | * |
| 261 | * Return the size of received data. |
| 262 | */ |
| 263 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) |
| 264 | { |
| 265 | struct msghdr msg; |
| 266 | struct iovec iov[1]; |
| 267 | ssize_t ret = -1; |
| 268 | |
| 269 | memset(&msg, 0, sizeof(msg)); |
| 270 | |
| 271 | iov[0].iov_base = buf; |
| 272 | iov[0].iov_len = len; |
| 273 | msg.msg_iov = iov; |
| 274 | msg.msg_iovlen = 1; |
| 275 | |
| 276 | do { |
| 277 | ret = recvmsg(sock, &msg, MSG_WAITALL); |
| 278 | } while (ret < 0 && errno == EINTR); |
| 279 | if (ret < 0) { |
| 280 | PERROR("recvmsg"); |
| 281 | } |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Send buf data of size len. Using sendmsg API. |
| 288 | * |
| 289 | * Return the size of sent data. |
| 290 | */ |
| 291 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) |
| 292 | { |
| 293 | struct msghdr msg; |
| 294 | struct iovec iov[1]; |
| 295 | ssize_t ret = -1; |
| 296 | |
| 297 | memset(&msg, 0, sizeof(msg)); |
| 298 | |
| 299 | iov[0].iov_base = buf; |
| 300 | iov[0].iov_len = len; |
| 301 | msg.msg_iov = iov; |
| 302 | msg.msg_iovlen = 1; |
| 303 | |
| 304 | ret = sendmsg(sock, &msg, 0); |
| 305 | if (ret < 0) { |
| 306 | /* |
| 307 | * Only warn about EPIPE when quiet mode is deactivated. |
| 308 | * We consider EPIPE as expected. |
| 309 | */ |
| 310 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 311 | PERROR("sendmsg"); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Shutdown cleanly a unix socket. |
| 320 | */ |
| 321 | int lttcomm_close_unix_sock(int sock) |
| 322 | { |
| 323 | int ret, closeret; |
| 324 | |
| 325 | /* Shutdown receptions and transmissions */ |
| 326 | ret = shutdown(sock, SHUT_RDWR); |
| 327 | if (ret < 0) { |
| 328 | PERROR("shutdown"); |
| 329 | } |
| 330 | |
| 331 | closeret = close(sock); |
| 332 | if (closeret) { |
| 333 | PERROR("close"); |
| 334 | } |
| 335 | |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Send a message accompanied by fd(s) over a unix socket. |
| 341 | * |
| 342 | * Returns the size of data sent, or negative error value. |
| 343 | */ |
| 344 | ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 345 | { |
| 346 | struct msghdr msg; |
| 347 | struct cmsghdr *cmptr; |
| 348 | struct iovec iov[1]; |
| 349 | ssize_t ret = -1; |
| 350 | unsigned int sizeof_fds = nb_fd * sizeof(int); |
| 351 | char tmp[CMSG_SPACE(sizeof_fds)]; |
| 352 | char dummy = 0; |
| 353 | |
| 354 | memset(&msg, 0, sizeof(msg)); |
| 355 | |
| 356 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | msg.msg_control = (caddr_t)tmp; |
| 360 | msg.msg_controllen = CMSG_LEN(sizeof_fds); |
| 361 | |
| 362 | cmptr = CMSG_FIRSTHDR(&msg); |
| 363 | cmptr->cmsg_level = SOL_SOCKET; |
| 364 | cmptr->cmsg_type = SCM_RIGHTS; |
| 365 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
| 366 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
| 367 | /* Sum of the length of all control messages in the buffer: */ |
| 368 | msg.msg_controllen = cmptr->cmsg_len; |
| 369 | |
| 370 | iov[0].iov_base = &dummy; |
| 371 | iov[0].iov_len = 1; |
| 372 | msg.msg_iov = iov; |
| 373 | msg.msg_iovlen = 1; |
| 374 | |
| 375 | do { |
| 376 | ret = sendmsg(sock, &msg, 0); |
| 377 | } while (ret < 0 && errno == EINTR); |
| 378 | if (ret < 0) { |
| 379 | /* |
| 380 | * Only warn about EPIPE when quiet mode is deactivated. |
| 381 | * We consider EPIPE as expected. |
| 382 | */ |
| 383 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 384 | PERROR("sendmsg"); |
| 385 | } |
| 386 | } |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Recv a message accompanied by fd(s) from a unix socket. |
| 392 | * |
| 393 | * Returns the size of received data, or negative error value. |
| 394 | * |
| 395 | * Expect at most "nb_fd" file descriptors. Returns the number of fd |
| 396 | * actually received in nb_fd. |
| 397 | */ |
| 398 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 399 | { |
| 400 | struct iovec iov[1]; |
| 401 | ssize_t ret = 0; |
| 402 | struct cmsghdr *cmsg; |
| 403 | size_t sizeof_fds = nb_fd * sizeof(int); |
| 404 | char recv_fd[CMSG_SPACE(sizeof_fds)]; |
| 405 | struct msghdr msg; |
| 406 | char dummy; |
| 407 | |
| 408 | memset(&msg, 0, sizeof(msg)); |
| 409 | |
| 410 | /* Prepare to receive the structures */ |
| 411 | iov[0].iov_base = &dummy; |
| 412 | iov[0].iov_len = 1; |
| 413 | msg.msg_iov = iov; |
| 414 | msg.msg_iovlen = 1; |
| 415 | msg.msg_control = recv_fd; |
| 416 | msg.msg_controllen = sizeof(recv_fd); |
| 417 | |
| 418 | do { |
| 419 | ret = recvmsg(sock, &msg, 0); |
| 420 | } while (ret < 0 && errno == EINTR); |
| 421 | if (ret < 0) { |
| 422 | PERROR("recvmsg fds"); |
| 423 | goto end; |
| 424 | } |
| 425 | if (ret != 1) { |
| 426 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", |
| 427 | ret, 1); |
| 428 | goto end; |
| 429 | } |
| 430 | if (msg.msg_flags & MSG_CTRUNC) { |
| 431 | fprintf(stderr, "Error: Control message truncated.\n"); |
| 432 | ret = -1; |
| 433 | goto end; |
| 434 | } |
| 435 | cmsg = CMSG_FIRSTHDR(&msg); |
| 436 | if (!cmsg) { |
| 437 | fprintf(stderr, "Error: Invalid control message header\n"); |
| 438 | ret = -1; |
| 439 | goto end; |
| 440 | } |
| 441 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
| 442 | fprintf(stderr, "Didn't received any fd\n"); |
| 443 | ret = -1; |
| 444 | goto end; |
| 445 | } |
| 446 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
| 447 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", |
| 448 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); |
| 449 | ret = -1; |
| 450 | goto end; |
| 451 | } |
| 452 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
| 453 | ret = sizeof_fds; |
| 454 | end: |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | ssize_t lttcomm_send_string(int sock, char *str, size_t len) |
| 459 | { |
| 460 | ssize_t slen, ret = -1; |
| 461 | |
| 462 | if (!str) { |
| 463 | goto end; |
| 464 | } |
| 465 | |
| 466 | /* Send string len first */ |
| 467 | slen = lttcomm_send_unix_sock(sock, &len, sizeof(len)); |
| 468 | |
| 469 | if (slen != sizeof(len)) { |
| 470 | fprintf(stderr, |
| 471 | "Unexpected sent size. Expected %zu got %zu\n", |
| 472 | sizeof(len), slen); |
| 473 | ret = -1; |
| 474 | goto end; |
| 475 | } |
| 476 | |
| 477 | /* Send the actual string */ |
| 478 | slen = lttcomm_send_unix_sock(sock, str, len); |
| 479 | if (slen != len) { |
| 480 | fprintf(stderr, |
| 481 | "Unexpected sent size. Expected %zu got %zu\n", |
| 482 | len, slen); |
| 483 | ret = -1; |
| 484 | goto end; |
| 485 | } |
| 486 | |
| 487 | ret = slen; |
| 488 | |
| 489 | end: |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Allocate and return the received string. |
| 495 | * Return NULL on error. |
| 496 | * Caller is responsible of freeing the allocated string. |
| 497 | */ |
| 498 | char *lttcomm_recv_string(int sock) |
| 499 | { |
| 500 | ssize_t rlen; |
| 501 | size_t len; |
| 502 | char *ret; |
| 503 | |
| 504 | /* Get the string len first */ |
| 505 | rlen = lttcomm_recv_unix_sock(sock, &len, sizeof(len)); |
| 506 | |
| 507 | if (rlen != sizeof(len)) { |
| 508 | fprintf(stderr, |
| 509 | "Unexpected received size. Expected %zu got %zu\n", |
| 510 | sizeof(len), rlen); |
| 511 | ret = NULL; |
| 512 | goto end; |
| 513 | } |
| 514 | |
| 515 | /* Account for the NULL byte */ |
| 516 | ret = malloc(len + 1); |
| 517 | if (!ret) { |
| 518 | ret = NULL; |
| 519 | goto end; |
| 520 | } |
| 521 | |
| 522 | /* Get the actual string */ |
| 523 | rlen = lttcomm_recv_unix_sock(sock, ret, len); |
| 524 | if (rlen != len) { |
| 525 | fprintf(stderr, |
| 526 | "Unexpected received size. Expected %zu got %zu\n", |
| 527 | len, rlen); |
| 528 | free(ret); |
| 529 | ret = NULL; |
| 530 | goto end; |
| 531 | } |
| 532 | |
| 533 | /* Set terminating NULL byte */ |
| 534 | ret[len] = '\0'; |
| 535 | |
| 536 | end: |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | /* |
| 542 | * Send a message with credentials over a unix socket. |
| 543 | * |
| 544 | * Returns the size of data sent, or negative error value. |
| 545 | */ |
| 546 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) |
| 547 | { |
| 548 | struct msghdr msg; |
| 549 | struct iovec iov[1]; |
| 550 | ssize_t ret = -1; |
| 551 | #ifdef __linux__ |
| 552 | struct cmsghdr *cmptr; |
| 553 | size_t sizeof_cred = sizeof(lttng_sock_cred); |
| 554 | char anc_buf[CMSG_SPACE(sizeof_cred)]; |
| 555 | lttng_sock_cred *creds; |
| 556 | #endif /* __linux__ */ |
| 557 | |
| 558 | memset(&msg, 0, sizeof(msg)); |
| 559 | |
| 560 | iov[0].iov_base = buf; |
| 561 | iov[0].iov_len = len; |
| 562 | msg.msg_iov = iov; |
| 563 | msg.msg_iovlen = 1; |
| 564 | |
| 565 | #ifdef __linux__ |
| 566 | msg.msg_control = (caddr_t) anc_buf; |
| 567 | msg.msg_controllen = CMSG_LEN(sizeof_cred); |
| 568 | |
| 569 | cmptr = CMSG_FIRSTHDR(&msg); |
| 570 | cmptr->cmsg_level = SOL_SOCKET; |
| 571 | cmptr->cmsg_type = LTTNG_SOCK_CREDS; |
| 572 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); |
| 573 | |
| 574 | creds = (lttng_sock_cred*) CMSG_DATA(cmptr); |
| 575 | |
| 576 | LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); |
| 577 | LTTNG_SOCK_SET_GID_CRED(creds, getegid()); |
| 578 | LTTNG_SOCK_SET_PID_CRED(creds, getpid()); |
| 579 | #endif /* __linux__ */ |
| 580 | |
| 581 | do { |
| 582 | ret = sendmsg(sock, &msg, 0); |
| 583 | } while (ret < 0 && errno == EINTR); |
| 584 | if (ret < 0) { |
| 585 | /* |
| 586 | * Only warn about EPIPE when quiet mode is deactivated. |
| 587 | * We consider EPIPE as expected. |
| 588 | */ |
| 589 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 590 | PERROR("sendmsg"); |
| 591 | } |
| 592 | } |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Recv a message accompanied with credentials from a unix socket. |
| 598 | * |
| 599 | * Returns the size of received data, or negative error value. |
| 600 | */ |
| 601 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, |
| 602 | lttng_sock_cred *creds) |
| 603 | { |
| 604 | struct msghdr msg; |
| 605 | struct iovec iov[1]; |
| 606 | ssize_t ret; |
| 607 | #ifdef __linux__ |
| 608 | struct cmsghdr *cmptr; |
| 609 | size_t sizeof_cred = sizeof(lttng_sock_cred); |
| 610 | char anc_buf[CMSG_SPACE(sizeof_cred)]; |
| 611 | #endif /* __linux__ */ |
| 612 | |
| 613 | memset(&msg, 0, sizeof(msg)); |
| 614 | |
| 615 | /* Not allowed */ |
| 616 | if (creds == NULL) { |
| 617 | ret = -1; |
| 618 | goto end; |
| 619 | } |
| 620 | |
| 621 | /* Prepare to receive the structures */ |
| 622 | iov[0].iov_base = buf; |
| 623 | iov[0].iov_len = len; |
| 624 | msg.msg_iov = iov; |
| 625 | msg.msg_iovlen = 1; |
| 626 | |
| 627 | #ifdef __linux__ |
| 628 | msg.msg_control = anc_buf; |
| 629 | msg.msg_controllen = sizeof(anc_buf); |
| 630 | #endif /* __linux__ */ |
| 631 | |
| 632 | do { |
| 633 | ret = recvmsg(sock, &msg, 0); |
| 634 | } while (ret < 0 && errno == EINTR); |
| 635 | if (ret < 0) { |
| 636 | PERROR("recvmsg fds"); |
| 637 | goto end; |
| 638 | } |
| 639 | |
| 640 | #ifdef __linux__ |
| 641 | if (msg.msg_flags & MSG_CTRUNC) { |
| 642 | fprintf(stderr, "Error: Control message truncated.\n"); |
| 643 | ret = -1; |
| 644 | goto end; |
| 645 | } |
| 646 | |
| 647 | cmptr = CMSG_FIRSTHDR(&msg); |
| 648 | if (cmptr == NULL) { |
| 649 | fprintf(stderr, "Error: Invalid control message header\n"); |
| 650 | ret = -1; |
| 651 | goto end; |
| 652 | } |
| 653 | |
| 654 | if (cmptr->cmsg_level != SOL_SOCKET || |
| 655 | cmptr->cmsg_type != LTTNG_SOCK_CREDS) { |
| 656 | fprintf(stderr, "Didn't received any credentials\n"); |
| 657 | ret = -1; |
| 658 | goto end; |
| 659 | } |
| 660 | |
| 661 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { |
| 662 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", |
| 663 | (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); |
| 664 | ret = -1; |
| 665 | goto end; |
| 666 | } |
| 667 | |
| 668 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); |
| 669 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) |
| 670 | { |
| 671 | int peer_ret; |
| 672 | |
| 673 | peer_ret = getpeereid(sock, &creds->uid, &creds->gid); |
| 674 | if (peer_ret != 0) { |
| 675 | return peer_ret; |
| 676 | } |
| 677 | } |
| 678 | #else |
| 679 | #error "Please implement credential support for your OS." |
| 680 | #endif /* __linux__ */ |
| 681 | |
| 682 | end: |
| 683 | return ret; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Set socket option to use credentials passing. |
| 688 | */ |
| 689 | #ifdef __linux__ |
| 690 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
| 691 | { |
| 692 | int ret, on = 1; |
| 693 | |
| 694 | /* Set socket for credentials retrieval */ |
| 695 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); |
| 696 | if (ret < 0) { |
| 697 | PERROR("setsockopt creds unix sock"); |
| 698 | } |
| 699 | return ret; |
| 700 | } |
| 701 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) |
| 702 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
| 703 | { |
| 704 | return 0; |
| 705 | } |
| 706 | #else |
| 707 | #error "Please implement credential support for your OS." |
| 708 | #endif /* __linux__ */ |