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