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