Fix: don't dereference NULL pointers
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-comm.c
CommitLineData
67c5b804
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
74d81a6c 3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
67c5b804 4 *
15f672f9
MD
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; only
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
67c5b804 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15f672f9
MD
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
67c5b804
MD
18 */
19
20#define _GNU_SOURCE
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/un.h>
29#include <unistd.h>
30#include <assert.h>
57773204 31#include <errno.h>
11ba4bcb 32#include <fcntl.h>
67c5b804 33
32ce8569 34#include <lttng/ust-ctl.h>
b728d87e 35#include <ust-comm.h>
74d81a6c 36#include <helper.h>
7bc53e94 37#include <lttng/ust-error.h>
32ce8569
MD
38#include <lttng/ust-events.h>
39#include <usterr-signal-safe.h>
40
41#include "../liblttng-ust/compat.h"
7bc53e94
MD
42
43#define USTCOMM_CODE_OFFSET(code) \
44 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
67c5b804 45
74d81a6c
MD
46#define USTCOMM_MAX_SEND_FDS 4
47
67c5b804
MD
48/*
49 * Human readable error message.
50 */
57773204 51static const char *ustcomm_readable_code[] = {
7bc53e94
MD
52 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK) ] = "Success",
53 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR) ] = "Unknown error",
54 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT) ] = "No entry",
64b2564e
DG
55 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST) ] = "Object already exists",
56 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL) ] = "Invalid argument",
57 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM) ] = "Permission denied",
58 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS) ] = "Not implemented",
74d81a6c 59 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING) ] = "Process is exiting",
32ce8569
MD
60
61 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number",
62 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type",
63 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version",
67c5b804
MD
64};
65
66/*
7bc53e94 67 * lttng_ust_strerror
67c5b804 68 *
7bc53e94
MD
69 * Receives positive error value.
70 * Return ptr to string representing a human readable
71 * error code from the ustcomm_return_code enum.
67c5b804 72 */
7bc53e94 73const char *lttng_ust_strerror(int code)
67c5b804 74{
7bc53e94
MD
75 if (code == LTTNG_UST_OK)
76 return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
77 if (code < LTTNG_UST_ERR)
78 return strerror(code);
79 if (code >= LTTNG_UST_ERR_NR)
80 code = LTTNG_UST_ERR;
81 return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
67c5b804
MD
82}
83
84/*
74d81a6c 85 * ustcomm_connect_unix_sock
67c5b804 86 *
74d81a6c 87 * Connect to unix socket using the path name.
67c5b804 88 */
57773204 89int ustcomm_connect_unix_sock(const char *pathname)
67c5b804
MD
90{
91 struct sockaddr_un sun;
7bc53e94 92 int fd, ret;
67c5b804 93
204d45df
MD
94 /*
95 * libust threads require the close-on-exec flag for all
96 * resources so it does not leak file descriptors upon exec.
97 */
11ba4bcb 98 fd = socket(PF_UNIX, SOCK_STREAM, 0);
67c5b804 99 if (fd < 0) {
32ce8569 100 PERROR("socket");
7bc53e94 101 ret = -errno;
67c5b804
MD
102 goto error;
103 }
11ba4bcb
MD
104 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
105 if (ret < 0) {
32ce8569 106 PERROR("fcntl");
7bc53e94 107 ret = -errno;
11ba4bcb
MD
108 goto error_fcntl;
109 }
67c5b804
MD
110
111 memset(&sun, 0, sizeof(sun));
112 sun.sun_family = AF_UNIX;
113 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
114 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
115
116 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
117 if (ret < 0) {
118 /*
0b9aa170
MD
119 * Don't print message on connect ENOENT error, because
120 * connect is used in normal execution to detect if
121 * sessiond is alive. ENOENT is when the unix socket
122 * file does not exist, and ECONNREFUSED is when the
123 * file exists but no sessiond is listening.
67c5b804 124 */
0b9aa170 125 if (errno != ECONNREFUSED && errno != ECONNRESET
bdd8ca83 126 && errno != ENOENT && errno != EACCES)
8cf811d3 127 PERROR("connect");
7bc53e94 128 ret = -errno;
8cf811d3
MD
129 if (ret == -ECONNREFUSED || ret == -ECONNRESET)
130 ret = -EPIPE;
67c5b804
MD
131 goto error_connect;
132 }
133
134 return fd;
135
136error_connect:
11ba4bcb 137error_fcntl:
7bc53e94
MD
138 {
139 int closeret;
140
141 closeret = close(fd);
142 if (closeret)
32ce8569 143 PERROR("close");
7bc53e94 144 }
67c5b804
MD
145error:
146 return ret;
147}
148
149/*
74d81a6c 150 * ustcomm_accept_unix_sock
67c5b804 151 *
74d81a6c
MD
152 * Do an accept(2) on the sock and return the
153 * new file descriptor. The socket MUST be bind(2) before.
67c5b804 154 */
57773204 155int ustcomm_accept_unix_sock(int sock)
67c5b804
MD
156{
157 int new_fd;
158 struct sockaddr_un sun;
159 socklen_t len = 0;
160
161 /* Blocking call */
162 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
163 if (new_fd < 0) {
b869b5ae
MD
164 if (errno != ECONNABORTED)
165 PERROR("accept");
166 new_fd = -errno;
167 if (new_fd == -ECONNABORTED)
168 new_fd = -EPIPE;
67c5b804 169 }
67c5b804 170 return new_fd;
67c5b804
MD
171}
172
173/*
74d81a6c 174 * ustcomm_create_unix_sock
67c5b804 175 *
74d81a6c
MD
176 * Creates a AF_UNIX local socket using pathname
177 * bind the socket upon creation and return the fd.
67c5b804 178 */
57773204 179int ustcomm_create_unix_sock(const char *pathname)
67c5b804
MD
180{
181 struct sockaddr_un sun;
7bc53e94 182 int fd, ret;
67c5b804
MD
183
184 /* Create server socket */
185 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
32ce8569 186 PERROR("socket");
7bc53e94 187 ret = -errno;
67c5b804
MD
188 goto error;
189 }
190
191 memset(&sun, 0, sizeof(sun));
192 sun.sun_family = AF_UNIX;
193 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
194 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
195
196 /* Unlink the old file if present */
197 (void) unlink(pathname);
198 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
199 if (ret < 0) {
32ce8569 200 PERROR("bind");
7bc53e94
MD
201 ret = -errno;
202 goto error_close;
67c5b804
MD
203 }
204
205 return fd;
206
7bc53e94
MD
207error_close:
208 {
209 int closeret;
210
211 closeret = close(fd);
212 if (closeret) {
32ce8569 213 PERROR("close");
7bc53e94
MD
214 }
215 }
67c5b804
MD
216error:
217 return ret;
218}
219
220/*
74d81a6c 221 * ustcomm_listen_unix_sock
67c5b804 222 *
74d81a6c 223 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
67c5b804 224 */
57773204 225int ustcomm_listen_unix_sock(int sock)
67c5b804
MD
226{
227 int ret;
228
e41474be 229 ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN);
67c5b804 230 if (ret < 0) {
7bc53e94 231 ret = -errno;
32ce8569 232 PERROR("listen");
67c5b804
MD
233 }
234
235 return ret;
236}
237
238/*
74d81a6c
MD
239 * ustcomm_close_unix_sock
240 *
241 * Shutdown cleanly a unix socket.
242 */
243int ustcomm_close_unix_sock(int sock)
244{
245 int ret;
246
247 ret = close(sock);
248 if (ret < 0) {
32ce8569 249 PERROR("close");
74d81a6c
MD
250 ret = -errno;
251 }
252
253 return ret;
254}
255
256/*
257 * ustcomm_recv_unix_sock
67c5b804 258 *
74d81a6c
MD
259 * Receive data of size len in put that data into
260 * the buf param. Using recvmsg API.
261 * Return the size of received data.
262 * Return 0 on orderly shutdown.
67c5b804 263 */
57773204 264ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
67c5b804 265{
913b87f1 266 struct msghdr msg;
67c5b804 267 struct iovec iov[1];
89c5b6ec
MD
268 ssize_t ret = -1;
269 size_t len_last;
67c5b804 270
913b87f1
MD
271 memset(&msg, 0, sizeof(msg));
272
67c5b804
MD
273 iov[0].iov_base = buf;
274 iov[0].iov_len = len;
275 msg.msg_iov = iov;
276 msg.msg_iovlen = 1;
277
7e3cfcbe 278 do {
89c5b6ec 279 len_last = iov[0].iov_len;
7e3cfcbe 280 ret = recvmsg(sock, &msg, 0);
89c5b6ec
MD
281 if (ret > 0) {
282 iov[0].iov_base += ret;
283 iov[0].iov_len -= ret;
284 assert(ret <= len_last);
285 }
286 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
7bc53e94
MD
287
288 if (ret < 0) {
289 int shutret;
290
8cf811d3 291 if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED)
32ce8569 292 PERROR("recvmsg");
7bc53e94 293 ret = -errno;
8cf811d3 294 if (ret == -ECONNRESET || ret == -ECONNREFUSED)
b869b5ae 295 ret = -EPIPE;
7bc53e94
MD
296
297 shutret = shutdown(sock, SHUT_RDWR);
298 if (shutret)
32ce8569 299 ERR("Socket shutdown error");
89c5b6ec
MD
300 } else if (ret > 0) {
301 ret = len;
67c5b804 302 }
89c5b6ec 303 /* ret = 0 means an orderly shutdown. */
67c5b804
MD
304
305 return ret;
306}
307
308/*
74d81a6c 309 * ustcomm_send_unix_sock
67c5b804 310 *
74d81a6c
MD
311 * Send buf data of size len. Using sendmsg API.
312 * Return the size of sent data.
67c5b804 313 */
32ce8569 314ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len)
67c5b804 315{
913b87f1 316 struct msghdr msg;
67c5b804 317 struct iovec iov[1];
7bc53e94 318 ssize_t ret;
67c5b804 319
913b87f1
MD
320 memset(&msg, 0, sizeof(msg));
321
32ce8569 322 iov[0].iov_base = (void *) buf;
67c5b804
MD
323 iov[0].iov_len = len;
324 msg.msg_iov = iov;
325 msg.msg_iovlen = 1;
326
1ea11eab
MD
327 /*
328 * Using the MSG_NOSIGNAL when sending data from sessiond to
329 * libust, so libust does not receive an unhandled SIGPIPE or
330 * SIGURG. The sessiond receiver side can be made more resilient
331 * by ignoring SIGPIPE, but we don't have this luxury on the
332 * libust side.
333 */
51d9d699
MD
334 do {
335 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
336 } while (ret < 0 && errno == EINTR);
7bc53e94
MD
337
338 if (ret < 0) {
339 int shutret;
340
74d81a6c 341 if (errno != EPIPE && errno != ECONNRESET)
32ce8569 342 PERROR("sendmsg");
7bc53e94 343 ret = -errno;
b869b5ae
MD
344 if (ret == -ECONNRESET)
345 ret = -EPIPE;
7bc53e94
MD
346
347 shutret = shutdown(sock, SHUT_RDWR);
348 if (shutret)
32ce8569 349 ERR("Socket shutdown error");
67c5b804
MD
350 }
351
352 return ret;
353}
354
355/*
74d81a6c 356 * Send a message accompanied by fd(s) over a unix socket.
67c5b804 357 *
74d81a6c 358 * Returns the size of data sent, or negative error value.
67c5b804 359 */
74d81a6c 360ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
67c5b804 361{
913b87f1 362 struct msghdr msg;
67c5b804
MD
363 struct cmsghdr *cmptr;
364 struct iovec iov[1];
365 ssize_t ret = -1;
366 unsigned int sizeof_fds = nb_fd * sizeof(int);
367 char tmp[CMSG_SPACE(sizeof_fds)];
74d81a6c 368 char dummy = 0;
67c5b804 369
913b87f1 370 memset(&msg, 0, sizeof(msg));
74d81a6c 371 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
913b87f1 372
74d81a6c
MD
373 if (nb_fd > USTCOMM_MAX_SEND_FDS)
374 return -EINVAL;
67c5b804
MD
375
376 msg.msg_control = (caddr_t)tmp;
377 msg.msg_controllen = CMSG_LEN(sizeof_fds);
378
379 cmptr = CMSG_FIRSTHDR(&msg);
f680d83c
MD
380 if (!cmptr)
381 return -EINVAL;
67c5b804
MD
382 cmptr->cmsg_level = SOL_SOCKET;
383 cmptr->cmsg_type = SCM_RIGHTS;
384 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
385 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
386 /* Sum of the length of all control messages in the buffer: */
387 msg.msg_controllen = cmptr->cmsg_len;
388
74d81a6c
MD
389 iov[0].iov_base = &dummy;
390 iov[0].iov_len = 1;
67c5b804
MD
391 msg.msg_iov = iov;
392 msg.msg_iovlen = 1;
393
51d9d699 394 do {
549df040 395 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
51d9d699 396 } while (ret < 0 && errno == EINTR);
7bc53e94 397 if (ret < 0) {
74d81a6c
MD
398 /*
399 * We consider EPIPE and ECONNRESET as expected.
400 */
401 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 402 PERROR("sendmsg");
74d81a6c 403 }
b869b5ae
MD
404 ret = -errno;
405 if (ret == -ECONNRESET)
406 ret = -EPIPE;
74d81a6c
MD
407 }
408 return ret;
409}
7bc53e94 410
74d81a6c
MD
411/*
412 * Recv a message accompanied by fd(s) from a unix socket.
413 *
414 * Returns the size of received data, or negative error value.
415 *
416 * Expect at most "nb_fd" file descriptors. Returns the number of fd
417 * actually received in nb_fd.
418 * Returns -EPIPE on orderly shutdown.
419 */
420ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
421{
422 struct iovec iov[1];
423 ssize_t ret = 0;
424 struct cmsghdr *cmsg;
425 size_t sizeof_fds = nb_fd * sizeof(int);
426 char recv_fd[CMSG_SPACE(sizeof_fds)];
427 struct msghdr msg;
428 char dummy;
7bc53e94 429
74d81a6c 430 memset(&msg, 0, sizeof(msg));
67c5b804 431
74d81a6c
MD
432 /* Prepare to receive the structures */
433 iov[0].iov_base = &dummy;
434 iov[0].iov_len = 1;
435 msg.msg_iov = iov;
436 msg.msg_iovlen = 1;
437 msg.msg_control = recv_fd;
438 msg.msg_controllen = sizeof(recv_fd);
439
440 do {
441 ret = recvmsg(sock, &msg, 0);
442 } while (ret < 0 && errno == EINTR);
443 if (ret < 0) {
444 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 445 PERROR("recvmsg fds");
74d81a6c 446 }
8cf811d3 447 ret = -errno;
b869b5ae
MD
448 if (ret == -ECONNRESET)
449 ret = -EPIPE;
74d81a6c
MD
450 goto end;
451 }
452 if (ret == 0) {
453 /* orderly shutdown */
454 ret = -EPIPE;
455 goto end;
456 }
457 if (ret != 1) {
32ce8569 458 ERR("Error: Received %zd bytes, expected %d\n",
74d81a6c
MD
459 ret, 1);
460 goto end;
461 }
462 if (msg.msg_flags & MSG_CTRUNC) {
32ce8569 463 ERR("Error: Control message truncated.\n");
74d81a6c
MD
464 ret = -1;
465 goto end;
466 }
467 cmsg = CMSG_FIRSTHDR(&msg);
468 if (!cmsg) {
32ce8569 469 ERR("Error: Invalid control message header\n");
74d81a6c
MD
470 ret = -1;
471 goto end;
472 }
473 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
32ce8569 474 ERR("Didn't received any fd\n");
74d81a6c
MD
475 ret = -1;
476 goto end;
477 }
478 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
32ce8569 479 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
74d81a6c
MD
480 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
481 ret = -1;
482 goto end;
483 }
484 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
485 ret = sizeof_fds;
486end:
67c5b804
MD
487 return ret;
488}
57773204
MD
489
490int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
491{
492 ssize_t len;
493
494 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
495 switch (len) {
496 case sizeof(*lum):
57773204 497 break;
57773204 498 default:
7bc53e94 499 if (len < 0) {
7bc53e94
MD
500 return len;
501 } else {
32ce8569 502 ERR("incorrect message size: %zd\n", len);
7bc53e94
MD
503 return -EINVAL;
504 }
57773204
MD
505 }
506 return 0;
507}
508
509int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
510 uint32_t expected_handle, uint32_t expected_cmd)
511{
512 ssize_t len;
513
514 memset(lur, 0, sizeof(*lur));
515 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
516 switch (len) {
517 case 0: /* orderly shutdown */
74d81a6c 518 return -EPIPE;
57773204 519 case sizeof(*lur):
57773204 520 if (lur->handle != expected_handle) {
32ce8569 521 ERR("Unexpected result message handle: "
74d81a6c
MD
522 "expected: %u vs received: %u\n",
523 expected_handle, lur->handle);
57773204
MD
524 return -EINVAL;
525 }
57773204 526 if (lur->cmd != expected_cmd) {
32ce8569 527 ERR("Unexpected result message command "
74d81a6c
MD
528 "expected: %u vs received: %u\n",
529 expected_cmd, lur->cmd);
57773204
MD
530 return -EINVAL;
531 }
7bc53e94 532 return lur->ret_code;
57773204 533 default:
8cf811d3 534 if (len >= 0) {
32ce8569 535 ERR("incorrect message size: %zd\n", len);
7bc53e94 536 }
8cf811d3 537 return len;
57773204
MD
538 }
539}
540
541int ustcomm_send_app_cmd(int sock,
542 struct ustcomm_ust_msg *lum,
543 struct ustcomm_ust_reply *lur)
544{
545 int ret;
546
547 ret = ustcomm_send_app_msg(sock, lum);
548 if (ret)
549 return ret;
c354a72c
MD
550 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
551 if (ret > 0)
552 return -EIO;
553 return ret;
57773204
MD
554}
555
57773204 556/*
74d81a6c
MD
557 * chan_data is allocated internally if this function returns the
558 * expected var_len.
57773204 559 */
74d81a6c 560ssize_t ustcomm_recv_channel_from_sessiond(int sock,
ff0f5728
MD
561 void **_chan_data, uint64_t var_len,
562 int *_wakeup_fd)
57773204 563{
74d81a6c 564 void *chan_data;
ff0f5728
MD
565 ssize_t len, nr_fd;
566 int wakeup_fd;
57773204 567
74d81a6c
MD
568 if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) {
569 len = -EINVAL;
570 goto error_check;
57773204 571 }
74d81a6c
MD
572 /* Receive variable length data */
573 chan_data = zmalloc(var_len);
574 if (!chan_data) {
575 len = -ENOMEM;
576 goto error_alloc;
57773204 577 }
74d81a6c
MD
578 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
579 if (len != var_len) {
580 goto error_recv;
57773204 581 }
ff0f5728
MD
582 /* recv wakeup fd */
583 nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
584 if (nr_fd <= 0) {
585 if (nr_fd < 0) {
586 len = nr_fd;
587 goto error_recv;
588 } else {
589 len = -EIO;
590 goto error_recv;
591 }
592 }
593 *_wakeup_fd = wakeup_fd;
74d81a6c
MD
594 *_chan_data = chan_data;
595 return len;
596
597error_recv:
598 free(chan_data);
599error_alloc:
600error_check:
601 return len;
602}
7bc53e94 603
74d81a6c
MD
604int ustcomm_recv_stream_from_sessiond(int sock,
605 uint64_t *memory_map_size,
606 int *shm_fd, int *wakeup_fd)
607{
608 ssize_t len;
609 int ret;
610 int fds[2];
611
612 /* recv shm fd and wakeup fd */
613 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
614 if (len <= 0) {
615 if (len < 0) {
616 ret = len;
617 goto error;
618 } else {
619 ret = -EIO;
620 goto error;
621 }
7bc53e94 622 }
74d81a6c
MD
623 *shm_fd = fds[0];
624 *wakeup_fd = fds[1];
625 return 0;
626
627error:
57773204
MD
628 return ret;
629}
32ce8569
MD
630
631/*
632 * Returns 0 on success, negative error value on error.
633 */
634int ustcomm_send_reg_msg(int sock,
635 enum ustctl_socket_type type,
636 uint32_t bits_per_long,
637 uint32_t uint8_t_alignment,
638 uint32_t uint16_t_alignment,
639 uint32_t uint32_t_alignment,
640 uint32_t uint64_t_alignment,
641 uint32_t long_alignment)
642{
643 ssize_t len;
644 struct ustctl_reg_msg reg_msg;
645
646 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
647 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
648 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
649 reg_msg.pid = getpid();
650 reg_msg.ppid = getppid();
651 reg_msg.uid = getuid();
652 reg_msg.gid = getgid();
653 reg_msg.bits_per_long = bits_per_long;
654 reg_msg.uint8_t_alignment = uint8_t_alignment;
655 reg_msg.uint16_t_alignment = uint16_t_alignment;
656 reg_msg.uint32_t_alignment = uint32_t_alignment;
657 reg_msg.uint64_t_alignment = uint64_t_alignment;
658 reg_msg.long_alignment = long_alignment;
659 reg_msg.socket_type = type;
660 lttng_ust_getprocname(reg_msg.name);
661 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
662
663 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
664 if (len > 0 && len != sizeof(reg_msg))
665 return -EIO;
666 if (len < 0)
667 return len;
668 return 0;
669}
670
7f2348b8
MD
671static
672int serialize_string_encoding(enum ustctl_string_encodings *ue,
673 enum lttng_string_encodings le)
674{
675 switch (le) {
676 case lttng_encode_none:
677 *ue = ustctl_encode_none;
678 break;
679 case lttng_encode_UTF8:
680 *ue = ustctl_encode_UTF8;
681 break;
682 case lttng_encode_ASCII:
683 *ue = ustctl_encode_ASCII;
684 break;
685 default:
686 return -EINVAL;
687 }
688 return 0;
689}
690
32ce8569 691static
2b213b16
MD
692int serialize_basic_type(enum ustctl_abstract_types *uatype,
693 enum lttng_abstract_types atype,
32ce8569
MD
694 union _ustctl_basic_type *ubt,
695 const union _lttng_basic_type *lbt)
696{
697 switch (atype) {
698 case atype_integer:
699 {
700 struct ustctl_integer_type *uit;
701 const struct lttng_integer_type *lit;
702
703 uit = &ubt->integer;
704 lit = &lbt->integer;
705 uit->size = lit->size;
706 uit->signedness = lit->signedness;
707 uit->reverse_byte_order = lit->reverse_byte_order;
708 uit->base = lit->base;
7f2348b8
MD
709 if (serialize_string_encoding(&uit->encoding, lit->encoding))
710 return -EINVAL;
32ce8569 711 uit->alignment = lit->alignment;
2b213b16 712 *uatype = ustctl_atype_integer;
32ce8569
MD
713 break;
714 }
715 case atype_string:
716 {
7f2348b8
MD
717 if (serialize_string_encoding(&ubt->string.encoding,
718 lbt->string.encoding))
719 return -EINVAL;
2b213b16 720 *uatype = ustctl_atype_string;
32ce8569
MD
721 break;
722 }
723 case atype_float:
724 {
725 struct ustctl_float_type *uft;
726 const struct lttng_float_type *lft;
727
728 uft = &ubt->_float;
729 lft = &lbt->_float;
730 uft->exp_dig = lft->exp_dig;
731 uft->mant_dig = lft->mant_dig;
732 uft->alignment = lft->alignment;
733 uft->reverse_byte_order = lft->reverse_byte_order;
2b213b16 734 *uatype = ustctl_atype_float;
32ce8569
MD
735 break;
736 }
737 case atype_enum:
738 case atype_array:
739 case atype_sequence:
740 default:
741 return -EINVAL;
742 }
743 return 0;
32ce8569
MD
744}
745
746static
747int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt)
748{
749 int ret;
750
751 switch (lt->atype) {
752 case atype_integer:
753 case atype_float:
754 case atype_string:
2b213b16 755 ret = serialize_basic_type(&ut->atype, lt->atype,
32ce8569
MD
756 &ut->u.basic, &lt->u.basic);
757 if (ret)
758 return ret;
759 break;
760 case atype_array:
761 {
762 struct ustctl_basic_type *ubt;
763 const struct lttng_basic_type *lbt;
764 int ret;
765
766 ubt = &ut->u.array.elem_type;
767 lbt = &lt->u.array.elem_type;
768 ut->u.array.length = lt->u.array.length;
2b213b16 769 ret = serialize_basic_type(&ubt->atype, lbt->atype,
32ce8569
MD
770 &ubt->u.basic, &lbt->u.basic);
771 if (ret)
772 return -EINVAL;
2b213b16 773 ut->atype = ustctl_atype_array;
32ce8569
MD
774 break;
775 }
776 case atype_sequence:
777 {
778 struct ustctl_basic_type *ubt;
779 const struct lttng_basic_type *lbt;
780 int ret;
781
782 ubt = &ut->u.sequence.length_type;
783 lbt = &lt->u.sequence.length_type;
2b213b16 784 ret = serialize_basic_type(&ubt->atype, lbt->atype,
32ce8569
MD
785 &ubt->u.basic, &lbt->u.basic);
786 if (ret)
787 return -EINVAL;
788 ubt = &ut->u.sequence.elem_type;
789 lbt = &lt->u.sequence.elem_type;
2b213b16 790 ret = serialize_basic_type(&ubt->atype, lbt->atype,
32ce8569
MD
791 &ubt->u.basic, &lbt->u.basic);
792 if (ret)
793 return -EINVAL;
2b213b16 794 ut->atype = ustctl_atype_sequence;
32ce8569
MD
795 break;
796 }
797 case atype_enum:
798 default:
799 return -EINVAL;
800 }
801 return 0;
802}
803
804static
805int serialize_fields(size_t *_nr_write_fields,
806 struct ustctl_field **ustctl_fields,
807 size_t nr_fields,
808 const struct lttng_event_field *lttng_fields)
809{
810 struct ustctl_field *fields;
811 int i, ret;
812 size_t nr_write_fields = 0;
813
814 fields = zmalloc(nr_fields * sizeof(*fields));
815 if (!fields)
816 return -ENOMEM;
817
818 for (i = 0; i < nr_fields; i++) {
819 struct ustctl_field *f;
820 const struct lttng_event_field *lf;
821
822 f = &fields[nr_write_fields];
823 lf = &lttng_fields[i];
824
825 /* skip 'nowrite' fields */
826 if (lf->nowrite)
827 continue;
828 strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
829 f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
830 ret = serialize_one_type(&f->type, &lf->type);
831 if (ret)
832 goto error_type;
833 nr_write_fields++;
834 }
835
836 *_nr_write_fields = nr_write_fields;
837 *ustctl_fields = fields;
838 return 0;
839
840error_type:
841 free(fields);
842 return ret;
843}
844
83e43212
MD
845static
846int serialize_ctx_fields(size_t *_nr_write_fields,
847 struct ustctl_field **ustctl_fields,
848 size_t nr_fields,
849 const struct lttng_ctx_field *lttng_fields)
850{
851 struct ustctl_field *fields;
852 int i, ret;
853 size_t nr_write_fields = 0;
854
855 fields = zmalloc(nr_fields * sizeof(*fields));
856 if (!fields)
857 return -ENOMEM;
858
859 for (i = 0; i < nr_fields; i++) {
860 struct ustctl_field *f;
861 const struct lttng_event_field *lf;
862
863 f = &fields[nr_write_fields];
864 lf = &lttng_fields[i].event_field;
865
866 /* skip 'nowrite' fields */
867 if (lf->nowrite)
868 continue;
869 strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
870 f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
871 ret = serialize_one_type(&f->type, &lf->type);
872 if (ret)
873 goto error_type;
874 nr_write_fields++;
875 }
876
877 *_nr_write_fields = nr_write_fields;
878 *ustctl_fields = fields;
879 return 0;
880
881error_type:
882 free(fields);
883 return ret;
884}
885
32ce8569
MD
886/*
887 * Returns 0 on success, negative error value on error.
888 */
889int ustcomm_register_event(int sock,
890 int session_objd, /* session descriptor */
891 int channel_objd, /* channel descriptor */
892 const char *event_name, /* event name (input) */
893 int loglevel,
894 const char *signature, /* event signature (input) */
895 size_t nr_fields, /* fields */
896 const struct lttng_event_field *lttng_fields,
897 const char *model_emf_uri,
898 uint32_t *id) /* event id (output) */
899{
900 ssize_t len;
901 struct {
902 struct ustcomm_notify_hdr header;
903 struct ustcomm_notify_event_msg m;
904 } msg;
905 struct {
906 struct ustcomm_notify_hdr header;
907 struct ustcomm_notify_event_reply r;
908 } reply;
909 size_t signature_len, fields_len, model_emf_uri_len;
3bf32af0 910 struct ustctl_field *fields = NULL;
32ce8569
MD
911 size_t nr_write_fields = 0;
912 int ret;
913
914 memset(&msg, 0, sizeof(msg));
915 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
916 msg.m.session_objd = session_objd;
917 msg.m.channel_objd = channel_objd;
918 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
919 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
920 msg.m.loglevel = loglevel;
921 signature_len = strlen(signature) + 1;
922 msg.m.signature_len = signature_len;
923
924 /* Calculate fields len, serialize fields. */
925 if (nr_fields > 0) {
926 ret = serialize_fields(&nr_write_fields, &fields,
927 nr_fields, lttng_fields);
928 if (ret)
929 return ret;
930 }
931
932 fields_len = sizeof(*fields) * nr_write_fields;
933 msg.m.fields_len = fields_len;
934 if (model_emf_uri) {
935 model_emf_uri_len = strlen(model_emf_uri) + 1;
936 } else {
937 model_emf_uri_len = 0;
938 }
939 msg.m.model_emf_uri_len = model_emf_uri_len;
940 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
941 if (len > 0 && len != sizeof(msg)) {
942 free(fields);
943 return -EIO;
944 }
945 if (len < 0) {
946 free(fields);
947 return len;
948 }
949
950 /* send signature */
951 len = ustcomm_send_unix_sock(sock, signature, signature_len);
952 if (len > 0 && len != signature_len) {
953 free(fields);
954 return -EIO;
955 }
956 if (len < 0) {
957 free(fields);
958 return len;
959 }
960
961 /* send fields */
962 if (fields_len > 0) {
963 len = ustcomm_send_unix_sock(sock, fields, fields_len);
964 free(fields);
965 if (len > 0 && len != fields_len) {
966 return -EIO;
967 }
968 if (len < 0) {
969 return len;
970 }
464ea3a7
MD
971 } else {
972 free(fields);
32ce8569
MD
973 }
974
975 if (model_emf_uri_len) {
976 /* send model_emf_uri */
977 len = ustcomm_send_unix_sock(sock, model_emf_uri,
978 model_emf_uri_len);
979 if (len > 0 && len != model_emf_uri_len)
980 return -EIO;
981 if (len < 0)
982 return len;
983 }
984
985 /* receive reply */
986 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
987 switch (len) {
988 case 0: /* orderly shutdown */
989 return -EPIPE;
990 case sizeof(reply):
991 if (reply.header.notify_cmd != msg.header.notify_cmd) {
992 ERR("Unexpected result message command "
993 "expected: %u vs received: %u\n",
994 msg.header.notify_cmd, reply.header.notify_cmd);
995 return -EINVAL;
996 }
997 if (reply.r.ret_code > 0)
998 return -EINVAL;
999 if (reply.r.ret_code < 0)
1000 return reply.r.ret_code;
1001 *id = reply.r.event_id;
1002 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1003 event_name, reply.r.ret_code, reply.r.event_id);
1004 return 0;
1005 default:
1006 if (len < 0) {
1007 /* Transport level error */
1008 if (errno == EPIPE || errno == ECONNRESET)
1009 len = -errno;
1010 return len;
1011 } else {
1012 ERR("incorrect message size: %zd\n", len);
1013 return len;
1014 }
1015 }
1016}
1017
1018/*
1019 * Returns 0 on success, negative error value on error.
1020 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1021 */
1022int ustcomm_register_channel(int sock,
1023 int session_objd, /* session descriptor */
1024 int channel_objd, /* channel descriptor */
1025 size_t nr_ctx_fields,
83e43212 1026 const struct lttng_ctx_field *ctx_fields,
32ce8569
MD
1027 uint32_t *chan_id, /* channel id (output) */
1028 int *header_type) /* header type (output) */
1029{
1030 ssize_t len;
1031 struct {
1032 struct ustcomm_notify_hdr header;
1033 struct ustcomm_notify_channel_msg m;
1034 } msg;
1035 struct {
1036 struct ustcomm_notify_hdr header;
1037 struct ustcomm_notify_channel_reply r;
1038 } reply;
1039 size_t fields_len;
83e43212 1040 struct ustctl_field *fields = NULL;
32ce8569
MD
1041 int ret;
1042 size_t nr_write_fields = 0;
1043
1044 memset(&msg, 0, sizeof(msg));
1045 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1046 msg.m.session_objd = session_objd;
1047 msg.m.channel_objd = channel_objd;
1048
1049 /* Calculate fields len, serialize fields. */
1050 if (nr_ctx_fields > 0) {
83e43212 1051 ret = serialize_ctx_fields(&nr_write_fields, &fields,
32ce8569
MD
1052 nr_ctx_fields, ctx_fields);
1053 if (ret)
1054 return ret;
1055 }
1056
1057 fields_len = sizeof(*fields) * nr_write_fields;
1058 msg.m.ctx_fields_len = fields_len;
1059 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1060 if (len > 0 && len != sizeof(msg)) {
1061 free(fields);
1062 return -EIO;
1063 }
1064 if (len < 0) {
1065 free(fields);
1066 return len;
1067 }
1068
1069 /* send fields */
1070 if (fields_len > 0) {
1071 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1072 free(fields);
1073 if (len > 0 && len != fields_len) {
1074 return -EIO;
1075 }
1076 if (len < 0) {
1077 return len;
1078 }
17ea789c
MD
1079 } else {
1080 free(fields);
32ce8569
MD
1081 }
1082
1083 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1084 switch (len) {
1085 case 0: /* orderly shutdown */
1086 return -EPIPE;
1087 case sizeof(reply):
1088 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1089 ERR("Unexpected result message command "
1090 "expected: %u vs received: %u\n",
1091 msg.header.notify_cmd, reply.header.notify_cmd);
1092 return -EINVAL;
1093 }
1094 if (reply.r.ret_code > 0)
1095 return -EINVAL;
1096 if (reply.r.ret_code < 0)
1097 return reply.r.ret_code;
1098 *chan_id = reply.r.chan_id;
1099 switch (reply.r.header_type) {
1100 case 1:
1101 case 2:
1102 *header_type = reply.r.header_type;
1103 break;
1104 default:
1105 ERR("Unexpected channel header type %u\n",
1106 reply.r.header_type);
1107 return -EINVAL;
1108 }
1109 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1110 reply.r.chan_id, reply.r.header_type);
1111 return 0;
1112 default:
1113 if (len < 0) {
1114 /* Transport level error */
1115 if (errno == EPIPE || errno == ECONNRESET)
1116 len = -errno;
1117 return len;
1118 } else {
1119 ERR("incorrect message size: %zd\n", len);
1120 return len;
1121 }
1122 }
1123}
ff517991
MD
1124
1125/*
1126 * Set socket reciving timeout.
1127 */
1128int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1129{
1130 int ret;
1131 struct timeval tv;
1132
1133 tv.tv_sec = msec / 1000;
1134 tv.tv_usec = (msec * 1000 % 1000000);
1135
1136 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1137 if (ret < 0) {
1138 PERROR("setsockopt SO_RCVTIMEO");
1139 ret = -errno;
1140 }
1141
1142 return ret;
1143}
1144
1145/*
1146 * Set socket sending timeout.
1147 */
1148int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1149{
1150 int ret;
1151 struct timeval tv;
1152
1153 tv.tv_sec = msec / 1000;
1154 tv.tv_usec = (msec * 1000) % 1000000;
1155
1156 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1157 if (ret < 0) {
1158 PERROR("setsockopt SO_SNDTIMEO");
1159 ret = -errno;
1160 }
1161
1162 return ret;
1163}
This page took 0.086291 seconds and 4 git commands to generate.