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