Fix: enable event with different loglevel error
[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 [ 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 /*
459 * Send a message with credentials over a unix socket.
460 *
461 * Returns the size of data sent, or negative error value.
462 */
463 ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len)
464 {
465 struct msghdr msg;
466 struct iovec iov[1];
467 ssize_t ret = -1;
468 #ifdef __linux__
469 struct cmsghdr *cmptr;
470 size_t sizeof_cred = sizeof(lttng_sock_cred);
471 char anc_buf[CMSG_SPACE(sizeof_cred)];
472 lttng_sock_cred *creds;
473 #endif /* __linux__ */
474
475 memset(&msg, 0, sizeof(msg));
476
477 iov[0].iov_base = buf;
478 iov[0].iov_len = len;
479 msg.msg_iov = iov;
480 msg.msg_iovlen = 1;
481
482 #ifdef __linux__
483 msg.msg_control = (caddr_t) anc_buf;
484 msg.msg_controllen = CMSG_LEN(sizeof_cred);
485
486 cmptr = CMSG_FIRSTHDR(&msg);
487 cmptr->cmsg_level = SOL_SOCKET;
488 cmptr->cmsg_type = LTTNG_SOCK_CREDS;
489 cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
490
491 creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
492
493 LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
494 LTTNG_SOCK_SET_GID_CRED(creds, getegid());
495 LTTNG_SOCK_SET_PID_CRED(creds, getpid());
496 #endif /* __linux__ */
497
498 do {
499 ret = sendmsg(sock, &msg, 0);
500 } while (ret < 0 && errno == EINTR);
501 if (ret < 0) {
502 /*
503 * Only warn about EPIPE when quiet mode is deactivated.
504 * We consider EPIPE as expected.
505 */
506 if (errno != EPIPE || !lttng_opt_quiet) {
507 PERROR("sendmsg");
508 }
509 }
510 return ret;
511 }
512
513 /*
514 * Recv a message accompanied with credentials from a unix socket.
515 *
516 * Returns the size of received data, or negative error value.
517 */
518 ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
519 lttng_sock_cred *creds)
520 {
521 struct msghdr msg;
522 struct iovec iov[1];
523 ssize_t ret;
524 #ifdef __linux__
525 struct cmsghdr *cmptr;
526 size_t sizeof_cred = sizeof(lttng_sock_cred);
527 char anc_buf[CMSG_SPACE(sizeof_cred)];
528 #endif /* __linux__ */
529
530 memset(&msg, 0, sizeof(msg));
531
532 /* Not allowed */
533 if (creds == NULL) {
534 ret = -1;
535 goto end;
536 }
537
538 /* Prepare to receive the structures */
539 iov[0].iov_base = buf;
540 iov[0].iov_len = len;
541 msg.msg_iov = iov;
542 msg.msg_iovlen = 1;
543
544 #ifdef __linux__
545 msg.msg_control = anc_buf;
546 msg.msg_controllen = sizeof(anc_buf);
547 #endif /* __linux__ */
548
549 do {
550 ret = recvmsg(sock, &msg, 0);
551 } while (ret < 0 && errno == EINTR);
552 if (ret < 0) {
553 PERROR("recvmsg fds");
554 goto end;
555 }
556
557 #ifdef __linux__
558 if (msg.msg_flags & MSG_CTRUNC) {
559 fprintf(stderr, "Error: Control message truncated.\n");
560 ret = -1;
561 goto end;
562 }
563
564 cmptr = CMSG_FIRSTHDR(&msg);
565 if (cmptr == NULL) {
566 fprintf(stderr, "Error: Invalid control message header\n");
567 ret = -1;
568 goto end;
569 }
570
571 if (cmptr->cmsg_level != SOL_SOCKET ||
572 cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
573 fprintf(stderr, "Didn't received any credentials\n");
574 ret = -1;
575 goto end;
576 }
577
578 if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) {
579 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
580 (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
581 ret = -1;
582 goto end;
583 }
584
585 memcpy(creds, CMSG_DATA(cmptr), sizeof_cred);
586 #elif defined(__FreeBSD__)
587 {
588 int peer_ret;
589
590 peer_ret = getpeereid(sock, &creds->uid, &creds->gid);
591 if (peer_ret != 0) {
592 return peer_ret;
593 }
594 }
595 #else
596 #error "Please implement credential support for your OS."
597 #endif /* __linux__ */
598
599 end:
600 return ret;
601 }
602
603 /*
604 * Set socket option to use credentials passing.
605 */
606 #ifdef __linux__
607 int lttcomm_setsockopt_creds_unix_sock(int sock)
608 {
609 int ret, on = 1;
610
611 /* Set socket for credentials retrieval */
612 ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
613 if (ret < 0) {
614 PERROR("setsockopt creds unix sock");
615 }
616 return ret;
617 }
618 #elif defined(__FreeBSD__)
619 int lttcomm_setsockopt_creds_unix_sock(int sock)
620 {
621 return 0;
622 }
623 #else
624 #error "Please implement credential support for your OS."
625 #endif /* __linux__ */
This page took 0.043078 seconds and 4 git commands to generate.