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