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