Fix: define _LGPL_SOURCE in C files
[lttng-tools.git] / src / common / sessiond-comm / unix.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
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
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
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #define _LGPL_SOURCE
21 #include <assert.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include <common/common.h>
32
33 #include "unix.h"
34
35 /*
36 * Connect to unix socket using the path name.
37 */
38 LTTNG_HIDDEN
39 int lttcomm_connect_unix_sock(const char *pathname)
40 {
41 struct sockaddr_un sun;
42 int fd, ret, closeret;
43
44 fd = socket(PF_UNIX, SOCK_STREAM, 0);
45 if (fd < 0) {
46 PERROR("socket");
47 ret = fd;
48 goto error;
49 }
50
51 memset(&sun, 0, sizeof(sun));
52 sun.sun_family = AF_UNIX;
53 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
54 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
55
56 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
57 if (ret < 0) {
58 /*
59 * Don't print message on connect error, because connect is used in
60 * normal execution to detect if sessiond is alive.
61 */
62 goto error_connect;
63 }
64
65 return fd;
66
67 error_connect:
68 closeret = close(fd);
69 if (closeret) {
70 PERROR("close");
71 }
72 error:
73 return ret;
74 }
75
76 /*
77 * Do an accept(2) on the sock and return the new file descriptor. The socket
78 * MUST be bind(2) before.
79 */
80 LTTNG_HIDDEN
81 int lttcomm_accept_unix_sock(int sock)
82 {
83 int new_fd;
84 struct sockaddr_un sun;
85 socklen_t len = 0;
86
87 /* Blocking call */
88 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
89 if (new_fd < 0) {
90 PERROR("accept");
91 }
92
93 return new_fd;
94 }
95
96 /*
97 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
98 * and return the fd.
99 */
100 LTTNG_HIDDEN
101 int lttcomm_create_unix_sock(const char *pathname)
102 {
103 struct sockaddr_un sun;
104 int fd;
105 int ret = -1;
106
107 /* Create server socket */
108 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
109 PERROR("socket");
110 goto error;
111 }
112
113 memset(&sun, 0, sizeof(sun));
114 sun.sun_family = AF_UNIX;
115 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
116 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
117
118 /* Unlink the old file if present */
119 (void) unlink(pathname);
120 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
121 if (ret < 0) {
122 PERROR("bind");
123 goto error;
124 }
125
126 return fd;
127
128 error:
129 if (fd >= 0) {
130 if (close(fd) < 0) {
131 PERROR("close create unix sock");
132 }
133 }
134 return ret;
135 }
136
137 /*
138 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
139 */
140 LTTNG_HIDDEN
141 int lttcomm_listen_unix_sock(int sock)
142 {
143 int ret;
144
145 ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN);
146 if (ret < 0) {
147 PERROR("listen");
148 }
149
150 return ret;
151 }
152
153 /*
154 * Receive data of size len in put that data into the buf param. Using recvmsg
155 * API.
156 *
157 * Return the size of received data.
158 */
159 LTTNG_HIDDEN
160 ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
161 {
162 struct msghdr msg;
163 struct iovec iov[1];
164 ssize_t ret = -1;
165 size_t len_last;
166
167 memset(&msg, 0, sizeof(msg));
168
169 iov[0].iov_base = buf;
170 iov[0].iov_len = len;
171 msg.msg_iov = iov;
172 msg.msg_iovlen = 1;
173
174 do {
175 len_last = iov[0].iov_len;
176 ret = recvmsg(sock, &msg, 0);
177 if (ret > 0) {
178 iov[0].iov_base += ret;
179 iov[0].iov_len -= ret;
180 assert(ret <= len_last);
181 }
182 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
183 if (ret < 0) {
184 PERROR("recvmsg");
185 } else if (ret > 0) {
186 ret = len;
187 }
188 /* Else ret = 0 meaning an orderly shutdown. */
189
190 return ret;
191 }
192
193 /*
194 * Send buf data of size len. Using sendmsg API.
195 *
196 * Return the size of sent data.
197 */
198 LTTNG_HIDDEN
199 ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
200 {
201 struct msghdr msg;
202 struct iovec iov[1];
203 ssize_t ret = -1;
204
205 memset(&msg, 0, sizeof(msg));
206
207 iov[0].iov_base = buf;
208 iov[0].iov_len = len;
209 msg.msg_iov = iov;
210 msg.msg_iovlen = 1;
211
212 ret = sendmsg(sock, &msg, 0);
213 if (ret < 0) {
214 /*
215 * Only warn about EPIPE when quiet mode is deactivated.
216 * We consider EPIPE as expected.
217 */
218 if (errno != EPIPE || !lttng_opt_quiet) {
219 PERROR("sendmsg");
220 }
221 }
222
223 return ret;
224 }
225
226 /*
227 * Shutdown cleanly a unix socket.
228 */
229 LTTNG_HIDDEN
230 int lttcomm_close_unix_sock(int sock)
231 {
232 int ret, closeret;
233
234 /* Shutdown receptions and transmissions */
235 ret = shutdown(sock, SHUT_RDWR);
236 if (ret < 0) {
237 PERROR("shutdown");
238 }
239
240 closeret = close(sock);
241 if (closeret) {
242 PERROR("close");
243 }
244
245 return ret;
246 }
247
248 /*
249 * Send a message accompanied by fd(s) over a unix socket.
250 *
251 * Returns the size of data sent, or negative error value.
252 */
253 LTTNG_HIDDEN
254 ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
255 {
256 struct msghdr msg;
257 struct cmsghdr *cmptr;
258 struct iovec iov[1];
259 ssize_t ret = -1;
260 unsigned int sizeof_fds = nb_fd * sizeof(int);
261 char tmp[CMSG_SPACE(sizeof_fds)];
262 char dummy = 0;
263
264 memset(&msg, 0, sizeof(msg));
265 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
266
267 if (nb_fd > LTTCOMM_MAX_SEND_FDS)
268 return -EINVAL;
269
270 msg.msg_control = (caddr_t)tmp;
271 msg.msg_controllen = CMSG_LEN(sizeof_fds);
272
273 cmptr = CMSG_FIRSTHDR(&msg);
274 cmptr->cmsg_level = SOL_SOCKET;
275 cmptr->cmsg_type = SCM_RIGHTS;
276 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
277 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
278 /* Sum of the length of all control messages in the buffer: */
279 msg.msg_controllen = cmptr->cmsg_len;
280
281 iov[0].iov_base = &dummy;
282 iov[0].iov_len = 1;
283 msg.msg_iov = iov;
284 msg.msg_iovlen = 1;
285
286 do {
287 ret = sendmsg(sock, &msg, 0);
288 } while (ret < 0 && errno == EINTR);
289 if (ret < 0) {
290 /*
291 * Only warn about EPIPE when quiet mode is deactivated.
292 * We consider EPIPE as expected.
293 */
294 if (errno != EPIPE || !lttng_opt_quiet) {
295 PERROR("sendmsg");
296 }
297 }
298 return ret;
299 }
300
301 /*
302 * Recv a message accompanied by fd(s) from a unix socket.
303 *
304 * Returns the size of received data, or negative error value.
305 *
306 * Expect at most "nb_fd" file descriptors. Returns the number of fd
307 * actually received in nb_fd.
308 */
309 LTTNG_HIDDEN
310 ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
311 {
312 struct iovec iov[1];
313 ssize_t ret = 0;
314 struct cmsghdr *cmsg;
315 size_t sizeof_fds = nb_fd * sizeof(int);
316 char recv_fd[CMSG_SPACE(sizeof_fds)];
317 struct msghdr msg;
318 char dummy;
319
320 memset(&msg, 0, sizeof(msg));
321
322 /* Prepare to receive the structures */
323 iov[0].iov_base = &dummy;
324 iov[0].iov_len = 1;
325 msg.msg_iov = iov;
326 msg.msg_iovlen = 1;
327 msg.msg_control = recv_fd;
328 msg.msg_controllen = sizeof(recv_fd);
329
330 do {
331 ret = recvmsg(sock, &msg, 0);
332 } while (ret < 0 && errno == EINTR);
333 if (ret < 0) {
334 PERROR("recvmsg fds");
335 goto end;
336 }
337 if (ret != 1) {
338 fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
339 ret, 1);
340 goto end;
341 }
342 if (msg.msg_flags & MSG_CTRUNC) {
343 fprintf(stderr, "Error: Control message truncated.\n");
344 ret = -1;
345 goto end;
346 }
347 cmsg = CMSG_FIRSTHDR(&msg);
348 if (!cmsg) {
349 fprintf(stderr, "Error: Invalid control message header\n");
350 ret = -1;
351 goto end;
352 }
353 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
354 fprintf(stderr, "Didn't received any fd\n");
355 ret = -1;
356 goto end;
357 }
358 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
359 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
360 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
361 ret = -1;
362 goto end;
363 }
364 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
365 ret = sizeof_fds;
366 end:
367 return ret;
368 }
369
370 /*
371 * Send a message with credentials over a unix socket.
372 *
373 * Returns the size of data sent, or negative error value.
374 */
375 LTTNG_HIDDEN
376 ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len)
377 {
378 struct msghdr msg;
379 struct iovec iov[1];
380 ssize_t ret = -1;
381 #ifdef __linux__
382 struct cmsghdr *cmptr;
383 size_t sizeof_cred = sizeof(lttng_sock_cred);
384 char anc_buf[CMSG_SPACE(sizeof_cred)];
385 lttng_sock_cred *creds;
386 #endif /* __linux__ */
387
388 memset(&msg, 0, sizeof(msg));
389 memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char));
390
391 iov[0].iov_base = buf;
392 iov[0].iov_len = len;
393 msg.msg_iov = iov;
394 msg.msg_iovlen = 1;
395
396 #ifdef __linux__
397 msg.msg_control = (caddr_t) anc_buf;
398 msg.msg_controllen = CMSG_LEN(sizeof_cred);
399
400 cmptr = CMSG_FIRSTHDR(&msg);
401 cmptr->cmsg_level = SOL_SOCKET;
402 cmptr->cmsg_type = LTTNG_SOCK_CREDS;
403 cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
404
405 creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
406
407 LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
408 LTTNG_SOCK_SET_GID_CRED(creds, getegid());
409 LTTNG_SOCK_SET_PID_CRED(creds, getpid());
410 #endif /* __linux__ */
411
412 do {
413 ret = sendmsg(sock, &msg, 0);
414 } while (ret < 0 && errno == EINTR);
415 if (ret < 0) {
416 /*
417 * Only warn about EPIPE when quiet mode is deactivated.
418 * We consider EPIPE as expected.
419 */
420 if (errno != EPIPE || !lttng_opt_quiet) {
421 PERROR("sendmsg");
422 }
423 }
424 return ret;
425 }
426
427 /*
428 * Recv a message accompanied with credentials from a unix socket.
429 *
430 * Returns the size of received data, or negative error value.
431 */
432 LTTNG_HIDDEN
433 ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
434 lttng_sock_cred *creds)
435 {
436 struct msghdr msg;
437 struct iovec iov[1];
438 ssize_t ret;
439 size_t len_last;
440 #ifdef __linux__
441 struct cmsghdr *cmptr;
442 size_t sizeof_cred = sizeof(lttng_sock_cred);
443 char anc_buf[CMSG_SPACE(sizeof_cred)];
444 #endif /* __linux__ */
445
446 memset(&msg, 0, sizeof(msg));
447
448 /* Not allowed */
449 if (creds == NULL) {
450 ret = -1;
451 goto end;
452 }
453
454 /* Prepare to receive the structures */
455 iov[0].iov_base = buf;
456 iov[0].iov_len = len;
457 msg.msg_iov = iov;
458 msg.msg_iovlen = 1;
459
460 #ifdef __linux__
461 msg.msg_control = anc_buf;
462 msg.msg_controllen = sizeof(anc_buf);
463 #endif /* __linux__ */
464
465 do {
466 len_last = iov[0].iov_len;
467 ret = recvmsg(sock, &msg, 0);
468 if (ret > 0) {
469 iov[0].iov_base += ret;
470 iov[0].iov_len -= ret;
471 assert(ret <= len_last);
472 }
473 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
474 if (ret < 0) {
475 PERROR("recvmsg fds");
476 goto end;
477 } else if (ret > 0) {
478 ret = len;
479 }
480 /* Else ret = 0 meaning an orderly shutdown. */
481
482 #ifdef __linux__
483 if (msg.msg_flags & MSG_CTRUNC) {
484 fprintf(stderr, "Error: Control message truncated.\n");
485 ret = -1;
486 goto end;
487 }
488
489 cmptr = CMSG_FIRSTHDR(&msg);
490 if (cmptr == NULL) {
491 fprintf(stderr, "Error: Invalid control message header\n");
492 ret = -1;
493 goto end;
494 }
495
496 if (cmptr->cmsg_level != SOL_SOCKET ||
497 cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
498 fprintf(stderr, "Didn't received any credentials\n");
499 ret = -1;
500 goto end;
501 }
502
503 if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) {
504 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
505 (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
506 ret = -1;
507 goto end;
508 }
509
510 memcpy(creds, CMSG_DATA(cmptr), sizeof_cred);
511 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
512 {
513 int peer_ret;
514
515 peer_ret = getpeereid(sock, &creds->uid, &creds->gid);
516 if (peer_ret != 0) {
517 return peer_ret;
518 }
519 }
520 #else
521 #error "Please implement credential support for your OS."
522 #endif /* __linux__ */
523
524 end:
525 return ret;
526 }
527
528 /*
529 * Set socket option to use credentials passing.
530 */
531 #ifdef __linux__
532 LTTNG_HIDDEN
533 int lttcomm_setsockopt_creds_unix_sock(int sock)
534 {
535 int ret, on = 1;
536
537 /* Set socket for credentials retrieval */
538 ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
539 if (ret < 0) {
540 PERROR("setsockopt creds unix sock");
541 }
542 return ret;
543 }
544 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
545 LTTNG_HIDDEN
546 int lttcomm_setsockopt_creds_unix_sock(int sock)
547 {
548 return 0;
549 }
550 #else
551 #error "Please implement credential support for your OS."
552 #endif /* __linux__ */
This page took 0.040185 seconds and 4 git commands to generate.