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