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