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