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