Commit | Line | Data |
---|---|---|
67c5b804 MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
15f672f9 | 3 | * Copyright (C) 2011 - 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> |
67c5b804 | 32 | |
4318ae1b | 33 | #include <lttng/ust-comm.h> |
67c5b804 MD |
34 | |
35 | /* | |
36 | * Human readable error message. | |
37 | */ | |
57773204 MD |
38 | static const char *ustcomm_readable_code[] = { |
39 | [ USTCOMM_ERR_INDEX(USTCOMM_ERR) ] = "Unknown error", | |
40 | [ USTCOMM_ERR_INDEX(USTCOMM_UND) ] = "Undefined command", | |
41 | [ USTCOMM_ERR_INDEX(USTCOMM_NOT_IMPLEMENTED) ] = "Not implemented", | |
42 | [ USTCOMM_ERR_INDEX(USTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain", | |
43 | [ USTCOMM_ERR_INDEX(USTCOMM_NO_SESSION) ] = "No session found", | |
44 | [ USTCOMM_ERR_INDEX(USTCOMM_LIST_FAIL) ] = "Unable to list traceable apps", | |
45 | [ USTCOMM_ERR_INDEX(USTCOMM_NO_APPS) ] = "No traceable apps found", | |
46 | [ USTCOMM_ERR_INDEX(USTCOMM_SESS_NOT_FOUND) ] = "Session name not found", | |
47 | [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACE) ] = "No trace found", | |
48 | [ USTCOMM_ERR_INDEX(USTCOMM_FATAL) ] = "Fatal error of the session daemon", | |
49 | [ USTCOMM_ERR_INDEX(USTCOMM_CREATE_FAIL) ] = "Create trace failed", | |
50 | [ USTCOMM_ERR_INDEX(USTCOMM_START_FAIL) ] = "Start trace failed", | |
51 | [ USTCOMM_ERR_INDEX(USTCOMM_STOP_FAIL) ] = "Stop trace failed", | |
52 | [ USTCOMM_ERR_INDEX(USTCOMM_NO_TRACEABLE) ] = "App is not traceable", | |
53 | [ USTCOMM_ERR_INDEX(USTCOMM_SELECT_SESS) ] = "A session MUST be selected", | |
54 | [ USTCOMM_ERR_INDEX(USTCOMM_EXIST_SESS) ] = "Session name already exist", | |
55 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NA) ] = "UST tracer not available", | |
56 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_EVENT_EXIST) ] = "UST event already exists", | |
57 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_SESS_FAIL) ] = "UST create session failed", | |
58 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_FAIL) ] = "UST create channel failed", | |
59 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_NOT_FOUND) ] = "UST channel not found", | |
60 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed", | |
61 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed", | |
62 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONTEXT_FAIL) ] = "Add UST context failed", | |
63 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_ENABLE_FAIL) ] = "Enable UST event failed", | |
64 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DISABLE_FAIL) ] = "Disable UST event failed", | |
65 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_META_FAIL) ] = "Opening metadata failed", | |
66 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_START_FAIL) ] = "Starting UST trace failed", | |
67 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STOP_FAIL) ] = "Stoping UST trace failed", | |
68 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_CONSUMER_FAIL) ] = "UST consumer start failed", | |
69 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_STREAM_FAIL) ] = "UST create stream failed", | |
70 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_FAIL) ] = "UST trace directory creation failed", | |
71 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_DIR_EXIST) ] = "UST trace directory already exist", | |
72 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_NO_SESSION) ] = "No UST session found", | |
73 | [ USTCOMM_ERR_INDEX(USTCOMM_KERN_LIST_FAIL) ] = "Listing UST events failed", | |
74 | [ USTCOMM_ERR_INDEX(USTCONSUMER_COMMAND_SOCK_READY) ] = "UST consumer command socket ready", | |
75 | [ USTCOMM_ERR_INDEX(USTCONSUMER_SUCCESS_RECV_FD) ] = "UST consumer success on receiving fds", | |
76 | [ USTCOMM_ERR_INDEX(USTCONSUMER_ERROR_RECV_FD) ] = "UST consumer error on receiving fds", | |
77 | [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_ERROR) ] = "UST consumer error in polling thread", | |
78 | [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_NVAL) ] = "UST consumer polling on closed fd", | |
79 | [ USTCOMM_ERR_INDEX(USTCONSUMER_POLL_HUP) ] = "UST consumer all fd hung up", | |
80 | [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_SUCCESS) ] = "UST consumer exiting normally", | |
81 | [ USTCOMM_ERR_INDEX(USTCONSUMER_EXIT_FAILURE) ] = "UST consumer exiting on error", | |
82 | [ USTCOMM_ERR_INDEX(USTCONSUMER_OUTFD_ERROR) ] = "UST consumer error opening the tracefile", | |
83 | [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EBADF) ] = "UST consumer splice EBADF", | |
84 | [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_EINVAL) ] = "UST consumer splice EINVAL", | |
85 | [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ENOMEM) ] = "UST consumer splice ENOMEM", | |
86 | [ USTCOMM_ERR_INDEX(USTCONSUMER_SPLICE_ESPIPE) ] = "UST consumer splice ESPIPE", | |
87 | [ USTCOMM_ERR_INDEX(USTCOMM_NO_EVENT) ] = "Event not found", | |
67c5b804 MD |
88 | }; |
89 | ||
90 | /* | |
91 | * lttcom_get_readable_code | |
92 | * | |
93 | * Return ptr to string representing a human readable | |
57773204 | 94 | * error code from the ustcomm_return_code enum. |
67c5b804 | 95 | */ |
57773204 | 96 | const char *ustcomm_get_readable_code(int code) |
67c5b804 | 97 | { |
57773204 MD |
98 | if (code == USTCOMM_OK) { |
99 | return "Success"; | |
67c5b804 | 100 | } |
57773204 MD |
101 | if (code >= USTCOMM_ERR && code < USTCOMM_NR) { |
102 | return ustcomm_readable_code[USTCOMM_ERR_INDEX(code)]; | |
103 | } | |
104 | return strerror(code); | |
67c5b804 MD |
105 | } |
106 | ||
107 | /* | |
57773204 | 108 | * ustcomm_connect_unix_sock |
67c5b804 MD |
109 | * |
110 | * Connect to unix socket using the path name. | |
111 | */ | |
57773204 | 112 | int ustcomm_connect_unix_sock(const char *pathname) |
67c5b804 MD |
113 | { |
114 | struct sockaddr_un sun; | |
115 | int fd; | |
116 | int ret; | |
117 | ||
204d45df MD |
118 | /* |
119 | * libust threads require the close-on-exec flag for all | |
120 | * resources so it does not leak file descriptors upon exec. | |
121 | */ | |
122 | fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); | |
67c5b804 MD |
123 | if (fd < 0) { |
124 | perror("socket"); | |
125 | ret = fd; | |
126 | goto error; | |
127 | } | |
128 | ||
129 | memset(&sun, 0, sizeof(sun)); | |
130 | sun.sun_family = AF_UNIX; | |
131 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
132 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
133 | ||
134 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); | |
135 | if (ret < 0) { | |
136 | /* | |
137 | * Don't print message on connect error, because connect | |
138 | * is used in normal execution to detect if sessiond is | |
139 | * alive. | |
140 | */ | |
141 | goto error_connect; | |
142 | } | |
143 | ||
144 | return fd; | |
145 | ||
146 | error_connect: | |
147 | close(fd); | |
148 | error: | |
149 | return ret; | |
150 | } | |
151 | ||
152 | /* | |
57773204 | 153 | * ustcomm_accept_unix_sock |
67c5b804 MD |
154 | * |
155 | * Do an accept(2) on the sock and return the | |
156 | * new file descriptor. The socket MUST be bind(2) before. | |
157 | */ | |
57773204 | 158 | int ustcomm_accept_unix_sock(int sock) |
67c5b804 MD |
159 | { |
160 | int new_fd; | |
161 | struct sockaddr_un sun; | |
162 | socklen_t len = 0; | |
163 | ||
164 | /* Blocking call */ | |
165 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); | |
166 | if (new_fd < 0) { | |
167 | perror("accept"); | |
168 | goto error; | |
169 | } | |
170 | ||
171 | return new_fd; | |
172 | ||
173 | error: | |
174 | return -1; | |
175 | } | |
176 | ||
177 | /* | |
57773204 | 178 | * ustcomm_create_unix_sock |
67c5b804 MD |
179 | * |
180 | * Creates a AF_UNIX local socket using pathname | |
181 | * bind the socket upon creation and return the fd. | |
182 | */ | |
57773204 | 183 | int ustcomm_create_unix_sock(const char *pathname) |
67c5b804 MD |
184 | { |
185 | struct sockaddr_un sun; | |
186 | int fd; | |
187 | int ret = -1; | |
188 | ||
189 | /* Create server socket */ | |
190 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
191 | perror("socket"); | |
192 | goto error; | |
193 | } | |
194 | ||
195 | memset(&sun, 0, sizeof(sun)); | |
196 | sun.sun_family = AF_UNIX; | |
197 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
198 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
199 | ||
200 | /* Unlink the old file if present */ | |
201 | (void) unlink(pathname); | |
202 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); | |
203 | if (ret < 0) { | |
204 | perror("bind"); | |
205 | goto error; | |
206 | } | |
207 | ||
208 | return fd; | |
209 | ||
210 | error: | |
211 | return ret; | |
212 | } | |
213 | ||
214 | /* | |
57773204 | 215 | * ustcomm_listen_unix_sock |
67c5b804 | 216 | * |
e41474be | 217 | * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN. |
67c5b804 | 218 | */ |
57773204 | 219 | int ustcomm_listen_unix_sock(int sock) |
67c5b804 MD |
220 | { |
221 | int ret; | |
222 | ||
e41474be | 223 | ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN); |
67c5b804 MD |
224 | if (ret < 0) { |
225 | perror("listen"); | |
226 | } | |
227 | ||
228 | return ret; | |
229 | } | |
230 | ||
231 | /* | |
57773204 | 232 | * ustcomm_recv_unix_sock |
67c5b804 MD |
233 | * |
234 | * Receive data of size len in put that data into | |
235 | * the buf param. Using recvmsg API. | |
236 | * Return the size of received data. | |
237 | */ | |
57773204 | 238 | ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len) |
67c5b804 MD |
239 | { |
240 | struct msghdr msg = { 0 }; | |
241 | struct iovec iov[1]; | |
242 | ssize_t ret = -1; | |
243 | ||
244 | iov[0].iov_base = buf; | |
245 | iov[0].iov_len = len; | |
246 | msg.msg_iov = iov; | |
247 | msg.msg_iovlen = 1; | |
248 | ||
249 | ret = recvmsg(sock, &msg, 0); | |
250 | if (ret < 0) { | |
251 | perror("recvmsg"); | |
252 | } | |
253 | ||
254 | return ret; | |
255 | } | |
256 | ||
257 | /* | |
57773204 | 258 | * ustcomm_send_unix_sock |
67c5b804 MD |
259 | * |
260 | * Send buf data of size len. Using sendmsg API. | |
261 | * Return the size of sent data. | |
262 | */ | |
57773204 | 263 | ssize_t ustcomm_send_unix_sock(int sock, void *buf, size_t len) |
67c5b804 MD |
264 | { |
265 | struct msghdr msg = { 0 }; | |
266 | struct iovec iov[1]; | |
267 | ssize_t ret = -1; | |
268 | ||
269 | iov[0].iov_base = buf; | |
270 | iov[0].iov_len = len; | |
271 | msg.msg_iov = iov; | |
272 | msg.msg_iovlen = 1; | |
273 | ||
1ea11eab MD |
274 | /* |
275 | * Using the MSG_NOSIGNAL when sending data from sessiond to | |
276 | * libust, so libust does not receive an unhandled SIGPIPE or | |
277 | * SIGURG. The sessiond receiver side can be made more resilient | |
278 | * by ignoring SIGPIPE, but we don't have this luxury on the | |
279 | * libust side. | |
280 | */ | |
281 | ret = sendmsg(sock, &msg, MSG_NOSIGNAL); | |
67c5b804 MD |
282 | if (ret < 0) { |
283 | perror("sendmsg"); | |
284 | } | |
285 | ||
286 | return ret; | |
287 | } | |
288 | ||
289 | /* | |
57773204 | 290 | * ustcomm_close_unix_sock |
67c5b804 MD |
291 | * |
292 | * Shutdown cleanly a unix socket. | |
293 | */ | |
57773204 | 294 | int ustcomm_close_unix_sock(int sock) |
67c5b804 MD |
295 | { |
296 | int ret; | |
297 | ||
298 | /* Shutdown receptions and transmissions */ | |
299 | ret = shutdown(sock, SHUT_RDWR); | |
300 | if (ret < 0) { | |
301 | perror("shutdown"); | |
302 | } | |
303 | ||
304 | return ret; | |
305 | } | |
306 | ||
307 | /* | |
57773204 | 308 | * ustcomm_send_fds_unix_sock |
67c5b804 MD |
309 | * |
310 | * Send multiple fds on a unix socket. | |
311 | */ | |
57773204 | 312 | ssize_t ustcomm_send_fds_unix_sock(int sock, void *buf, int *fds, size_t nb_fd, size_t len) |
67c5b804 MD |
313 | { |
314 | struct msghdr msg = { 0 }; | |
315 | struct cmsghdr *cmptr; | |
316 | struct iovec iov[1]; | |
317 | ssize_t ret = -1; | |
318 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
319 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
320 | ||
321 | /* | |
322 | * Note: the consumerd receiver only supports receiving one FD per | |
323 | * message. | |
324 | */ | |
325 | assert(nb_fd == 1); | |
326 | ||
327 | msg.msg_control = (caddr_t)tmp; | |
328 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
329 | ||
330 | cmptr = CMSG_FIRSTHDR(&msg); | |
331 | cmptr->cmsg_level = SOL_SOCKET; | |
332 | cmptr->cmsg_type = SCM_RIGHTS; | |
333 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); | |
334 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); | |
335 | /* Sum of the length of all control messages in the buffer: */ | |
336 | msg.msg_controllen = cmptr->cmsg_len; | |
337 | ||
338 | iov[0].iov_base = buf; | |
339 | iov[0].iov_len = len; | |
340 | msg.msg_iov = iov; | |
341 | msg.msg_iovlen = 1; | |
342 | ||
343 | ret = sendmsg(sock, &msg, 0); | |
344 | if (ret < 0) { | |
345 | perror("sendmsg"); | |
346 | } | |
347 | ||
348 | return ret; | |
349 | } | |
57773204 MD |
350 | |
351 | int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum) | |
352 | { | |
353 | ssize_t len; | |
354 | ||
355 | len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum)); | |
356 | switch (len) { | |
357 | case sizeof(*lum): | |
358 | printf("message successfully sent\n"); | |
359 | break; | |
360 | case -1: | |
361 | if (errno == ECONNRESET) { | |
362 | printf("remote end closed connection\n"); | |
363 | return 0; | |
364 | } | |
365 | return -1; | |
366 | default: | |
367 | printf("incorrect message size: %zd\n", len); | |
368 | return -1; | |
369 | } | |
370 | return 0; | |
371 | } | |
372 | ||
373 | int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur, | |
374 | uint32_t expected_handle, uint32_t expected_cmd) | |
375 | { | |
376 | ssize_t len; | |
377 | ||
378 | memset(lur, 0, sizeof(*lur)); | |
379 | len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur)); | |
380 | switch (len) { | |
381 | case 0: /* orderly shutdown */ | |
382 | printf("Application has performed an orderly shutdown\n"); | |
383 | return -EINVAL; | |
384 | case sizeof(*lur): | |
385 | printf("result message received\n"); | |
386 | if (lur->handle != expected_handle) { | |
387 | printf("Unexpected result message handle\n"); | |
388 | return -EINVAL; | |
389 | } | |
390 | ||
391 | if (lur->cmd != expected_cmd) { | |
392 | printf("Unexpected result message command\n"); | |
393 | return -EINVAL; | |
394 | } | |
395 | if (lur->ret_code != USTCOMM_OK) { | |
396 | printf("remote operation failed with code %d.\n", | |
397 | lur->ret_code); | |
398 | return lur->ret_code; | |
399 | } | |
400 | return 0; | |
401 | case -1: | |
402 | if (errno == ECONNRESET) { | |
403 | printf("remote end closed connection\n"); | |
404 | return -EINVAL; | |
405 | } | |
406 | return -1; | |
407 | default: | |
408 | printf("incorrect message size: %zd\n", len); | |
409 | return len > 0 ? -1 : len; | |
410 | } | |
411 | } | |
412 | ||
413 | int ustcomm_send_app_cmd(int sock, | |
414 | struct ustcomm_ust_msg *lum, | |
415 | struct ustcomm_ust_reply *lur) | |
416 | { | |
417 | int ret; | |
418 | ||
419 | ret = ustcomm_send_app_msg(sock, lum); | |
420 | if (ret) | |
421 | return ret; | |
422 | ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd); | |
423 | if (ret) | |
424 | return ret; | |
425 | return 0; | |
426 | } | |
427 | ||
428 | ||
429 | /* | |
430 | * Receives a single fd from socket. | |
431 | * | |
432 | * Returns the size of received data | |
433 | */ | |
434 | int ustcomm_recv_fd(int sock) | |
435 | { | |
436 | struct iovec iov[1]; | |
437 | int ret = 0; | |
438 | int data_fd; | |
439 | struct cmsghdr *cmsg; | |
440 | char recv_fd[CMSG_SPACE(sizeof(int))]; | |
441 | struct msghdr msg = { 0 }; | |
442 | union { | |
443 | unsigned char vc[4]; | |
444 | int vi; | |
445 | } tmp; | |
446 | int i; | |
447 | ||
448 | /* Prepare to receive the structures */ | |
449 | iov[0].iov_base = &data_fd; | |
450 | iov[0].iov_len = sizeof(data_fd); | |
451 | msg.msg_iov = iov; | |
452 | msg.msg_iovlen = 1; | |
453 | msg.msg_control = recv_fd; | |
454 | msg.msg_controllen = sizeof(recv_fd); | |
455 | ||
456 | printf("Waiting to receive fd\n"); | |
457 | if ((ret = recvmsg(sock, &msg, 0)) < 0) { | |
458 | perror("recvmsg"); | |
459 | goto end; | |
460 | } | |
461 | if (ret != sizeof(data_fd)) { | |
462 | printf("Received %d bytes, expected %ld", ret, sizeof(data_fd)); | |
463 | goto end; | |
464 | } | |
465 | cmsg = CMSG_FIRSTHDR(&msg); | |
466 | if (!cmsg) { | |
467 | printf("Invalid control message header\n"); | |
468 | ret = -1; | |
469 | goto end; | |
470 | } | |
471 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { | |
472 | printf("Didn't received any fd\n"); | |
473 | ret = -1; | |
474 | goto end; | |
475 | } | |
476 | /* this is our fd */ | |
477 | for (i = 0; i < sizeof(int); i++) | |
478 | tmp.vc[i] = CMSG_DATA(cmsg)[i]; | |
479 | ret = tmp.vi; | |
480 | printf("received fd %d\n", ret); | |
481 | end: | |
482 | return ret; | |
483 | } |