| 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 | |
| 109 | [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready", |
| 110 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds", |
| 111 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds", |
| 112 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command", |
| 113 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread", |
| 114 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd", |
| 115 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up", |
| 116 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally", |
| 117 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error", |
| 118 | [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile", |
| 119 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF", |
| 120 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL", |
| 121 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM", |
| 122 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE", |
| 123 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found", |
| 124 | [ LTTCOMM_ERR_INDEX(LTTCOMM_INVALID) ] = "Invalid parameter", |
| 125 | }; |
| 126 | |
| 127 | /* |
| 128 | * Return ptr to string representing a human readable error code from the |
| 129 | * lttcomm_return_code enum. |
| 130 | * |
| 131 | * These code MUST be negative in other to treat that as an error value. |
| 132 | */ |
| 133 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) |
| 134 | { |
| 135 | int tmp_code = -code; |
| 136 | |
| 137 | if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) { |
| 138 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)]; |
| 139 | } |
| 140 | |
| 141 | return "Unknown error code"; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Connect to unix socket using the path name. |
| 146 | */ |
| 147 | int lttcomm_connect_unix_sock(const char *pathname) |
| 148 | { |
| 149 | struct sockaddr_un sun; |
| 150 | int fd, ret, closeret; |
| 151 | |
| 152 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 153 | if (fd < 0) { |
| 154 | PERROR("socket"); |
| 155 | ret = fd; |
| 156 | goto error; |
| 157 | } |
| 158 | |
| 159 | memset(&sun, 0, sizeof(sun)); |
| 160 | sun.sun_family = AF_UNIX; |
| 161 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 162 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
| 163 | |
| 164 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 165 | if (ret < 0) { |
| 166 | /* |
| 167 | * Don't print message on connect error, because connect is used in |
| 168 | * normal execution to detect if sessiond is alive. |
| 169 | */ |
| 170 | goto error_connect; |
| 171 | } |
| 172 | |
| 173 | return fd; |
| 174 | |
| 175 | error_connect: |
| 176 | closeret = close(fd); |
| 177 | if (closeret) { |
| 178 | PERROR("close"); |
| 179 | } |
| 180 | error: |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Do an accept(2) on the sock and return the new file descriptor. The socket |
| 186 | * MUST be bind(2) before. |
| 187 | */ |
| 188 | int lttcomm_accept_unix_sock(int sock) |
| 189 | { |
| 190 | int new_fd; |
| 191 | struct sockaddr_un sun; |
| 192 | socklen_t len = 0; |
| 193 | |
| 194 | /* Blocking call */ |
| 195 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); |
| 196 | if (new_fd < 0) { |
| 197 | PERROR("accept"); |
| 198 | } |
| 199 | |
| 200 | return new_fd; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation |
| 205 | * and return the fd. |
| 206 | */ |
| 207 | int lttcomm_create_unix_sock(const char *pathname) |
| 208 | { |
| 209 | struct sockaddr_un sun; |
| 210 | int fd; |
| 211 | int ret = -1; |
| 212 | |
| 213 | /* Create server socket */ |
| 214 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 215 | PERROR("socket"); |
| 216 | goto error; |
| 217 | } |
| 218 | |
| 219 | memset(&sun, 0, sizeof(sun)); |
| 220 | sun.sun_family = AF_UNIX; |
| 221 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 222 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
| 223 | |
| 224 | /* Unlink the old file if present */ |
| 225 | (void) unlink(pathname); |
| 226 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 227 | if (ret < 0) { |
| 228 | PERROR("bind"); |
| 229 | goto error; |
| 230 | } |
| 231 | |
| 232 | return fd; |
| 233 | |
| 234 | error: |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
| 240 | */ |
| 241 | int lttcomm_listen_unix_sock(int sock) |
| 242 | { |
| 243 | int ret; |
| 244 | |
| 245 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); |
| 246 | if (ret < 0) { |
| 247 | PERROR("listen"); |
| 248 | } |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Receive data of size len in put that data into the buf param. Using recvmsg |
| 255 | * API. |
| 256 | * |
| 257 | * Return the size of received data. |
| 258 | */ |
| 259 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) |
| 260 | { |
| 261 | struct msghdr msg; |
| 262 | struct iovec iov[1]; |
| 263 | ssize_t ret = -1; |
| 264 | |
| 265 | memset(&msg, 0, sizeof(msg)); |
| 266 | |
| 267 | iov[0].iov_base = buf; |
| 268 | iov[0].iov_len = len; |
| 269 | msg.msg_iov = iov; |
| 270 | msg.msg_iovlen = 1; |
| 271 | |
| 272 | do { |
| 273 | ret = recvmsg(sock, &msg, MSG_WAITALL); |
| 274 | } while (ret < 0 && errno == EINTR); |
| 275 | if (ret < 0) { |
| 276 | PERROR("recvmsg"); |
| 277 | } |
| 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Send buf data of size len. Using sendmsg API. |
| 284 | * |
| 285 | * Return the size of sent data. |
| 286 | */ |
| 287 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) |
| 288 | { |
| 289 | struct msghdr msg; |
| 290 | struct iovec iov[1]; |
| 291 | ssize_t ret = -1; |
| 292 | |
| 293 | memset(&msg, 0, sizeof(msg)); |
| 294 | |
| 295 | iov[0].iov_base = buf; |
| 296 | iov[0].iov_len = len; |
| 297 | msg.msg_iov = iov; |
| 298 | msg.msg_iovlen = 1; |
| 299 | |
| 300 | ret = sendmsg(sock, &msg, 0); |
| 301 | if (ret < 0) { |
| 302 | /* |
| 303 | * Only warn about EPIPE when quiet mode is deactivated. |
| 304 | * We consider EPIPE as expected. |
| 305 | */ |
| 306 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 307 | PERROR("sendmsg"); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Shutdown cleanly a unix socket. |
| 316 | */ |
| 317 | int lttcomm_close_unix_sock(int sock) |
| 318 | { |
| 319 | int ret, closeret; |
| 320 | |
| 321 | /* Shutdown receptions and transmissions */ |
| 322 | ret = shutdown(sock, SHUT_RDWR); |
| 323 | if (ret < 0) { |
| 324 | PERROR("shutdown"); |
| 325 | } |
| 326 | |
| 327 | closeret = close(sock); |
| 328 | if (closeret) { |
| 329 | PERROR("close"); |
| 330 | } |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Send a message accompanied by fd(s) over a unix socket. |
| 337 | * |
| 338 | * Returns the size of data sent, or negative error value. |
| 339 | */ |
| 340 | ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 341 | { |
| 342 | struct msghdr msg; |
| 343 | struct cmsghdr *cmptr; |
| 344 | struct iovec iov[1]; |
| 345 | ssize_t ret = -1; |
| 346 | unsigned int sizeof_fds = nb_fd * sizeof(int); |
| 347 | char tmp[CMSG_SPACE(sizeof_fds)]; |
| 348 | char dummy = 0; |
| 349 | |
| 350 | memset(&msg, 0, sizeof(msg)); |
| 351 | |
| 352 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | msg.msg_control = (caddr_t)tmp; |
| 356 | msg.msg_controllen = CMSG_LEN(sizeof_fds); |
| 357 | |
| 358 | cmptr = CMSG_FIRSTHDR(&msg); |
| 359 | cmptr->cmsg_level = SOL_SOCKET; |
| 360 | cmptr->cmsg_type = SCM_RIGHTS; |
| 361 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
| 362 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
| 363 | /* Sum of the length of all control messages in the buffer: */ |
| 364 | msg.msg_controllen = cmptr->cmsg_len; |
| 365 | |
| 366 | iov[0].iov_base = &dummy; |
| 367 | iov[0].iov_len = 1; |
| 368 | msg.msg_iov = iov; |
| 369 | msg.msg_iovlen = 1; |
| 370 | |
| 371 | do { |
| 372 | ret = sendmsg(sock, &msg, 0); |
| 373 | } while (ret < 0 && errno == EINTR); |
| 374 | if (ret < 0) { |
| 375 | /* |
| 376 | * Only warn about EPIPE when quiet mode is deactivated. |
| 377 | * We consider EPIPE as expected. |
| 378 | */ |
| 379 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 380 | PERROR("sendmsg"); |
| 381 | } |
| 382 | } |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Recv a message accompanied by fd(s) from a unix socket. |
| 388 | * |
| 389 | * Returns the size of received data, or negative error value. |
| 390 | * |
| 391 | * Expect at most "nb_fd" file descriptors. Returns the number of fd |
| 392 | * actually received in nb_fd. |
| 393 | */ |
| 394 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
| 395 | { |
| 396 | struct iovec iov[1]; |
| 397 | ssize_t ret = 0; |
| 398 | struct cmsghdr *cmsg; |
| 399 | size_t sizeof_fds = nb_fd * sizeof(int); |
| 400 | char recv_fd[CMSG_SPACE(sizeof_fds)]; |
| 401 | struct msghdr msg; |
| 402 | char dummy; |
| 403 | |
| 404 | memset(&msg, 0, sizeof(msg)); |
| 405 | |
| 406 | /* Prepare to receive the structures */ |
| 407 | iov[0].iov_base = &dummy; |
| 408 | iov[0].iov_len = 1; |
| 409 | msg.msg_iov = iov; |
| 410 | msg.msg_iovlen = 1; |
| 411 | msg.msg_control = recv_fd; |
| 412 | msg.msg_controllen = sizeof(recv_fd); |
| 413 | |
| 414 | do { |
| 415 | ret = recvmsg(sock, &msg, 0); |
| 416 | } while (ret < 0 && errno == EINTR); |
| 417 | if (ret < 0) { |
| 418 | PERROR("recvmsg fds"); |
| 419 | goto end; |
| 420 | } |
| 421 | if (ret != 1) { |
| 422 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", |
| 423 | ret, 1); |
| 424 | goto end; |
| 425 | } |
| 426 | if (msg.msg_flags & MSG_CTRUNC) { |
| 427 | fprintf(stderr, "Error: Control message truncated.\n"); |
| 428 | ret = -1; |
| 429 | goto end; |
| 430 | } |
| 431 | cmsg = CMSG_FIRSTHDR(&msg); |
| 432 | if (!cmsg) { |
| 433 | fprintf(stderr, "Error: Invalid control message header\n"); |
| 434 | ret = -1; |
| 435 | goto end; |
| 436 | } |
| 437 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
| 438 | fprintf(stderr, "Didn't received any fd\n"); |
| 439 | ret = -1; |
| 440 | goto end; |
| 441 | } |
| 442 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
| 443 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", |
| 444 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); |
| 445 | ret = -1; |
| 446 | goto end; |
| 447 | } |
| 448 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
| 449 | ret = sizeof_fds; |
| 450 | end: |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Send a message with credentials over a unix socket. |
| 456 | * |
| 457 | * Returns the size of data sent, or negative error value. |
| 458 | */ |
| 459 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) |
| 460 | { |
| 461 | struct msghdr msg; |
| 462 | struct iovec iov[1]; |
| 463 | ssize_t ret = -1; |
| 464 | #ifdef __linux__ |
| 465 | struct cmsghdr *cmptr; |
| 466 | size_t sizeof_cred = sizeof(lttng_sock_cred); |
| 467 | char anc_buf[CMSG_SPACE(sizeof_cred)]; |
| 468 | lttng_sock_cred *creds; |
| 469 | #endif /* __linux__ */ |
| 470 | |
| 471 | memset(&msg, 0, sizeof(msg)); |
| 472 | |
| 473 | iov[0].iov_base = buf; |
| 474 | iov[0].iov_len = len; |
| 475 | msg.msg_iov = iov; |
| 476 | msg.msg_iovlen = 1; |
| 477 | |
| 478 | #ifdef __linux__ |
| 479 | msg.msg_control = (caddr_t) anc_buf; |
| 480 | msg.msg_controllen = CMSG_LEN(sizeof_cred); |
| 481 | |
| 482 | cmptr = CMSG_FIRSTHDR(&msg); |
| 483 | cmptr->cmsg_level = SOL_SOCKET; |
| 484 | cmptr->cmsg_type = LTTNG_SOCK_CREDS; |
| 485 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); |
| 486 | |
| 487 | creds = (lttng_sock_cred*) CMSG_DATA(cmptr); |
| 488 | |
| 489 | LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); |
| 490 | LTTNG_SOCK_SET_GID_CRED(creds, getegid()); |
| 491 | LTTNG_SOCK_SET_PID_CRED(creds, getpid()); |
| 492 | #endif /* __linux__ */ |
| 493 | |
| 494 | do { |
| 495 | ret = sendmsg(sock, &msg, 0); |
| 496 | } while (ret < 0 && errno == EINTR); |
| 497 | if (ret < 0) { |
| 498 | /* |
| 499 | * Only warn about EPIPE when quiet mode is deactivated. |
| 500 | * We consider EPIPE as expected. |
| 501 | */ |
| 502 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 503 | PERROR("sendmsg"); |
| 504 | } |
| 505 | } |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Recv a message accompanied with credentials from a unix socket. |
| 511 | * |
| 512 | * Returns the size of received data, or negative error value. |
| 513 | */ |
| 514 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, |
| 515 | lttng_sock_cred *creds) |
| 516 | { |
| 517 | struct msghdr msg; |
| 518 | struct iovec iov[1]; |
| 519 | ssize_t ret; |
| 520 | #ifdef __linux__ |
| 521 | struct cmsghdr *cmptr; |
| 522 | size_t sizeof_cred = sizeof(lttng_sock_cred); |
| 523 | char anc_buf[CMSG_SPACE(sizeof_cred)]; |
| 524 | #endif /* __linux__ */ |
| 525 | |
| 526 | memset(&msg, 0, sizeof(msg)); |
| 527 | |
| 528 | /* Not allowed */ |
| 529 | if (creds == NULL) { |
| 530 | ret = -1; |
| 531 | goto end; |
| 532 | } |
| 533 | |
| 534 | /* Prepare to receive the structures */ |
| 535 | iov[0].iov_base = buf; |
| 536 | iov[0].iov_len = len; |
| 537 | msg.msg_iov = iov; |
| 538 | msg.msg_iovlen = 1; |
| 539 | |
| 540 | #ifdef __linux__ |
| 541 | msg.msg_control = anc_buf; |
| 542 | msg.msg_controllen = sizeof(anc_buf); |
| 543 | #endif /* __linux__ */ |
| 544 | |
| 545 | do { |
| 546 | ret = recvmsg(sock, &msg, 0); |
| 547 | } while (ret < 0 && errno == EINTR); |
| 548 | if (ret < 0) { |
| 549 | PERROR("recvmsg fds"); |
| 550 | goto end; |
| 551 | } |
| 552 | |
| 553 | #ifdef __linux__ |
| 554 | if (msg.msg_flags & MSG_CTRUNC) { |
| 555 | fprintf(stderr, "Error: Control message truncated.\n"); |
| 556 | ret = -1; |
| 557 | goto end; |
| 558 | } |
| 559 | |
| 560 | cmptr = CMSG_FIRSTHDR(&msg); |
| 561 | if (cmptr == NULL) { |
| 562 | fprintf(stderr, "Error: Invalid control message header\n"); |
| 563 | ret = -1; |
| 564 | goto end; |
| 565 | } |
| 566 | |
| 567 | if (cmptr->cmsg_level != SOL_SOCKET || |
| 568 | cmptr->cmsg_type != LTTNG_SOCK_CREDS) { |
| 569 | fprintf(stderr, "Didn't received any credentials\n"); |
| 570 | ret = -1; |
| 571 | goto end; |
| 572 | } |
| 573 | |
| 574 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { |
| 575 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", |
| 576 | (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); |
| 577 | ret = -1; |
| 578 | goto end; |
| 579 | } |
| 580 | |
| 581 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); |
| 582 | #elif defined(__FreeBSD__) |
| 583 | { |
| 584 | int peer_ret; |
| 585 | |
| 586 | peer_ret = getpeereid(sock, &creds->uid, &creds->gid); |
| 587 | if (peer_ret != 0) { |
| 588 | return peer_ret; |
| 589 | } |
| 590 | } |
| 591 | #else |
| 592 | #error "Please implement credential support for your OS." |
| 593 | #endif /* __linux__ */ |
| 594 | |
| 595 | end: |
| 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Set socket option to use credentials passing. |
| 601 | */ |
| 602 | #ifdef __linux__ |
| 603 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
| 604 | { |
| 605 | int ret, on = 1; |
| 606 | |
| 607 | /* Set socket for credentials retrieval */ |
| 608 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); |
| 609 | if (ret < 0) { |
| 610 | PERROR("setsockopt creds unix sock"); |
| 611 | } |
| 612 | return ret; |
| 613 | } |
| 614 | #elif defined(__FreeBSD__) |
| 615 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
| 616 | { |
| 617 | return 0; |
| 618 | } |
| 619 | #else |
| 620 | #error "Please implement credential support for your OS." |
| 621 | #endif /* __linux__ */ |