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