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