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