Rename lttng_ust_enum_get to lttng_ust_enum_get_from_desc
[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>
6548fca4 36#include <ust-fd.h>
74d81a6c 37#include <helper.h>
7bc53e94 38#include <lttng/ust-error.h>
32ce8569 39#include <lttng/ust-events.h>
53569322 40#include <lttng/ust-dynamic-type.h>
32ce8569
MD
41#include <usterr-signal-safe.h>
42
43#include "../liblttng-ust/compat.h"
7bc53e94
MD
44
45#define USTCOMM_CODE_OFFSET(code) \
46 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
67c5b804 47
74d81a6c
MD
48#define USTCOMM_MAX_SEND_FDS 4
49
53569322
MD
50static
51ssize_t count_fields_recursive(size_t nr_fields,
52 const struct lttng_event_field *lttng_fields);
53static
54int serialize_one_field(struct lttng_session *session,
55 struct ustctl_field *fields, size_t *iter_output,
56 const struct lttng_event_field *lf);
57
67c5b804
MD
58/*
59 * Human readable error message.
60 */
57773204 61static const char *ustcomm_readable_code[] = {
7bc53e94
MD
62 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK) ] = "Success",
63 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR) ] = "Unknown error",
64 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT) ] = "No entry",
64b2564e
DG
65 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST) ] = "Object already exists",
66 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL) ] = "Invalid argument",
67 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM) ] = "Permission denied",
68 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS) ] = "Not implemented",
74d81a6c 69 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING) ] = "Process is exiting",
32ce8569
MD
70
71 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number",
72 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type",
73 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version",
67c5b804
MD
74};
75
76/*
7bc53e94 77 * lttng_ust_strerror
67c5b804 78 *
7bc53e94
MD
79 * Receives positive error value.
80 * Return ptr to string representing a human readable
81 * error code from the ustcomm_return_code enum.
67c5b804 82 */
7bc53e94 83const char *lttng_ust_strerror(int code)
67c5b804 84{
7bc53e94
MD
85 if (code == LTTNG_UST_OK)
86 return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
87 if (code < LTTNG_UST_ERR)
88 return strerror(code);
89 if (code >= LTTNG_UST_ERR_NR)
90 code = LTTNG_UST_ERR;
91 return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
67c5b804
MD
92}
93
94/*
74d81a6c 95 * ustcomm_connect_unix_sock
67c5b804 96 *
74d81a6c 97 * Connect to unix socket using the path name.
6548fca4
MD
98 *
99 * Caller handles FD tracker.
67c5b804 100 */
451d66b2 101int ustcomm_connect_unix_sock(const char *pathname, long timeout)
67c5b804
MD
102{
103 struct sockaddr_un sun;
7bc53e94 104 int fd, ret;
67c5b804 105
204d45df
MD
106 /*
107 * libust threads require the close-on-exec flag for all
108 * resources so it does not leak file descriptors upon exec.
109 */
11ba4bcb 110 fd = socket(PF_UNIX, SOCK_STREAM, 0);
67c5b804 111 if (fd < 0) {
32ce8569 112 PERROR("socket");
7bc53e94 113 ret = -errno;
67c5b804
MD
114 goto error;
115 }
451d66b2
MD
116 if (timeout >= 0) {
117 /* Give at least 10ms. */
118 if (timeout < 10)
119 timeout = 10;
120 ret = ustcomm_setsockopt_snd_timeout(fd, timeout);
121 if (ret < 0) {
122 WARN("Error setting connect socket send timeout");
123 }
124 }
11ba4bcb
MD
125 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
126 if (ret < 0) {
32ce8569 127 PERROR("fcntl");
7bc53e94 128 ret = -errno;
11ba4bcb
MD
129 goto error_fcntl;
130 }
67c5b804
MD
131
132 memset(&sun, 0, sizeof(sun));
133 sun.sun_family = AF_UNIX;
134 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
135 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
136
137 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
138 if (ret < 0) {
139 /*
0b9aa170
MD
140 * Don't print message on connect ENOENT error, because
141 * connect is used in normal execution to detect if
142 * sessiond is alive. ENOENT is when the unix socket
143 * file does not exist, and ECONNREFUSED is when the
144 * file exists but no sessiond is listening.
67c5b804 145 */
0b9aa170 146 if (errno != ECONNREFUSED && errno != ECONNRESET
bdd8ca83 147 && errno != ENOENT && errno != EACCES)
8cf811d3 148 PERROR("connect");
7bc53e94 149 ret = -errno;
8cf811d3
MD
150 if (ret == -ECONNREFUSED || ret == -ECONNRESET)
151 ret = -EPIPE;
67c5b804
MD
152 goto error_connect;
153 }
154
155 return fd;
156
157error_connect:
11ba4bcb 158error_fcntl:
7bc53e94
MD
159 {
160 int closeret;
161
162 closeret = close(fd);
163 if (closeret)
32ce8569 164 PERROR("close");
7bc53e94 165 }
67c5b804
MD
166error:
167 return ret;
168}
169
170/*
74d81a6c 171 * ustcomm_accept_unix_sock
67c5b804 172 *
74d81a6c
MD
173 * Do an accept(2) on the sock and return the
174 * new file descriptor. The socket MUST be bind(2) before.
67c5b804 175 */
57773204 176int ustcomm_accept_unix_sock(int sock)
67c5b804
MD
177{
178 int new_fd;
179 struct sockaddr_un sun;
180 socklen_t len = 0;
181
182 /* Blocking call */
183 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
184 if (new_fd < 0) {
b869b5ae
MD
185 if (errno != ECONNABORTED)
186 PERROR("accept");
187 new_fd = -errno;
188 if (new_fd == -ECONNABORTED)
189 new_fd = -EPIPE;
67c5b804 190 }
67c5b804 191 return new_fd;
67c5b804
MD
192}
193
194/*
74d81a6c 195 * ustcomm_create_unix_sock
67c5b804 196 *
74d81a6c
MD
197 * Creates a AF_UNIX local socket using pathname
198 * bind the socket upon creation and return the fd.
67c5b804 199 */
57773204 200int ustcomm_create_unix_sock(const char *pathname)
67c5b804
MD
201{
202 struct sockaddr_un sun;
7bc53e94 203 int fd, ret;
67c5b804
MD
204
205 /* Create server socket */
206 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
32ce8569 207 PERROR("socket");
7bc53e94 208 ret = -errno;
67c5b804
MD
209 goto error;
210 }
211
212 memset(&sun, 0, sizeof(sun));
213 sun.sun_family = AF_UNIX;
214 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
215 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
216
217 /* Unlink the old file if present */
218 (void) unlink(pathname);
219 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
220 if (ret < 0) {
32ce8569 221 PERROR("bind");
7bc53e94
MD
222 ret = -errno;
223 goto error_close;
67c5b804
MD
224 }
225
226 return fd;
227
7bc53e94
MD
228error_close:
229 {
230 int closeret;
231
232 closeret = close(fd);
233 if (closeret) {
32ce8569 234 PERROR("close");
7bc53e94
MD
235 }
236 }
67c5b804
MD
237error:
238 return ret;
239}
240
241/*
74d81a6c 242 * ustcomm_listen_unix_sock
67c5b804 243 *
74d81a6c 244 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
67c5b804 245 */
57773204 246int ustcomm_listen_unix_sock(int sock)
67c5b804
MD
247{
248 int ret;
249
e41474be 250 ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN);
67c5b804 251 if (ret < 0) {
7bc53e94 252 ret = -errno;
32ce8569 253 PERROR("listen");
67c5b804
MD
254 }
255
256 return ret;
257}
258
259/*
74d81a6c
MD
260 * ustcomm_close_unix_sock
261 *
262 * Shutdown cleanly a unix socket.
6548fca4
MD
263 *
264 * Handles fd tracker internally.
74d81a6c
MD
265 */
266int ustcomm_close_unix_sock(int sock)
267{
268 int ret;
269
6548fca4 270 lttng_ust_lock_fd_tracker();
74d81a6c 271 ret = close(sock);
6548fca4
MD
272 if (!ret) {
273 lttng_ust_delete_fd_from_tracker(sock);
274 } else {
32ce8569 275 PERROR("close");
74d81a6c
MD
276 ret = -errno;
277 }
6548fca4 278 lttng_ust_unlock_fd_tracker();
74d81a6c
MD
279
280 return ret;
281}
282
283/*
284 * ustcomm_recv_unix_sock
67c5b804 285 *
74d81a6c
MD
286 * Receive data of size len in put that data into
287 * the buf param. Using recvmsg API.
288 * Return the size of received data.
289 * Return 0 on orderly shutdown.
67c5b804 290 */
57773204 291ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
67c5b804 292{
913b87f1 293 struct msghdr msg;
67c5b804 294 struct iovec iov[1];
89c5b6ec
MD
295 ssize_t ret = -1;
296 size_t len_last;
67c5b804 297
913b87f1
MD
298 memset(&msg, 0, sizeof(msg));
299
67c5b804
MD
300 iov[0].iov_base = buf;
301 iov[0].iov_len = len;
302 msg.msg_iov = iov;
303 msg.msg_iovlen = 1;
304
7e3cfcbe 305 do {
89c5b6ec 306 len_last = iov[0].iov_len;
7e3cfcbe 307 ret = recvmsg(sock, &msg, 0);
89c5b6ec
MD
308 if (ret > 0) {
309 iov[0].iov_base += ret;
310 iov[0].iov_len -= ret;
311 assert(ret <= len_last);
312 }
313 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
7bc53e94
MD
314
315 if (ret < 0) {
316 int shutret;
317
8cf811d3 318 if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED)
32ce8569 319 PERROR("recvmsg");
7bc53e94 320 ret = -errno;
8cf811d3 321 if (ret == -ECONNRESET || ret == -ECONNREFUSED)
b869b5ae 322 ret = -EPIPE;
7bc53e94
MD
323
324 shutret = shutdown(sock, SHUT_RDWR);
325 if (shutret)
32ce8569 326 ERR("Socket shutdown error");
89c5b6ec
MD
327 } else if (ret > 0) {
328 ret = len;
67c5b804 329 }
89c5b6ec 330 /* ret = 0 means an orderly shutdown. */
67c5b804
MD
331
332 return ret;
333}
334
335/*
74d81a6c 336 * ustcomm_send_unix_sock
67c5b804 337 *
74d81a6c
MD
338 * Send buf data of size len. Using sendmsg API.
339 * Return the size of sent data.
67c5b804 340 */
32ce8569 341ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len)
67c5b804 342{
913b87f1 343 struct msghdr msg;
67c5b804 344 struct iovec iov[1];
7bc53e94 345 ssize_t ret;
67c5b804 346
913b87f1
MD
347 memset(&msg, 0, sizeof(msg));
348
32ce8569 349 iov[0].iov_base = (void *) buf;
67c5b804
MD
350 iov[0].iov_len = len;
351 msg.msg_iov = iov;
352 msg.msg_iovlen = 1;
353
1ea11eab
MD
354 /*
355 * Using the MSG_NOSIGNAL when sending data from sessiond to
356 * libust, so libust does not receive an unhandled SIGPIPE or
357 * SIGURG. The sessiond receiver side can be made more resilient
358 * by ignoring SIGPIPE, but we don't have this luxury on the
359 * libust side.
360 */
51d9d699
MD
361 do {
362 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
363 } while (ret < 0 && errno == EINTR);
7bc53e94
MD
364
365 if (ret < 0) {
366 int shutret;
367
74d81a6c 368 if (errno != EPIPE && errno != ECONNRESET)
32ce8569 369 PERROR("sendmsg");
7bc53e94 370 ret = -errno;
b869b5ae
MD
371 if (ret == -ECONNRESET)
372 ret = -EPIPE;
7bc53e94
MD
373
374 shutret = shutdown(sock, SHUT_RDWR);
375 if (shutret)
32ce8569 376 ERR("Socket shutdown error");
67c5b804
MD
377 }
378
379 return ret;
380}
381
382/*
74d81a6c 383 * Send a message accompanied by fd(s) over a unix socket.
67c5b804 384 *
74d81a6c 385 * Returns the size of data sent, or negative error value.
67c5b804 386 */
74d81a6c 387ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
67c5b804 388{
913b87f1 389 struct msghdr msg;
67c5b804
MD
390 struct cmsghdr *cmptr;
391 struct iovec iov[1];
392 ssize_t ret = -1;
393 unsigned int sizeof_fds = nb_fd * sizeof(int);
394 char tmp[CMSG_SPACE(sizeof_fds)];
74d81a6c 395 char dummy = 0;
67c5b804 396
913b87f1 397 memset(&msg, 0, sizeof(msg));
74d81a6c 398 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
913b87f1 399
74d81a6c
MD
400 if (nb_fd > USTCOMM_MAX_SEND_FDS)
401 return -EINVAL;
67c5b804
MD
402
403 msg.msg_control = (caddr_t)tmp;
404 msg.msg_controllen = CMSG_LEN(sizeof_fds);
405
406 cmptr = CMSG_FIRSTHDR(&msg);
34daae3e
MD
407 if (!cmptr)
408 return -EINVAL;
67c5b804
MD
409 cmptr->cmsg_level = SOL_SOCKET;
410 cmptr->cmsg_type = SCM_RIGHTS;
411 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
412 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
413 /* Sum of the length of all control messages in the buffer: */
414 msg.msg_controllen = cmptr->cmsg_len;
415
74d81a6c
MD
416 iov[0].iov_base = &dummy;
417 iov[0].iov_len = 1;
67c5b804
MD
418 msg.msg_iov = iov;
419 msg.msg_iovlen = 1;
420
51d9d699 421 do {
0dafcd63 422 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
51d9d699 423 } while (ret < 0 && errno == EINTR);
7bc53e94 424 if (ret < 0) {
74d81a6c
MD
425 /*
426 * We consider EPIPE and ECONNRESET as expected.
427 */
428 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 429 PERROR("sendmsg");
74d81a6c 430 }
b869b5ae
MD
431 ret = -errno;
432 if (ret == -ECONNRESET)
433 ret = -EPIPE;
74d81a6c
MD
434 }
435 return ret;
436}
7bc53e94 437
74d81a6c
MD
438/*
439 * Recv a message accompanied by fd(s) from a unix socket.
440 *
441 * Returns the size of received data, or negative error value.
442 *
443 * Expect at most "nb_fd" file descriptors. Returns the number of fd
444 * actually received in nb_fd.
445 * Returns -EPIPE on orderly shutdown.
446 */
447ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
448{
449 struct iovec iov[1];
450 ssize_t ret = 0;
451 struct cmsghdr *cmsg;
452 size_t sizeof_fds = nb_fd * sizeof(int);
453 char recv_fd[CMSG_SPACE(sizeof_fds)];
454 struct msghdr msg;
455 char dummy;
7bc53e94 456
74d81a6c 457 memset(&msg, 0, sizeof(msg));
67c5b804 458
74d81a6c
MD
459 /* Prepare to receive the structures */
460 iov[0].iov_base = &dummy;
461 iov[0].iov_len = 1;
462 msg.msg_iov = iov;
463 msg.msg_iovlen = 1;
464 msg.msg_control = recv_fd;
465 msg.msg_controllen = sizeof(recv_fd);
466
467 do {
468 ret = recvmsg(sock, &msg, 0);
469 } while (ret < 0 && errno == EINTR);
470 if (ret < 0) {
471 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 472 PERROR("recvmsg fds");
74d81a6c 473 }
8cf811d3 474 ret = -errno;
b869b5ae
MD
475 if (ret == -ECONNRESET)
476 ret = -EPIPE;
74d81a6c
MD
477 goto end;
478 }
479 if (ret == 0) {
480 /* orderly shutdown */
481 ret = -EPIPE;
482 goto end;
483 }
484 if (ret != 1) {
32ce8569 485 ERR("Error: Received %zd bytes, expected %d\n",
74d81a6c
MD
486 ret, 1);
487 goto end;
488 }
489 if (msg.msg_flags & MSG_CTRUNC) {
32ce8569 490 ERR("Error: Control message truncated.\n");
74d81a6c
MD
491 ret = -1;
492 goto end;
493 }
494 cmsg = CMSG_FIRSTHDR(&msg);
495 if (!cmsg) {
32ce8569 496 ERR("Error: Invalid control message header\n");
74d81a6c
MD
497 ret = -1;
498 goto end;
499 }
500 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
32ce8569 501 ERR("Didn't received any fd\n");
74d81a6c
MD
502 ret = -1;
503 goto end;
504 }
505 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
32ce8569 506 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
74d81a6c
MD
507 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
508 ret = -1;
509 goto end;
510 }
511 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
512 ret = sizeof_fds;
513end:
67c5b804
MD
514 return ret;
515}
57773204
MD
516
517int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
518{
519 ssize_t len;
520
521 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
522 switch (len) {
523 case sizeof(*lum):
57773204 524 break;
57773204 525 default:
7bc53e94 526 if (len < 0) {
7bc53e94
MD
527 return len;
528 } else {
32ce8569 529 ERR("incorrect message size: %zd\n", len);
7bc53e94
MD
530 return -EINVAL;
531 }
57773204
MD
532 }
533 return 0;
534}
535
536int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
537 uint32_t expected_handle, uint32_t expected_cmd)
538{
539 ssize_t len;
540
541 memset(lur, 0, sizeof(*lur));
542 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
543 switch (len) {
544 case 0: /* orderly shutdown */
74d81a6c 545 return -EPIPE;
57773204 546 case sizeof(*lur):
5dafeeaa
MD
547 {
548 int err = 0;
549
57773204 550 if (lur->handle != expected_handle) {
32ce8569 551 ERR("Unexpected result message handle: "
74d81a6c
MD
552 "expected: %u vs received: %u\n",
553 expected_handle, lur->handle);
5dafeeaa 554 err = 1;
57773204 555 }
57773204 556 if (lur->cmd != expected_cmd) {
32ce8569 557 ERR("Unexpected result message command "
74d81a6c
MD
558 "expected: %u vs received: %u\n",
559 expected_cmd, lur->cmd);
5dafeeaa
MD
560 err = 1;
561 }
562 if (err) {
57773204 563 return -EINVAL;
5dafeeaa
MD
564 } else {
565 return lur->ret_code;
57773204 566 }
5dafeeaa 567 }
57773204 568 default:
8cf811d3 569 if (len >= 0) {
32ce8569 570 ERR("incorrect message size: %zd\n", len);
7bc53e94 571 }
8cf811d3 572 return len;
57773204
MD
573 }
574}
575
576int ustcomm_send_app_cmd(int sock,
577 struct ustcomm_ust_msg *lum,
578 struct ustcomm_ust_reply *lur)
579{
580 int ret;
581
582 ret = ustcomm_send_app_msg(sock, lum);
583 if (ret)
584 return ret;
c354a72c
MD
585 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
586 if (ret > 0)
587 return -EIO;
588 return ret;
57773204
MD
589}
590
57773204 591/*
74d81a6c
MD
592 * chan_data is allocated internally if this function returns the
593 * expected var_len.
57773204 594 */
74d81a6c 595ssize_t ustcomm_recv_channel_from_sessiond(int sock,
ff0f5728
MD
596 void **_chan_data, uint64_t var_len,
597 int *_wakeup_fd)
57773204 598{
74d81a6c 599 void *chan_data;
ff0f5728 600 ssize_t len, nr_fd;
f5c453e9 601 int wakeup_fd, ret;
57773204 602
74d81a6c
MD
603 if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) {
604 len = -EINVAL;
605 goto error_check;
57773204 606 }
74d81a6c
MD
607 /* Receive variable length data */
608 chan_data = zmalloc(var_len);
609 if (!chan_data) {
610 len = -ENOMEM;
611 goto error_alloc;
57773204 612 }
74d81a6c
MD
613 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
614 if (len != var_len) {
615 goto error_recv;
57773204 616 }
ff0f5728 617 /* recv wakeup fd */
6548fca4 618 lttng_ust_lock_fd_tracker();
ff0f5728
MD
619 nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
620 if (nr_fd <= 0) {
6548fca4 621 lttng_ust_unlock_fd_tracker();
ff0f5728
MD
622 if (nr_fd < 0) {
623 len = nr_fd;
624 goto error_recv;
625 } else {
626 len = -EIO;
627 goto error_recv;
628 }
629 }
f5c453e9
JR
630
631 ret = lttng_ust_add_fd_to_tracker(wakeup_fd);
632 if (ret < 0) {
633 lttng_ust_unlock_fd_tracker();
634 ret = close(wakeup_fd);
635 if (ret) {
636 PERROR("close on wakeup_fd");
637 }
638 len = -EIO;
639 goto error_recv;
640 }
641
642 *_wakeup_fd = ret;
6548fca4 643 lttng_ust_unlock_fd_tracker();
f5c453e9 644
74d81a6c
MD
645 *_chan_data = chan_data;
646 return len;
647
648error_recv:
649 free(chan_data);
650error_alloc:
651error_check:
652 return len;
653}
7bc53e94 654
74d81a6c
MD
655int ustcomm_recv_stream_from_sessiond(int sock,
656 uint64_t *memory_map_size,
657 int *shm_fd, int *wakeup_fd)
658{
659 ssize_t len;
660 int ret;
661 int fds[2];
662
663 /* recv shm fd and wakeup fd */
6548fca4 664 lttng_ust_lock_fd_tracker();
74d81a6c
MD
665 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
666 if (len <= 0) {
6548fca4 667 lttng_ust_unlock_fd_tracker();
74d81a6c
MD
668 if (len < 0) {
669 ret = len;
670 goto error;
671 } else {
672 ret = -EIO;
673 goto error;
674 }
7bc53e94 675 }
f5c453e9
JR
676
677 ret = lttng_ust_add_fd_to_tracker(fds[0]);
678 if (ret < 0) {
679 lttng_ust_unlock_fd_tracker();
680 ret = close(fds[0]);
681 if (ret) {
682 PERROR("close on received shm_fd");
683 }
684 ret = -EIO;
685 goto error;
686 }
687 *shm_fd = ret;
688
689 ret = lttng_ust_add_fd_to_tracker(fds[1]);
690 if (ret < 0) {
691 lttng_ust_unlock_fd_tracker();
692 ret = close(*shm_fd);
693 if (ret) {
694 PERROR("close on shm_fd");
695 }
696 *shm_fd = -1;
697 ret = close(fds[1]);
698 if (ret) {
699 PERROR("close on received wakeup_fd");
700 }
701 ret = -EIO;
702 goto error;
703 }
704 *wakeup_fd = ret;
6548fca4 705 lttng_ust_unlock_fd_tracker();
74d81a6c
MD
706 return 0;
707
708error:
57773204
MD
709 return ret;
710}
32ce8569
MD
711
712/*
713 * Returns 0 on success, negative error value on error.
714 */
715int ustcomm_send_reg_msg(int sock,
716 enum ustctl_socket_type type,
717 uint32_t bits_per_long,
718 uint32_t uint8_t_alignment,
719 uint32_t uint16_t_alignment,
720 uint32_t uint32_t_alignment,
721 uint32_t uint64_t_alignment,
722 uint32_t long_alignment)
723{
724 ssize_t len;
725 struct ustctl_reg_msg reg_msg;
726
727 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
728 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
729 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
730 reg_msg.pid = getpid();
731 reg_msg.ppid = getppid();
732 reg_msg.uid = getuid();
733 reg_msg.gid = getgid();
734 reg_msg.bits_per_long = bits_per_long;
735 reg_msg.uint8_t_alignment = uint8_t_alignment;
736 reg_msg.uint16_t_alignment = uint16_t_alignment;
737 reg_msg.uint32_t_alignment = uint32_t_alignment;
738 reg_msg.uint64_t_alignment = uint64_t_alignment;
739 reg_msg.long_alignment = long_alignment;
740 reg_msg.socket_type = type;
741 lttng_ust_getprocname(reg_msg.name);
742 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
743
744 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
745 if (len > 0 && len != sizeof(reg_msg))
746 return -EIO;
747 if (len < 0)
748 return len;
749 return 0;
750}
751
53569322
MD
752static
753ssize_t count_one_type(const struct lttng_type *lt)
754{
755 switch (lt->atype) {
756 case atype_integer:
757 case atype_float:
758 case atype_string:
759 case atype_enum:
760 case atype_array:
761 case atype_sequence:
762 return 1;
763 case atype_struct:
764 //TODO: implement non-empty struct.
765 return 1;
766 case atype_dynamic:
767 {
768 const struct lttng_event_field *choices;
769 size_t nr_choices;
770 int ret;
771
772 ret = lttng_ust_dynamic_type_choices(&nr_choices,
773 &choices);
774 if (ret)
775 return ret;
776 /*
777 * One field for enum, one field for variant, and
778 * one field per choice.
779 */
780 return count_fields_recursive(nr_choices, choices) + 2;
781 }
782 default:
783 return -EINVAL;
784 }
785 return 0;
786}
787
788static
789ssize_t count_fields_recursive(size_t nr_fields,
790 const struct lttng_event_field *lttng_fields)
791{
792 int i;
793 ssize_t ret, count = 0;
794
795 for (i = 0; i < nr_fields; i++) {
796 const struct lttng_event_field *lf;
797
798 lf = &lttng_fields[i];
799 /* skip 'nowrite' fields */
800 if (lf->nowrite)
801 continue;
802 ret = count_one_type(&lf->type);
803 if (ret < 0)
804 return ret; /* error */
805 count += ret;
806 }
807 return count;
808}
809
810static
811ssize_t count_ctx_fields_recursive(size_t nr_fields,
812 const struct lttng_ctx_field *lttng_fields)
813{
814 int i;
815 ssize_t ret, count = 0;
816
817 for (i = 0; i < nr_fields; i++) {
818 const struct lttng_event_field *lf;
819
820 lf = &lttng_fields[i].event_field;
821 /* skip 'nowrite' fields */
822 if (lf->nowrite)
823 continue;
824 ret = count_one_type(&lf->type);
825 if (ret < 0)
826 return ret; /* error */
827 count += ret;
828 }
829 return count;
830}
831
7f2348b8 832static
735ea6a8 833int serialize_string_encoding(int32_t *ue,
7f2348b8
MD
834 enum lttng_string_encodings le)
835{
836 switch (le) {
837 case lttng_encode_none:
838 *ue = ustctl_encode_none;
839 break;
840 case lttng_encode_UTF8:
841 *ue = ustctl_encode_UTF8;
842 break;
843 case lttng_encode_ASCII:
844 *ue = ustctl_encode_ASCII;
845 break;
846 default:
847 return -EINVAL;
848 }
849 return 0;
850}
851
32ce8569 852static
c785c634
MD
853int serialize_integer_type(struct ustctl_integer_type *uit,
854 const struct lttng_integer_type *lit)
855{
856 uit->size = lit->size;
857 uit->signedness = lit->signedness;
858 uit->reverse_byte_order = lit->reverse_byte_order;
859 uit->base = lit->base;
860 if (serialize_string_encoding(&uit->encoding, lit->encoding))
861 return -EINVAL;
862 uit->alignment = lit->alignment;
863 return 0;
864}
865
866static
867int serialize_basic_type(struct lttng_session *session,
868 enum ustctl_abstract_types *uatype,
2b213b16 869 enum lttng_abstract_types atype,
32ce8569
MD
870 union _ustctl_basic_type *ubt,
871 const union _lttng_basic_type *lbt)
872{
873 switch (atype) {
874 case atype_integer:
875 {
c785c634 876 if (serialize_integer_type(&ubt->integer, &lbt->integer))
7f2348b8 877 return -EINVAL;
2b213b16 878 *uatype = ustctl_atype_integer;
32ce8569
MD
879 break;
880 }
881 case atype_string:
882 {
7f2348b8
MD
883 if (serialize_string_encoding(&ubt->string.encoding,
884 lbt->string.encoding))
885 return -EINVAL;
2b213b16 886 *uatype = ustctl_atype_string;
32ce8569
MD
887 break;
888 }
889 case atype_float:
890 {
891 struct ustctl_float_type *uft;
892 const struct lttng_float_type *lft;
893
894 uft = &ubt->_float;
895 lft = &lbt->_float;
896 uft->exp_dig = lft->exp_dig;
897 uft->mant_dig = lft->mant_dig;
898 uft->alignment = lft->alignment;
899 uft->reverse_byte_order = lft->reverse_byte_order;
2b213b16 900 *uatype = ustctl_atype_float;
32ce8569
MD
901 break;
902 }
903 case atype_enum:
c785c634
MD
904 {
905 strncpy(ubt->enumeration.name, lbt->enumeration.desc->name,
906 LTTNG_UST_SYM_NAME_LEN);
907 ubt->enumeration.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
908 if (serialize_integer_type(&ubt->enumeration.container_type,
909 &lbt->enumeration.container_type))
910 return -EINVAL;
911 if (session) {
912 const struct lttng_enum *_enum;
913
b33b46f7 914 _enum = lttng_ust_enum_get_from_desc(session, lbt->enumeration.desc);
c785c634
MD
915 if (!_enum)
916 return -EINVAL;
917 ubt->enumeration.id = _enum->id;
918 } else {
919 ubt->enumeration.id = -1ULL;
920 }
921 *uatype = ustctl_atype_enum;
922 break;
923 }
32ce8569
MD
924 case atype_array:
925 case atype_sequence:
926 default:
927 return -EINVAL;
928 }
929 return 0;
32ce8569
MD
930}
931
932static
53569322
MD
933int serialize_dynamic_type(struct lttng_session *session,
934 struct ustctl_field *fields, size_t *iter_output,
935 const struct lttng_event_field *lf)
32ce8569 936{
53569322
MD
937 const struct lttng_event_field *choices;
938 char tag_field_name[LTTNG_UST_SYM_NAME_LEN];
939 const struct lttng_type *tag_type;
940 const struct lttng_event_field *tag_field_generic;
941 struct lttng_event_field tag_field = {
942 .name = tag_field_name,
943 .nowrite = 0,
944 };
945 struct ustctl_field *uf;
946 size_t nr_choices, i;
32ce8569
MD
947 int ret;
948
53569322
MD
949 tag_field_generic = lttng_ust_dynamic_type_tag_field();
950 tag_type = &tag_field_generic->type;
951
952 /* Serialize enum field. */
953 strncpy(tag_field_name, lf->name, LTTNG_UST_SYM_NAME_LEN);
954 tag_field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
955 strncat(tag_field_name,
956 "_tag",
957 LTTNG_UST_SYM_NAME_LEN - strlen(tag_field_name) - 1);
958 tag_field.type = *tag_type;
959 ret = serialize_one_field(session, fields, iter_output,
960 &tag_field);
961 if (ret)
962 return ret;
963
964 /* Serialize variant field. */
965 uf = &fields[*iter_output];
966 ret = lttng_ust_dynamic_type_choices(&nr_choices, &choices);
967 if (ret)
968 return ret;
969
970 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
971 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
972 uf->type.atype = ustctl_atype_variant;
973 uf->type.u.variant.nr_choices = nr_choices;
974 strncpy(uf->type.u.variant.tag_name,
975 tag_field_name,
976 LTTNG_UST_SYM_NAME_LEN);
977 uf->type.u.variant.tag_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
978 (*iter_output)++;
979
980 /* Serialize choice fields after variant. */
981 for (i = 0; i < nr_choices; i++) {
982 ret = serialize_one_field(session, fields,
983 iter_output, &choices[i]);
984 if (ret)
985 return ret;
986 }
987 return 0;
988}
989
990static
991int serialize_one_field(struct lttng_session *session,
992 struct ustctl_field *fields, size_t *iter_output,
993 const struct lttng_event_field *lf)
994{
995 const struct lttng_type *lt = &lf->type;
996 int ret;
997
998 /* skip 'nowrite' fields */
999 if (lf->nowrite)
1000 return 0;
1001
32ce8569
MD
1002 switch (lt->atype) {
1003 case atype_integer:
1004 case atype_float:
1005 case atype_string:
c785c634 1006 case atype_enum:
53569322
MD
1007 {
1008 struct ustctl_field *uf = &fields[*iter_output];
1009 struct ustctl_type *ut = &uf->type;
1010
1011 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1012 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
c785c634 1013 ret = serialize_basic_type(session, &ut->atype, lt->atype,
32ce8569
MD
1014 &ut->u.basic, &lt->u.basic);
1015 if (ret)
1016 return ret;
53569322 1017 (*iter_output)++;
32ce8569 1018 break;
53569322 1019 }
32ce8569
MD
1020 case atype_array:
1021 {
53569322
MD
1022 struct ustctl_field *uf = &fields[*iter_output];
1023 struct ustctl_type *ut = &uf->type;
32ce8569
MD
1024 struct ustctl_basic_type *ubt;
1025 const struct lttng_basic_type *lbt;
32ce8569 1026
53569322
MD
1027 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1028 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1029 uf->type.atype = ustctl_atype_array;
32ce8569
MD
1030 ubt = &ut->u.array.elem_type;
1031 lbt = &lt->u.array.elem_type;
1032 ut->u.array.length = lt->u.array.length;
c785c634 1033 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
32ce8569
MD
1034 &ubt->u.basic, &lbt->u.basic);
1035 if (ret)
1036 return -EINVAL;
2b213b16 1037 ut->atype = ustctl_atype_array;
53569322 1038 (*iter_output)++;
32ce8569
MD
1039 break;
1040 }
1041 case atype_sequence:
1042 {
53569322
MD
1043 struct ustctl_field *uf = &fields[*iter_output];
1044 struct ustctl_type *ut = &uf->type;
32ce8569
MD
1045 struct ustctl_basic_type *ubt;
1046 const struct lttng_basic_type *lbt;
1047 int ret;
1048
53569322
MD
1049 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1050 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1051 uf->type.atype = ustctl_atype_sequence;
32ce8569
MD
1052 ubt = &ut->u.sequence.length_type;
1053 lbt = &lt->u.sequence.length_type;
c785c634 1054 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
32ce8569
MD
1055 &ubt->u.basic, &lbt->u.basic);
1056 if (ret)
1057 return -EINVAL;
1058 ubt = &ut->u.sequence.elem_type;
1059 lbt = &lt->u.sequence.elem_type;
c785c634 1060 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
32ce8569
MD
1061 &ubt->u.basic, &lbt->u.basic);
1062 if (ret)
1063 return -EINVAL;
2b213b16 1064 ut->atype = ustctl_atype_sequence;
53569322
MD
1065 (*iter_output)++;
1066 break;
1067 }
1068 case atype_dynamic:
1069 {
1070 ret = serialize_dynamic_type(session, fields, iter_output, lf);
1071 if (ret)
1072 return -EINVAL;
1073 break;
1074 }
1075 case atype_struct:
1076 {
1077 struct ustctl_field *uf = &fields[*iter_output];
1078
1079 /*
1080 * TODO: add support for non-empty struct.
1081 */
1082 if (lf->type.u._struct.nr_fields != 0) {
1083 return -EINVAL;
1084 }
1085 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1086 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1087 uf->type.atype = ustctl_atype_struct;
1088 uf->type.u._struct.nr_fields = 0;
1089 (*iter_output)++;
32ce8569
MD
1090 break;
1091 }
32ce8569
MD
1092 default:
1093 return -EINVAL;
1094 }
1095 return 0;
1096}
1097
1098static
c785c634
MD
1099int serialize_fields(struct lttng_session *session,
1100 size_t *_nr_write_fields,
32ce8569
MD
1101 struct ustctl_field **ustctl_fields,
1102 size_t nr_fields,
1103 const struct lttng_event_field *lttng_fields)
1104{
1105 struct ustctl_field *fields;
53569322
MD
1106 int ret;
1107 size_t i, iter_output = 0;
1108 ssize_t nr_write_fields;
1109
1110 nr_write_fields = count_fields_recursive(nr_fields, lttng_fields);
1111 if (nr_write_fields < 0) {
1112 return (int) nr_write_fields;
1113 }
32ce8569 1114
53569322 1115 fields = zmalloc(nr_write_fields * sizeof(*fields));
32ce8569
MD
1116 if (!fields)
1117 return -ENOMEM;
1118
1119 for (i = 0; i < nr_fields; i++) {
53569322
MD
1120 ret = serialize_one_field(session, fields, &iter_output,
1121 &lttng_fields[i]);
32ce8569
MD
1122 if (ret)
1123 goto error_type;
32ce8569
MD
1124 }
1125
1126 *_nr_write_fields = nr_write_fields;
1127 *ustctl_fields = fields;
1128 return 0;
1129
1130error_type:
1131 free(fields);
1132 return ret;
1133}
1134
c785c634
MD
1135static
1136int serialize_entries(struct ustctl_enum_entry **_entries,
1137 size_t nr_entries,
1138 const struct lttng_enum_entry *lttng_entries)
1139{
1140 struct ustctl_enum_entry *entries;
1141 int i;
1142
1143 /* Serialize the entries */
1144 entries = zmalloc(nr_entries * sizeof(*entries));
1145 if (!entries)
1146 return -ENOMEM;
1147 for (i = 0; i < nr_entries; i++) {
1148 struct ustctl_enum_entry *uentry;
1149 const struct lttng_enum_entry *lentry;
1150
1151 uentry = &entries[i];
1152 lentry = &lttng_entries[i];
1153
a6f80644
MD
1154 uentry->start.value = lentry->start.value;
1155 uentry->start.signedness = lentry->start.signedness;
1156 uentry->end.value = lentry->end.value;
1157 uentry->end.signedness = lentry->end.signedness;
c785c634
MD
1158 strncpy(uentry->string, lentry->string, LTTNG_UST_SYM_NAME_LEN);
1159 uentry->string[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
3e762260
PP
1160
1161 if (lentry->u.extra.options & LTTNG_ENUM_ENTRY_OPTION_IS_AUTO) {
1162 uentry->u.extra.options |=
1163 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO;
1164 }
c785c634
MD
1165 }
1166 *_entries = entries;
1167 return 0;
1168}
1169
83e43212 1170static
53569322
MD
1171int serialize_ctx_fields(struct lttng_session *session,
1172 size_t *_nr_write_fields,
83e43212
MD
1173 struct ustctl_field **ustctl_fields,
1174 size_t nr_fields,
1175 const struct lttng_ctx_field *lttng_fields)
1176{
1177 struct ustctl_field *fields;
53569322
MD
1178 int ret;
1179 size_t i, iter_output = 0;
1180 ssize_t nr_write_fields;
83e43212 1181
53569322
MD
1182 nr_write_fields = count_ctx_fields_recursive(nr_fields,
1183 lttng_fields);
1184 if (nr_write_fields < 0) {
1185 return (int) nr_write_fields;
1186 }
1187
1188 fields = zmalloc(nr_write_fields * sizeof(*fields));
83e43212
MD
1189 if (!fields)
1190 return -ENOMEM;
1191
1192 for (i = 0; i < nr_fields; i++) {
53569322
MD
1193 ret = serialize_one_field(session, fields, &iter_output,
1194 &lttng_fields[i].event_field);
83e43212
MD
1195 if (ret)
1196 goto error_type;
83e43212
MD
1197 }
1198
1199 *_nr_write_fields = nr_write_fields;
1200 *ustctl_fields = fields;
1201 return 0;
1202
1203error_type:
1204 free(fields);
1205 return ret;
1206}
1207
32ce8569
MD
1208/*
1209 * Returns 0 on success, negative error value on error.
1210 */
1211int ustcomm_register_event(int sock,
c785c634 1212 struct lttng_session *session,
32ce8569
MD
1213 int session_objd, /* session descriptor */
1214 int channel_objd, /* channel descriptor */
1215 const char *event_name, /* event name (input) */
1216 int loglevel,
1217 const char *signature, /* event signature (input) */
1218 size_t nr_fields, /* fields */
1219 const struct lttng_event_field *lttng_fields,
1220 const char *model_emf_uri,
1221 uint32_t *id) /* event id (output) */
1222{
1223 ssize_t len;
1224 struct {
1225 struct ustcomm_notify_hdr header;
1226 struct ustcomm_notify_event_msg m;
1227 } msg;
1228 struct {
1229 struct ustcomm_notify_hdr header;
1230 struct ustcomm_notify_event_reply r;
1231 } reply;
1232 size_t signature_len, fields_len, model_emf_uri_len;
3bf32af0 1233 struct ustctl_field *fields = NULL;
32ce8569
MD
1234 size_t nr_write_fields = 0;
1235 int ret;
1236
1237 memset(&msg, 0, sizeof(msg));
1238 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1239 msg.m.session_objd = session_objd;
1240 msg.m.channel_objd = channel_objd;
1241 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
1242 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1243 msg.m.loglevel = loglevel;
1244 signature_len = strlen(signature) + 1;
1245 msg.m.signature_len = signature_len;
1246
1247 /* Calculate fields len, serialize fields. */
1248 if (nr_fields > 0) {
c785c634 1249 ret = serialize_fields(session, &nr_write_fields, &fields,
32ce8569
MD
1250 nr_fields, lttng_fields);
1251 if (ret)
1252 return ret;
1253 }
1254
1255 fields_len = sizeof(*fields) * nr_write_fields;
1256 msg.m.fields_len = fields_len;
1257 if (model_emf_uri) {
1258 model_emf_uri_len = strlen(model_emf_uri) + 1;
1259 } else {
1260 model_emf_uri_len = 0;
1261 }
1262 msg.m.model_emf_uri_len = model_emf_uri_len;
c785c634 1263
32ce8569
MD
1264 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1265 if (len > 0 && len != sizeof(msg)) {
c785c634
MD
1266 ret = -EIO;
1267 goto error_fields;
32ce8569
MD
1268 }
1269 if (len < 0) {
c785c634
MD
1270 ret = len;
1271 goto error_fields;
32ce8569
MD
1272 }
1273
1274 /* send signature */
1275 len = ustcomm_send_unix_sock(sock, signature, signature_len);
1276 if (len > 0 && len != signature_len) {
6b95617c
MD
1277 ret = -EIO;
1278 goto error_fields;
32ce8569
MD
1279 }
1280 if (len < 0) {
6b95617c
MD
1281 ret = len;
1282 goto error_fields;
32ce8569
MD
1283 }
1284
1285 /* send fields */
1286 if (fields_len > 0) {
1287 len = ustcomm_send_unix_sock(sock, fields, fields_len);
32ce8569 1288 if (len > 0 && len != fields_len) {
c785c634
MD
1289 ret = -EIO;
1290 goto error_fields;
32ce8569
MD
1291 }
1292 if (len < 0) {
c785c634
MD
1293 ret = len;
1294 goto error_fields;
32ce8569
MD
1295 }
1296 }
6b95617c 1297 free(fields);
32ce8569
MD
1298
1299 if (model_emf_uri_len) {
1300 /* send model_emf_uri */
1301 len = ustcomm_send_unix_sock(sock, model_emf_uri,
1302 model_emf_uri_len);
c785c634 1303 if (len > 0 && len != model_emf_uri_len) {
6b95617c 1304 return -EIO;
c785c634
MD
1305 }
1306 if (len < 0) {
6b95617c 1307 return len;
c785c634 1308 }
32ce8569
MD
1309 }
1310
1311 /* receive reply */
1312 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1313 switch (len) {
1314 case 0: /* orderly shutdown */
1315 return -EPIPE;
1316 case sizeof(reply):
1317 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1318 ERR("Unexpected result message command "
1319 "expected: %u vs received: %u\n",
1320 msg.header.notify_cmd, reply.header.notify_cmd);
1321 return -EINVAL;
1322 }
1323 if (reply.r.ret_code > 0)
1324 return -EINVAL;
1325 if (reply.r.ret_code < 0)
1326 return reply.r.ret_code;
1327 *id = reply.r.event_id;
1328 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1329 event_name, reply.r.ret_code, reply.r.event_id);
1330 return 0;
1331 default:
1332 if (len < 0) {
1333 /* Transport level error */
1334 if (errno == EPIPE || errno == ECONNRESET)
1335 len = -errno;
1336 return len;
1337 } else {
1338 ERR("incorrect message size: %zd\n", len);
1339 return len;
1340 }
1341 }
6b95617c 1342 /* Unreached. */
c785c634 1343
6b95617c 1344 /* Error path only. */
c785c634
MD
1345error_fields:
1346 free(fields);
1347 return ret;
1348}
1349
1350/*
1351 * Returns 0 on success, negative error value on error.
1352 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1353 */
1354int ustcomm_register_enum(int sock,
1355 int session_objd, /* session descriptor */
1356 const char *enum_name, /* enum name (input) */
1357 size_t nr_entries, /* entries */
1358 const struct lttng_enum_entry *lttng_entries,
1359 uint64_t *id)
1360{
1361 ssize_t len;
1362 struct {
1363 struct ustcomm_notify_hdr header;
1364 struct ustcomm_notify_enum_msg m;
1365 } msg;
1366 struct {
1367 struct ustcomm_notify_hdr header;
1368 struct ustcomm_notify_enum_reply r;
1369 } reply;
1370 size_t entries_len;
1371 struct ustctl_enum_entry *entries = NULL;
1372 int ret;
1373
1374 memset(&msg, 0, sizeof(msg));
1375 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1376 msg.m.session_objd = session_objd;
1377 strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN);
1378 msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1379
1380 /* Calculate entries len, serialize entries. */
1381 if (nr_entries > 0) {
1382 ret = serialize_entries(&entries,
1383 nr_entries, lttng_entries);
1384 if (ret)
1385 return ret;
1386 }
1387
1388 entries_len = sizeof(*entries) * nr_entries;
1389 msg.m.entries_len = entries_len;
1390
1391 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1392 if (len > 0 && len != sizeof(msg)) {
1393 ret = -EIO;
1394 goto error_entries;
1395 }
1396 if (len < 0) {
1397 ret = len;
1398 goto error_entries;
1399 }
1400
1401 /* send entries */
1402 if (entries_len > 0) {
1403 len = ustcomm_send_unix_sock(sock, entries, entries_len);
1404 if (len > 0 && len != entries_len) {
1405 ret = -EIO;
1406 goto error_entries;
1407 }
1408 if (len < 0) {
1409 ret = len;
1410 goto error_entries;
1411 }
1412 }
1413 free(entries);
1414 entries = NULL;
1415
1416 /* receive reply */
1417 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1418 switch (len) {
1419 case 0: /* orderly shutdown */
1420 return -EPIPE;
1421 case sizeof(reply):
1422 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1423 ERR("Unexpected result message command "
1424 "expected: %u vs received: %u\n",
1425 msg.header.notify_cmd, reply.header.notify_cmd);
1426 return -EINVAL;
1427 }
1428 if (reply.r.ret_code > 0)
1429 return -EINVAL;
1430 if (reply.r.ret_code < 0)
1431 return reply.r.ret_code;
1432 *id = reply.r.enum_id;
1433 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1434 enum_name, reply.r.ret_code);
1435 return 0;
1436 default:
1437 if (len < 0) {
1438 /* Transport level error */
1439 if (errno == EPIPE || errno == ECONNRESET)
1440 len = -errno;
1441 return len;
1442 } else {
1443 ERR("incorrect message size: %zd\n", len);
1444 return len;
1445 }
1446 }
1447 return ret;
1448
1449error_entries:
1450 free(entries);
1451 return ret;
32ce8569
MD
1452}
1453
1454/*
1455 * Returns 0 on success, negative error value on error.
1456 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1457 */
1458int ustcomm_register_channel(int sock,
53569322 1459 struct lttng_session *session,
32ce8569
MD
1460 int session_objd, /* session descriptor */
1461 int channel_objd, /* channel descriptor */
1462 size_t nr_ctx_fields,
83e43212 1463 const struct lttng_ctx_field *ctx_fields,
32ce8569
MD
1464 uint32_t *chan_id, /* channel id (output) */
1465 int *header_type) /* header type (output) */
1466{
1467 ssize_t len;
1468 struct {
1469 struct ustcomm_notify_hdr header;
1470 struct ustcomm_notify_channel_msg m;
1471 } msg;
1472 struct {
1473 struct ustcomm_notify_hdr header;
1474 struct ustcomm_notify_channel_reply r;
1475 } reply;
1476 size_t fields_len;
83e43212 1477 struct ustctl_field *fields = NULL;
32ce8569
MD
1478 int ret;
1479 size_t nr_write_fields = 0;
1480
1481 memset(&msg, 0, sizeof(msg));
1482 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1483 msg.m.session_objd = session_objd;
1484 msg.m.channel_objd = channel_objd;
1485
1486 /* Calculate fields len, serialize fields. */
1487 if (nr_ctx_fields > 0) {
53569322 1488 ret = serialize_ctx_fields(session, &nr_write_fields, &fields,
32ce8569
MD
1489 nr_ctx_fields, ctx_fields);
1490 if (ret)
1491 return ret;
1492 }
1493
1494 fields_len = sizeof(*fields) * nr_write_fields;
1495 msg.m.ctx_fields_len = fields_len;
1496 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1497 if (len > 0 && len != sizeof(msg)) {
1498 free(fields);
1499 return -EIO;
1500 }
1501 if (len < 0) {
1502 free(fields);
1503 return len;
1504 }
1505
1506 /* send fields */
1507 if (fields_len > 0) {
1508 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1509 free(fields);
1510 if (len > 0 && len != fields_len) {
1511 return -EIO;
1512 }
1513 if (len < 0) {
1514 return len;
1515 }
17ea789c
MD
1516 } else {
1517 free(fields);
32ce8569
MD
1518 }
1519
1520 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1521 switch (len) {
1522 case 0: /* orderly shutdown */
1523 return -EPIPE;
1524 case sizeof(reply):
1525 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1526 ERR("Unexpected result message command "
1527 "expected: %u vs received: %u\n",
1528 msg.header.notify_cmd, reply.header.notify_cmd);
1529 return -EINVAL;
1530 }
1531 if (reply.r.ret_code > 0)
1532 return -EINVAL;
1533 if (reply.r.ret_code < 0)
1534 return reply.r.ret_code;
1535 *chan_id = reply.r.chan_id;
1536 switch (reply.r.header_type) {
1537 case 1:
1538 case 2:
1539 *header_type = reply.r.header_type;
1540 break;
1541 default:
1542 ERR("Unexpected channel header type %u\n",
1543 reply.r.header_type);
1544 return -EINVAL;
1545 }
1546 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1547 reply.r.chan_id, reply.r.header_type);
1548 return 0;
1549 default:
1550 if (len < 0) {
1551 /* Transport level error */
1552 if (errno == EPIPE || errno == ECONNRESET)
1553 len = -errno;
1554 return len;
1555 } else {
1556 ERR("incorrect message size: %zd\n", len);
1557 return len;
1558 }
1559 }
1560}
ff517991
MD
1561
1562/*
1563 * Set socket reciving timeout.
1564 */
1565int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1566{
1567 int ret;
1568 struct timeval tv;
1569
1570 tv.tv_sec = msec / 1000;
1571 tv.tv_usec = (msec * 1000 % 1000000);
1572
1573 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1574 if (ret < 0) {
1575 PERROR("setsockopt SO_RCVTIMEO");
1576 ret = -errno;
1577 }
1578
1579 return ret;
1580}
1581
1582/*
1583 * Set socket sending timeout.
1584 */
1585int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1586{
1587 int ret;
1588 struct timeval tv;
1589
1590 tv.tv_sec = msec / 1000;
1591 tv.tv_usec = (msec * 1000) % 1000000;
1592
1593 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1594 if (ret < 0) {
1595 PERROR("setsockopt SO_SNDTIMEO");
1596 ret = -errno;
1597 }
1598
1599 return ret;
1600}
This page took 0.121274 seconds and 4 git commands to generate.