Remove now unused metadata printf code
[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)];
82
67c5b804
MD
83}
84
85/*
74d81a6c 86 * ustcomm_connect_unix_sock
67c5b804 87 *
74d81a6c 88 * Connect to unix socket using the path name.
67c5b804 89 */
57773204 90int ustcomm_connect_unix_sock(const char *pathname)
67c5b804
MD
91{
92 struct sockaddr_un sun;
7bc53e94 93 int fd, ret;
67c5b804 94
204d45df
MD
95 /*
96 * libust threads require the close-on-exec flag for all
97 * resources so it does not leak file descriptors upon exec.
98 */
11ba4bcb 99 fd = socket(PF_UNIX, SOCK_STREAM, 0);
67c5b804 100 if (fd < 0) {
32ce8569 101 PERROR("socket");
7bc53e94 102 ret = -errno;
67c5b804
MD
103 goto error;
104 }
11ba4bcb
MD
105 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
106 if (ret < 0) {
32ce8569 107 PERROR("fcntl");
7bc53e94 108 ret = -errno;
11ba4bcb
MD
109 goto error_fcntl;
110 }
67c5b804
MD
111
112 memset(&sun, 0, sizeof(sun));
113 sun.sun_family = AF_UNIX;
114 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
115 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
116
117 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
118 if (ret < 0) {
119 /*
120 * Don't print message on connect error, because connect
121 * is used in normal execution to detect if sessiond is
122 * alive.
123 */
7bc53e94 124 ret = -errno;
67c5b804
MD
125 goto error_connect;
126 }
127
128 return fd;
129
130error_connect:
11ba4bcb 131error_fcntl:
7bc53e94
MD
132 {
133 int closeret;
134
135 closeret = close(fd);
136 if (closeret)
32ce8569 137 PERROR("close");
7bc53e94 138 }
67c5b804
MD
139error:
140 return ret;
141}
142
143/*
74d81a6c 144 * ustcomm_accept_unix_sock
67c5b804 145 *
74d81a6c
MD
146 * Do an accept(2) on the sock and return the
147 * new file descriptor. The socket MUST be bind(2) before.
67c5b804 148 */
57773204 149int ustcomm_accept_unix_sock(int sock)
67c5b804
MD
150{
151 int new_fd;
152 struct sockaddr_un sun;
153 socklen_t len = 0;
154
155 /* Blocking call */
156 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
157 if (new_fd < 0) {
32ce8569 158 PERROR("accept");
7bc53e94 159 return -errno;
67c5b804 160 }
67c5b804 161 return new_fd;
67c5b804
MD
162}
163
164/*
74d81a6c 165 * ustcomm_create_unix_sock
67c5b804 166 *
74d81a6c
MD
167 * Creates a AF_UNIX local socket using pathname
168 * bind the socket upon creation and return the fd.
67c5b804 169 */
57773204 170int ustcomm_create_unix_sock(const char *pathname)
67c5b804
MD
171{
172 struct sockaddr_un sun;
7bc53e94 173 int fd, ret;
67c5b804
MD
174
175 /* Create server socket */
176 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
32ce8569 177 PERROR("socket");
7bc53e94 178 ret = -errno;
67c5b804
MD
179 goto error;
180 }
181
182 memset(&sun, 0, sizeof(sun));
183 sun.sun_family = AF_UNIX;
184 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
185 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
186
187 /* Unlink the old file if present */
188 (void) unlink(pathname);
189 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
190 if (ret < 0) {
32ce8569 191 PERROR("bind");
7bc53e94
MD
192 ret = -errno;
193 goto error_close;
67c5b804
MD
194 }
195
196 return fd;
197
7bc53e94
MD
198error_close:
199 {
200 int closeret;
201
202 closeret = close(fd);
203 if (closeret) {
32ce8569 204 PERROR("close");
7bc53e94
MD
205 }
206 }
67c5b804
MD
207error:
208 return ret;
209}
210
211/*
74d81a6c 212 * ustcomm_listen_unix_sock
67c5b804 213 *
74d81a6c 214 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
67c5b804 215 */
57773204 216int ustcomm_listen_unix_sock(int sock)
67c5b804
MD
217{
218 int ret;
219
e41474be 220 ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN);
67c5b804 221 if (ret < 0) {
7bc53e94 222 ret = -errno;
32ce8569 223 PERROR("listen");
67c5b804
MD
224 }
225
226 return ret;
227}
228
229/*
74d81a6c
MD
230 * ustcomm_close_unix_sock
231 *
232 * Shutdown cleanly a unix socket.
233 */
234int ustcomm_close_unix_sock(int sock)
235{
236 int ret;
237
238 ret = close(sock);
239 if (ret < 0) {
32ce8569 240 PERROR("close");
74d81a6c
MD
241 ret = -errno;
242 }
243
244 return ret;
245}
246
247/*
248 * ustcomm_recv_unix_sock
67c5b804 249 *
74d81a6c
MD
250 * Receive data of size len in put that data into
251 * the buf param. Using recvmsg API.
252 * Return the size of received data.
253 * Return 0 on orderly shutdown.
67c5b804 254 */
57773204 255ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
67c5b804 256{
913b87f1 257 struct msghdr msg;
67c5b804 258 struct iovec iov[1];
7bc53e94 259 ssize_t ret;
67c5b804 260
913b87f1
MD
261 memset(&msg, 0, sizeof(msg));
262
67c5b804
MD
263 iov[0].iov_base = buf;
264 iov[0].iov_len = len;
265 msg.msg_iov = iov;
266 msg.msg_iovlen = 1;
267
7e3cfcbe
MD
268 do {
269 ret = recvmsg(sock, &msg, 0);
270 } while (ret < 0 && errno == EINTR);
7bc53e94
MD
271
272 if (ret < 0) {
273 int shutret;
274
74d81a6c 275 if (errno != EPIPE && errno != ECONNRESET)
32ce8569 276 PERROR("recvmsg");
7bc53e94
MD
277 ret = -errno;
278
279 shutret = shutdown(sock, SHUT_RDWR);
280 if (shutret)
32ce8569 281 ERR("Socket shutdown error");
67c5b804
MD
282 }
283
284 return ret;
285}
286
287/*
74d81a6c 288 * ustcomm_send_unix_sock
67c5b804 289 *
74d81a6c
MD
290 * Send buf data of size len. Using sendmsg API.
291 * Return the size of sent data.
67c5b804 292 */
32ce8569 293ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len)
67c5b804 294{
913b87f1 295 struct msghdr msg;
67c5b804 296 struct iovec iov[1];
7bc53e94 297 ssize_t ret;
67c5b804 298
913b87f1
MD
299 memset(&msg, 0, sizeof(msg));
300
32ce8569 301 iov[0].iov_base = (void *) buf;
67c5b804
MD
302 iov[0].iov_len = len;
303 msg.msg_iov = iov;
304 msg.msg_iovlen = 1;
305
1ea11eab
MD
306 /*
307 * Using the MSG_NOSIGNAL when sending data from sessiond to
308 * libust, so libust does not receive an unhandled SIGPIPE or
309 * SIGURG. The sessiond receiver side can be made more resilient
310 * by ignoring SIGPIPE, but we don't have this luxury on the
311 * libust side.
312 */
51d9d699
MD
313 do {
314 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
315 } while (ret < 0 && errno == EINTR);
7bc53e94
MD
316
317 if (ret < 0) {
318 int shutret;
319
74d81a6c 320 if (errno != EPIPE && errno != ECONNRESET)
32ce8569 321 PERROR("sendmsg");
7bc53e94
MD
322 ret = -errno;
323
324 shutret = shutdown(sock, SHUT_RDWR);
325 if (shutret)
32ce8569 326 ERR("Socket shutdown error");
67c5b804
MD
327 }
328
329 return ret;
330}
331
332/*
74d81a6c 333 * Send a message accompanied by fd(s) over a unix socket.
67c5b804 334 *
74d81a6c 335 * Returns the size of data sent, or negative error value.
67c5b804 336 */
74d81a6c 337ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
67c5b804 338{
913b87f1 339 struct msghdr msg;
67c5b804
MD
340 struct cmsghdr *cmptr;
341 struct iovec iov[1];
342 ssize_t ret = -1;
343 unsigned int sizeof_fds = nb_fd * sizeof(int);
344 char tmp[CMSG_SPACE(sizeof_fds)];
74d81a6c 345 char dummy = 0;
67c5b804 346
913b87f1 347 memset(&msg, 0, sizeof(msg));
74d81a6c 348 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
913b87f1 349
74d81a6c
MD
350 if (nb_fd > USTCOMM_MAX_SEND_FDS)
351 return -EINVAL;
67c5b804
MD
352
353 msg.msg_control = (caddr_t)tmp;
354 msg.msg_controllen = CMSG_LEN(sizeof_fds);
355
356 cmptr = CMSG_FIRSTHDR(&msg);
357 cmptr->cmsg_level = SOL_SOCKET;
358 cmptr->cmsg_type = SCM_RIGHTS;
359 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
360 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
361 /* Sum of the length of all control messages in the buffer: */
362 msg.msg_controllen = cmptr->cmsg_len;
363
74d81a6c
MD
364 iov[0].iov_base = &dummy;
365 iov[0].iov_len = 1;
67c5b804
MD
366 msg.msg_iov = iov;
367 msg.msg_iovlen = 1;
368
51d9d699 369 do {
74d81a6c 370 ret = sendmsg(sock, &msg, 0);
51d9d699 371 } while (ret < 0 && errno == EINTR);
7bc53e94 372 if (ret < 0) {
74d81a6c
MD
373 /*
374 * We consider EPIPE and ECONNRESET as expected.
375 */
376 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 377 PERROR("sendmsg");
74d81a6c
MD
378 }
379 }
380 return ret;
381}
7bc53e94 382
74d81a6c
MD
383/*
384 * Recv a message accompanied by fd(s) from a unix socket.
385 *
386 * Returns the size of received data, or negative error value.
387 *
388 * Expect at most "nb_fd" file descriptors. Returns the number of fd
389 * actually received in nb_fd.
390 * Returns -EPIPE on orderly shutdown.
391 */
392ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
393{
394 struct iovec iov[1];
395 ssize_t ret = 0;
396 struct cmsghdr *cmsg;
397 size_t sizeof_fds = nb_fd * sizeof(int);
398 char recv_fd[CMSG_SPACE(sizeof_fds)];
399 struct msghdr msg;
400 char dummy;
7bc53e94 401
74d81a6c 402 memset(&msg, 0, sizeof(msg));
67c5b804 403
74d81a6c
MD
404 /* Prepare to receive the structures */
405 iov[0].iov_base = &dummy;
406 iov[0].iov_len = 1;
407 msg.msg_iov = iov;
408 msg.msg_iovlen = 1;
409 msg.msg_control = recv_fd;
410 msg.msg_controllen = sizeof(recv_fd);
411
412 do {
413 ret = recvmsg(sock, &msg, 0);
414 } while (ret < 0 && errno == EINTR);
415 if (ret < 0) {
416 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 417 PERROR("recvmsg fds");
74d81a6c
MD
418 }
419 if (errno == EPIPE || errno == ECONNRESET)
420 ret = -errno;
421 goto end;
422 }
423 if (ret == 0) {
424 /* orderly shutdown */
425 ret = -EPIPE;
426 goto end;
427 }
428 if (ret != 1) {
32ce8569 429 ERR("Error: Received %zd bytes, expected %d\n",
74d81a6c
MD
430 ret, 1);
431 goto end;
432 }
433 if (msg.msg_flags & MSG_CTRUNC) {
32ce8569 434 ERR("Error: Control message truncated.\n");
74d81a6c
MD
435 ret = -1;
436 goto end;
437 }
438 cmsg = CMSG_FIRSTHDR(&msg);
439 if (!cmsg) {
32ce8569 440 ERR("Error: Invalid control message header\n");
74d81a6c
MD
441 ret = -1;
442 goto end;
443 }
444 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
32ce8569 445 ERR("Didn't received any fd\n");
74d81a6c
MD
446 ret = -1;
447 goto end;
448 }
449 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
32ce8569 450 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
74d81a6c
MD
451 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
452 ret = -1;
453 goto end;
454 }
455 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
456 ret = sizeof_fds;
457end:
67c5b804
MD
458 return ret;
459}
57773204
MD
460
461int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
462{
463 ssize_t len;
464
465 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
466 switch (len) {
467 case sizeof(*lum):
57773204 468 break;
57773204 469 default:
7bc53e94 470 if (len < 0) {
7bc53e94
MD
471 return len;
472 } else {
32ce8569 473 ERR("incorrect message size: %zd\n", len);
7bc53e94
MD
474 return -EINVAL;
475 }
57773204
MD
476 }
477 return 0;
478}
479
480int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
481 uint32_t expected_handle, uint32_t expected_cmd)
482{
483 ssize_t len;
484
485 memset(lur, 0, sizeof(*lur));
486 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
487 switch (len) {
488 case 0: /* orderly shutdown */
74d81a6c 489 return -EPIPE;
57773204 490 case sizeof(*lur):
57773204 491 if (lur->handle != expected_handle) {
32ce8569 492 ERR("Unexpected result message handle: "
74d81a6c
MD
493 "expected: %u vs received: %u\n",
494 expected_handle, lur->handle);
57773204
MD
495 return -EINVAL;
496 }
57773204 497 if (lur->cmd != expected_cmd) {
32ce8569 498 ERR("Unexpected result message command "
74d81a6c
MD
499 "expected: %u vs received: %u\n",
500 expected_cmd, lur->cmd);
57773204
MD
501 return -EINVAL;
502 }
7bc53e94 503 return lur->ret_code;
57773204 504 default:
7bc53e94
MD
505 if (len < 0) {
506 /* Transport level error */
74d81a6c
MD
507 if (errno == EPIPE || errno == ECONNRESET)
508 len = -errno;
7bc53e94
MD
509 return len;
510 } else {
32ce8569 511 ERR("incorrect message size: %zd\n", len);
7bc53e94
MD
512 return len;
513 }
57773204
MD
514 }
515}
516
517int ustcomm_send_app_cmd(int sock,
518 struct ustcomm_ust_msg *lum,
519 struct ustcomm_ust_reply *lur)
520{
521 int ret;
522
523 ret = ustcomm_send_app_msg(sock, lum);
524 if (ret)
525 return ret;
c354a72c
MD
526 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
527 if (ret > 0)
528 return -EIO;
529 return ret;
57773204
MD
530}
531
57773204 532/*
74d81a6c
MD
533 * chan_data is allocated internally if this function returns the
534 * expected var_len.
57773204 535 */
74d81a6c
MD
536ssize_t ustcomm_recv_channel_from_sessiond(int sock,
537 void **_chan_data, uint64_t var_len)
57773204 538{
74d81a6c
MD
539 void *chan_data;
540 ssize_t len;
57773204 541
74d81a6c
MD
542 if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) {
543 len = -EINVAL;
544 goto error_check;
57773204 545 }
74d81a6c
MD
546 /* Receive variable length data */
547 chan_data = zmalloc(var_len);
548 if (!chan_data) {
549 len = -ENOMEM;
550 goto error_alloc;
57773204 551 }
74d81a6c
MD
552 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
553 if (len != var_len) {
554 goto error_recv;
57773204 555 }
74d81a6c
MD
556 *_chan_data = chan_data;
557 return len;
558
559error_recv:
560 free(chan_data);
561error_alloc:
562error_check:
563 return len;
564}
7bc53e94 565
74d81a6c
MD
566int ustcomm_recv_stream_from_sessiond(int sock,
567 uint64_t *memory_map_size,
568 int *shm_fd, int *wakeup_fd)
569{
570 ssize_t len;
571 int ret;
572 int fds[2];
573
574 /* recv shm fd and wakeup fd */
575 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
576 if (len <= 0) {
577 if (len < 0) {
578 ret = len;
579 goto error;
580 } else {
581 ret = -EIO;
582 goto error;
583 }
7bc53e94 584 }
74d81a6c
MD
585 *shm_fd = fds[0];
586 *wakeup_fd = fds[1];
587 return 0;
588
589error:
57773204
MD
590 return ret;
591}
32ce8569
MD
592
593/*
594 * Returns 0 on success, negative error value on error.
595 */
596int ustcomm_send_reg_msg(int sock,
597 enum ustctl_socket_type type,
598 uint32_t bits_per_long,
599 uint32_t uint8_t_alignment,
600 uint32_t uint16_t_alignment,
601 uint32_t uint32_t_alignment,
602 uint32_t uint64_t_alignment,
603 uint32_t long_alignment)
604{
605 ssize_t len;
606 struct ustctl_reg_msg reg_msg;
607
608 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
609 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
610 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
611 reg_msg.pid = getpid();
612 reg_msg.ppid = getppid();
613 reg_msg.uid = getuid();
614 reg_msg.gid = getgid();
615 reg_msg.bits_per_long = bits_per_long;
616 reg_msg.uint8_t_alignment = uint8_t_alignment;
617 reg_msg.uint16_t_alignment = uint16_t_alignment;
618 reg_msg.uint32_t_alignment = uint32_t_alignment;
619 reg_msg.uint64_t_alignment = uint64_t_alignment;
620 reg_msg.long_alignment = long_alignment;
621 reg_msg.socket_type = type;
622 lttng_ust_getprocname(reg_msg.name);
623 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
624
625 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
626 if (len > 0 && len != sizeof(reg_msg))
627 return -EIO;
628 if (len < 0)
629 return len;
630 return 0;
631}
632
633static
2b213b16
MD
634int serialize_basic_type(enum ustctl_abstract_types *uatype,
635 enum lttng_abstract_types atype,
32ce8569
MD
636 union _ustctl_basic_type *ubt,
637 const union _lttng_basic_type *lbt)
638{
639 switch (atype) {
640 case atype_integer:
641 {
642 struct ustctl_integer_type *uit;
643 const struct lttng_integer_type *lit;
644
645 uit = &ubt->integer;
646 lit = &lbt->integer;
647 uit->size = lit->size;
648 uit->signedness = lit->signedness;
649 uit->reverse_byte_order = lit->reverse_byte_order;
650 uit->base = lit->base;
651 uit->encoding = lit->encoding;
652 uit->alignment = lit->alignment;
2b213b16 653 *uatype = ustctl_atype_integer;
32ce8569
MD
654 break;
655 }
656 case atype_string:
657 {
658 ubt->string.encoding = lbt->string.encoding;
2b213b16 659 *uatype = ustctl_atype_string;
32ce8569
MD
660 break;
661 }
662 case atype_float:
663 {
664 struct ustctl_float_type *uft;
665 const struct lttng_float_type *lft;
666
667 uft = &ubt->_float;
668 lft = &lbt->_float;
669 uft->exp_dig = lft->exp_dig;
670 uft->mant_dig = lft->mant_dig;
671 uft->alignment = lft->alignment;
672 uft->reverse_byte_order = lft->reverse_byte_order;
2b213b16 673 *uatype = ustctl_atype_float;
32ce8569
MD
674 break;
675 }
676 case atype_enum:
677 case atype_array:
678 case atype_sequence:
679 default:
680 return -EINVAL;
681 }
682 return 0;
32ce8569
MD
683}
684
685static
686int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt)
687{
688 int ret;
689
690 switch (lt->atype) {
691 case atype_integer:
692 case atype_float:
693 case atype_string:
2b213b16 694 ret = serialize_basic_type(&ut->atype, lt->atype,
32ce8569
MD
695 &ut->u.basic, &lt->u.basic);
696 if (ret)
697 return ret;
698 break;
699 case atype_array:
700 {
701 struct ustctl_basic_type *ubt;
702 const struct lttng_basic_type *lbt;
703 int ret;
704
705 ubt = &ut->u.array.elem_type;
706 lbt = &lt->u.array.elem_type;
707 ut->u.array.length = lt->u.array.length;
2b213b16 708 ret = serialize_basic_type(&ubt->atype, lbt->atype,
32ce8569
MD
709 &ubt->u.basic, &lbt->u.basic);
710 if (ret)
711 return -EINVAL;
2b213b16 712 ut->atype = ustctl_atype_array;
32ce8569
MD
713 break;
714 }
715 case atype_sequence:
716 {
717 struct ustctl_basic_type *ubt;
718 const struct lttng_basic_type *lbt;
719 int ret;
720
721 ubt = &ut->u.sequence.length_type;
722 lbt = &lt->u.sequence.length_type;
2b213b16 723 ret = serialize_basic_type(&ubt->atype, lbt->atype,
32ce8569
MD
724 &ubt->u.basic, &lbt->u.basic);
725 if (ret)
726 return -EINVAL;
727 ubt = &ut->u.sequence.elem_type;
728 lbt = &lt->u.sequence.elem_type;
2b213b16 729 ret = serialize_basic_type(&ubt->atype, lbt->atype,
32ce8569
MD
730 &ubt->u.basic, &lbt->u.basic);
731 if (ret)
732 return -EINVAL;
2b213b16 733 ut->atype = ustctl_atype_sequence;
32ce8569
MD
734 break;
735 }
736 case atype_enum:
737 default:
738 return -EINVAL;
739 }
740 return 0;
741}
742
743static
744int serialize_fields(size_t *_nr_write_fields,
745 struct ustctl_field **ustctl_fields,
746 size_t nr_fields,
747 const struct lttng_event_field *lttng_fields)
748{
749 struct ustctl_field *fields;
750 int i, ret;
751 size_t nr_write_fields = 0;
752
753 fields = zmalloc(nr_fields * sizeof(*fields));
754 if (!fields)
755 return -ENOMEM;
756
757 for (i = 0; i < nr_fields; i++) {
758 struct ustctl_field *f;
759 const struct lttng_event_field *lf;
760
761 f = &fields[nr_write_fields];
762 lf = &lttng_fields[i];
763
764 /* skip 'nowrite' fields */
765 if (lf->nowrite)
766 continue;
767 strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
768 f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
769 ret = serialize_one_type(&f->type, &lf->type);
770 if (ret)
771 goto error_type;
772 nr_write_fields++;
773 }
774
775 *_nr_write_fields = nr_write_fields;
776 *ustctl_fields = fields;
777 return 0;
778
779error_type:
780 free(fields);
781 return ret;
782}
783
784/*
785 * Returns 0 on success, negative error value on error.
786 */
787int ustcomm_register_event(int sock,
788 int session_objd, /* session descriptor */
789 int channel_objd, /* channel descriptor */
790 const char *event_name, /* event name (input) */
791 int loglevel,
792 const char *signature, /* event signature (input) */
793 size_t nr_fields, /* fields */
794 const struct lttng_event_field *lttng_fields,
795 const char *model_emf_uri,
796 uint32_t *id) /* event id (output) */
797{
798 ssize_t len;
799 struct {
800 struct ustcomm_notify_hdr header;
801 struct ustcomm_notify_event_msg m;
802 } msg;
803 struct {
804 struct ustcomm_notify_hdr header;
805 struct ustcomm_notify_event_reply r;
806 } reply;
807 size_t signature_len, fields_len, model_emf_uri_len;
808 struct ustctl_field *fields;
809 size_t nr_write_fields = 0;
810 int ret;
811
812 memset(&msg, 0, sizeof(msg));
813 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
814 msg.m.session_objd = session_objd;
815 msg.m.channel_objd = channel_objd;
816 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
817 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
818 msg.m.loglevel = loglevel;
819 signature_len = strlen(signature) + 1;
820 msg.m.signature_len = signature_len;
821
822 /* Calculate fields len, serialize fields. */
823 if (nr_fields > 0) {
824 ret = serialize_fields(&nr_write_fields, &fields,
825 nr_fields, lttng_fields);
826 if (ret)
827 return ret;
828 }
829
830 fields_len = sizeof(*fields) * nr_write_fields;
831 msg.m.fields_len = fields_len;
832 if (model_emf_uri) {
833 model_emf_uri_len = strlen(model_emf_uri) + 1;
834 } else {
835 model_emf_uri_len = 0;
836 }
837 msg.m.model_emf_uri_len = model_emf_uri_len;
838 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
839 if (len > 0 && len != sizeof(msg)) {
840 free(fields);
841 return -EIO;
842 }
843 if (len < 0) {
844 free(fields);
845 return len;
846 }
847
848 /* send signature */
849 len = ustcomm_send_unix_sock(sock, signature, signature_len);
850 if (len > 0 && len != signature_len) {
851 free(fields);
852 return -EIO;
853 }
854 if (len < 0) {
855 free(fields);
856 return len;
857 }
858
859 /* send fields */
860 if (fields_len > 0) {
861 len = ustcomm_send_unix_sock(sock, fields, fields_len);
862 free(fields);
863 if (len > 0 && len != fields_len) {
864 return -EIO;
865 }
866 if (len < 0) {
867 return len;
868 }
869 }
870
871 if (model_emf_uri_len) {
872 /* send model_emf_uri */
873 len = ustcomm_send_unix_sock(sock, model_emf_uri,
874 model_emf_uri_len);
875 if (len > 0 && len != model_emf_uri_len)
876 return -EIO;
877 if (len < 0)
878 return len;
879 }
880
881 /* receive reply */
882 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
883 switch (len) {
884 case 0: /* orderly shutdown */
885 return -EPIPE;
886 case sizeof(reply):
887 if (reply.header.notify_cmd != msg.header.notify_cmd) {
888 ERR("Unexpected result message command "
889 "expected: %u vs received: %u\n",
890 msg.header.notify_cmd, reply.header.notify_cmd);
891 return -EINVAL;
892 }
893 if (reply.r.ret_code > 0)
894 return -EINVAL;
895 if (reply.r.ret_code < 0)
896 return reply.r.ret_code;
897 *id = reply.r.event_id;
898 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
899 event_name, reply.r.ret_code, reply.r.event_id);
900 return 0;
901 default:
902 if (len < 0) {
903 /* Transport level error */
904 if (errno == EPIPE || errno == ECONNRESET)
905 len = -errno;
906 return len;
907 } else {
908 ERR("incorrect message size: %zd\n", len);
909 return len;
910 }
911 }
912}
913
914/*
915 * Returns 0 on success, negative error value on error.
916 * Returns -EPIPE or -ECONNRESET if other end has hung up.
917 */
918int ustcomm_register_channel(int sock,
919 int session_objd, /* session descriptor */
920 int channel_objd, /* channel descriptor */
921 size_t nr_ctx_fields,
922 const struct lttng_event_field *ctx_fields,
923 uint32_t *chan_id, /* channel id (output) */
924 int *header_type) /* header type (output) */
925{
926 ssize_t len;
927 struct {
928 struct ustcomm_notify_hdr header;
929 struct ustcomm_notify_channel_msg m;
930 } msg;
931 struct {
932 struct ustcomm_notify_hdr header;
933 struct ustcomm_notify_channel_reply r;
934 } reply;
935 size_t fields_len;
936 struct ustctl_field *fields;
937 int ret;
938 size_t nr_write_fields = 0;
939
940 memset(&msg, 0, sizeof(msg));
941 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
942 msg.m.session_objd = session_objd;
943 msg.m.channel_objd = channel_objd;
944
945 /* Calculate fields len, serialize fields. */
946 if (nr_ctx_fields > 0) {
947 ret = serialize_fields(&nr_write_fields, &fields,
948 nr_ctx_fields, ctx_fields);
949 if (ret)
950 return ret;
951 }
952
953 fields_len = sizeof(*fields) * nr_write_fields;
954 msg.m.ctx_fields_len = fields_len;
955 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
956 if (len > 0 && len != sizeof(msg)) {
957 free(fields);
958 return -EIO;
959 }
960 if (len < 0) {
961 free(fields);
962 return len;
963 }
964
965 /* send fields */
966 if (fields_len > 0) {
967 len = ustcomm_send_unix_sock(sock, fields, fields_len);
968 free(fields);
969 if (len > 0 && len != fields_len) {
970 return -EIO;
971 }
972 if (len < 0) {
973 return len;
974 }
975 }
976
977 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
978 switch (len) {
979 case 0: /* orderly shutdown */
980 return -EPIPE;
981 case sizeof(reply):
982 if (reply.header.notify_cmd != msg.header.notify_cmd) {
983 ERR("Unexpected result message command "
984 "expected: %u vs received: %u\n",
985 msg.header.notify_cmd, reply.header.notify_cmd);
986 return -EINVAL;
987 }
988 if (reply.r.ret_code > 0)
989 return -EINVAL;
990 if (reply.r.ret_code < 0)
991 return reply.r.ret_code;
992 *chan_id = reply.r.chan_id;
993 switch (reply.r.header_type) {
994 case 1:
995 case 2:
996 *header_type = reply.r.header_type;
997 break;
998 default:
999 ERR("Unexpected channel header type %u\n",
1000 reply.r.header_type);
1001 return -EINVAL;
1002 }
1003 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1004 reply.r.chan_id, reply.r.header_type);
1005 return 0;
1006 default:
1007 if (len < 0) {
1008 /* Transport level error */
1009 if (errno == EPIPE || errno == ECONNRESET)
1010 len = -errno;
1011 return len;
1012 } else {
1013 ERR("incorrect message size: %zd\n", len);
1014 return len;
1015 }
1016 }
1017}
This page took 0.07128 seconds and 4 git commands to generate.