fix: ustcomm_close_unix_sock should close, not shutdown
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-comm.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
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
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>
31 #include <errno.h>
32
33 #include <ust-comm.h>
34
35 /*
36 * Human readable error message.
37 */
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",
88 };
89
90 /*
91 * lttcom_get_readable_code
92 *
93 * Return ptr to string representing a human readable
94 * error code from the ustcomm_return_code enum.
95 */
96 const char *ustcomm_get_readable_code(int code)
97 {
98 if (code == USTCOMM_OK) {
99 return "Success";
100 }
101 if (code >= USTCOMM_ERR && code < USTCOMM_NR) {
102 return ustcomm_readable_code[USTCOMM_ERR_INDEX(code)];
103 }
104 return strerror(code);
105 }
106
107 /*
108 * ustcomm_connect_unix_sock
109 *
110 * Connect to unix socket using the path name.
111 */
112 int ustcomm_connect_unix_sock(const char *pathname)
113 {
114 struct sockaddr_un sun;
115 int fd;
116 int ret;
117
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);
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 /*
153 * ustcomm_accept_unix_sock
154 *
155 * Do an accept(2) on the sock and return the
156 * new file descriptor. The socket MUST be bind(2) before.
157 */
158 int ustcomm_accept_unix_sock(int sock)
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 /*
178 * ustcomm_create_unix_sock
179 *
180 * Creates a AF_UNIX local socket using pathname
181 * bind the socket upon creation and return the fd.
182 */
183 int ustcomm_create_unix_sock(const char *pathname)
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 /*
215 * ustcomm_listen_unix_sock
216 *
217 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
218 */
219 int ustcomm_listen_unix_sock(int sock)
220 {
221 int ret;
222
223 ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN);
224 if (ret < 0) {
225 perror("listen");
226 }
227
228 return ret;
229 }
230
231 /*
232 * ustcomm_recv_unix_sock
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 */
238 ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
239 {
240 struct msghdr msg;
241 struct iovec iov[1];
242 ssize_t ret = -1;
243
244 memset(&msg, 0, sizeof(msg));
245
246 iov[0].iov_base = buf;
247 iov[0].iov_len = len;
248 msg.msg_iov = iov;
249 msg.msg_iovlen = 1;
250
251 do {
252 ret = recvmsg(sock, &msg, 0);
253 } while (ret < 0 && errno == EINTR);
254 if (ret < 0 && errno != EPIPE) {
255 perror("recvmsg");
256 }
257
258 return ret;
259 }
260
261 /*
262 * ustcomm_send_unix_sock
263 *
264 * Send buf data of size len. Using sendmsg API.
265 * Return the size of sent data.
266 */
267 ssize_t ustcomm_send_unix_sock(int sock, void *buf, size_t len)
268 {
269 struct msghdr msg;
270 struct iovec iov[1];
271 ssize_t ret = -1;
272
273 memset(&msg, 0, sizeof(msg));
274
275 iov[0].iov_base = buf;
276 iov[0].iov_len = len;
277 msg.msg_iov = iov;
278 msg.msg_iovlen = 1;
279
280 /*
281 * Using the MSG_NOSIGNAL when sending data from sessiond to
282 * libust, so libust does not receive an unhandled SIGPIPE or
283 * SIGURG. The sessiond receiver side can be made more resilient
284 * by ignoring SIGPIPE, but we don't have this luxury on the
285 * libust side.
286 */
287 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
288 if (ret < 0 && errno != EPIPE) {
289 perror("sendmsg");
290 }
291
292 return ret;
293 }
294
295 /*
296 * ustcomm_close_unix_sock
297 *
298 * Shutdown cleanly a unix socket.
299 */
300 int ustcomm_close_unix_sock(int sock)
301 {
302 int ret;
303
304 ret = close(sock);
305 if (ret < 0) {
306 perror("close");
307 }
308
309 return ret;
310 }
311
312 /*
313 * ustcomm_send_fds_unix_sock
314 *
315 * Send multiple fds on a unix socket.
316 */
317 ssize_t ustcomm_send_fds_unix_sock(int sock, void *buf, int *fds, size_t nb_fd, size_t len)
318 {
319 struct msghdr msg;
320 struct cmsghdr *cmptr;
321 struct iovec iov[1];
322 ssize_t ret = -1;
323 unsigned int sizeof_fds = nb_fd * sizeof(int);
324 char tmp[CMSG_SPACE(sizeof_fds)];
325
326 memset(&msg, 0, sizeof(msg));
327
328 /*
329 * Note: the consumerd receiver only supports receiving one FD per
330 * message.
331 */
332 assert(nb_fd == 1);
333
334 msg.msg_control = (caddr_t)tmp;
335 msg.msg_controllen = CMSG_LEN(sizeof_fds);
336
337 cmptr = CMSG_FIRSTHDR(&msg);
338 cmptr->cmsg_level = SOL_SOCKET;
339 cmptr->cmsg_type = SCM_RIGHTS;
340 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
341 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
342 /* Sum of the length of all control messages in the buffer: */
343 msg.msg_controllen = cmptr->cmsg_len;
344
345 iov[0].iov_base = buf;
346 iov[0].iov_len = len;
347 msg.msg_iov = iov;
348 msg.msg_iovlen = 1;
349
350 ret = sendmsg(sock, &msg, 0);
351 if (ret < 0 && errno != EPIPE) {
352 perror("sendmsg");
353 }
354
355 return ret;
356 }
357
358 int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
359 {
360 ssize_t len;
361
362 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
363 switch (len) {
364 case sizeof(*lum):
365 break;
366 case -1:
367 if (errno == ECONNRESET) {
368 fprintf(stderr, "remote end closed connection\n");
369 return 0;
370 }
371 return -1;
372 default:
373 fprintf(stderr, "incorrect message size: %zd\n", len);
374 return -1;
375 }
376 return 0;
377 }
378
379 int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
380 uint32_t expected_handle, uint32_t expected_cmd)
381 {
382 ssize_t len;
383
384 memset(lur, 0, sizeof(*lur));
385 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
386 switch (len) {
387 case 0: /* orderly shutdown */
388 return -EINVAL;
389 case sizeof(*lur):
390 if (lur->handle != expected_handle) {
391 fprintf(stderr, "Unexpected result message handle\n");
392 return -EINVAL;
393 }
394
395 if (lur->cmd != expected_cmd) {
396 fprintf(stderr, "Unexpected result message command\n");
397 return -EINVAL;
398 }
399 if (lur->ret_code != USTCOMM_OK) {
400 /*
401 * Some errors are normal.. we should put this
402 * in a debug level message...
403 * fprintf(stderr, "remote operation failed with code %d.\n",
404 * lur->ret_code);
405 */
406 return lur->ret_code;
407 }
408 return 0;
409 case -1:
410 if (errno == ECONNRESET) {
411 fprintf(stderr, "remote end closed connection\n");
412 return -EINVAL;
413 }
414 return -1;
415 default:
416 fprintf(stderr, "incorrect message size: %zd\n", len);
417 return len > 0 ? -1 : len;
418 }
419 }
420
421 int ustcomm_send_app_cmd(int sock,
422 struct ustcomm_ust_msg *lum,
423 struct ustcomm_ust_reply *lur)
424 {
425 int ret;
426
427 ret = ustcomm_send_app_msg(sock, lum);
428 if (ret)
429 return ret;
430 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
431 if (ret)
432 return ret;
433 return 0;
434 }
435
436
437 /*
438 * Receives a single fd from socket.
439 *
440 * Returns the size of received data
441 */
442 int ustcomm_recv_fd(int sock)
443 {
444 struct iovec iov[1];
445 int ret = 0;
446 int data_fd;
447 struct cmsghdr *cmsg;
448 char recv_fd[CMSG_SPACE(sizeof(int))];
449 struct msghdr msg;
450 union {
451 unsigned char vc[4];
452 int vi;
453 } tmp;
454 int i;
455
456 memset(&msg, 0, sizeof(msg));
457
458 /* Prepare to receive the structures */
459 iov[0].iov_base = &data_fd;
460 iov[0].iov_len = sizeof(data_fd);
461 msg.msg_iov = iov;
462 msg.msg_iovlen = 1;
463 msg.msg_control = recv_fd;
464 msg.msg_controllen = sizeof(recv_fd);
465
466 do {
467 ret = recvmsg(sock, &msg, 0);
468 } while (ret < 0 && errno == EINTR);
469 if (ret < 0) {
470 if (errno != EPIPE) {
471 perror("recvmsg");
472 }
473 goto end;
474 }
475 if (ret != sizeof(data_fd)) {
476 fprintf(stderr, "Received %d bytes, expected %zd", ret, sizeof(data_fd));
477 goto end;
478 }
479 cmsg = CMSG_FIRSTHDR(&msg);
480 if (!cmsg) {
481 fprintf(stderr, "Invalid control message header\n");
482 ret = -1;
483 goto end;
484 }
485 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
486 fprintf(stderr, "Didn't received any fd\n");
487 ret = -1;
488 goto end;
489 }
490 /* this is our fd */
491 for (i = 0; i < sizeof(int); i++)
492 tmp.vc[i] = CMSG_DATA(cmsg)[i];
493 ret = tmp.vi;
494 /*
495 * Useful for fd leak debug.
496 * fprintf(stderr, "received fd %d\n", ret);
497 */
498 end:
499 return ret;
500 }
This page took 0.0402 seconds and 4 git commands to generate.