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