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