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