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