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