Commit | Line | Data |
---|---|---|
67c5b804 MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
74d81a6c | 3 | * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
67c5b804 | 4 | * |
15f672f9 MD |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; only | |
8 | * version 2.1 of the License. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
67c5b804 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15f672f9 MD |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
67c5b804 MD |
18 | */ |
19 | ||
20 | #define _GNU_SOURCE | |
21 | #include <limits.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/socket.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/un.h> | |
29 | #include <unistd.h> | |
30 | #include <assert.h> | |
57773204 | 31 | #include <errno.h> |
11ba4bcb | 32 | #include <fcntl.h> |
67c5b804 | 33 | |
32ce8569 | 34 | #include <lttng/ust-ctl.h> |
b728d87e | 35 | #include <ust-comm.h> |
74d81a6c | 36 | #include <helper.h> |
7bc53e94 | 37 | #include <lttng/ust-error.h> |
32ce8569 MD |
38 | #include <lttng/ust-events.h> |
39 | #include <usterr-signal-safe.h> | |
40 | ||
41 | #include "../liblttng-ust/compat.h" | |
7bc53e94 MD |
42 | |
43 | #define USTCOMM_CODE_OFFSET(code) \ | |
44 | (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1)) | |
67c5b804 | 45 | |
74d81a6c MD |
46 | #define USTCOMM_MAX_SEND_FDS 4 |
47 | ||
67c5b804 MD |
48 | /* |
49 | * Human readable error message. | |
50 | */ | |
57773204 | 51 | static const char *ustcomm_readable_code[] = { |
7bc53e94 MD |
52 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK) ] = "Success", |
53 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR) ] = "Unknown error", | |
54 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT) ] = "No entry", | |
64b2564e DG |
55 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST) ] = "Object already exists", |
56 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL) ] = "Invalid argument", | |
57 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM) ] = "Permission denied", | |
58 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS) ] = "Not implemented", | |
74d81a6c | 59 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING) ] = "Process is exiting", |
32ce8569 MD |
60 | |
61 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number", | |
62 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type", | |
63 | [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version", | |
67c5b804 MD |
64 | }; |
65 | ||
66 | /* | |
7bc53e94 | 67 | * lttng_ust_strerror |
67c5b804 | 68 | * |
7bc53e94 MD |
69 | * Receives positive error value. |
70 | * Return ptr to string representing a human readable | |
71 | * error code from the ustcomm_return_code enum. | |
67c5b804 | 72 | */ |
7bc53e94 | 73 | const char *lttng_ust_strerror(int code) |
67c5b804 | 74 | { |
7bc53e94 MD |
75 | if (code == LTTNG_UST_OK) |
76 | return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)]; | |
77 | if (code < LTTNG_UST_ERR) | |
78 | return strerror(code); | |
79 | if (code >= LTTNG_UST_ERR_NR) | |
80 | code = LTTNG_UST_ERR; | |
81 | return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)]; | |
67c5b804 MD |
82 | } |
83 | ||
84 | /* | |
74d81a6c | 85 | * ustcomm_connect_unix_sock |
67c5b804 | 86 | * |
74d81a6c | 87 | * Connect to unix socket using the path name. |
67c5b804 | 88 | */ |
57773204 | 89 | int ustcomm_connect_unix_sock(const char *pathname) |
67c5b804 MD |
90 | { |
91 | struct sockaddr_un sun; | |
7bc53e94 | 92 | int fd, ret; |
67c5b804 | 93 | |
204d45df MD |
94 | /* |
95 | * libust threads require the close-on-exec flag for all | |
96 | * resources so it does not leak file descriptors upon exec. | |
97 | */ | |
11ba4bcb | 98 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
67c5b804 | 99 | if (fd < 0) { |
32ce8569 | 100 | PERROR("socket"); |
7bc53e94 | 101 | ret = -errno; |
67c5b804 MD |
102 | goto error; |
103 | } | |
11ba4bcb MD |
104 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); |
105 | if (ret < 0) { | |
32ce8569 | 106 | PERROR("fcntl"); |
7bc53e94 | 107 | ret = -errno; |
11ba4bcb MD |
108 | goto error_fcntl; |
109 | } | |
67c5b804 MD |
110 | |
111 | memset(&sun, 0, sizeof(sun)); | |
112 | sun.sun_family = AF_UNIX; | |
113 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
114 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
115 | ||
116 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); | |
117 | if (ret < 0) { | |
118 | /* | |
0b9aa170 MD |
119 | * Don't print message on connect ENOENT error, because |
120 | * connect is used in normal execution to detect if | |
121 | * sessiond is alive. ENOENT is when the unix socket | |
122 | * file does not exist, and ECONNREFUSED is when the | |
123 | * file exists but no sessiond is listening. | |
67c5b804 | 124 | */ |
0b9aa170 | 125 | if (errno != ECONNREFUSED && errno != ECONNRESET |
bdd8ca83 | 126 | && errno != ENOENT && errno != EACCES) |
8cf811d3 | 127 | PERROR("connect"); |
7bc53e94 | 128 | ret = -errno; |
8cf811d3 MD |
129 | if (ret == -ECONNREFUSED || ret == -ECONNRESET) |
130 | ret = -EPIPE; | |
67c5b804 MD |
131 | goto error_connect; |
132 | } | |
133 | ||
134 | return fd; | |
135 | ||
136 | error_connect: | |
11ba4bcb | 137 | error_fcntl: |
7bc53e94 MD |
138 | { |
139 | int closeret; | |
140 | ||
141 | closeret = close(fd); | |
142 | if (closeret) | |
32ce8569 | 143 | PERROR("close"); |
7bc53e94 | 144 | } |
67c5b804 MD |
145 | error: |
146 | return ret; | |
147 | } | |
148 | ||
149 | /* | |
74d81a6c | 150 | * ustcomm_accept_unix_sock |
67c5b804 | 151 | * |
74d81a6c MD |
152 | * Do an accept(2) on the sock and return the |
153 | * new file descriptor. The socket MUST be bind(2) before. | |
67c5b804 | 154 | */ |
57773204 | 155 | int ustcomm_accept_unix_sock(int sock) |
67c5b804 MD |
156 | { |
157 | int new_fd; | |
158 | struct sockaddr_un sun; | |
159 | socklen_t len = 0; | |
160 | ||
161 | /* Blocking call */ | |
162 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); | |
163 | if (new_fd < 0) { | |
b869b5ae MD |
164 | if (errno != ECONNABORTED) |
165 | PERROR("accept"); | |
166 | new_fd = -errno; | |
167 | if (new_fd == -ECONNABORTED) | |
168 | new_fd = -EPIPE; | |
67c5b804 | 169 | } |
67c5b804 | 170 | return new_fd; |
67c5b804 MD |
171 | } |
172 | ||
173 | /* | |
74d81a6c | 174 | * ustcomm_create_unix_sock |
67c5b804 | 175 | * |
74d81a6c MD |
176 | * Creates a AF_UNIX local socket using pathname |
177 | * bind the socket upon creation and return the fd. | |
67c5b804 | 178 | */ |
57773204 | 179 | int ustcomm_create_unix_sock(const char *pathname) |
67c5b804 MD |
180 | { |
181 | struct sockaddr_un sun; | |
7bc53e94 | 182 | int fd, ret; |
67c5b804 MD |
183 | |
184 | /* Create server socket */ | |
185 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
32ce8569 | 186 | PERROR("socket"); |
7bc53e94 | 187 | ret = -errno; |
67c5b804 MD |
188 | goto error; |
189 | } | |
190 | ||
191 | memset(&sun, 0, sizeof(sun)); | |
192 | sun.sun_family = AF_UNIX; | |
193 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
194 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
195 | ||
196 | /* Unlink the old file if present */ | |
197 | (void) unlink(pathname); | |
198 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); | |
199 | if (ret < 0) { | |
32ce8569 | 200 | PERROR("bind"); |
7bc53e94 MD |
201 | ret = -errno; |
202 | goto error_close; | |
67c5b804 MD |
203 | } |
204 | ||
205 | return fd; | |
206 | ||
7bc53e94 MD |
207 | error_close: |
208 | { | |
209 | int closeret; | |
210 | ||
211 | closeret = close(fd); | |
212 | if (closeret) { | |
32ce8569 | 213 | PERROR("close"); |
7bc53e94 MD |
214 | } |
215 | } | |
67c5b804 MD |
216 | error: |
217 | return ret; | |
218 | } | |
219 | ||
220 | /* | |
74d81a6c | 221 | * ustcomm_listen_unix_sock |
67c5b804 | 222 | * |
74d81a6c | 223 | * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN. |
67c5b804 | 224 | */ |
57773204 | 225 | int ustcomm_listen_unix_sock(int sock) |
67c5b804 MD |
226 | { |
227 | int ret; | |
228 | ||
e41474be | 229 | ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN); |
67c5b804 | 230 | if (ret < 0) { |
7bc53e94 | 231 | ret = -errno; |
32ce8569 | 232 | PERROR("listen"); |
67c5b804 MD |
233 | } |
234 | ||
235 | return ret; | |
236 | } | |
237 | ||
238 | /* | |
74d81a6c MD |
239 | * ustcomm_close_unix_sock |
240 | * | |
241 | * Shutdown cleanly a unix socket. | |
242 | */ | |
243 | int ustcomm_close_unix_sock(int sock) | |
244 | { | |
245 | int ret; | |
246 | ||
247 | ret = close(sock); | |
248 | if (ret < 0) { | |
32ce8569 | 249 | PERROR("close"); |
74d81a6c MD |
250 | ret = -errno; |
251 | } | |
252 | ||
253 | return ret; | |
254 | } | |
255 | ||
256 | /* | |
257 | * ustcomm_recv_unix_sock | |
67c5b804 | 258 | * |
74d81a6c MD |
259 | * Receive data of size len in put that data into |
260 | * the buf param. Using recvmsg API. | |
261 | * Return the size of received data. | |
262 | * Return 0 on orderly shutdown. | |
67c5b804 | 263 | */ |
57773204 | 264 | ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len) |
67c5b804 | 265 | { |
913b87f1 | 266 | struct msghdr msg; |
67c5b804 | 267 | struct iovec iov[1]; |
7bc53e94 | 268 | ssize_t ret; |
67c5b804 | 269 | |
913b87f1 MD |
270 | memset(&msg, 0, sizeof(msg)); |
271 | ||
67c5b804 MD |
272 | iov[0].iov_base = buf; |
273 | iov[0].iov_len = len; | |
274 | msg.msg_iov = iov; | |
275 | msg.msg_iovlen = 1; | |
276 | ||
7e3cfcbe MD |
277 | do { |
278 | ret = recvmsg(sock, &msg, 0); | |
279 | } while (ret < 0 && errno == EINTR); | |
7bc53e94 MD |
280 | |
281 | if (ret < 0) { | |
282 | int shutret; | |
283 | ||
8cf811d3 | 284 | if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED) |
32ce8569 | 285 | PERROR("recvmsg"); |
7bc53e94 | 286 | ret = -errno; |
8cf811d3 | 287 | if (ret == -ECONNRESET || ret == -ECONNREFUSED) |
b869b5ae | 288 | ret = -EPIPE; |
7bc53e94 MD |
289 | |
290 | shutret = shutdown(sock, SHUT_RDWR); | |
291 | if (shutret) | |
32ce8569 | 292 | ERR("Socket shutdown error"); |
67c5b804 MD |
293 | } |
294 | ||
295 | return ret; | |
296 | } | |
297 | ||
298 | /* | |
74d81a6c | 299 | * ustcomm_send_unix_sock |
67c5b804 | 300 | * |
74d81a6c MD |
301 | * Send buf data of size len. Using sendmsg API. |
302 | * Return the size of sent data. | |
67c5b804 | 303 | */ |
32ce8569 | 304 | ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len) |
67c5b804 | 305 | { |
913b87f1 | 306 | struct msghdr msg; |
67c5b804 | 307 | struct iovec iov[1]; |
7bc53e94 | 308 | ssize_t ret; |
67c5b804 | 309 | |
913b87f1 MD |
310 | memset(&msg, 0, sizeof(msg)); |
311 | ||
32ce8569 | 312 | iov[0].iov_base = (void *) buf; |
67c5b804 MD |
313 | iov[0].iov_len = len; |
314 | msg.msg_iov = iov; | |
315 | msg.msg_iovlen = 1; | |
316 | ||
1ea11eab MD |
317 | /* |
318 | * Using the MSG_NOSIGNAL when sending data from sessiond to | |
319 | * libust, so libust does not receive an unhandled SIGPIPE or | |
320 | * SIGURG. The sessiond receiver side can be made more resilient | |
321 | * by ignoring SIGPIPE, but we don't have this luxury on the | |
322 | * libust side. | |
323 | */ | |
51d9d699 MD |
324 | do { |
325 | ret = sendmsg(sock, &msg, MSG_NOSIGNAL); | |
326 | } while (ret < 0 && errno == EINTR); | |
7bc53e94 MD |
327 | |
328 | if (ret < 0) { | |
329 | int shutret; | |
330 | ||
74d81a6c | 331 | if (errno != EPIPE && errno != ECONNRESET) |
32ce8569 | 332 | PERROR("sendmsg"); |
7bc53e94 | 333 | ret = -errno; |
b869b5ae MD |
334 | if (ret == -ECONNRESET) |
335 | ret = -EPIPE; | |
7bc53e94 MD |
336 | |
337 | shutret = shutdown(sock, SHUT_RDWR); | |
338 | if (shutret) | |
32ce8569 | 339 | ERR("Socket shutdown error"); |
67c5b804 MD |
340 | } |
341 | ||
342 | return ret; | |
343 | } | |
344 | ||
345 | /* | |
74d81a6c | 346 | * Send a message accompanied by fd(s) over a unix socket. |
67c5b804 | 347 | * |
74d81a6c | 348 | * Returns the size of data sent, or negative error value. |
67c5b804 | 349 | */ |
74d81a6c | 350 | ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
67c5b804 | 351 | { |
913b87f1 | 352 | struct msghdr msg; |
67c5b804 MD |
353 | struct cmsghdr *cmptr; |
354 | struct iovec iov[1]; | |
355 | ssize_t ret = -1; | |
356 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
357 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
74d81a6c | 358 | char dummy = 0; |
67c5b804 | 359 | |
913b87f1 | 360 | memset(&msg, 0, sizeof(msg)); |
74d81a6c | 361 | memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char)); |
913b87f1 | 362 | |
74d81a6c MD |
363 | if (nb_fd > USTCOMM_MAX_SEND_FDS) |
364 | return -EINVAL; | |
67c5b804 MD |
365 | |
366 | msg.msg_control = (caddr_t)tmp; | |
367 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
368 | ||
369 | cmptr = CMSG_FIRSTHDR(&msg); | |
370 | cmptr->cmsg_level = SOL_SOCKET; | |
371 | cmptr->cmsg_type = SCM_RIGHTS; | |
372 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); | |
373 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); | |
374 | /* Sum of the length of all control messages in the buffer: */ | |
375 | msg.msg_controllen = cmptr->cmsg_len; | |
376 | ||
74d81a6c MD |
377 | iov[0].iov_base = &dummy; |
378 | iov[0].iov_len = 1; | |
67c5b804 MD |
379 | msg.msg_iov = iov; |
380 | msg.msg_iovlen = 1; | |
381 | ||
51d9d699 | 382 | do { |
74d81a6c | 383 | ret = sendmsg(sock, &msg, 0); |
51d9d699 | 384 | } while (ret < 0 && errno == EINTR); |
7bc53e94 | 385 | if (ret < 0) { |
74d81a6c MD |
386 | /* |
387 | * We consider EPIPE and ECONNRESET as expected. | |
388 | */ | |
389 | if (errno != EPIPE && errno != ECONNRESET) { | |
32ce8569 | 390 | PERROR("sendmsg"); |
74d81a6c | 391 | } |
b869b5ae MD |
392 | ret = -errno; |
393 | if (ret == -ECONNRESET) | |
394 | ret = -EPIPE; | |
74d81a6c MD |
395 | } |
396 | return ret; | |
397 | } | |
7bc53e94 | 398 | |
74d81a6c MD |
399 | /* |
400 | * Recv a message accompanied by fd(s) from a unix socket. | |
401 | * | |
402 | * Returns the size of received data, or negative error value. | |
403 | * | |
404 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
405 | * actually received in nb_fd. | |
406 | * Returns -EPIPE on orderly shutdown. | |
407 | */ | |
408 | ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) | |
409 | { | |
410 | struct iovec iov[1]; | |
411 | ssize_t ret = 0; | |
412 | struct cmsghdr *cmsg; | |
413 | size_t sizeof_fds = nb_fd * sizeof(int); | |
414 | char recv_fd[CMSG_SPACE(sizeof_fds)]; | |
415 | struct msghdr msg; | |
416 | char dummy; | |
7bc53e94 | 417 | |
74d81a6c | 418 | memset(&msg, 0, sizeof(msg)); |
67c5b804 | 419 | |
74d81a6c MD |
420 | /* Prepare to receive the structures */ |
421 | iov[0].iov_base = &dummy; | |
422 | iov[0].iov_len = 1; | |
423 | msg.msg_iov = iov; | |
424 | msg.msg_iovlen = 1; | |
425 | msg.msg_control = recv_fd; | |
426 | msg.msg_controllen = sizeof(recv_fd); | |
427 | ||
428 | do { | |
429 | ret = recvmsg(sock, &msg, 0); | |
430 | } while (ret < 0 && errno == EINTR); | |
431 | if (ret < 0) { | |
432 | if (errno != EPIPE && errno != ECONNRESET) { | |
32ce8569 | 433 | PERROR("recvmsg fds"); |
74d81a6c | 434 | } |
8cf811d3 | 435 | ret = -errno; |
b869b5ae MD |
436 | if (ret == -ECONNRESET) |
437 | ret = -EPIPE; | |
74d81a6c MD |
438 | goto end; |
439 | } | |
440 | if (ret == 0) { | |
441 | /* orderly shutdown */ | |
442 | ret = -EPIPE; | |
443 | goto end; | |
444 | } | |
445 | if (ret != 1) { | |
32ce8569 | 446 | ERR("Error: Received %zd bytes, expected %d\n", |
74d81a6c MD |
447 | ret, 1); |
448 | goto end; | |
449 | } | |
450 | if (msg.msg_flags & MSG_CTRUNC) { | |
32ce8569 | 451 | ERR("Error: Control message truncated.\n"); |
74d81a6c MD |
452 | ret = -1; |
453 | goto end; | |
454 | } | |
455 | cmsg = CMSG_FIRSTHDR(&msg); | |
456 | if (!cmsg) { | |
32ce8569 | 457 | ERR("Error: Invalid control message header\n"); |
74d81a6c MD |
458 | ret = -1; |
459 | goto end; | |
460 | } | |
461 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { | |
32ce8569 | 462 | ERR("Didn't received any fd\n"); |
74d81a6c MD |
463 | ret = -1; |
464 | goto end; | |
465 | } | |
466 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { | |
32ce8569 | 467 | ERR("Error: Received %zu bytes of ancillary data, expected %zu\n", |
74d81a6c MD |
468 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); |
469 | ret = -1; | |
470 | goto end; | |
471 | } | |
472 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); | |
473 | ret = sizeof_fds; | |
474 | end: | |
67c5b804 MD |
475 | return ret; |
476 | } | |
57773204 MD |
477 | |
478 | int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum) | |
479 | { | |
480 | ssize_t len; | |
481 | ||
482 | len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum)); | |
483 | switch (len) { | |
484 | case sizeof(*lum): | |
57773204 | 485 | break; |
57773204 | 486 | default: |
7bc53e94 | 487 | if (len < 0) { |
7bc53e94 MD |
488 | return len; |
489 | } else { | |
32ce8569 | 490 | ERR("incorrect message size: %zd\n", len); |
7bc53e94 MD |
491 | return -EINVAL; |
492 | } | |
57773204 MD |
493 | } |
494 | return 0; | |
495 | } | |
496 | ||
497 | int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur, | |
498 | uint32_t expected_handle, uint32_t expected_cmd) | |
499 | { | |
500 | ssize_t len; | |
501 | ||
502 | memset(lur, 0, sizeof(*lur)); | |
503 | len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur)); | |
504 | switch (len) { | |
505 | case 0: /* orderly shutdown */ | |
74d81a6c | 506 | return -EPIPE; |
57773204 | 507 | case sizeof(*lur): |
57773204 | 508 | if (lur->handle != expected_handle) { |
32ce8569 | 509 | ERR("Unexpected result message handle: " |
74d81a6c MD |
510 | "expected: %u vs received: %u\n", |
511 | expected_handle, lur->handle); | |
57773204 MD |
512 | return -EINVAL; |
513 | } | |
57773204 | 514 | if (lur->cmd != expected_cmd) { |
32ce8569 | 515 | ERR("Unexpected result message command " |
74d81a6c MD |
516 | "expected: %u vs received: %u\n", |
517 | expected_cmd, lur->cmd); | |
57773204 MD |
518 | return -EINVAL; |
519 | } | |
7bc53e94 | 520 | return lur->ret_code; |
57773204 | 521 | default: |
8cf811d3 | 522 | if (len >= 0) { |
32ce8569 | 523 | ERR("incorrect message size: %zd\n", len); |
7bc53e94 | 524 | } |
8cf811d3 | 525 | return len; |
57773204 MD |
526 | } |
527 | } | |
528 | ||
529 | int ustcomm_send_app_cmd(int sock, | |
530 | struct ustcomm_ust_msg *lum, | |
531 | struct ustcomm_ust_reply *lur) | |
532 | { | |
533 | int ret; | |
534 | ||
535 | ret = ustcomm_send_app_msg(sock, lum); | |
536 | if (ret) | |
537 | return ret; | |
c354a72c MD |
538 | ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd); |
539 | if (ret > 0) | |
540 | return -EIO; | |
541 | return ret; | |
57773204 MD |
542 | } |
543 | ||
57773204 | 544 | /* |
74d81a6c MD |
545 | * chan_data is allocated internally if this function returns the |
546 | * expected var_len. | |
57773204 | 547 | */ |
74d81a6c | 548 | ssize_t ustcomm_recv_channel_from_sessiond(int sock, |
ff0f5728 MD |
549 | void **_chan_data, uint64_t var_len, |
550 | int *_wakeup_fd) | |
57773204 | 551 | { |
74d81a6c | 552 | void *chan_data; |
ff0f5728 MD |
553 | ssize_t len, nr_fd; |
554 | int wakeup_fd; | |
57773204 | 555 | |
74d81a6c MD |
556 | if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) { |
557 | len = -EINVAL; | |
558 | goto error_check; | |
57773204 | 559 | } |
74d81a6c MD |
560 | /* Receive variable length data */ |
561 | chan_data = zmalloc(var_len); | |
562 | if (!chan_data) { | |
563 | len = -ENOMEM; | |
564 | goto error_alloc; | |
57773204 | 565 | } |
74d81a6c MD |
566 | len = ustcomm_recv_unix_sock(sock, chan_data, var_len); |
567 | if (len != var_len) { | |
568 | goto error_recv; | |
57773204 | 569 | } |
ff0f5728 MD |
570 | /* recv wakeup fd */ |
571 | nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1); | |
572 | if (nr_fd <= 0) { | |
573 | if (nr_fd < 0) { | |
574 | len = nr_fd; | |
575 | goto error_recv; | |
576 | } else { | |
577 | len = -EIO; | |
578 | goto error_recv; | |
579 | } | |
580 | } | |
581 | *_wakeup_fd = wakeup_fd; | |
74d81a6c MD |
582 | *_chan_data = chan_data; |
583 | return len; | |
584 | ||
585 | error_recv: | |
586 | free(chan_data); | |
587 | error_alloc: | |
588 | error_check: | |
589 | return len; | |
590 | } | |
7bc53e94 | 591 | |
74d81a6c MD |
592 | int ustcomm_recv_stream_from_sessiond(int sock, |
593 | uint64_t *memory_map_size, | |
594 | int *shm_fd, int *wakeup_fd) | |
595 | { | |
596 | ssize_t len; | |
597 | int ret; | |
598 | int fds[2]; | |
599 | ||
600 | /* recv shm fd and wakeup fd */ | |
601 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); | |
602 | if (len <= 0) { | |
603 | if (len < 0) { | |
604 | ret = len; | |
605 | goto error; | |
606 | } else { | |
607 | ret = -EIO; | |
608 | goto error; | |
609 | } | |
7bc53e94 | 610 | } |
74d81a6c MD |
611 | *shm_fd = fds[0]; |
612 | *wakeup_fd = fds[1]; | |
613 | return 0; | |
614 | ||
615 | error: | |
57773204 MD |
616 | return ret; |
617 | } | |
32ce8569 MD |
618 | |
619 | /* | |
620 | * Returns 0 on success, negative error value on error. | |
621 | */ | |
622 | int ustcomm_send_reg_msg(int sock, | |
623 | enum ustctl_socket_type type, | |
624 | uint32_t bits_per_long, | |
625 | uint32_t uint8_t_alignment, | |
626 | uint32_t uint16_t_alignment, | |
627 | uint32_t uint32_t_alignment, | |
628 | uint32_t uint64_t_alignment, | |
629 | uint32_t long_alignment) | |
630 | { | |
631 | ssize_t len; | |
632 | struct ustctl_reg_msg reg_msg; | |
633 | ||
634 | reg_msg.magic = LTTNG_UST_COMM_MAGIC; | |
635 | reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION; | |
636 | reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION; | |
637 | reg_msg.pid = getpid(); | |
638 | reg_msg.ppid = getppid(); | |
639 | reg_msg.uid = getuid(); | |
640 | reg_msg.gid = getgid(); | |
641 | reg_msg.bits_per_long = bits_per_long; | |
642 | reg_msg.uint8_t_alignment = uint8_t_alignment; | |
643 | reg_msg.uint16_t_alignment = uint16_t_alignment; | |
644 | reg_msg.uint32_t_alignment = uint32_t_alignment; | |
645 | reg_msg.uint64_t_alignment = uint64_t_alignment; | |
646 | reg_msg.long_alignment = long_alignment; | |
647 | reg_msg.socket_type = type; | |
648 | lttng_ust_getprocname(reg_msg.name); | |
649 | memset(reg_msg.padding, 0, sizeof(reg_msg.padding)); | |
650 | ||
651 | len = ustcomm_send_unix_sock(sock, ®_msg, sizeof(reg_msg)); | |
652 | if (len > 0 && len != sizeof(reg_msg)) | |
653 | return -EIO; | |
654 | if (len < 0) | |
655 | return len; | |
656 | return 0; | |
657 | } | |
658 | ||
7f2348b8 MD |
659 | static |
660 | int serialize_string_encoding(enum ustctl_string_encodings *ue, | |
661 | enum lttng_string_encodings le) | |
662 | { | |
663 | switch (le) { | |
664 | case lttng_encode_none: | |
665 | *ue = ustctl_encode_none; | |
666 | break; | |
667 | case lttng_encode_UTF8: | |
668 | *ue = ustctl_encode_UTF8; | |
669 | break; | |
670 | case lttng_encode_ASCII: | |
671 | *ue = ustctl_encode_ASCII; | |
672 | break; | |
673 | default: | |
674 | return -EINVAL; | |
675 | } | |
676 | return 0; | |
677 | } | |
678 | ||
32ce8569 | 679 | static |
2b213b16 MD |
680 | int serialize_basic_type(enum ustctl_abstract_types *uatype, |
681 | enum lttng_abstract_types atype, | |
32ce8569 MD |
682 | union _ustctl_basic_type *ubt, |
683 | const union _lttng_basic_type *lbt) | |
684 | { | |
685 | switch (atype) { | |
686 | case atype_integer: | |
687 | { | |
688 | struct ustctl_integer_type *uit; | |
689 | const struct lttng_integer_type *lit; | |
690 | ||
691 | uit = &ubt->integer; | |
692 | lit = &lbt->integer; | |
693 | uit->size = lit->size; | |
694 | uit->signedness = lit->signedness; | |
695 | uit->reverse_byte_order = lit->reverse_byte_order; | |
696 | uit->base = lit->base; | |
7f2348b8 MD |
697 | if (serialize_string_encoding(&uit->encoding, lit->encoding)) |
698 | return -EINVAL; | |
32ce8569 | 699 | uit->alignment = lit->alignment; |
2b213b16 | 700 | *uatype = ustctl_atype_integer; |
32ce8569 MD |
701 | break; |
702 | } | |
703 | case atype_string: | |
704 | { | |
7f2348b8 MD |
705 | if (serialize_string_encoding(&ubt->string.encoding, |
706 | lbt->string.encoding)) | |
707 | return -EINVAL; | |
2b213b16 | 708 | *uatype = ustctl_atype_string; |
32ce8569 MD |
709 | break; |
710 | } | |
711 | case atype_float: | |
712 | { | |
713 | struct ustctl_float_type *uft; | |
714 | const struct lttng_float_type *lft; | |
715 | ||
716 | uft = &ubt->_float; | |
717 | lft = &lbt->_float; | |
718 | uft->exp_dig = lft->exp_dig; | |
719 | uft->mant_dig = lft->mant_dig; | |
720 | uft->alignment = lft->alignment; | |
721 | uft->reverse_byte_order = lft->reverse_byte_order; | |
2b213b16 | 722 | *uatype = ustctl_atype_float; |
32ce8569 MD |
723 | break; |
724 | } | |
725 | case atype_enum: | |
726 | case atype_array: | |
727 | case atype_sequence: | |
728 | default: | |
729 | return -EINVAL; | |
730 | } | |
731 | return 0; | |
32ce8569 MD |
732 | } |
733 | ||
734 | static | |
735 | int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt) | |
736 | { | |
737 | int ret; | |
738 | ||
739 | switch (lt->atype) { | |
740 | case atype_integer: | |
741 | case atype_float: | |
742 | case atype_string: | |
2b213b16 | 743 | ret = serialize_basic_type(&ut->atype, lt->atype, |
32ce8569 MD |
744 | &ut->u.basic, <->u.basic); |
745 | if (ret) | |
746 | return ret; | |
747 | break; | |
748 | case atype_array: | |
749 | { | |
750 | struct ustctl_basic_type *ubt; | |
751 | const struct lttng_basic_type *lbt; | |
752 | int ret; | |
753 | ||
754 | ubt = &ut->u.array.elem_type; | |
755 | lbt = <->u.array.elem_type; | |
756 | ut->u.array.length = lt->u.array.length; | |
2b213b16 | 757 | ret = serialize_basic_type(&ubt->atype, lbt->atype, |
32ce8569 MD |
758 | &ubt->u.basic, &lbt->u.basic); |
759 | if (ret) | |
760 | return -EINVAL; | |
2b213b16 | 761 | ut->atype = ustctl_atype_array; |
32ce8569 MD |
762 | break; |
763 | } | |
764 | case atype_sequence: | |
765 | { | |
766 | struct ustctl_basic_type *ubt; | |
767 | const struct lttng_basic_type *lbt; | |
768 | int ret; | |
769 | ||
770 | ubt = &ut->u.sequence.length_type; | |
771 | lbt = <->u.sequence.length_type; | |
2b213b16 | 772 | ret = serialize_basic_type(&ubt->atype, lbt->atype, |
32ce8569 MD |
773 | &ubt->u.basic, &lbt->u.basic); |
774 | if (ret) | |
775 | return -EINVAL; | |
776 | ubt = &ut->u.sequence.elem_type; | |
777 | lbt = <->u.sequence.elem_type; | |
2b213b16 | 778 | ret = serialize_basic_type(&ubt->atype, lbt->atype, |
32ce8569 MD |
779 | &ubt->u.basic, &lbt->u.basic); |
780 | if (ret) | |
781 | return -EINVAL; | |
2b213b16 | 782 | ut->atype = ustctl_atype_sequence; |
32ce8569 MD |
783 | break; |
784 | } | |
785 | case atype_enum: | |
786 | default: | |
787 | return -EINVAL; | |
788 | } | |
789 | return 0; | |
790 | } | |
791 | ||
792 | static | |
793 | int serialize_fields(size_t *_nr_write_fields, | |
794 | struct ustctl_field **ustctl_fields, | |
795 | size_t nr_fields, | |
796 | const struct lttng_event_field *lttng_fields) | |
797 | { | |
798 | struct ustctl_field *fields; | |
799 | int i, ret; | |
800 | size_t nr_write_fields = 0; | |
801 | ||
802 | fields = zmalloc(nr_fields * sizeof(*fields)); | |
803 | if (!fields) | |
804 | return -ENOMEM; | |
805 | ||
806 | for (i = 0; i < nr_fields; i++) { | |
807 | struct ustctl_field *f; | |
808 | const struct lttng_event_field *lf; | |
809 | ||
810 | f = &fields[nr_write_fields]; | |
811 | lf = <tng_fields[i]; | |
812 | ||
813 | /* skip 'nowrite' fields */ | |
814 | if (lf->nowrite) | |
815 | continue; | |
816 | strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN); | |
817 | f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
818 | ret = serialize_one_type(&f->type, &lf->type); | |
819 | if (ret) | |
820 | goto error_type; | |
821 | nr_write_fields++; | |
822 | } | |
823 | ||
824 | *_nr_write_fields = nr_write_fields; | |
825 | *ustctl_fields = fields; | |
826 | return 0; | |
827 | ||
828 | error_type: | |
829 | free(fields); | |
830 | return ret; | |
831 | } | |
832 | ||
83e43212 MD |
833 | static |
834 | int serialize_ctx_fields(size_t *_nr_write_fields, | |
835 | struct ustctl_field **ustctl_fields, | |
836 | size_t nr_fields, | |
837 | const struct lttng_ctx_field *lttng_fields) | |
838 | { | |
839 | struct ustctl_field *fields; | |
840 | int i, ret; | |
841 | size_t nr_write_fields = 0; | |
842 | ||
843 | fields = zmalloc(nr_fields * sizeof(*fields)); | |
844 | if (!fields) | |
845 | return -ENOMEM; | |
846 | ||
847 | for (i = 0; i < nr_fields; i++) { | |
848 | struct ustctl_field *f; | |
849 | const struct lttng_event_field *lf; | |
850 | ||
851 | f = &fields[nr_write_fields]; | |
852 | lf = <tng_fields[i].event_field; | |
853 | ||
854 | /* skip 'nowrite' fields */ | |
855 | if (lf->nowrite) | |
856 | continue; | |
857 | strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN); | |
858 | f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
859 | ret = serialize_one_type(&f->type, &lf->type); | |
860 | if (ret) | |
861 | goto error_type; | |
862 | nr_write_fields++; | |
863 | } | |
864 | ||
865 | *_nr_write_fields = nr_write_fields; | |
866 | *ustctl_fields = fields; | |
867 | return 0; | |
868 | ||
869 | error_type: | |
870 | free(fields); | |
871 | return ret; | |
872 | } | |
873 | ||
32ce8569 MD |
874 | /* |
875 | * Returns 0 on success, negative error value on error. | |
876 | */ | |
877 | int ustcomm_register_event(int sock, | |
878 | int session_objd, /* session descriptor */ | |
879 | int channel_objd, /* channel descriptor */ | |
880 | const char *event_name, /* event name (input) */ | |
881 | int loglevel, | |
882 | const char *signature, /* event signature (input) */ | |
883 | size_t nr_fields, /* fields */ | |
884 | const struct lttng_event_field *lttng_fields, | |
885 | const char *model_emf_uri, | |
886 | uint32_t *id) /* event id (output) */ | |
887 | { | |
888 | ssize_t len; | |
889 | struct { | |
890 | struct ustcomm_notify_hdr header; | |
891 | struct ustcomm_notify_event_msg m; | |
892 | } msg; | |
893 | struct { | |
894 | struct ustcomm_notify_hdr header; | |
895 | struct ustcomm_notify_event_reply r; | |
896 | } reply; | |
897 | size_t signature_len, fields_len, model_emf_uri_len; | |
3bf32af0 | 898 | struct ustctl_field *fields = NULL; |
32ce8569 MD |
899 | size_t nr_write_fields = 0; |
900 | int ret; | |
901 | ||
902 | memset(&msg, 0, sizeof(msg)); | |
903 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; | |
904 | msg.m.session_objd = session_objd; | |
905 | msg.m.channel_objd = channel_objd; | |
906 | strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN); | |
907 | msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
908 | msg.m.loglevel = loglevel; | |
909 | signature_len = strlen(signature) + 1; | |
910 | msg.m.signature_len = signature_len; | |
911 | ||
912 | /* Calculate fields len, serialize fields. */ | |
913 | if (nr_fields > 0) { | |
914 | ret = serialize_fields(&nr_write_fields, &fields, | |
915 | nr_fields, lttng_fields); | |
916 | if (ret) | |
917 | return ret; | |
918 | } | |
919 | ||
920 | fields_len = sizeof(*fields) * nr_write_fields; | |
921 | msg.m.fields_len = fields_len; | |
922 | if (model_emf_uri) { | |
923 | model_emf_uri_len = strlen(model_emf_uri) + 1; | |
924 | } else { | |
925 | model_emf_uri_len = 0; | |
926 | } | |
927 | msg.m.model_emf_uri_len = model_emf_uri_len; | |
928 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
929 | if (len > 0 && len != sizeof(msg)) { | |
930 | free(fields); | |
931 | return -EIO; | |
932 | } | |
933 | if (len < 0) { | |
934 | free(fields); | |
935 | return len; | |
936 | } | |
937 | ||
938 | /* send signature */ | |
939 | len = ustcomm_send_unix_sock(sock, signature, signature_len); | |
940 | if (len > 0 && len != signature_len) { | |
941 | free(fields); | |
942 | return -EIO; | |
943 | } | |
944 | if (len < 0) { | |
945 | free(fields); | |
946 | return len; | |
947 | } | |
948 | ||
949 | /* send fields */ | |
950 | if (fields_len > 0) { | |
951 | len = ustcomm_send_unix_sock(sock, fields, fields_len); | |
952 | free(fields); | |
953 | if (len > 0 && len != fields_len) { | |
954 | return -EIO; | |
955 | } | |
956 | if (len < 0) { | |
957 | return len; | |
958 | } | |
959 | } | |
960 | ||
961 | if (model_emf_uri_len) { | |
962 | /* send model_emf_uri */ | |
963 | len = ustcomm_send_unix_sock(sock, model_emf_uri, | |
964 | model_emf_uri_len); | |
965 | if (len > 0 && len != model_emf_uri_len) | |
966 | return -EIO; | |
967 | if (len < 0) | |
968 | return len; | |
969 | } | |
970 | ||
971 | /* receive reply */ | |
972 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); | |
973 | switch (len) { | |
974 | case 0: /* orderly shutdown */ | |
975 | return -EPIPE; | |
976 | case sizeof(reply): | |
977 | if (reply.header.notify_cmd != msg.header.notify_cmd) { | |
978 | ERR("Unexpected result message command " | |
979 | "expected: %u vs received: %u\n", | |
980 | msg.header.notify_cmd, reply.header.notify_cmd); | |
981 | return -EINVAL; | |
982 | } | |
983 | if (reply.r.ret_code > 0) | |
984 | return -EINVAL; | |
985 | if (reply.r.ret_code < 0) | |
986 | return reply.r.ret_code; | |
987 | *id = reply.r.event_id; | |
988 | DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n", | |
989 | event_name, reply.r.ret_code, reply.r.event_id); | |
990 | return 0; | |
991 | default: | |
992 | if (len < 0) { | |
993 | /* Transport level error */ | |
994 | if (errno == EPIPE || errno == ECONNRESET) | |
995 | len = -errno; | |
996 | return len; | |
997 | } else { | |
998 | ERR("incorrect message size: %zd\n", len); | |
999 | return len; | |
1000 | } | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | /* | |
1005 | * Returns 0 on success, negative error value on error. | |
1006 | * Returns -EPIPE or -ECONNRESET if other end has hung up. | |
1007 | */ | |
1008 | int ustcomm_register_channel(int sock, | |
1009 | int session_objd, /* session descriptor */ | |
1010 | int channel_objd, /* channel descriptor */ | |
1011 | size_t nr_ctx_fields, | |
83e43212 | 1012 | const struct lttng_ctx_field *ctx_fields, |
32ce8569 MD |
1013 | uint32_t *chan_id, /* channel id (output) */ |
1014 | int *header_type) /* header type (output) */ | |
1015 | { | |
1016 | ssize_t len; | |
1017 | struct { | |
1018 | struct ustcomm_notify_hdr header; | |
1019 | struct ustcomm_notify_channel_msg m; | |
1020 | } msg; | |
1021 | struct { | |
1022 | struct ustcomm_notify_hdr header; | |
1023 | struct ustcomm_notify_channel_reply r; | |
1024 | } reply; | |
1025 | size_t fields_len; | |
83e43212 | 1026 | struct ustctl_field *fields = NULL; |
32ce8569 MD |
1027 | int ret; |
1028 | size_t nr_write_fields = 0; | |
1029 | ||
1030 | memset(&msg, 0, sizeof(msg)); | |
1031 | msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; | |
1032 | msg.m.session_objd = session_objd; | |
1033 | msg.m.channel_objd = channel_objd; | |
1034 | ||
1035 | /* Calculate fields len, serialize fields. */ | |
1036 | if (nr_ctx_fields > 0) { | |
83e43212 | 1037 | ret = serialize_ctx_fields(&nr_write_fields, &fields, |
32ce8569 MD |
1038 | nr_ctx_fields, ctx_fields); |
1039 | if (ret) | |
1040 | return ret; | |
1041 | } | |
1042 | ||
1043 | fields_len = sizeof(*fields) * nr_write_fields; | |
1044 | msg.m.ctx_fields_len = fields_len; | |
1045 | len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg)); | |
1046 | if (len > 0 && len != sizeof(msg)) { | |
1047 | free(fields); | |
1048 | return -EIO; | |
1049 | } | |
1050 | if (len < 0) { | |
1051 | free(fields); | |
1052 | return len; | |
1053 | } | |
1054 | ||
1055 | /* send fields */ | |
1056 | if (fields_len > 0) { | |
1057 | len = ustcomm_send_unix_sock(sock, fields, fields_len); | |
1058 | free(fields); | |
1059 | if (len > 0 && len != fields_len) { | |
1060 | return -EIO; | |
1061 | } | |
1062 | if (len < 0) { | |
1063 | return len; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply)); | |
1068 | switch (len) { | |
1069 | case 0: /* orderly shutdown */ | |
1070 | return -EPIPE; | |
1071 | case sizeof(reply): | |
1072 | if (reply.header.notify_cmd != msg.header.notify_cmd) { | |
1073 | ERR("Unexpected result message command " | |
1074 | "expected: %u vs received: %u\n", | |
1075 | msg.header.notify_cmd, reply.header.notify_cmd); | |
1076 | return -EINVAL; | |
1077 | } | |
1078 | if (reply.r.ret_code > 0) | |
1079 | return -EINVAL; | |
1080 | if (reply.r.ret_code < 0) | |
1081 | return reply.r.ret_code; | |
1082 | *chan_id = reply.r.chan_id; | |
1083 | switch (reply.r.header_type) { | |
1084 | case 1: | |
1085 | case 2: | |
1086 | *header_type = reply.r.header_type; | |
1087 | break; | |
1088 | default: | |
1089 | ERR("Unexpected channel header type %u\n", | |
1090 | reply.r.header_type); | |
1091 | return -EINVAL; | |
1092 | } | |
1093 | DBG("Sent register channel notification: chan_id %d, header_type %d\n", | |
1094 | reply.r.chan_id, reply.r.header_type); | |
1095 | return 0; | |
1096 | default: | |
1097 | if (len < 0) { | |
1098 | /* Transport level error */ | |
1099 | if (errno == EPIPE || errno == ECONNRESET) | |
1100 | len = -errno; | |
1101 | return len; | |
1102 | } else { | |
1103 | ERR("incorrect message size: %zd\n", len); | |
1104 | return len; | |
1105 | } | |
1106 | } | |
1107 | } | |
ff517991 MD |
1108 | |
1109 | /* | |
1110 | * Set socket reciving timeout. | |
1111 | */ | |
1112 | int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec) | |
1113 | { | |
1114 | int ret; | |
1115 | struct timeval tv; | |
1116 | ||
1117 | tv.tv_sec = msec / 1000; | |
1118 | tv.tv_usec = (msec * 1000 % 1000000); | |
1119 | ||
1120 | ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
1121 | if (ret < 0) { | |
1122 | PERROR("setsockopt SO_RCVTIMEO"); | |
1123 | ret = -errno; | |
1124 | } | |
1125 | ||
1126 | return ret; | |
1127 | } | |
1128 | ||
1129 | /* | |
1130 | * Set socket sending timeout. | |
1131 | */ | |
1132 | int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec) | |
1133 | { | |
1134 | int ret; | |
1135 | struct timeval tv; | |
1136 | ||
1137 | tv.tv_sec = msec / 1000; | |
1138 | tv.tv_usec = (msec * 1000) % 1000000; | |
1139 | ||
1140 | ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); | |
1141 | if (ret < 0) { | |
1142 | PERROR("setsockopt SO_SNDTIMEO"); | |
1143 | ret = -errno; | |
1144 | } | |
1145 | ||
1146 | return ret; | |
1147 | } |