Fix: report error to client on consumerd error
[lttng-tools.git] / src / common / sessiond-comm / sessiond-comm.c
CommitLineData
826d496d 1/*
917216f6
DG
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fac6795d 4 *
d14d33bf
AM
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.
fac6795d 8 *
917216f6
DG
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.
fac6795d 13 *
d14d33bf
AM
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.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
1e307fab 20#include <assert.h>
fac6795d
DG
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
fac6795d
DG
25#include <sys/stat.h>
26#include <sys/types.h>
fac6795d 27#include <unistd.h>
3bd1e081 28#include <errno.h>
fac6795d 29
990570ed 30#include <common/defaults.h>
4c462e79 31#include <common/error.h>
990570ed 32
10a8a223 33#include "sessiond-comm.h"
fac6795d
DG
34
35/*
36 * Human readable error message.
37 */
38static 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",
7d29a247
DG
42 [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented",
43 [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain",
fac6795d
DG
44 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found",
45 [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
e065084a 46 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found",
f3ed775e 47 [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found",
ce3d728c 48 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found",
ca95a216 49 [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
ae856491 50 [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_DIR_FAIL) ] = "Create directory failed",
379473d2 51 [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed",
520ff687 52 [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed",
379473d2
DG
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected",
27673bb6 55 [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist",
d9800920 56 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket",
2b0bf864 57 [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found",
8e0af1b4 58 [ LTTCOMM_ERR_INDEX(LTTCOMM_EPERM) ] = "Permission denied",
20fe2104 59 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available",
394016d1 60 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_VERSION) ] = "Kernel tracer version is not compatible",
7d29a247 61 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists",
20fe2104 62 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed",
42224349 63 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_EXIST) ] = "Kernel channel already exists",
20fe2104 64 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed",
f3ed775e 65 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found",
26cc6b4e 66 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed",
d36b8583 67 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed",
d65106b1 68 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed",
f34daff7
DG
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed",
70 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed",
aaf26714 71 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed",
8c0faa1d
DG
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",
84291629 78 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found",
2ef84c95 79 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed",
4466912f 80 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CALIBRATE_FAIL) ] = "UST calibration failed",
e0c7ec2b 81 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_VERSION) ] = "UST tracer version is not compatible",
0177d773 82 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed",
2b0bf864 83 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed",
f6a9efaa 84 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST) ] = "UST channel already exist",
2bdd86d4
MD
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",
7753dea8
MD
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",
2bdd86d4
MD
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",
3b25d04d 101 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_EXIST) ] = "UST event already exist",
be4f0921 102 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND)] = "UST event not found",
ae357384 103 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST)] = "UST context already exist",
ae856491 104 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_INVAL)] = "UST invalid context",
30905cf4 105 [ LTTCOMM_ERR_INDEX(LTTCOMM_NEED_ROOT_SESSIOND) ] = "Tracing the kernel requires a root lttng-sessiond daemon and \"tracing\" group user membership",
80e327fa
DG
106 [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STARTED) ] = "Tracing already started",
107 [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STOPPED) ] = "Tracing already stopped",
bd29c13d 108 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_ENOSYS) ] = "Kernel event type not supported",
80e327fa 109
3bd1e081
MD
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",
f2fc6720 113 [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command",
3bd1e081
MD
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",
d65106b1 124 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found",
b024d072 125 [ LTTCOMM_ERR_INDEX(LTTCOMM_INVALID) ] = "Invalid parameter",
5c827ce0
DG
126 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_USTCONSUMERD) ] = "No UST consumer detected",
127 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_KERNCONSUMERD) ] = "No kernel consumer detected",
fac6795d
DG
128};
129
130/*
917216f6
DG
131 * Return ptr to string representing a human readable error code from the
132 * lttcomm_return_code enum.
fac6795d 133 *
917216f6 134 * These code MUST be negative in other to treat that as an error value.
fac6795d
DG
135 */
136const 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/*
917216f6 148 * Connect to unix socket using the path name.
fac6795d
DG
149 */
150int lttcomm_connect_unix_sock(const char *pathname)
151{
686204ab 152 struct sockaddr_un sun;
4c462e79 153 int fd, ret, closeret;
fac6795d 154
686204ab 155 fd = socket(PF_UNIX, SOCK_STREAM, 0);
fac6795d 156 if (fd < 0) {
4c462e79 157 PERROR("socket");
a8afeb4a 158 ret = fd;
fac6795d
DG
159 goto error;
160 }
161
686204ab
MD
162 memset(&sun, 0, sizeof(sun));
163 sun.sun_family = AF_UNIX;
164 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
99497cd0 165 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
fac6795d 166
686204ab
MD
167 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
168 if (ret < 0) {
a8afeb4a 169 /*
917216f6
DG
170 * Don't print message on connect error, because connect is used in
171 * normal execution to detect if sessiond is alive.
a8afeb4a
MD
172 */
173 goto error_connect;
686204ab 174 }
fac6795d 175
686204ab 176 return fd;
fac6795d 177
a8afeb4a 178error_connect:
4c462e79
MD
179 closeret = close(fd);
180 if (closeret) {
181 PERROR("close");
182 }
fac6795d 183error:
a8afeb4a 184 return ret;
fac6795d
DG
185}
186
187/*
917216f6
DG
188 * Do an accept(2) on the sock and return the new file descriptor. The socket
189 * MUST be bind(2) before.
fac6795d
DG
190 */
191int 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) {
4c462e79 200 PERROR("accept");
fac6795d
DG
201 }
202
203 return new_fd;
fac6795d
DG
204}
205
206/*
917216f6
DG
207 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
208 * and return the fd.
fac6795d
DG
209 */
210int 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) {
4c462e79 218 PERROR("socket");
fac6795d
DG
219 goto error;
220 }
221
222 memset(&sun, 0, sizeof(sun));
223 sun.sun_family = AF_UNIX;
99497cd0
MD
224 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
225 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
fac6795d 226
7d8234d9
MD
227 /* Unlink the old file if present */
228 (void) unlink(pathname);
fac6795d
DG
229 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
230 if (ret < 0) {
4c462e79 231 PERROR("bind");
fac6795d
DG
232 goto error;
233 }
234
235 return fd;
236
237error:
238 return ret;
239}
240
241/*
f158a754 242 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
fac6795d
DG
243 */
244int lttcomm_listen_unix_sock(int sock)
245{
246 int ret;
247
f158a754 248 ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN);
fac6795d 249 if (ret < 0) {
4c462e79 250 PERROR("listen");
fac6795d
DG
251 }
252
253 return ret;
254}
255
256/*
917216f6
DG
257 * Receive data of size len in put that data into the buf param. Using recvmsg
258 * API.
fac6795d 259 *
917216f6 260 * Return the size of received data.
fac6795d
DG
261 */
262ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
263{
156ea65a 264 struct msghdr msg;
fac6795d
DG
265 struct iovec iov[1];
266 ssize_t ret = -1;
267
156ea65a
MD
268 memset(&msg, 0, sizeof(msg));
269
fac6795d
DG
270 iov[0].iov_base = buf;
271 iov[0].iov_len = len;
272 msg.msg_iov = iov;
273 msg.msg_iovlen = 1;
274
91fe73e2
MD
275 do {
276 ret = recvmsg(sock, &msg, MSG_WAITALL);
277 } while (ret < 0 && errno == EINTR);
fac6795d 278 if (ret < 0) {
4c462e79 279 PERROR("recvmsg");
fac6795d
DG
280 }
281
282 return ret;
283}
284
285/*
917216f6 286 * Send buf data of size len. Using sendmsg API.
fac6795d 287 *
917216f6 288 * Return the size of sent data.
fac6795d
DG
289 */
290ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
291{
156ea65a 292 struct msghdr msg;
fac6795d
DG
293 struct iovec iov[1];
294 ssize_t ret = -1;
295
156ea65a
MD
296 memset(&msg, 0, sizeof(msg));
297
fac6795d
DG
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) {
d2641a83
MD
305 /*
306 * Only warn about EPIPE when quiet mode is deactivated.
307 * We consider EPIPE as expected.
308 */
97e19046 309 if (errno != EPIPE || !lttng_opt_quiet) {
d2641a83
MD
310 PERROR("sendmsg");
311 }
fac6795d
DG
312 }
313
314 return ret;
315}
87378cf5
DG
316
317/*
917216f6 318 * Shutdown cleanly a unix socket.
87378cf5
DG
319 */
320int lttcomm_close_unix_sock(int sock)
321{
4c462e79 322 int ret, closeret;
87378cf5
DG
323
324 /* Shutdown receptions and transmissions */
325 ret = shutdown(sock, SHUT_RDWR);
326 if (ret < 0) {
4c462e79 327 PERROR("shutdown");
87378cf5
DG
328 }
329
4c462e79
MD
330 closeret = close(sock);
331 if (closeret) {
332 PERROR("close");
333 }
b0ef677e 334
87378cf5
DG
335 return ret;
336}
8c0faa1d
DG
337
338/*
3bd1e081
MD
339 * Send a message accompanied by fd(s) over a unix socket.
340 *
341 * Returns the size of data sent, or negative error value.
8c0faa1d 342 */
3bd1e081 343ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
8c0faa1d 344{
156ea65a 345 struct msghdr msg;
8c0faa1d
DG
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)];
3bd1e081 351 char dummy = 0;
8c0faa1d 352
156ea65a
MD
353 memset(&msg, 0, sizeof(msg));
354
3bd1e081
MD
355 if (nb_fd > LTTCOMM_MAX_SEND_FDS)
356 return -EINVAL;
8c0faa1d
DG
357
358 msg.msg_control = (caddr_t)tmp;
359 msg.msg_controllen = CMSG_LEN(sizeof_fds);
360
361 cmptr = CMSG_FIRSTHDR(&msg);
8c0faa1d
DG
362 cmptr->cmsg_level = SOL_SOCKET;
363 cmptr->cmsg_type = SCM_RIGHTS;
159c7ff4 364 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
8c0faa1d 365 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
159c7ff4
MD
366 /* Sum of the length of all control messages in the buffer: */
367 msg.msg_controllen = cmptr->cmsg_len;
8c0faa1d 368
3bd1e081
MD
369 iov[0].iov_base = &dummy;
370 iov[0].iov_len = 1;
8c0faa1d
DG
371 msg.msg_iov = iov;
372 msg.msg_iovlen = 1;
373
66724ea3
DG
374 do {
375 ret = sendmsg(sock, &msg, 0);
376 } while (ret < 0 && errno == EINTR);
8c0faa1d 377 if (ret < 0) {
d2641a83
MD
378 /*
379 * Only warn about EPIPE when quiet mode is deactivated.
380 * We consider EPIPE as expected.
381 */
97e19046 382 if (errno != EPIPE || !lttng_opt_quiet) {
d2641a83
MD
383 PERROR("sendmsg");
384 }
8c0faa1d 385 }
8c0faa1d
DG
386 return ret;
387}
09de5c68
DG
388
389/*
3bd1e081 390 * Recv a message accompanied by fd(s) from a unix socket.
09de5c68 391 *
3bd1e081
MD
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.
09de5c68 396 */
3bd1e081 397ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
09de5c68
DG
398{
399 struct iovec iov[1];
3bd1e081 400 ssize_t ret = 0;
09de5c68 401 struct cmsghdr *cmsg;
3bd1e081
MD
402 size_t sizeof_fds = nb_fd * sizeof(int);
403 char recv_fd[CMSG_SPACE(sizeof_fds)];
156ea65a 404 struct msghdr msg;
3bd1e081 405 char dummy;
09de5c68 406
156ea65a
MD
407 memset(&msg, 0, sizeof(msg));
408
09de5c68 409 /* Prepare to receive the structures */
3bd1e081
MD
410 iov[0].iov_base = &dummy;
411 iov[0].iov_len = 1;
09de5c68
DG
412 msg.msg_iov = iov;
413 msg.msg_iovlen = 1;
414 msg.msg_control = recv_fd;
415 msg.msg_controllen = sizeof(recv_fd);
416
91fe73e2
MD
417 do {
418 ret = recvmsg(sock, &msg, 0);
419 } while (ret < 0 && errno == EINTR);
09de5c68 420 if (ret < 0) {
4c462e79 421 PERROR("recvmsg fds");
09de5c68
DG
422 goto end;
423 }
3bd1e081
MD
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;
09de5c68
DG
432 goto end;
433 }
09de5c68
DG
434 cmsg = CMSG_FIRSTHDR(&msg);
435 if (!cmsg) {
3bd1e081 436 fprintf(stderr, "Error: Invalid control message header\n");
09de5c68
DG
437 ret = -1;
438 goto end;
439 }
09de5c68 440 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
3bd1e081 441 fprintf(stderr, "Didn't received any fd\n");
09de5c68
DG
442 ret = -1;
443 goto end;
444 }
3bd1e081
MD
445 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
446 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
b17231c6 447 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
3bd1e081
MD
448 ret = -1;
449 goto end;
09de5c68 450 }
3bd1e081
MD
451 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
452 ret = sizeof_fds;
09de5c68
DG
453end:
454 return ret;
455}
d19d9ada
DG
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 */
462ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len)
463{
156ea65a 464 struct msghdr msg;
d19d9ada
DG
465 struct iovec iov[1];
466 ssize_t ret = -1;
d27c42b8
MD
467#ifdef __linux__
468 struct cmsghdr *cmptr;
b17231c6 469 size_t sizeof_cred = sizeof(lttng_sock_cred);
d19d9ada 470 char anc_buf[CMSG_SPACE(sizeof_cred)];
b17231c6 471 lttng_sock_cred *creds;
d27c42b8 472#endif /* __linux__ */
d19d9ada 473
156ea65a
MD
474 memset(&msg, 0, sizeof(msg));
475
d19d9ada
DG
476 iov[0].iov_base = buf;
477 iov[0].iov_len = len;
478 msg.msg_iov = iov;
479 msg.msg_iovlen = 1;
480
d27c42b8 481#ifdef __linux__
d19d9ada
DG
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;
b17231c6 487 cmptr->cmsg_type = LTTNG_SOCK_CREDS;
d19d9ada
DG
488 cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
489
b17231c6 490 creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
d19d9ada 491
b17231c6
DG
492 LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
493 LTTNG_SOCK_SET_GID_CRED(creds, getegid());
494 LTTNG_SOCK_SET_PID_CRED(creds, getpid());
d27c42b8 495#endif /* __linux__ */
d19d9ada 496
66724ea3
DG
497 do {
498 ret = sendmsg(sock, &msg, 0);
499 } while (ret < 0 && errno == EINTR);
d19d9ada 500 if (ret < 0) {
d2641a83
MD
501 /*
502 * Only warn about EPIPE when quiet mode is deactivated.
503 * We consider EPIPE as expected.
504 */
97e19046 505 if (errno != EPIPE || !lttng_opt_quiet) {
d2641a83
MD
506 PERROR("sendmsg");
507 }
d19d9ada 508 }
d19d9ada
DG
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 */
517ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
b17231c6 518 lttng_sock_cred *creds)
d19d9ada 519{
156ea65a 520 struct msghdr msg;
d19d9ada
DG
521 struct iovec iov[1];
522 ssize_t ret;
d27c42b8
MD
523#ifdef __linux__
524 struct cmsghdr *cmptr;
b17231c6 525 size_t sizeof_cred = sizeof(lttng_sock_cred);
d19d9ada 526 char anc_buf[CMSG_SPACE(sizeof_cred)];
d27c42b8 527#endif /* __linux__ */
d19d9ada 528
156ea65a
MD
529 memset(&msg, 0, sizeof(msg));
530
d19d9ada
DG
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
d27c42b8 543#ifdef __linux__
d19d9ada
DG
544 msg.msg_control = anc_buf;
545 msg.msg_controllen = sizeof(anc_buf);
d27c42b8 546#endif /* __linux__ */
d19d9ada 547
91fe73e2
MD
548 do {
549 ret = recvmsg(sock, &msg, 0);
550 } while (ret < 0 && errno == EINTR);
d19d9ada 551 if (ret < 0) {
4c462e79 552 PERROR("recvmsg fds");
d19d9ada
DG
553 goto end;
554 }
555
d27c42b8 556#ifdef __linux__
d19d9ada
DG
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 ||
b17231c6 571 cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
d19d9ada
DG
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",
b17231c6 579 (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
d19d9ada
DG
580 ret = -1;
581 goto end;
582 }
583
584 memcpy(creds, CMSG_DATA(cmptr), sizeof_cred);
d27c42b8 585#elif defined(__FreeBSD__)
9e89c931
MD
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 }
67cf0ab7 593 }
d27c42b8
MD
594#else
595#error "Please implement credential support for your OS."
596#endif /* __linux__ */
d19d9ada
DG
597
598end:
599 return ret;
600}
601
602/*
603 * Set socket option to use credentials passing.
604 */
d27c42b8 605#ifdef __linux__
d19d9ada
DG
606int 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) {
4c462e79 613 PERROR("setsockopt creds unix sock");
d19d9ada 614 }
d19d9ada
DG
615 return ret;
616}
d27c42b8
MD
617#elif defined(__FreeBSD__)
618int 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.100134 seconds and 4 git commands to generate.