Fix: coding style document has erroneous semicolon at end of macro
[lttng-tools.git] / src / common / unix.c
CommitLineData
0d37f2bc
DG
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
6c1c0768 19#define _LGPL_SOURCE
0d37f2bc
DG
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
90e535ef 30#include <common/common.h>
2038dd6c 31#include <common/sessiond-comm/sessiond-comm.h>
0d37f2bc
DG
32
33#include "unix.h"
34
35/*
36 * Connect to unix socket using the path name.
37 */
90e535ef 38LTTNG_HIDDEN
0d37f2bc
DG
39int lttcomm_connect_unix_sock(const char *pathname)
40{
665886a6 41 struct sockaddr_un s_un;
0d37f2bc
DG
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
665886a6
MJ
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';
0d37f2bc 55
665886a6 56 ret = connect(fd, (struct sockaddr *) &s_un, sizeof(s_un));
0d37f2bc
DG
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
67error_connect:
68 closeret = close(fd);
69 if (closeret) {
70 PERROR("close");
71 }
72error:
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 */
90e535ef 80LTTNG_HIDDEN
0d37f2bc
DG
81int lttcomm_accept_unix_sock(int sock)
82{
83 int new_fd;
665886a6 84 struct sockaddr_un s_un;
0d37f2bc
DG
85 socklen_t len = 0;
86
87 /* Blocking call */
665886a6 88 new_fd = accept(sock, (struct sockaddr *) &s_un, &len);
0d37f2bc
DG
89 if (new_fd < 0) {
90 PERROR("accept");
91 }
92
93 return new_fd;
94}
95
7567352f
MD
96LTTNG_HIDDEN
97int 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
0d37f2bc
DG
106/*
107 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
108 * and return the fd.
109 */
90e535ef 110LTTNG_HIDDEN
0d37f2bc
DG
111int lttcomm_create_unix_sock(const char *pathname)
112{
665886a6 113 struct sockaddr_un s_un;
0d37f2bc
DG
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
665886a6
MJ
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';
0d37f2bc
DG
127
128 /* Unlink the old file if present */
129 (void) unlink(pathname);
665886a6 130 ret = bind(fd, (struct sockaddr *) &s_un, sizeof(s_un));
0d37f2bc
DG
131 if (ret < 0) {
132 PERROR("bind");
133 goto error;
134 }
135
136 return fd;
137
138error:
17e75273
DG
139 if (fd >= 0) {
140 if (close(fd) < 0) {
141 PERROR("close create unix sock");
142 }
143 }
0d37f2bc
DG
144 return ret;
145}
146
147/*
148 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
149 */
90e535ef 150LTTNG_HIDDEN
0d37f2bc
DG
151int 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 */
90e535ef 169LTTNG_HIDDEN
0d37f2bc
DG
170ssize_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;
7c5aef62 175 size_t len_last;
0d37f2bc
DG
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 {
7c5aef62 185 len_last = iov[0].iov_len;
fbb1fd3a 186 ret = lttng_recvmsg_nosigpipe(sock, &msg);
7c5aef62
DG
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));
0d37f2bc
DG
193 if (ret < 0) {
194 PERROR("recvmsg");
7c5aef62
DG
195 } else if (ret > 0) {
196 ret = len;
0d37f2bc 197 }
7c5aef62 198 /* Else ret = 0 meaning an orderly shutdown. */
0d37f2bc
DG
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 */
90e535ef 208LTTNG_HIDDEN
c2d69327 209ssize_t lttcomm_send_unix_sock(int sock, const void *buf, size_t len)
0d37f2bc
DG
210{
211 struct msghdr msg;
212 struct iovec iov[1];
213 ssize_t ret = -1;
214
215 memset(&msg, 0, sizeof(msg));
216
c2d69327 217 iov[0].iov_base = (void *) buf;
0d37f2bc
DG
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 */
90e535ef 239LTTNG_HIDDEN
0d37f2bc
DG
240int 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 */
90e535ef 263LTTNG_HIDDEN
0d37f2bc
DG
264ssize_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));
c617c0c6 275 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
0d37f2bc
DG
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);
1d8d0328
MJ
284 if (!cmptr) {
285 return -1;
286 }
0d37f2bc
DG
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 */
90e535ef 322LTTNG_HIDDEN
0d37f2bc
DG
323ssize_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;
379end:
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 */
90e535ef 388LTTNG_HIDDEN
0d37f2bc
DG
389ssize_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;
8bbffd54
MJ
399
400 memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char));
0d37f2bc
DG
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);
1d8d0328
MJ
415 if (!cmptr) {
416 return -1;
417 }
0d37f2bc
DG
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 */
90e535ef 449LTTNG_HIDDEN
0d37f2bc
DG
450ssize_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;
4100e49a 456 size_t len_last;
0d37f2bc
DG
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 {
4100e49a 483 len_last = iov[0].iov_len;
0d37f2bc 484 ret = recvmsg(sock, &msg, 0);
4100e49a
DG
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));
0d37f2bc
DG
491 if (ret < 0) {
492 PERROR("recvmsg fds");
493 goto end;
4100e49a
DG
494 } else if (ret > 0) {
495 ret = len;
0d37f2bc 496 }
4100e49a 497 /* Else ret = 0 meaning an orderly shutdown. */
0d37f2bc
DG
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);
06414417 528#elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__))
0d37f2bc
DG
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
541end:
542 return ret;
543}
544
545/*
546 * Set socket option to use credentials passing.
547 */
548#ifdef __linux__
90e535ef 549LTTNG_HIDDEN
0d37f2bc
DG
550int 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}
06414417 561#elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__))
90e535ef 562LTTNG_HIDDEN
0d37f2bc
DG
563int 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.085045 seconds and 4 git commands to generate.