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