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