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