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