Fix: lttng-ust-comm.c: return number of fd rather size of array
[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 *
74d81a6c
MD
441 * Expect at most "nb_fd" file descriptors. Returns the number of fd
442 * actually received in nb_fd.
443 * Returns -EPIPE on orderly shutdown.
444 */
445ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
446{
447 struct iovec iov[1];
448 ssize_t ret = 0;
449 struct cmsghdr *cmsg;
450 size_t sizeof_fds = nb_fd * sizeof(int);
451 char recv_fd[CMSG_SPACE(sizeof_fds)];
452 struct msghdr msg;
453 char dummy;
7bc53e94 454
74d81a6c 455 memset(&msg, 0, sizeof(msg));
67c5b804 456
74d81a6c
MD
457 /* Prepare to receive the structures */
458 iov[0].iov_base = &dummy;
459 iov[0].iov_len = 1;
460 msg.msg_iov = iov;
461 msg.msg_iovlen = 1;
462 msg.msg_control = recv_fd;
463 msg.msg_controllen = sizeof(recv_fd);
464
465 do {
466 ret = recvmsg(sock, &msg, 0);
467 } while (ret < 0 && errno == EINTR);
468 if (ret < 0) {
469 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 470 PERROR("recvmsg fds");
74d81a6c 471 }
8cf811d3 472 ret = -errno;
b869b5ae
MD
473 if (ret == -ECONNRESET)
474 ret = -EPIPE;
74d81a6c
MD
475 goto end;
476 }
477 if (ret == 0) {
478 /* orderly shutdown */
479 ret = -EPIPE;
480 goto end;
481 }
482 if (ret != 1) {
32ce8569 483 ERR("Error: Received %zd bytes, expected %d\n",
74d81a6c
MD
484 ret, 1);
485 goto end;
486 }
487 if (msg.msg_flags & MSG_CTRUNC) {
32ce8569 488 ERR("Error: Control message truncated.\n");
74d81a6c
MD
489 ret = -1;
490 goto end;
491 }
492 cmsg = CMSG_FIRSTHDR(&msg);
493 if (!cmsg) {
32ce8569 494 ERR("Error: Invalid control message header\n");
74d81a6c
MD
495 ret = -1;
496 goto end;
497 }
498 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
32ce8569 499 ERR("Didn't received any fd\n");
74d81a6c
MD
500 ret = -1;
501 goto end;
502 }
503 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
32ce8569 504 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
74d81a6c
MD
505 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
506 ret = -1;
507 goto end;
508 }
509 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
9376394f 510 ret = nb_fd;
74d81a6c 511end:
67c5b804
MD
512 return ret;
513}
57773204
MD
514
515int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
516{
517 ssize_t len;
518
519 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
520 switch (len) {
521 case sizeof(*lum):
57773204 522 break;
57773204 523 default:
7bc53e94 524 if (len < 0) {
7bc53e94
MD
525 return len;
526 } else {
32ce8569 527 ERR("incorrect message size: %zd\n", len);
7bc53e94
MD
528 return -EINVAL;
529 }
57773204
MD
530 }
531 return 0;
532}
533
534int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
535 uint32_t expected_handle, uint32_t expected_cmd)
536{
537 ssize_t len;
538
539 memset(lur, 0, sizeof(*lur));
540 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
541 switch (len) {
542 case 0: /* orderly shutdown */
74d81a6c 543 return -EPIPE;
57773204 544 case sizeof(*lur):
5dafeeaa
MD
545 {
546 int err = 0;
547
57773204 548 if (lur->handle != expected_handle) {
32ce8569 549 ERR("Unexpected result message handle: "
74d81a6c
MD
550 "expected: %u vs received: %u\n",
551 expected_handle, lur->handle);
5dafeeaa 552 err = 1;
57773204 553 }
57773204 554 if (lur->cmd != expected_cmd) {
32ce8569 555 ERR("Unexpected result message command "
74d81a6c
MD
556 "expected: %u vs received: %u\n",
557 expected_cmd, lur->cmd);
5dafeeaa
MD
558 err = 1;
559 }
560 if (err) {
57773204 561 return -EINVAL;
5dafeeaa
MD
562 } else {
563 return lur->ret_code;
57773204 564 }
5dafeeaa 565 }
57773204 566 default:
8cf811d3 567 if (len >= 0) {
32ce8569 568 ERR("incorrect message size: %zd\n", len);
7bc53e94 569 }
8cf811d3 570 return len;
57773204
MD
571 }
572}
573
574int ustcomm_send_app_cmd(int sock,
575 struct ustcomm_ust_msg *lum,
576 struct ustcomm_ust_reply *lur)
577{
578 int ret;
579
580 ret = ustcomm_send_app_msg(sock, lum);
581 if (ret)
582 return ret;
c354a72c
MD
583 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
584 if (ret > 0)
585 return -EIO;
586 return ret;
57773204
MD
587}
588
57773204 589/*
74d81a6c
MD
590 * chan_data is allocated internally if this function returns the
591 * expected var_len.
57773204 592 */
74d81a6c 593ssize_t ustcomm_recv_channel_from_sessiond(int sock,
ff0f5728
MD
594 void **_chan_data, uint64_t var_len,
595 int *_wakeup_fd)
57773204 596{
74d81a6c 597 void *chan_data;
ff0f5728 598 ssize_t len, nr_fd;
f5c453e9 599 int wakeup_fd, ret;
57773204 600
74d81a6c
MD
601 if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) {
602 len = -EINVAL;
603 goto error_check;
57773204 604 }
74d81a6c
MD
605 /* Receive variable length data */
606 chan_data = zmalloc(var_len);
607 if (!chan_data) {
608 len = -ENOMEM;
609 goto error_alloc;
57773204 610 }
74d81a6c
MD
611 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
612 if (len != var_len) {
613 goto error_recv;
57773204 614 }
ff0f5728 615 /* recv wakeup fd */
6548fca4 616 lttng_ust_lock_fd_tracker();
ff0f5728
MD
617 nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
618 if (nr_fd <= 0) {
6548fca4 619 lttng_ust_unlock_fd_tracker();
ff0f5728
MD
620 if (nr_fd < 0) {
621 len = nr_fd;
622 goto error_recv;
623 } else {
624 len = -EIO;
625 goto error_recv;
626 }
627 }
f5c453e9
JR
628
629 ret = lttng_ust_add_fd_to_tracker(wakeup_fd);
630 if (ret < 0) {
631 lttng_ust_unlock_fd_tracker();
632 ret = close(wakeup_fd);
633 if (ret) {
634 PERROR("close on wakeup_fd");
635 }
636 len = -EIO;
637 goto error_recv;
638 }
639
640 *_wakeup_fd = ret;
6548fca4 641 lttng_ust_unlock_fd_tracker();
f5c453e9 642
74d81a6c
MD
643 *_chan_data = chan_data;
644 return len;
645
646error_recv:
647 free(chan_data);
648error_alloc:
649error_check:
650 return len;
651}
7bc53e94 652
74d81a6c
MD
653int ustcomm_recv_stream_from_sessiond(int sock,
654 uint64_t *memory_map_size,
655 int *shm_fd, int *wakeup_fd)
656{
657 ssize_t len;
658 int ret;
659 int fds[2];
660
661 /* recv shm fd and wakeup fd */
6548fca4 662 lttng_ust_lock_fd_tracker();
74d81a6c
MD
663 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
664 if (len <= 0) {
6548fca4 665 lttng_ust_unlock_fd_tracker();
74d81a6c
MD
666 if (len < 0) {
667 ret = len;
668 goto error;
669 } else {
670 ret = -EIO;
671 goto error;
672 }
7bc53e94 673 }
f5c453e9
JR
674
675 ret = lttng_ust_add_fd_to_tracker(fds[0]);
676 if (ret < 0) {
677 lttng_ust_unlock_fd_tracker();
678 ret = close(fds[0]);
679 if (ret) {
680 PERROR("close on received shm_fd");
681 }
682 ret = -EIO;
683 goto error;
684 }
685 *shm_fd = ret;
686
687 ret = lttng_ust_add_fd_to_tracker(fds[1]);
688 if (ret < 0) {
689 lttng_ust_unlock_fd_tracker();
690 ret = close(*shm_fd);
691 if (ret) {
692 PERROR("close on shm_fd");
693 }
694 *shm_fd = -1;
695 ret = close(fds[1]);
696 if (ret) {
697 PERROR("close on received wakeup_fd");
698 }
699 ret = -EIO;
700 goto error;
701 }
702 *wakeup_fd = ret;
6548fca4 703 lttng_ust_unlock_fd_tracker();
74d81a6c
MD
704 return 0;
705
706error:
57773204
MD
707 return ret;
708}
32ce8569
MD
709
710/*
711 * Returns 0 on success, negative error value on error.
712 */
713int ustcomm_send_reg_msg(int sock,
714 enum ustctl_socket_type type,
715 uint32_t bits_per_long,
716 uint32_t uint8_t_alignment,
717 uint32_t uint16_t_alignment,
718 uint32_t uint32_t_alignment,
719 uint32_t uint64_t_alignment,
720 uint32_t long_alignment)
721{
722 ssize_t len;
723 struct ustctl_reg_msg reg_msg;
724
725 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
726 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
727 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
728 reg_msg.pid = getpid();
729 reg_msg.ppid = getppid();
730 reg_msg.uid = getuid();
731 reg_msg.gid = getgid();
732 reg_msg.bits_per_long = bits_per_long;
733 reg_msg.uint8_t_alignment = uint8_t_alignment;
734 reg_msg.uint16_t_alignment = uint16_t_alignment;
735 reg_msg.uint32_t_alignment = uint32_t_alignment;
736 reg_msg.uint64_t_alignment = uint64_t_alignment;
737 reg_msg.long_alignment = long_alignment;
738 reg_msg.socket_type = type;
739 lttng_ust_getprocname(reg_msg.name);
740 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
741
742 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
743 if (len > 0 && len != sizeof(reg_msg))
744 return -EIO;
745 if (len < 0)
746 return len;
747 return 0;
748}
749
53569322
MD
750static
751ssize_t count_one_type(const struct lttng_type *lt)
752{
753 switch (lt->atype) {
754 case atype_integer:
755 case atype_float:
756 case atype_string:
757 case atype_enum:
758 case atype_array:
759 case atype_sequence:
760 return 1;
761 case atype_struct:
762 //TODO: implement non-empty struct.
763 return 1;
764 case atype_dynamic:
765 {
766 const struct lttng_event_field *choices;
767 size_t nr_choices;
768 int ret;
769
770 ret = lttng_ust_dynamic_type_choices(&nr_choices,
771 &choices);
772 if (ret)
773 return ret;
774 /*
775 * One field for enum, one field for variant, and
776 * one field per choice.
777 */
778 return count_fields_recursive(nr_choices, choices) + 2;
779 }
780 default:
781 return -EINVAL;
782 }
783 return 0;
784}
785
786static
787ssize_t count_fields_recursive(size_t nr_fields,
788 const struct lttng_event_field *lttng_fields)
789{
790 int i;
791 ssize_t ret, count = 0;
792
793 for (i = 0; i < nr_fields; i++) {
794 const struct lttng_event_field *lf;
795
796 lf = &lttng_fields[i];
797 /* skip 'nowrite' fields */
798 if (lf->nowrite)
799 continue;
800 ret = count_one_type(&lf->type);
801 if (ret < 0)
802 return ret; /* error */
803 count += ret;
804 }
805 return count;
806}
807
808static
809ssize_t count_ctx_fields_recursive(size_t nr_fields,
810 const struct lttng_ctx_field *lttng_fields)
811{
812 int i;
813 ssize_t ret, count = 0;
814
815 for (i = 0; i < nr_fields; i++) {
816 const struct lttng_event_field *lf;
817
818 lf = &lttng_fields[i].event_field;
819 /* skip 'nowrite' fields */
820 if (lf->nowrite)
821 continue;
822 ret = count_one_type(&lf->type);
823 if (ret < 0)
824 return ret; /* error */
825 count += ret;
826 }
827 return count;
828}
829
7f2348b8 830static
735ea6a8 831int serialize_string_encoding(int32_t *ue,
7f2348b8
MD
832 enum lttng_string_encodings le)
833{
834 switch (le) {
835 case lttng_encode_none:
836 *ue = ustctl_encode_none;
837 break;
838 case lttng_encode_UTF8:
839 *ue = ustctl_encode_UTF8;
840 break;
841 case lttng_encode_ASCII:
842 *ue = ustctl_encode_ASCII;
843 break;
844 default:
845 return -EINVAL;
846 }
847 return 0;
848}
849
32ce8569 850static
c785c634
MD
851int serialize_integer_type(struct ustctl_integer_type *uit,
852 const struct lttng_integer_type *lit)
853{
d292c6e0
GAPG
854 int32_t encoding;
855
c785c634
MD
856 uit->size = lit->size;
857 uit->signedness = lit->signedness;
858 uit->reverse_byte_order = lit->reverse_byte_order;
859 uit->base = lit->base;
d292c6e0 860 if (serialize_string_encoding(&encoding, lit->encoding))
c785c634 861 return -EINVAL;
d292c6e0 862 uit->encoding = encoding;
c785c634
MD
863 uit->alignment = lit->alignment;
864 return 0;
865}
866
867static
868int serialize_basic_type(struct lttng_session *session,
869 enum ustctl_abstract_types *uatype,
2b213b16 870 enum lttng_abstract_types atype,
32ce8569
MD
871 union _ustctl_basic_type *ubt,
872 const union _lttng_basic_type *lbt)
873{
874 switch (atype) {
875 case atype_integer:
876 {
c785c634 877 if (serialize_integer_type(&ubt->integer, &lbt->integer))
7f2348b8 878 return -EINVAL;
2b213b16 879 *uatype = ustctl_atype_integer;
32ce8569
MD
880 break;
881 }
882 case atype_string:
883 {
d292c6e0
GAPG
884 int32_t encoding;
885
886 if (serialize_string_encoding(&encoding, lbt->string.encoding))
7f2348b8 887 return -EINVAL;
d292c6e0 888 ubt->string.encoding = encoding;
2b213b16 889 *uatype = ustctl_atype_string;
32ce8569
MD
890 break;
891 }
892 case atype_float:
893 {
894 struct ustctl_float_type *uft;
895 const struct lttng_float_type *lft;
896
897 uft = &ubt->_float;
898 lft = &lbt->_float;
899 uft->exp_dig = lft->exp_dig;
900 uft->mant_dig = lft->mant_dig;
901 uft->alignment = lft->alignment;
902 uft->reverse_byte_order = lft->reverse_byte_order;
2b213b16 903 *uatype = ustctl_atype_float;
32ce8569
MD
904 break;
905 }
906 case atype_enum:
c785c634
MD
907 {
908 strncpy(ubt->enumeration.name, lbt->enumeration.desc->name,
909 LTTNG_UST_SYM_NAME_LEN);
910 ubt->enumeration.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
911 if (serialize_integer_type(&ubt->enumeration.container_type,
912 &lbt->enumeration.container_type))
913 return -EINVAL;
914 if (session) {
915 const struct lttng_enum *_enum;
916
b33b46f7 917 _enum = lttng_ust_enum_get_from_desc(session, lbt->enumeration.desc);
c785c634
MD
918 if (!_enum)
919 return -EINVAL;
920 ubt->enumeration.id = _enum->id;
921 } else {
922 ubt->enumeration.id = -1ULL;
923 }
924 *uatype = ustctl_atype_enum;
925 break;
926 }
32ce8569
MD
927 case atype_array:
928 case atype_sequence:
929 default:
930 return -EINVAL;
931 }
932 return 0;
32ce8569
MD
933}
934
935static
53569322
MD
936int serialize_dynamic_type(struct lttng_session *session,
937 struct ustctl_field *fields, size_t *iter_output,
938 const struct lttng_event_field *lf)
32ce8569 939{
53569322
MD
940 const struct lttng_event_field *choices;
941 char tag_field_name[LTTNG_UST_SYM_NAME_LEN];
942 const struct lttng_type *tag_type;
943 const struct lttng_event_field *tag_field_generic;
944 struct lttng_event_field tag_field = {
945 .name = tag_field_name,
946 .nowrite = 0,
947 };
948 struct ustctl_field *uf;
949 size_t nr_choices, i;
32ce8569
MD
950 int ret;
951
53569322
MD
952 tag_field_generic = lttng_ust_dynamic_type_tag_field();
953 tag_type = &tag_field_generic->type;
954
955 /* Serialize enum field. */
956 strncpy(tag_field_name, lf->name, LTTNG_UST_SYM_NAME_LEN);
957 tag_field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
958 strncat(tag_field_name,
959 "_tag",
960 LTTNG_UST_SYM_NAME_LEN - strlen(tag_field_name) - 1);
961 tag_field.type = *tag_type;
962 ret = serialize_one_field(session, fields, iter_output,
963 &tag_field);
964 if (ret)
965 return ret;
966
967 /* Serialize variant field. */
968 uf = &fields[*iter_output];
969 ret = lttng_ust_dynamic_type_choices(&nr_choices, &choices);
970 if (ret)
971 return ret;
972
973 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
974 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
975 uf->type.atype = ustctl_atype_variant;
976 uf->type.u.variant.nr_choices = nr_choices;
977 strncpy(uf->type.u.variant.tag_name,
978 tag_field_name,
979 LTTNG_UST_SYM_NAME_LEN);
980 uf->type.u.variant.tag_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
981 (*iter_output)++;
982
983 /* Serialize choice fields after variant. */
984 for (i = 0; i < nr_choices; i++) {
985 ret = serialize_one_field(session, fields,
986 iter_output, &choices[i]);
987 if (ret)
988 return ret;
989 }
990 return 0;
991}
992
993static
994int serialize_one_field(struct lttng_session *session,
995 struct ustctl_field *fields, size_t *iter_output,
996 const struct lttng_event_field *lf)
997{
998 const struct lttng_type *lt = &lf->type;
999 int ret;
1000
1001 /* skip 'nowrite' fields */
1002 if (lf->nowrite)
1003 return 0;
1004
32ce8569
MD
1005 switch (lt->atype) {
1006 case atype_integer:
1007 case atype_float:
1008 case atype_string:
c785c634 1009 case atype_enum:
53569322
MD
1010 {
1011 struct ustctl_field *uf = &fields[*iter_output];
1012 struct ustctl_type *ut = &uf->type;
d292c6e0 1013 enum ustctl_abstract_types atype;
53569322
MD
1014
1015 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1016 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
d292c6e0 1017 ret = serialize_basic_type(session, &atype, lt->atype,
32ce8569
MD
1018 &ut->u.basic, &lt->u.basic);
1019 if (ret)
1020 return ret;
d292c6e0 1021 ut->atype = atype;
53569322 1022 (*iter_output)++;
32ce8569 1023 break;
53569322 1024 }
32ce8569
MD
1025 case atype_array:
1026 {
53569322
MD
1027 struct ustctl_field *uf = &fields[*iter_output];
1028 struct ustctl_type *ut = &uf->type;
32ce8569
MD
1029 struct ustctl_basic_type *ubt;
1030 const struct lttng_basic_type *lbt;
d292c6e0 1031 enum ustctl_abstract_types atype;
32ce8569 1032
53569322
MD
1033 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1034 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1035 uf->type.atype = ustctl_atype_array;
32ce8569
MD
1036 ubt = &ut->u.array.elem_type;
1037 lbt = &lt->u.array.elem_type;
1038 ut->u.array.length = lt->u.array.length;
d292c6e0 1039 ret = serialize_basic_type(session, &atype, lbt->atype,
32ce8569
MD
1040 &ubt->u.basic, &lbt->u.basic);
1041 if (ret)
1042 return -EINVAL;
d292c6e0 1043 ubt->atype = atype;
2b213b16 1044 ut->atype = ustctl_atype_array;
53569322 1045 (*iter_output)++;
32ce8569
MD
1046 break;
1047 }
1048 case atype_sequence:
1049 {
53569322
MD
1050 struct ustctl_field *uf = &fields[*iter_output];
1051 struct ustctl_type *ut = &uf->type;
32ce8569
MD
1052 struct ustctl_basic_type *ubt;
1053 const struct lttng_basic_type *lbt;
d292c6e0 1054 enum ustctl_abstract_types atype;
32ce8569
MD
1055 int ret;
1056
53569322
MD
1057 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1058 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1059 uf->type.atype = ustctl_atype_sequence;
32ce8569
MD
1060 ubt = &ut->u.sequence.length_type;
1061 lbt = &lt->u.sequence.length_type;
d292c6e0 1062 ret = serialize_basic_type(session, &atype, lbt->atype,
32ce8569
MD
1063 &ubt->u.basic, &lbt->u.basic);
1064 if (ret)
1065 return -EINVAL;
d292c6e0 1066 ubt->atype = atype;
32ce8569
MD
1067 ubt = &ut->u.sequence.elem_type;
1068 lbt = &lt->u.sequence.elem_type;
d292c6e0 1069 ret = serialize_basic_type(session, &atype, lbt->atype,
32ce8569
MD
1070 &ubt->u.basic, &lbt->u.basic);
1071 if (ret)
1072 return -EINVAL;
d292c6e0 1073 ubt->atype = atype;
2b213b16 1074 ut->atype = ustctl_atype_sequence;
53569322
MD
1075 (*iter_output)++;
1076 break;
1077 }
1078 case atype_dynamic:
1079 {
1080 ret = serialize_dynamic_type(session, fields, iter_output, lf);
1081 if (ret)
1082 return -EINVAL;
1083 break;
1084 }
1085 case atype_struct:
1086 {
1087 struct ustctl_field *uf = &fields[*iter_output];
1088
1089 /*
1090 * TODO: add support for non-empty struct.
1091 */
1092 if (lf->type.u._struct.nr_fields != 0) {
1093 return -EINVAL;
1094 }
1095 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1096 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1097 uf->type.atype = ustctl_atype_struct;
1098 uf->type.u._struct.nr_fields = 0;
1099 (*iter_output)++;
32ce8569
MD
1100 break;
1101 }
32ce8569
MD
1102 default:
1103 return -EINVAL;
1104 }
1105 return 0;
1106}
1107
1108static
c785c634
MD
1109int serialize_fields(struct lttng_session *session,
1110 size_t *_nr_write_fields,
32ce8569
MD
1111 struct ustctl_field **ustctl_fields,
1112 size_t nr_fields,
1113 const struct lttng_event_field *lttng_fields)
1114{
1115 struct ustctl_field *fields;
53569322
MD
1116 int ret;
1117 size_t i, iter_output = 0;
1118 ssize_t nr_write_fields;
1119
1120 nr_write_fields = count_fields_recursive(nr_fields, lttng_fields);
1121 if (nr_write_fields < 0) {
1122 return (int) nr_write_fields;
1123 }
32ce8569 1124
53569322 1125 fields = zmalloc(nr_write_fields * sizeof(*fields));
32ce8569
MD
1126 if (!fields)
1127 return -ENOMEM;
1128
1129 for (i = 0; i < nr_fields; i++) {
53569322
MD
1130 ret = serialize_one_field(session, fields, &iter_output,
1131 &lttng_fields[i]);
32ce8569
MD
1132 if (ret)
1133 goto error_type;
32ce8569
MD
1134 }
1135
1136 *_nr_write_fields = nr_write_fields;
1137 *ustctl_fields = fields;
1138 return 0;
1139
1140error_type:
1141 free(fields);
1142 return ret;
1143}
1144
c785c634
MD
1145static
1146int serialize_entries(struct ustctl_enum_entry **_entries,
1147 size_t nr_entries,
1148 const struct lttng_enum_entry *lttng_entries)
1149{
1150 struct ustctl_enum_entry *entries;
1151 int i;
1152
1153 /* Serialize the entries */
1154 entries = zmalloc(nr_entries * sizeof(*entries));
1155 if (!entries)
1156 return -ENOMEM;
1157 for (i = 0; i < nr_entries; i++) {
1158 struct ustctl_enum_entry *uentry;
1159 const struct lttng_enum_entry *lentry;
1160
1161 uentry = &entries[i];
1162 lentry = &lttng_entries[i];
1163
a6f80644
MD
1164 uentry->start.value = lentry->start.value;
1165 uentry->start.signedness = lentry->start.signedness;
1166 uentry->end.value = lentry->end.value;
1167 uentry->end.signedness = lentry->end.signedness;
c785c634
MD
1168 strncpy(uentry->string, lentry->string, LTTNG_UST_SYM_NAME_LEN);
1169 uentry->string[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
3e762260
PP
1170
1171 if (lentry->u.extra.options & LTTNG_ENUM_ENTRY_OPTION_IS_AUTO) {
1172 uentry->u.extra.options |=
1173 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO;
1174 }
c785c634
MD
1175 }
1176 *_entries = entries;
1177 return 0;
1178}
1179
83e43212 1180static
53569322
MD
1181int serialize_ctx_fields(struct lttng_session *session,
1182 size_t *_nr_write_fields,
83e43212
MD
1183 struct ustctl_field **ustctl_fields,
1184 size_t nr_fields,
1185 const struct lttng_ctx_field *lttng_fields)
1186{
1187 struct ustctl_field *fields;
53569322
MD
1188 int ret;
1189 size_t i, iter_output = 0;
1190 ssize_t nr_write_fields;
83e43212 1191
53569322
MD
1192 nr_write_fields = count_ctx_fields_recursive(nr_fields,
1193 lttng_fields);
1194 if (nr_write_fields < 0) {
1195 return (int) nr_write_fields;
1196 }
1197
1198 fields = zmalloc(nr_write_fields * sizeof(*fields));
83e43212
MD
1199 if (!fields)
1200 return -ENOMEM;
1201
1202 for (i = 0; i < nr_fields; i++) {
53569322
MD
1203 ret = serialize_one_field(session, fields, &iter_output,
1204 &lttng_fields[i].event_field);
83e43212
MD
1205 if (ret)
1206 goto error_type;
83e43212
MD
1207 }
1208
1209 *_nr_write_fields = nr_write_fields;
1210 *ustctl_fields = fields;
1211 return 0;
1212
1213error_type:
1214 free(fields);
1215 return ret;
1216}
1217
32ce8569
MD
1218/*
1219 * Returns 0 on success, negative error value on error.
1220 */
1221int ustcomm_register_event(int sock,
c785c634 1222 struct lttng_session *session,
32ce8569
MD
1223 int session_objd, /* session descriptor */
1224 int channel_objd, /* channel descriptor */
1225 const char *event_name, /* event name (input) */
1226 int loglevel,
1227 const char *signature, /* event signature (input) */
1228 size_t nr_fields, /* fields */
1229 const struct lttng_event_field *lttng_fields,
1230 const char *model_emf_uri,
1231 uint32_t *id) /* event id (output) */
1232{
1233 ssize_t len;
1234 struct {
1235 struct ustcomm_notify_hdr header;
1236 struct ustcomm_notify_event_msg m;
1237 } msg;
1238 struct {
1239 struct ustcomm_notify_hdr header;
1240 struct ustcomm_notify_event_reply r;
1241 } reply;
1242 size_t signature_len, fields_len, model_emf_uri_len;
3bf32af0 1243 struct ustctl_field *fields = NULL;
32ce8569
MD
1244 size_t nr_write_fields = 0;
1245 int ret;
1246
1247 memset(&msg, 0, sizeof(msg));
1248 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1249 msg.m.session_objd = session_objd;
1250 msg.m.channel_objd = channel_objd;
1251 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
1252 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1253 msg.m.loglevel = loglevel;
1254 signature_len = strlen(signature) + 1;
1255 msg.m.signature_len = signature_len;
1256
1257 /* Calculate fields len, serialize fields. */
1258 if (nr_fields > 0) {
c785c634 1259 ret = serialize_fields(session, &nr_write_fields, &fields,
32ce8569
MD
1260 nr_fields, lttng_fields);
1261 if (ret)
1262 return ret;
1263 }
1264
1265 fields_len = sizeof(*fields) * nr_write_fields;
1266 msg.m.fields_len = fields_len;
1267 if (model_emf_uri) {
1268 model_emf_uri_len = strlen(model_emf_uri) + 1;
1269 } else {
1270 model_emf_uri_len = 0;
1271 }
1272 msg.m.model_emf_uri_len = model_emf_uri_len;
c785c634 1273
32ce8569
MD
1274 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1275 if (len > 0 && len != sizeof(msg)) {
c785c634
MD
1276 ret = -EIO;
1277 goto error_fields;
32ce8569
MD
1278 }
1279 if (len < 0) {
c785c634
MD
1280 ret = len;
1281 goto error_fields;
32ce8569
MD
1282 }
1283
1284 /* send signature */
1285 len = ustcomm_send_unix_sock(sock, signature, signature_len);
1286 if (len > 0 && len != signature_len) {
6b95617c
MD
1287 ret = -EIO;
1288 goto error_fields;
32ce8569
MD
1289 }
1290 if (len < 0) {
6b95617c
MD
1291 ret = len;
1292 goto error_fields;
32ce8569
MD
1293 }
1294
1295 /* send fields */
1296 if (fields_len > 0) {
1297 len = ustcomm_send_unix_sock(sock, fields, fields_len);
32ce8569 1298 if (len > 0 && len != fields_len) {
c785c634
MD
1299 ret = -EIO;
1300 goto error_fields;
32ce8569
MD
1301 }
1302 if (len < 0) {
c785c634
MD
1303 ret = len;
1304 goto error_fields;
32ce8569
MD
1305 }
1306 }
6b95617c 1307 free(fields);
32ce8569
MD
1308
1309 if (model_emf_uri_len) {
1310 /* send model_emf_uri */
1311 len = ustcomm_send_unix_sock(sock, model_emf_uri,
1312 model_emf_uri_len);
c785c634 1313 if (len > 0 && len != model_emf_uri_len) {
6b95617c 1314 return -EIO;
c785c634
MD
1315 }
1316 if (len < 0) {
6b95617c 1317 return len;
c785c634 1318 }
32ce8569
MD
1319 }
1320
1321 /* receive reply */
1322 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1323 switch (len) {
1324 case 0: /* orderly shutdown */
1325 return -EPIPE;
1326 case sizeof(reply):
1327 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1328 ERR("Unexpected result message command "
1329 "expected: %u vs received: %u\n",
1330 msg.header.notify_cmd, reply.header.notify_cmd);
1331 return -EINVAL;
1332 }
1333 if (reply.r.ret_code > 0)
1334 return -EINVAL;
1335 if (reply.r.ret_code < 0)
1336 return reply.r.ret_code;
1337 *id = reply.r.event_id;
1338 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1339 event_name, reply.r.ret_code, reply.r.event_id);
1340 return 0;
1341 default:
1342 if (len < 0) {
1343 /* Transport level error */
1344 if (errno == EPIPE || errno == ECONNRESET)
1345 len = -errno;
1346 return len;
1347 } else {
1348 ERR("incorrect message size: %zd\n", len);
1349 return len;
1350 }
1351 }
6b95617c 1352 /* Unreached. */
c785c634 1353
6b95617c 1354 /* Error path only. */
c785c634
MD
1355error_fields:
1356 free(fields);
1357 return ret;
1358}
1359
1360/*
1361 * Returns 0 on success, negative error value on error.
1362 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1363 */
1364int ustcomm_register_enum(int sock,
1365 int session_objd, /* session descriptor */
1366 const char *enum_name, /* enum name (input) */
1367 size_t nr_entries, /* entries */
1368 const struct lttng_enum_entry *lttng_entries,
1369 uint64_t *id)
1370{
1371 ssize_t len;
1372 struct {
1373 struct ustcomm_notify_hdr header;
1374 struct ustcomm_notify_enum_msg m;
1375 } msg;
1376 struct {
1377 struct ustcomm_notify_hdr header;
1378 struct ustcomm_notify_enum_reply r;
1379 } reply;
1380 size_t entries_len;
1381 struct ustctl_enum_entry *entries = NULL;
1382 int ret;
1383
1384 memset(&msg, 0, sizeof(msg));
1385 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1386 msg.m.session_objd = session_objd;
1387 strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN);
1388 msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1389
1390 /* Calculate entries len, serialize entries. */
1391 if (nr_entries > 0) {
1392 ret = serialize_entries(&entries,
1393 nr_entries, lttng_entries);
1394 if (ret)
1395 return ret;
1396 }
1397
1398 entries_len = sizeof(*entries) * nr_entries;
1399 msg.m.entries_len = entries_len;
1400
1401 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1402 if (len > 0 && len != sizeof(msg)) {
1403 ret = -EIO;
1404 goto error_entries;
1405 }
1406 if (len < 0) {
1407 ret = len;
1408 goto error_entries;
1409 }
1410
1411 /* send entries */
1412 if (entries_len > 0) {
1413 len = ustcomm_send_unix_sock(sock, entries, entries_len);
1414 if (len > 0 && len != entries_len) {
1415 ret = -EIO;
1416 goto error_entries;
1417 }
1418 if (len < 0) {
1419 ret = len;
1420 goto error_entries;
1421 }
1422 }
1423 free(entries);
1424 entries = NULL;
1425
1426 /* receive reply */
1427 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1428 switch (len) {
1429 case 0: /* orderly shutdown */
1430 return -EPIPE;
1431 case sizeof(reply):
1432 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1433 ERR("Unexpected result message command "
1434 "expected: %u vs received: %u\n",
1435 msg.header.notify_cmd, reply.header.notify_cmd);
1436 return -EINVAL;
1437 }
1438 if (reply.r.ret_code > 0)
1439 return -EINVAL;
1440 if (reply.r.ret_code < 0)
1441 return reply.r.ret_code;
1442 *id = reply.r.enum_id;
1443 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1444 enum_name, reply.r.ret_code);
1445 return 0;
1446 default:
1447 if (len < 0) {
1448 /* Transport level error */
1449 if (errno == EPIPE || errno == ECONNRESET)
1450 len = -errno;
1451 return len;
1452 } else {
1453 ERR("incorrect message size: %zd\n", len);
1454 return len;
1455 }
1456 }
1457 return ret;
1458
1459error_entries:
1460 free(entries);
1461 return ret;
32ce8569
MD
1462}
1463
1464/*
1465 * Returns 0 on success, negative error value on error.
1466 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1467 */
1468int ustcomm_register_channel(int sock,
53569322 1469 struct lttng_session *session,
32ce8569
MD
1470 int session_objd, /* session descriptor */
1471 int channel_objd, /* channel descriptor */
1472 size_t nr_ctx_fields,
83e43212 1473 const struct lttng_ctx_field *ctx_fields,
32ce8569
MD
1474 uint32_t *chan_id, /* channel id (output) */
1475 int *header_type) /* header type (output) */
1476{
1477 ssize_t len;
1478 struct {
1479 struct ustcomm_notify_hdr header;
1480 struct ustcomm_notify_channel_msg m;
1481 } msg;
1482 struct {
1483 struct ustcomm_notify_hdr header;
1484 struct ustcomm_notify_channel_reply r;
1485 } reply;
1486 size_t fields_len;
83e43212 1487 struct ustctl_field *fields = NULL;
32ce8569
MD
1488 int ret;
1489 size_t nr_write_fields = 0;
1490
1491 memset(&msg, 0, sizeof(msg));
1492 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1493 msg.m.session_objd = session_objd;
1494 msg.m.channel_objd = channel_objd;
1495
1496 /* Calculate fields len, serialize fields. */
1497 if (nr_ctx_fields > 0) {
53569322 1498 ret = serialize_ctx_fields(session, &nr_write_fields, &fields,
32ce8569
MD
1499 nr_ctx_fields, ctx_fields);
1500 if (ret)
1501 return ret;
1502 }
1503
1504 fields_len = sizeof(*fields) * nr_write_fields;
1505 msg.m.ctx_fields_len = fields_len;
1506 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1507 if (len > 0 && len != sizeof(msg)) {
1508 free(fields);
1509 return -EIO;
1510 }
1511 if (len < 0) {
1512 free(fields);
1513 return len;
1514 }
1515
1516 /* send fields */
1517 if (fields_len > 0) {
1518 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1519 free(fields);
1520 if (len > 0 && len != fields_len) {
1521 return -EIO;
1522 }
1523 if (len < 0) {
1524 return len;
1525 }
17ea789c
MD
1526 } else {
1527 free(fields);
32ce8569
MD
1528 }
1529
1530 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1531 switch (len) {
1532 case 0: /* orderly shutdown */
1533 return -EPIPE;
1534 case sizeof(reply):
1535 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1536 ERR("Unexpected result message command "
1537 "expected: %u vs received: %u\n",
1538 msg.header.notify_cmd, reply.header.notify_cmd);
1539 return -EINVAL;
1540 }
1541 if (reply.r.ret_code > 0)
1542 return -EINVAL;
1543 if (reply.r.ret_code < 0)
1544 return reply.r.ret_code;
1545 *chan_id = reply.r.chan_id;
1546 switch (reply.r.header_type) {
1547 case 1:
1548 case 2:
1549 *header_type = reply.r.header_type;
1550 break;
1551 default:
1552 ERR("Unexpected channel header type %u\n",
1553 reply.r.header_type);
1554 return -EINVAL;
1555 }
1556 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1557 reply.r.chan_id, reply.r.header_type);
1558 return 0;
1559 default:
1560 if (len < 0) {
1561 /* Transport level error */
1562 if (errno == EPIPE || errno == ECONNRESET)
1563 len = -errno;
1564 return len;
1565 } else {
1566 ERR("incorrect message size: %zd\n", len);
1567 return len;
1568 }
1569 }
1570}
ff517991
MD
1571
1572/*
1573 * Set socket reciving timeout.
1574 */
1575int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1576{
1577 int ret;
1578 struct timeval tv;
1579
1580 tv.tv_sec = msec / 1000;
1581 tv.tv_usec = (msec * 1000 % 1000000);
1582
1583 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1584 if (ret < 0) {
1585 PERROR("setsockopt SO_RCVTIMEO");
1586 ret = -errno;
1587 }
1588
1589 return ret;
1590}
1591
1592/*
1593 * Set socket sending timeout.
1594 */
1595int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1596{
1597 int ret;
1598 struct timeval tv;
1599
1600 tv.tv_sec = msec / 1000;
1601 tv.tv_usec = (msec * 1000) % 1000000;
1602
1603 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1604 if (ret < 0) {
1605 PERROR("setsockopt SO_SNDTIMEO");
1606 ret = -errno;
1607 }
1608
1609 return ret;
1610}
This page took 0.115866 seconds and 4 git commands to generate.