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