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> |
6548fca4 | 36 | #include <ust-fd.h> |
74d81a6c | 37 | #include <helper.h> |
7bc53e94 | 38 | #include <lttng/ust-error.h> |
32ce8569 | 39 | #include <lttng/ust-events.h> |
53569322 | 40 | #include <lttng/ust-dynamic-type.h> |
32ce8569 MD |
41 | #include <usterr-signal-safe.h> |
42 | ||
43 | #include "../liblttng-ust/compat.h" | |
7bc53e94 MD |
44 | |
45 | #define USTCOMM_CODE_OFFSET(code) \ | |
46 | (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1)) | |
67c5b804 | 47 | |
74d81a6c MD |
48 | #define USTCOMM_MAX_SEND_FDS 4 |
49 | ||
53569322 MD |
50 | static |
51 | ssize_t count_fields_recursive(size_t nr_fields, | |
52 | const struct lttng_event_field *lttng_fields); | |
53 | static | |
54 | int serialize_one_field(struct lttng_session *session, | |
55 | struct ustctl_field *fields, size_t *iter_output, | |
56 | const struct lttng_event_field *lf); | |
57 | ||
67c5b804 MD |
58 | /* |
59 | * Human readable error message. | |
60 | */ | |
57773204 | 61 | static const char *ustcomm_readable_code[] = { |
7bc53e94 MD |
62 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK) ] = "Success", |
63 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR) ] = "Unknown error", | |
64 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT) ] = "No entry", | |
64b2564e DG |
65 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST) ] = "Object already exists", |
66 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL) ] = "Invalid argument", | |
67 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM) ] = "Permission denied", | |
68 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS) ] = "Not implemented", | |
74d81a6c | 69 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING) ] = "Process is exiting", |
32ce8569 MD |
70 | |
71 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number", | |
72 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type", | |
73 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version", | |
67c5b804 MD |
74 | }; |
75 | ||
76 | /* | |
7bc53e94 | 77 | * lttng_ust_strerror |
67c5b804 | 78 | * |
7bc53e94 MD |
79 | * Receives positive error value. |
80 | * Return ptr to string representing a human readable | |
81 | * error code from the ustcomm_return_code enum. | |
67c5b804 | 82 | */ |
7bc53e94 | 83 | const char *lttng_ust_strerror(int code) |
67c5b804 | 84 | { |
7bc53e94 MD |
85 | if (code == LTTNG_UST_OK) |
86 | return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)]; | |
87 | if (code < LTTNG_UST_ERR) | |
88 | return strerror(code); | |
89 | if (code >= LTTNG_UST_ERR_NR) | |
90 | code = LTTNG_UST_ERR; | |
91 | return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)]; | |
67c5b804 MD |
92 | } |
93 | ||
94 | /* | |
74d81a6c | 95 | * ustcomm_connect_unix_sock |
67c5b804 | 96 | * |
74d81a6c | 97 | * Connect to unix socket using the path name. |
6548fca4 MD |
98 | * |
99 | * Caller handles FD tracker. | |
67c5b804 | 100 | */ |
451d66b2 | 101 | int ustcomm_connect_unix_sock(const char *pathname, long timeout) |
67c5b804 MD |
102 | { |
103 | struct sockaddr_un sun; | |
7bc53e94 | 104 | int fd, ret; |
67c5b804 | 105 | |
204d45df MD |
106 | /* |
107 | * libust threads require the close-on-exec flag for all | |
108 | * resources so it does not leak file descriptors upon exec. | |
18ef3e51 | 109 | * SOCK_CLOEXEC is not used since it is linux specific. |
204d45df | 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 | * | |
74d81a6c MD |
442 | * Expect at most "nb_fd" file descriptors. Returns the number of fd |
443 | * actually received in nb_fd. | |
444 | * Returns -EPIPE on orderly shutdown. | |
445 | */ | |
446 | ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) | |
447 | { | |
448 | struct iovec iov[1]; | |
449 | ssize_t ret = 0; | |
450 | struct cmsghdr *cmsg; | |
451 | size_t sizeof_fds = nb_fd * sizeof(int); | |
452 | char recv_fd[CMSG_SPACE(sizeof_fds)]; | |
453 | struct msghdr msg; | |
454 | char dummy; | |
18ef3e51 | 455 | int i; |
7bc53e94 | 456 | |
74d81a6c | 457 | memset(&msg, 0, sizeof(msg)); |
67c5b804 | 458 | |
74d81a6c MD |
459 | /* Prepare to receive the structures */ |
460 | iov[0].iov_base = &dummy; | |
461 | iov[0].iov_len = 1; | |
462 | msg.msg_iov = iov; | |
463 | msg.msg_iovlen = 1; | |
464 | msg.msg_control = recv_fd; | |
465 | msg.msg_controllen = sizeof(recv_fd); | |
466 | ||
467 | do { | |
468 | ret = recvmsg(sock, &msg, 0); | |
469 | } while (ret < 0 && errno == EINTR); | |
470 | if (ret < 0) { | |
471 | if (errno != EPIPE && errno != ECONNRESET) { | |
32ce8569 | 472 | PERROR("recvmsg fds"); |
74d81a6c | 473 | } |
8cf811d3 | 474 | ret = -errno; |
b869b5ae MD |
475 | if (ret == -ECONNRESET) |
476 | ret = -EPIPE; | |
74d81a6c MD |
477 | goto end; |
478 | } | |
479 | if (ret == 0) { | |
480 | /* orderly shutdown */ | |
481 | ret = -EPIPE; | |
482 | goto end; | |
483 | } | |
484 | if (ret != 1) { | |
32ce8569 | 485 | ERR("Error: Received %zd bytes, expected %d\n", |
74d81a6c MD |
486 | ret, 1); |
487 | goto end; | |
488 | } | |
489 | if (msg.msg_flags & MSG_CTRUNC) { | |
32ce8569 | 490 | ERR("Error: Control message truncated.\n"); |
74d81a6c MD |
491 | ret = -1; |
492 | goto end; | |
493 | } | |
494 | cmsg = CMSG_FIRSTHDR(&msg); | |
495 | if (!cmsg) { | |
32ce8569 | 496 | ERR("Error: Invalid control message header\n"); |
74d81a6c MD |
497 | ret = -1; |
498 | goto end; | |
499 | } | |
500 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { | |
32ce8569 | 501 | ERR("Didn't received any fd\n"); |
74d81a6c MD |
502 | ret = -1; |
503 | goto end; | |
504 | } | |
505 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { | |
32ce8569 | 506 | ERR("Error: Received %zu bytes of ancillary data, expected %zu\n", |
74d81a6c MD |
507 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); |
508 | ret = -1; | |
509 | goto end; | |
510 | } | |
18ef3e51 | 511 | |
74d81a6c | 512 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
18ef3e51 JR |
513 | |
514 | /* Set FD_CLOEXEC */ | |
515 | for (i = 0; i < nb_fd; i++) { | |
516 | ret = fcntl(fds[i], F_SETFD, FD_CLOEXEC); | |
517 | if (ret < 0) { | |
518 | PERROR("fcntl failed to set FD_CLOEXEC on fd %d", | |
519 | fds[i]); | |
520 | } | |
521 | } | |
522 | ||
b2a050e0 | 523 | ret = nb_fd; |
74d81a6c | 524 | end: |
67c5b804 MD |
525 | return ret; |
526 | } | |
57773204 MD |
527 | |
528 | int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum) | |
529 | { | |
530 | ssize_t len; | |
531 | ||
532 | len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum)); | |
533 | switch (len) { | |
534 | case sizeof(*lum): | |
57773204 | 535 | break; |
57773204 | 536 | default: |
7bc53e94 | 537 | if (len < 0) { |
7bc53e94 MD |
538 | return len; |
539 | } else { | |
32ce8569 | 540 | ERR("incorrect message size: %zd\n", len); |
7bc53e94 MD |
541 | return -EINVAL; |
542 | } | |
57773204 MD |
543 | } |
544 | return 0; | |
545 | } | |
546 | ||
547 | int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur, | |
548 | uint32_t expected_handle, uint32_t expected_cmd) | |
549 | { | |
550 | ssize_t len; | |
551 | ||
552 | memset(lur, 0, sizeof(*lur)); | |
553 | len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur)); | |
554 | switch (len) { | |
555 | case 0: /* orderly shutdown */ | |
74d81a6c | 556 | return -EPIPE; |
57773204 | 557 | case sizeof(*lur): |
5dafeeaa MD |
558 | { |
559 | int err = 0; | |
560 | ||
57773204 | 561 | if (lur->handle != expected_handle) { |
32ce8569 | 562 | ERR("Unexpected result message handle: " |
74d81a6c MD |
563 | "expected: %u vs received: %u\n", |
564 | expected_handle, lur->handle); | |
5dafeeaa | 565 | err = 1; |
57773204 | 566 | } |
57773204 | 567 | if (lur->cmd != expected_cmd) { |
32ce8569 | 568 | ERR("Unexpected result message command " |
74d81a6c MD |
569 | "expected: %u vs received: %u\n", |
570 | expected_cmd, lur->cmd); | |
5dafeeaa MD |
571 | err = 1; |
572 | } | |
573 | if (err) { | |
57773204 | 574 | return -EINVAL; |
5dafeeaa MD |
575 | } else { |
576 | return lur->ret_code; | |
57773204 | 577 | } |
5dafeeaa | 578 | } |
57773204 | 579 | default: |
8cf811d3 | 580 | if (len >= 0) { |
32ce8569 | 581 | ERR("incorrect message size: %zd\n", len); |
7bc53e94 | 582 | } |
8cf811d3 | 583 | return len; |
57773204 MD |
584 | } |
585 | } | |
586 | ||
587 | int ustcomm_send_app_cmd(int sock, | |
588 | struct ustcomm_ust_msg *lum, | |
589 | struct ustcomm_ust_reply *lur) | |
590 | { | |
591 | int ret; | |
592 | ||
593 | ret = ustcomm_send_app_msg(sock, lum); | |
594 | if (ret) | |
595 | return ret; | |
c354a72c MD |
596 | ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd); |
597 | if (ret > 0) | |
598 | return -EIO; | |
599 | return ret; | |
57773204 MD |
600 | } |
601 | ||
57773204 | 602 | /* |
74d81a6c MD |
603 | * chan_data is allocated internally if this function returns the |
604 | * expected var_len. | |
57773204 | 605 | */ |
74d81a6c | 606 | ssize_t ustcomm_recv_channel_from_sessiond(int sock, |
ff0f5728 MD |
607 | void **_chan_data, uint64_t var_len, |
608 | int *_wakeup_fd) | |
57773204 | 609 | { |
74d81a6c | 610 | void *chan_data; |
ff0f5728 | 611 | ssize_t len, nr_fd; |
f5c453e9 | 612 | int wakeup_fd, ret; |
57773204 | 613 | |
74d81a6c MD |
614 | if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) { |
615 | len = -EINVAL; | |
616 | goto error_check; | |
57773204 | 617 | } |
74d81a6c MD |
618 | /* Receive variable length data */ |
619 | chan_data = zmalloc(var_len); | |
620 | if (!chan_data) { | |
621 | len = -ENOMEM; | |
622 | goto error_alloc; | |
57773204 | 623 | } |
74d81a6c MD |
624 | len = ustcomm_recv_unix_sock(sock, chan_data, var_len); |
625 | if (len != var_len) { | |
626 | goto error_recv; | |
57773204 | 627 | } |
ff0f5728 | 628 | /* recv wakeup fd */ |
6548fca4 | 629 | lttng_ust_lock_fd_tracker(); |
ff0f5728 MD |
630 | nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1); |
631 | if (nr_fd <= 0) { | |
6548fca4 | 632 | lttng_ust_unlock_fd_tracker(); |
ff0f5728 MD |
633 | if (nr_fd < 0) { |
634 | len = nr_fd; | |
635 | goto error_recv; | |
636 | } else { | |
637 | len = -EIO; | |
638 | goto error_recv; | |
639 | } | |
640 | } | |
f5c453e9 JR |
641 | |
642 | ret = lttng_ust_add_fd_to_tracker(wakeup_fd); | |
643 | if (ret < 0) { | |
644 | lttng_ust_unlock_fd_tracker(); | |
645 | ret = close(wakeup_fd); | |
646 | if (ret) { | |
647 | PERROR("close on wakeup_fd"); | |
648 | } | |
649 | len = -EIO; | |
650 | goto error_recv; | |
651 | } | |
652 | ||
653 | *_wakeup_fd = ret; | |
6548fca4 | 654 | lttng_ust_unlock_fd_tracker(); |
f5c453e9 | 655 | |
74d81a6c MD |
656 | *_chan_data = chan_data; |
657 | return len; | |
658 | ||
659 | error_recv: | |
660 | free(chan_data); | |
661 | error_alloc: | |
662 | error_check: | |
663 | return len; | |
664 | } | |
7bc53e94 | 665 | |
74d81a6c MD |
666 | int ustcomm_recv_stream_from_sessiond(int sock, |
667 | uint64_t *memory_map_size, | |
668 | int *shm_fd, int *wakeup_fd) | |
669 | { | |
670 | ssize_t len; | |
671 | int ret; | |
672 | int fds[2]; | |
673 | ||
674 | /* recv shm fd and wakeup fd */ | |
6548fca4 | 675 | lttng_ust_lock_fd_tracker(); |
74d81a6c MD |
676 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); |
677 | if (len <= 0) { | |
6548fca4 | 678 | lttng_ust_unlock_fd_tracker(); |
74d81a6c MD |
679 | if (len < 0) { |
680 | ret = len; | |
681 | goto error; | |
682 | } else { | |
683 | ret = -EIO; | |
684 | goto error; | |
685 | } | |
7bc53e94 | 686 | } |
f5c453e9 JR |
687 | |
688 | ret = lttng_ust_add_fd_to_tracker(fds[0]); | |
689 | if (ret < 0) { | |
690 | lttng_ust_unlock_fd_tracker(); | |
691 | ret = close(fds[0]); | |
692 | if (ret) { | |
693 | PERROR("close on received shm_fd"); | |
694 | } | |
695 | ret = -EIO; | |
696 | goto error; | |
697 | } | |
698 | *shm_fd = ret; | |
699 | ||
700 | ret = lttng_ust_add_fd_to_tracker(fds[1]); | |
701 | if (ret < 0) { | |
702 | lttng_ust_unlock_fd_tracker(); | |
703 | ret = close(*shm_fd); | |
704 | if (ret) { | |
705 | PERROR("close on shm_fd"); | |
706 | } | |
707 | *shm_fd = -1; | |
708 | ret = close(fds[1]); | |
709 | if (ret) { | |
710 | PERROR("close on received wakeup_fd"); | |
711 | } | |
712 | ret = -EIO; | |
713 | goto error; | |
714 | } | |
715 | *wakeup_fd = ret; | |
6548fca4 | 716 | lttng_ust_unlock_fd_tracker(); |
74d81a6c MD |
717 | return 0; |
718 | ||
719 | error: | |
57773204 MD |
720 | return ret; |
721 | } | |
32ce8569 MD |
722 | |
723 | /* | |
724 | * Returns 0 on success, negative error value on error. | |
725 | */ | |
726 | int ustcomm_send_reg_msg(int sock, | |
727 | enum ustctl_socket_type type, | |
728 | uint32_t bits_per_long, | |
729 | uint32_t uint8_t_alignment, | |
730 | uint32_t uint16_t_alignment, | |
731 | uint32_t uint32_t_alignment, | |
732 | uint32_t uint64_t_alignment, | |
733 | uint32_t long_alignment) | |
734 | { | |
735 | ssize_t len; | |
736 | struct ustctl_reg_msg reg_msg; | |
737 | ||
738 | reg_msg.magic = LTTNG_UST_COMM_MAGIC; | |
739 | reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION; | |
740 | reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION; | |
741 | reg_msg.pid = getpid(); | |
742 | reg_msg.ppid = getppid(); | |
743 | reg_msg.uid = getuid(); | |
744 | reg_msg.gid = getgid(); | |
745 | reg_msg.bits_per_long = bits_per_long; | |
746 | reg_msg.uint8_t_alignment = uint8_t_alignment; | |
747 | reg_msg.uint16_t_alignment = uint16_t_alignment; | |
748 | reg_msg.uint32_t_alignment = uint32_t_alignment; | |
749 | reg_msg.uint64_t_alignment = uint64_t_alignment; | |
750 | reg_msg.long_alignment = long_alignment; | |
751 | reg_msg.socket_type = type; | |
752 | lttng_ust_getprocname(reg_msg.name); | |
753 | memset(reg_msg.padding, 0, sizeof(reg_msg.padding)); | |
754 | ||
755 | len = ustcomm_send_unix_sock(sock, ®_msg, sizeof(reg_msg)); | |
756 | if (len > 0 && len != sizeof(reg_msg)) | |
757 | return -EIO; | |
758 | if (len < 0) | |
759 | return len; | |
760 | return 0; | |
761 | } | |
762 | ||
53569322 MD |
763 | static |
764 | ssize_t count_one_type(const struct lttng_type *lt) | |
765 | { | |
766 | switch (lt->atype) { | |
767 | case atype_integer: | |
768 | case atype_float: | |
769 | case atype_string: | |
770 | case atype_enum: | |
771 | case atype_array: | |
772 | case atype_sequence: | |
773 | return 1; | |
774 | case atype_struct: | |
775 | //TODO: implement non-empty struct. | |
776 | return 1; | |
777 | case atype_dynamic: | |
778 | { | |
779 | const struct lttng_event_field *choices; | |
780 | size_t nr_choices; | |
781 | int ret; | |
782 | ||
783 | ret = lttng_ust_dynamic_type_choices(&nr_choices, | |
784 | &choices); | |
785 | if (ret) | |
786 | return ret; | |
787 | /* | |
788 | * One field for enum, one field for variant, and | |
789 | * one field per choice. | |
790 | */ | |
791 | return count_fields_recursive(nr_choices, choices) + 2; | |
792 | } | |
793 | default: | |
794 | return -EINVAL; | |
795 | } | |
796 | return 0; | |
797 | } | |
798 | ||
799 | static | |
800 | ssize_t count_fields_recursive(size_t nr_fields, | |
801 | const struct lttng_event_field *lttng_fields) | |
802 | { | |
803 | int i; | |
804 | ssize_t ret, count = 0; | |
805 | ||
806 | for (i = 0; i < nr_fields; i++) { | |
807 | const struct lttng_event_field *lf; | |
808 | ||
809 | lf = <tng_fields[i]; | |
810 | /* skip 'nowrite' fields */ | |
811 | if (lf->nowrite) | |
812 | continue; | |
813 | ret = count_one_type(&lf->type); | |
814 | if (ret < 0) | |
815 | return ret; /* error */ | |
816 | count += ret; | |
817 | } | |
818 | return count; | |
819 | } | |
820 | ||
821 | static | |
822 | ssize_t count_ctx_fields_recursive(size_t nr_fields, | |
823 | const struct lttng_ctx_field *lttng_fields) | |
824 | { | |
825 | int i; | |
826 | ssize_t ret, count = 0; | |
827 | ||
828 | for (i = 0; i < nr_fields; i++) { | |
829 | const struct lttng_event_field *lf; | |
830 | ||
831 | lf = <tng_fields[i].event_field; | |
832 | /* skip 'nowrite' fields */ | |
833 | if (lf->nowrite) | |
834 | continue; | |
835 | ret = count_one_type(&lf->type); | |
836 | if (ret < 0) | |
837 | return ret; /* error */ | |
838 | count += ret; | |
839 | } | |
840 | return count; | |
841 | } | |
842 | ||
7f2348b8 | 843 | static |
735ea6a8 | 844 | int serialize_string_encoding(int32_t *ue, |
7f2348b8 MD |
845 | enum lttng_string_encodings le) |
846 | { | |
847 | switch (le) { | |
848 | case lttng_encode_none: | |
849 | *ue = ustctl_encode_none; | |
850 | break; | |
851 | case lttng_encode_UTF8: | |
852 | *ue = ustctl_encode_UTF8; | |
853 | break; | |
854 | case lttng_encode_ASCII: | |
855 | *ue = ustctl_encode_ASCII; | |
856 | break; | |
857 | default: | |
858 | return -EINVAL; | |
859 | } | |
860 | return 0; | |
861 | } | |
862 | ||
32ce8569 | 863 | static |
c785c634 MD |
864 | int serialize_integer_type(struct ustctl_integer_type *uit, |
865 | const struct lttng_integer_type *lit) | |
866 | { | |
973eac63 GAPG |
867 | int32_t encoding; |
868 | ||
c785c634 MD |
869 | uit->size = lit->size; |
870 | uit->signedness = lit->signedness; | |
871 | uit->reverse_byte_order = lit->reverse_byte_order; | |
872 | uit->base = lit->base; | |
973eac63 | 873 | if (serialize_string_encoding(&encoding, lit->encoding)) |
c785c634 | 874 | return -EINVAL; |
973eac63 | 875 | uit->encoding = encoding; |
c785c634 MD |
876 | uit->alignment = lit->alignment; |
877 | return 0; | |
878 | } | |
879 | ||
880 | static | |
881 | int serialize_basic_type(struct lttng_session *session, | |
882 | enum ustctl_abstract_types *uatype, | |
2b213b16 | 883 | enum lttng_abstract_types atype, |
32ce8569 MD |
884 | union _ustctl_basic_type *ubt, |
885 | const union _lttng_basic_type *lbt) | |
886 | { | |
887 | switch (atype) { | |
888 | case atype_integer: | |
889 | { | |
c785c634 | 890 | if (serialize_integer_type(&ubt->integer, &lbt->integer)) |
7f2348b8 | 891 | return -EINVAL; |
2b213b16 | 892 | *uatype = ustctl_atype_integer; |
32ce8569 MD |
893 | break; |
894 | } | |
895 | case atype_string: | |
896 | { | |
973eac63 GAPG |
897 | int32_t encoding; |
898 | ||
899 | if (serialize_string_encoding(&encoding, lbt->string.encoding)) | |
7f2348b8 | 900 | return -EINVAL; |
973eac63 | 901 | ubt->string.encoding = encoding; |
2b213b16 | 902 | *uatype = ustctl_atype_string; |
32ce8569 MD |
903 | break; |
904 | } | |
905 | case atype_float: | |
906 | { | |
907 | struct ustctl_float_type *uft; | |
908 | const struct lttng_float_type *lft; | |
909 | ||
910 | uft = &ubt->_float; | |
911 | lft = &lbt->_float; | |
912 | uft->exp_dig = lft->exp_dig; | |
913 | uft->mant_dig = lft->mant_dig; | |
914 | uft->alignment = lft->alignment; | |
915 | uft->reverse_byte_order = lft->reverse_byte_order; | |
2b213b16 | 916 | *uatype = ustctl_atype_float; |
32ce8569 MD |
917 | break; |
918 | } | |
919 | case atype_enum: | |
c785c634 MD |
920 | { |
921 | strncpy(ubt->enumeration.name, lbt->enumeration.desc->name, | |
922 | LTTNG_UST_SYM_NAME_LEN); | |
923 | ubt->enumeration.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
924 | if (serialize_integer_type(&ubt->enumeration.container_type, | |
925 | &lbt->enumeration.container_type)) | |
926 | return -EINVAL; | |
927 | if (session) { | |
928 | const struct lttng_enum *_enum; | |
929 | ||
b33b46f7 | 930 | _enum = lttng_ust_enum_get_from_desc(session, lbt->enumeration.desc); |
c785c634 MD |
931 | if (!_enum) |
932 | return -EINVAL; | |
933 | ubt->enumeration.id = _enum->id; | |
934 | } else { | |
935 | ubt->enumeration.id = -1ULL; | |
936 | } | |
937 | *uatype = ustctl_atype_enum; | |
938 | break; | |
939 | } | |
32ce8569 MD |
940 | case atype_array: |
941 | case atype_sequence: | |
942 | default: | |
943 | return -EINVAL; | |
944 | } | |
945 | return 0; | |
32ce8569 MD |
946 | } |
947 | ||
948 | static | |
53569322 MD |
949 | int serialize_dynamic_type(struct lttng_session *session, |
950 | struct ustctl_field *fields, size_t *iter_output, | |
951 | const struct lttng_event_field *lf) | |
32ce8569 | 952 | { |
53569322 MD |
953 | const struct lttng_event_field *choices; |
954 | char tag_field_name[LTTNG_UST_SYM_NAME_LEN]; | |
955 | const struct lttng_type *tag_type; | |
956 | const struct lttng_event_field *tag_field_generic; | |
957 | struct lttng_event_field tag_field = { | |
958 | .name = tag_field_name, | |
959 | .nowrite = 0, | |
960 | }; | |
961 | struct ustctl_field *uf; | |
962 | size_t nr_choices, i; | |
32ce8569 MD |
963 | int ret; |
964 | ||
53569322 MD |
965 | tag_field_generic = lttng_ust_dynamic_type_tag_field(); |
966 | tag_type = &tag_field_generic->type; | |
967 | ||
968 | /* Serialize enum field. */ | |
969 | strncpy(tag_field_name, lf->name, LTTNG_UST_SYM_NAME_LEN); | |
970 | tag_field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
971 | strncat(tag_field_name, | |
972 | "_tag", | |
973 | LTTNG_UST_SYM_NAME_LEN - strlen(tag_field_name) - 1); | |
974 | tag_field.type = *tag_type; | |
975 | ret = serialize_one_field(session, fields, iter_output, | |
976 | &tag_field); | |
977 | if (ret) | |
978 | return ret; | |
979 | ||
980 | /* Serialize variant field. */ | |
981 | uf = &fields[*iter_output]; | |
982 | ret = lttng_ust_dynamic_type_choices(&nr_choices, &choices); | |
983 | if (ret) | |
984 | return ret; | |
985 | ||
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_variant; | |
989 | uf->type.u.variant.nr_choices = nr_choices; | |
990 | strncpy(uf->type.u.variant.tag_name, | |
991 | tag_field_name, | |
992 | LTTNG_UST_SYM_NAME_LEN); | |
993 | uf->type.u.variant.tag_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
994 | (*iter_output)++; | |
995 | ||
996 | /* Serialize choice fields after variant. */ | |
997 | for (i = 0; i < nr_choices; i++) { | |
998 | ret = serialize_one_field(session, fields, | |
999 | iter_output, &choices[i]); | |
1000 | if (ret) | |
1001 | return ret; | |
1002 | } | |
1003 | return 0; | |
1004 | } | |
1005 | ||
1006 | static | |
1007 | int serialize_one_field(struct lttng_session *session, | |
1008 | struct ustctl_field *fields, size_t *iter_output, | |
1009 | const struct lttng_event_field *lf) | |
1010 | { | |
1011 | const struct lttng_type *lt = &lf->type; | |
1012 | int ret; | |
1013 | ||
1014 | /* skip 'nowrite' fields */ | |
1015 | if (lf->nowrite) | |
1016 | return 0; | |
1017 | ||
32ce8569 MD |
1018 | switch (lt->atype) { |
1019 | case atype_integer: | |
1020 | case atype_float: | |
1021 | case atype_string: | |
c785c634 | 1022 | case atype_enum: |
53569322 MD |
1023 | { |
1024 | struct ustctl_field *uf = &fields[*iter_output]; | |
1025 | struct ustctl_type *ut = &uf->type; | |
973eac63 | 1026 | enum ustctl_abstract_types atype; |
53569322 MD |
1027 | |
1028 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); | |
1029 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
973eac63 | 1030 | ret = serialize_basic_type(session, &atype, lt->atype, |
32ce8569 MD |
1031 | &ut->u.basic, <->u.basic); |
1032 | if (ret) | |
1033 | return ret; | |
973eac63 | 1034 | ut->atype = atype; |
53569322 | 1035 | (*iter_output)++; |
32ce8569 | 1036 | break; |
53569322 | 1037 | } |
32ce8569 MD |
1038 | case atype_array: |
1039 | { | |
53569322 MD |
1040 | struct ustctl_field *uf = &fields[*iter_output]; |
1041 | struct ustctl_type *ut = &uf->type; | |
32ce8569 MD |
1042 | struct ustctl_basic_type *ubt; |
1043 | const struct lttng_basic_type *lbt; | |
973eac63 | 1044 | enum ustctl_abstract_types atype; |
32ce8569 | 1045 | |
53569322 MD |
1046 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
1047 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
1048 | uf->type.atype = ustctl_atype_array; | |
32ce8569 MD |
1049 | ubt = &ut->u.array.elem_type; |
1050 | lbt = <->u.array.elem_type; | |
1051 | ut->u.array.length = lt->u.array.length; | |
973eac63 | 1052 | ret = serialize_basic_type(session, &atype, lbt->atype, |
32ce8569 MD |
1053 | &ubt->u.basic, &lbt->u.basic); |
1054 | if (ret) | |
1055 | return -EINVAL; | |
973eac63 | 1056 | ubt->atype = atype; |
2b213b16 | 1057 | ut->atype = ustctl_atype_array; |
53569322 | 1058 | (*iter_output)++; |
32ce8569 MD |
1059 | break; |
1060 | } | |
1061 | case atype_sequence: | |
1062 | { | |
53569322 MD |
1063 | struct ustctl_field *uf = &fields[*iter_output]; |
1064 | struct ustctl_type *ut = &uf->type; | |
32ce8569 MD |
1065 | struct ustctl_basic_type *ubt; |
1066 | const struct lttng_basic_type *lbt; | |
973eac63 | 1067 | enum ustctl_abstract_types atype; |
32ce8569 MD |
1068 | int ret; |
1069 | ||
53569322 MD |
1070 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); |
1071 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
1072 | uf->type.atype = ustctl_atype_sequence; | |
32ce8569 MD |
1073 | ubt = &ut->u.sequence.length_type; |
1074 | lbt = <->u.sequence.length_type; | |
973eac63 | 1075 | ret = serialize_basic_type(session, &atype, lbt->atype, |
32ce8569 MD |
1076 | &ubt->u.basic, &lbt->u.basic); |
1077 | if (ret) | |
1078 | return -EINVAL; | |
973eac63 | 1079 | ubt->atype = atype; |
32ce8569 MD |
1080 | ubt = &ut->u.sequence.elem_type; |
1081 | lbt = <->u.sequence.elem_type; | |
973eac63 | 1082 | ret = serialize_basic_type(session, &atype, lbt->atype, |
32ce8569 MD |
1083 | &ubt->u.basic, &lbt->u.basic); |
1084 | if (ret) | |
1085 | return -EINVAL; | |
973eac63 | 1086 | ubt->atype = atype; |
2b213b16 | 1087 | ut->atype = ustctl_atype_sequence; |
53569322 MD |
1088 | (*iter_output)++; |
1089 | break; | |
1090 | } | |
1091 | case atype_dynamic: | |
1092 | { | |
1093 | ret = serialize_dynamic_type(session, fields, iter_output, lf); | |
1094 | if (ret) | |
1095 | return -EINVAL; | |
1096 | break; | |
1097 | } | |
1098 | case atype_struct: | |
1099 | { | |
1100 | struct ustctl_field *uf = &fields[*iter_output]; | |
1101 | ||
1102 | /* | |
1103 | * TODO: add support for non-empty struct. | |
1104 | */ | |
1105 | if (lf->type.u._struct.nr_fields != 0) { | |
1106 | return -EINVAL; | |
1107 | } | |
1108 | strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN); | |
1109 | uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
1110 | uf->type.atype = ustctl_atype_struct; | |
1111 | uf->type.u._struct.nr_fields = 0; | |
1112 | (*iter_output)++; | |
32ce8569 MD |
1113 | break; |
1114 | } | |
32ce8569 MD |
1115 | default: |
1116 | return -EINVAL; | |
1117 | } | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | static | |
c785c634 MD |
1122 | int serialize_fields(struct lttng_session *session, |
1123 | size_t *_nr_write_fields, | |
32ce8569 MD |
1124 | struct ustctl_field **ustctl_fields, |
1125 | size_t nr_fields, | |
1126 | const struct lttng_event_field *lttng_fields) | |
1127 | { | |
1128 | struct ustctl_field *fields; | |
53569322 MD |
1129 | int ret; |
1130 | size_t i, iter_output = 0; | |
1131 | ssize_t nr_write_fields; | |
1132 | ||
1133 | nr_write_fields = count_fields_recursive(nr_fields, lttng_fields); | |
1134 | if (nr_write_fields < 0) { | |
1135 | return (int) nr_write_fields; | |
1136 | } | |
32ce8569 | 1137 | |
53569322 | 1138 | fields = zmalloc(nr_write_fields * sizeof(*fields)); |
32ce8569 MD |
1139 | if (!fields) |
1140 | return -ENOMEM; | |
1141 | ||
1142 | for (i = 0; i < nr_fields; i++) { | |
53569322 MD |
1143 | ret = serialize_one_field(session, fields, &iter_output, |
1144 | <tng_fields[i]); | |
32ce8569 MD |
1145 | if (ret) |
1146 | goto error_type; | |
32ce8569 MD |
1147 | } |
1148 | ||
1149 | *_nr_write_fields = nr_write_fields; | |
1150 | *ustctl_fields = fields; | |
1151 | return 0; | |
1152 | ||
1153 | error_type: | |
1154 | free(fields); | |
1155 | return ret; | |
1156 | } | |
1157 | ||
c785c634 MD |
1158 | static |
1159 | int serialize_entries(struct ustctl_enum_entry **_entries, | |
1160 | size_t nr_entries, | |
1161 | const struct lttng_enum_entry *lttng_entries) | |
1162 | { | |
1163 | struct ustctl_enum_entry *entries; | |
1164 | int i; | |
1165 | ||
1166 | /* Serialize the entries */ | |
1167 | entries = zmalloc(nr_entries * sizeof(*entries)); | |
1168 | if (!entries) | |
1169 | return -ENOMEM; | |
1170 | for (i = 0; i < nr_entries; i++) { | |
1171 | struct ustctl_enum_entry *uentry; | |
1172 | const struct lttng_enum_entry *lentry; | |
1173 | ||
1174 | uentry = &entries[i]; | |
1175 | lentry = <tng_entries[i]; | |
1176 | ||
a6f80644 MD |
1177 | uentry->start.value = lentry->start.value; |
1178 | uentry->start.signedness = lentry->start.signedness; | |
1179 | uentry->end.value = lentry->end.value; | |
1180 | uentry->end.signedness = lentry->end.signedness; | |
c785c634 MD |
1181 | strncpy(uentry->string, lentry->string, LTTNG_UST_SYM_NAME_LEN); |
1182 | uentry->string[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
3e762260 PP |
1183 | |
1184 | if (lentry->u.extra.options & LTTNG_ENUM_ENTRY_OPTION_IS_AUTO) { | |
1185 | uentry->u.extra.options |= | |
1186 | USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO; | |
1187 | } | |
c785c634 MD |
1188 | } |
1189 | *_entries = entries; | |
1190 | return 0; | |
1191 | } | |
1192 | ||
83e43212 | 1193 | static |
53569322 MD |
1194 | int serialize_ctx_fields(struct lttng_session *session, |
1195 | size_t *_nr_write_fields, | |
83e43212 MD |
1196 | struct ustctl_field **ustctl_fields, |
1197 | size_t nr_fields, | |
1198 | const struct lttng_ctx_field *lttng_fields) | |
1199 | { | |
1200 | struct ustctl_field *fields; | |
53569322 MD |
1201 | int ret; |
1202 | size_t i, iter_output = 0; | |
1203 | ssize_t nr_write_fields; | |
83e43212 | 1204 | |
53569322 MD |
1205 | nr_write_fields = count_ctx_fields_recursive(nr_fields, |
1206 | lttng_fields); | |
1207 | if (nr_write_fields < 0) { | |
1208 | return (int) nr_write_fields; | |
1209 | } | |
1210 | ||
1211 | fields = zmalloc(nr_write_fields * sizeof(*fields)); | |
83e43212 MD |
1212 | if (!fields) |
1213 | return -ENOMEM; | |
1214 | ||
1215 | for (i = 0; i < nr_fields; i++) { | |
53569322 MD |
1216 | ret = serialize_one_field(session, fields, &iter_output, |
1217 | <tng_fields[i].event_field); | |
83e43212 MD |
1218 | if (ret) |
1219 | goto error_type; | |
83e43212 MD |
1220 | } |
1221 | ||
1222 | *_nr_write_fields = nr_write_fields; | |
1223 | *ustctl_fields = fields; | |
1224 | return 0; | |
1225 | ||
1226 | error_type: | |
1227 | free(fields); | |
1228 | return ret; | |
1229 | } | |
1230 | ||
32ce8569 MD |
1231 | /* |
1232 | * Returns 0 on success, negative error value on error. | |
1233 | */ | |
1234 | int ustcomm_register_event(int sock, | |
c785c634 | 1235 | struct lttng_session *session, |
32ce8569 MD |
1236 | int session_objd, /* session descriptor */ |
1237 | int channel_objd, /* channel descriptor */ | |
1238 | const char *event_name, /* event name (input) */ | |
1239 | int loglevel, | |
1240 | const char *signature, /* event signature (input) */ | |
1241 | size_t nr_fields, /* fields */ | |
1242 | const struct lttng_event_field *lttng_fields, | |
1243 | const char *model_emf_uri, | |
1244 | uint32_t *id) /* event id (output) */ | |
1245 | { | |
1246 | ssize_t len; | |
1247 | struct { | |
1248 | struct ustcomm_notify_hdr header; | |
1249 | struct ustcomm_notify_event_msg m; | |
1250 | } msg; | |
1251 | struct { | |
1252 | struct ustcomm_notify_hdr header; | |
1253 | struct ustcomm_notify_event_reply r; | |
1254 | } reply; | |
1255 | size_t signature_len, fields_len, model_emf_uri_len; | |
3bf32af0 | 1256 | struct ustctl_field *fields = NULL; |
32ce8569 MD |
1257 | size_t nr_write_fields = 0; |
1258 | int ret; | |
1259 | ||
1260 | memset(&msg, 0, sizeof(msg)); | |
1261 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; | |
1262 | msg.m.session_objd = session_objd; | |
1263 | msg.m.channel_objd = channel_objd; | |
1264 | strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN); | |
1265 | msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
1266 | msg.m.loglevel = loglevel; | |
1267 | signature_len = strlen(signature) + 1; | |
1268 | msg.m.signature_len = signature_len; | |
1269 | ||
1270 | /* Calculate fields len, serialize fields. */ | |
1271 | if (nr_fields > 0) { | |
c785c634 | 1272 | ret = serialize_fields(session, &nr_write_fields, &fields, |
32ce8569 MD |
1273 | nr_fields, lttng_fields); |
1274 | if (ret) | |
1275 | return ret; | |
1276 | } | |
1277 | ||
1278 | fields_len = sizeof(*fields) * nr_write_fields; | |
1279 | msg.m.fields_len = fields_len; | |
1280 | if (model_emf_uri) { | |
1281 | model_emf_uri_len = strlen(model_emf_uri) + 1; | |
1282 | } else { | |
1283 | model_emf_uri_len = 0; | |
1284 | } | |
1285 | msg.m.model_emf_uri_len = model_emf_uri_len; | |
c785c634 | 1286 | |
32ce8569 MD |
1287 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
1288 | if (len > 0 && len != sizeof(msg)) { | |
c785c634 MD |
1289 | ret = -EIO; |
1290 | goto error_fields; | |
32ce8569 MD |
1291 | } |
1292 | if (len < 0) { | |
c785c634 MD |
1293 | ret = len; |
1294 | goto error_fields; | |
32ce8569 MD |
1295 | } |
1296 | ||
1297 | /* send signature */ | |
1298 | len = ustcomm_send_unix_sock(sock, signature, signature_len); | |
1299 | if (len > 0 && len != signature_len) { | |
6b95617c MD |
1300 | ret = -EIO; |
1301 | goto error_fields; | |
32ce8569 MD |
1302 | } |
1303 | if (len < 0) { | |
6b95617c MD |
1304 | ret = len; |
1305 | goto error_fields; | |
32ce8569 MD |
1306 | } |
1307 | ||
1308 | /* send fields */ | |
1309 | if (fields_len > 0) { | |
1310 | len = ustcomm_send_unix_sock(sock, fields, fields_len); | |
32ce8569 | 1311 | if (len > 0 && len != fields_len) { |
c785c634 MD |
1312 | ret = -EIO; |
1313 | goto error_fields; | |
32ce8569 MD |
1314 | } |
1315 | if (len < 0) { | |
c785c634 MD |
1316 | ret = len; |
1317 | goto error_fields; | |
32ce8569 MD |
1318 | } |
1319 | } | |
6b95617c | 1320 | free(fields); |
32ce8569 MD |
1321 | |
1322 | if (model_emf_uri_len) { | |
1323 | /* send model_emf_uri */ | |
1324 | len = ustcomm_send_unix_sock(sock, model_emf_uri, | |
1325 | model_emf_uri_len); | |
c785c634 | 1326 | if (len > 0 && len != model_emf_uri_len) { |
6b95617c | 1327 | return -EIO; |
c785c634 MD |
1328 | } |
1329 | if (len < 0) { | |
6b95617c | 1330 | return len; |
c785c634 | 1331 | } |
32ce8569 MD |
1332 | } |
1333 | ||
1334 | /* receive reply */ | |
1335 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); | |
1336 | switch (len) { | |
1337 | case 0: /* orderly shutdown */ | |
1338 | return -EPIPE; | |
1339 | case sizeof(reply): | |
1340 | if (reply.header.notify_cmd != msg.header.notify_cmd) { | |
1341 | ERR("Unexpected result message command " | |
1342 | "expected: %u vs received: %u\n", | |
1343 | msg.header.notify_cmd, reply.header.notify_cmd); | |
1344 | return -EINVAL; | |
1345 | } | |
1346 | if (reply.r.ret_code > 0) | |
1347 | return -EINVAL; | |
1348 | if (reply.r.ret_code < 0) | |
1349 | return reply.r.ret_code; | |
1350 | *id = reply.r.event_id; | |
1351 | DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n", | |
1352 | event_name, reply.r.ret_code, reply.r.event_id); | |
1353 | return 0; | |
1354 | default: | |
1355 | if (len < 0) { | |
1356 | /* Transport level error */ | |
1357 | if (errno == EPIPE || errno == ECONNRESET) | |
1358 | len = -errno; | |
1359 | return len; | |
1360 | } else { | |
1361 | ERR("incorrect message size: %zd\n", len); | |
1362 | return len; | |
1363 | } | |
1364 | } | |
6b95617c | 1365 | /* Unreached. */ |
c785c634 | 1366 | |
6b95617c | 1367 | /* Error path only. */ |
c785c634 MD |
1368 | error_fields: |
1369 | free(fields); | |
1370 | return ret; | |
1371 | } | |
1372 | ||
1373 | /* | |
1374 | * Returns 0 on success, negative error value on error. | |
1375 | * Returns -EPIPE or -ECONNRESET if other end has hung up. | |
1376 | */ | |
1377 | int ustcomm_register_enum(int sock, | |
1378 | int session_objd, /* session descriptor */ | |
1379 | const char *enum_name, /* enum name (input) */ | |
1380 | size_t nr_entries, /* entries */ | |
1381 | const struct lttng_enum_entry *lttng_entries, | |
1382 | uint64_t *id) | |
1383 | { | |
1384 | ssize_t len; | |
1385 | struct { | |
1386 | struct ustcomm_notify_hdr header; | |
1387 | struct ustcomm_notify_enum_msg m; | |
1388 | } msg; | |
1389 | struct { | |
1390 | struct ustcomm_notify_hdr header; | |
1391 | struct ustcomm_notify_enum_reply r; | |
1392 | } reply; | |
1393 | size_t entries_len; | |
1394 | struct ustctl_enum_entry *entries = NULL; | |
1395 | int ret; | |
1396 | ||
1397 | memset(&msg, 0, sizeof(msg)); | |
1398 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM; | |
1399 | msg.m.session_objd = session_objd; | |
1400 | strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN); | |
1401 | msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
1402 | ||
1403 | /* Calculate entries len, serialize entries. */ | |
1404 | if (nr_entries > 0) { | |
1405 | ret = serialize_entries(&entries, | |
1406 | nr_entries, lttng_entries); | |
1407 | if (ret) | |
1408 | return ret; | |
1409 | } | |
1410 | ||
1411 | entries_len = sizeof(*entries) * nr_entries; | |
1412 | msg.m.entries_len = entries_len; | |
1413 | ||
1414 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
1415 | if (len > 0 && len != sizeof(msg)) { | |
1416 | ret = -EIO; | |
1417 | goto error_entries; | |
1418 | } | |
1419 | if (len < 0) { | |
1420 | ret = len; | |
1421 | goto error_entries; | |
1422 | } | |
1423 | ||
1424 | /* send entries */ | |
1425 | if (entries_len > 0) { | |
1426 | len = ustcomm_send_unix_sock(sock, entries, entries_len); | |
1427 | if (len > 0 && len != entries_len) { | |
1428 | ret = -EIO; | |
1429 | goto error_entries; | |
1430 | } | |
1431 | if (len < 0) { | |
1432 | ret = len; | |
1433 | goto error_entries; | |
1434 | } | |
1435 | } | |
1436 | free(entries); | |
1437 | entries = NULL; | |
1438 | ||
1439 | /* receive reply */ | |
1440 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); | |
1441 | switch (len) { | |
1442 | case 0: /* orderly shutdown */ | |
1443 | return -EPIPE; | |
1444 | case sizeof(reply): | |
1445 | if (reply.header.notify_cmd != msg.header.notify_cmd) { | |
1446 | ERR("Unexpected result message command " | |
1447 | "expected: %u vs received: %u\n", | |
1448 | msg.header.notify_cmd, reply.header.notify_cmd); | |
1449 | return -EINVAL; | |
1450 | } | |
1451 | if (reply.r.ret_code > 0) | |
1452 | return -EINVAL; | |
1453 | if (reply.r.ret_code < 0) | |
1454 | return reply.r.ret_code; | |
1455 | *id = reply.r.enum_id; | |
1456 | DBG("Sent register enum notification for name \"%s\": ret_code %d\n", | |
1457 | enum_name, reply.r.ret_code); | |
1458 | return 0; | |
1459 | default: | |
1460 | if (len < 0) { | |
1461 | /* Transport level error */ | |
1462 | if (errno == EPIPE || errno == ECONNRESET) | |
1463 | len = -errno; | |
1464 | return len; | |
1465 | } else { | |
1466 | ERR("incorrect message size: %zd\n", len); | |
1467 | return len; | |
1468 | } | |
1469 | } | |
1470 | return ret; | |
1471 | ||
1472 | error_entries: | |
1473 | free(entries); | |
1474 | return ret; | |
32ce8569 MD |
1475 | } |
1476 | ||
1477 | /* | |
1478 | * Returns 0 on success, negative error value on error. | |
1479 | * Returns -EPIPE or -ECONNRESET if other end has hung up. | |
1480 | */ | |
1481 | int ustcomm_register_channel(int sock, | |
53569322 | 1482 | struct lttng_session *session, |
32ce8569 MD |
1483 | int session_objd, /* session descriptor */ |
1484 | int channel_objd, /* channel descriptor */ | |
1485 | size_t nr_ctx_fields, | |
83e43212 | 1486 | const struct lttng_ctx_field *ctx_fields, |
32ce8569 MD |
1487 | uint32_t *chan_id, /* channel id (output) */ |
1488 | int *header_type) /* header type (output) */ | |
1489 | { | |
1490 | ssize_t len; | |
1491 | struct { | |
1492 | struct ustcomm_notify_hdr header; | |
1493 | struct ustcomm_notify_channel_msg m; | |
1494 | } msg; | |
1495 | struct { | |
1496 | struct ustcomm_notify_hdr header; | |
1497 | struct ustcomm_notify_channel_reply r; | |
1498 | } reply; | |
1499 | size_t fields_len; | |
83e43212 | 1500 | struct ustctl_field *fields = NULL; |
32ce8569 MD |
1501 | int ret; |
1502 | size_t nr_write_fields = 0; | |
1503 | ||
1504 | memset(&msg, 0, sizeof(msg)); | |
1505 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; | |
1506 | msg.m.session_objd = session_objd; | |
1507 | msg.m.channel_objd = channel_objd; | |
1508 | ||
1509 | /* Calculate fields len, serialize fields. */ | |
1510 | if (nr_ctx_fields > 0) { | |
53569322 | 1511 | ret = serialize_ctx_fields(session, &nr_write_fields, &fields, |
32ce8569 MD |
1512 | nr_ctx_fields, ctx_fields); |
1513 | if (ret) | |
1514 | return ret; | |
1515 | } | |
1516 | ||
1517 | fields_len = sizeof(*fields) * nr_write_fields; | |
1518 | msg.m.ctx_fields_len = fields_len; | |
1519 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
1520 | if (len > 0 && len != sizeof(msg)) { | |
1521 | free(fields); | |
1522 | return -EIO; | |
1523 | } | |
1524 | if (len < 0) { | |
1525 | free(fields); | |
1526 | return len; | |
1527 | } | |
1528 | ||
1529 | /* send fields */ | |
1530 | if (fields_len > 0) { | |
1531 | len = ustcomm_send_unix_sock(sock, fields, fields_len); | |
1532 | free(fields); | |
1533 | if (len > 0 && len != fields_len) { | |
1534 | return -EIO; | |
1535 | } | |
1536 | if (len < 0) { | |
1537 | return len; | |
1538 | } | |
17ea789c MD |
1539 | } else { |
1540 | free(fields); | |
32ce8569 MD |
1541 | } |
1542 | ||
1543 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); | |
1544 | switch (len) { | |
1545 | case 0: /* orderly shutdown */ | |
1546 | return -EPIPE; | |
1547 | case sizeof(reply): | |
1548 | if (reply.header.notify_cmd != msg.header.notify_cmd) { | |
1549 | ERR("Unexpected result message command " | |
1550 | "expected: %u vs received: %u\n", | |
1551 | msg.header.notify_cmd, reply.header.notify_cmd); | |
1552 | return -EINVAL; | |
1553 | } | |
1554 | if (reply.r.ret_code > 0) | |
1555 | return -EINVAL; | |
1556 | if (reply.r.ret_code < 0) | |
1557 | return reply.r.ret_code; | |
1558 | *chan_id = reply.r.chan_id; | |
1559 | switch (reply.r.header_type) { | |
1560 | case 1: | |
1561 | case 2: | |
1562 | *header_type = reply.r.header_type; | |
1563 | break; | |
1564 | default: | |
1565 | ERR("Unexpected channel header type %u\n", | |
1566 | reply.r.header_type); | |
1567 | return -EINVAL; | |
1568 | } | |
1569 | DBG("Sent register channel notification: chan_id %d, header_type %d\n", | |
1570 | reply.r.chan_id, reply.r.header_type); | |
1571 | return 0; | |
1572 | default: | |
1573 | if (len < 0) { | |
1574 | /* Transport level error */ | |
1575 | if (errno == EPIPE || errno == ECONNRESET) | |
1576 | len = -errno; | |
1577 | return len; | |
1578 | } else { | |
1579 | ERR("incorrect message size: %zd\n", len); | |
1580 | return len; | |
1581 | } | |
1582 | } | |
1583 | } | |
ff517991 MD |
1584 | |
1585 | /* | |
1586 | * Set socket reciving timeout. | |
1587 | */ | |
1588 | int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec) | |
1589 | { | |
1590 | int ret; | |
1591 | struct timeval tv; | |
1592 | ||
1593 | tv.tv_sec = msec / 1000; | |
1594 | tv.tv_usec = (msec * 1000 % 1000000); | |
1595 | ||
1596 | ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
1597 | if (ret < 0) { | |
1598 | PERROR("setsockopt SO_RCVTIMEO"); | |
1599 | ret = -errno; | |
1600 | } | |
1601 | ||
1602 | return ret; | |
1603 | } | |
1604 | ||
1605 | /* | |
1606 | * Set socket sending timeout. | |
1607 | */ | |
1608 | int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec) | |
1609 | { | |
1610 | int ret; | |
1611 | struct timeval tv; | |
1612 | ||
1613 | tv.tv_sec = msec / 1000; | |
1614 | tv.tv_usec = (msec * 1000) % 1000000; | |
1615 | ||
1616 | ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); | |
1617 | if (ret < 0) { | |
1618 | PERROR("setsockopt SO_SNDTIMEO"); | |
1619 | ret = -errno; | |
1620 | } | |
1621 | ||
1622 | return ret; | |
1623 | } |