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