UST 2.0 support
[lttng-tools.git] / liblttng-sessiond-comm / lttng-sessiond-comm.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; only version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
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 <errno.h>
31
32 #include <lttng-sessiond-comm.h>
33
34 /*
35 * Human readable error message.
36 */
37 static const char *lttcomm_readable_code[] = {
38 [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success",
39 [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error",
40 [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command",
41 [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented",
42 [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain",
43 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found",
44 [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
45 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found",
46 [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found",
47 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found",
48 [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
49 [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed",
50 [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed",
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed",
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed",
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed",
68 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed",
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed",
70 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed",
71 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed",
72 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed",
73 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist",
74 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found",
75 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed",
76 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed",
77 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found",
78 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed",
79 [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready",
80 [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds",
81 [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds",
82 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread",
83 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd",
84 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up",
85 [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally",
86 [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error",
87 [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile",
88 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF",
89 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL",
90 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM",
91 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE",
92 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found",
93 };
94
95 /*
96 * Return ptr to string representing a human readable error code from the
97 * lttcomm_return_code enum.
98 *
99 * These code MUST be negative in other to treat that as an error value.
100 */
101 const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
102 {
103 int tmp_code = -code;
104
105 if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) {
106 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)];
107 }
108
109 return "Unknown error code";
110 }
111
112 /*
113 * Connect to unix socket using the path name.
114 */
115 int lttcomm_connect_unix_sock(const char *pathname)
116 {
117 struct sockaddr_un sun;
118 int fd;
119 int ret;
120
121 fd = socket(PF_UNIX, SOCK_STREAM, 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 is used in
137 * normal execution to detect if sessiond is alive.
138 */
139 goto error_connect;
140 }
141
142 return fd;
143
144 error_connect:
145 close(fd);
146 error:
147 return ret;
148 }
149
150 /*
151 * Do an accept(2) on the sock and return the new file descriptor. The socket
152 * MUST be bind(2) before.
153 */
154 int lttcomm_accept_unix_sock(int sock)
155 {
156 int new_fd;
157 struct sockaddr_un sun;
158 socklen_t len = 0;
159
160 /* Blocking call */
161 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
162 if (new_fd < 0) {
163 perror("accept");
164 goto error;
165 }
166
167 return new_fd;
168
169 error:
170 return -1;
171 }
172
173 /*
174 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
175 * and return the fd.
176 */
177 int lttcomm_create_unix_sock(const char *pathname)
178 {
179 struct sockaddr_un sun;
180 int fd;
181 int ret = -1;
182
183 /* Create server socket */
184 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
185 perror("socket");
186 goto error;
187 }
188
189 memset(&sun, 0, sizeof(sun));
190 sun.sun_family = AF_UNIX;
191 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
192 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
193
194 /* Unlink the old file if present */
195 (void) unlink(pathname);
196 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
197 if (ret < 0) {
198 perror("bind");
199 goto error;
200 }
201
202 return fd;
203
204 error:
205 return ret;
206 }
207
208 /*
209 * Make the socket listen using MAX_LISTEN.
210 */
211 int lttcomm_listen_unix_sock(int sock)
212 {
213 int ret;
214
215 ret = listen(sock, MAX_LISTEN);
216 if (ret < 0) {
217 perror("listen");
218 }
219
220 return ret;
221 }
222
223 /*
224 * Receive data of size len in put that data into the buf param. Using recvmsg
225 * API.
226 *
227 * Return the size of received data.
228 */
229 ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
230 {
231 struct msghdr msg = { 0 };
232 struct iovec iov[1];
233 ssize_t ret = -1;
234
235 iov[0].iov_base = buf;
236 iov[0].iov_len = len;
237 msg.msg_iov = iov;
238 msg.msg_iovlen = 1;
239
240 ret = recvmsg(sock, &msg, 0);
241 if (ret < 0) {
242 perror("recvmsg");
243 }
244
245 return ret;
246 }
247
248 /*
249 * Send buf data of size len. Using sendmsg API.
250 *
251 * Return the size of sent data.
252 */
253 ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
254 {
255 struct msghdr msg = { 0 };
256 struct iovec iov[1];
257 ssize_t ret = -1;
258
259 iov[0].iov_base = buf;
260 iov[0].iov_len = len;
261 msg.msg_iov = iov;
262 msg.msg_iovlen = 1;
263
264 ret = sendmsg(sock, &msg, 0);
265 if (ret < 0) {
266 perror("sendmsg");
267 }
268
269 return ret;
270 }
271
272 /*
273 * Shutdown cleanly a unix socket.
274 */
275 int lttcomm_close_unix_sock(int sock)
276 {
277 int ret;
278
279 /* Shutdown receptions and transmissions */
280 ret = shutdown(sock, SHUT_RDWR);
281 if (ret < 0) {
282 perror("shutdown");
283 }
284
285 return ret;
286 }
287
288 /*
289 * Send a message accompanied by fd(s) over a unix socket.
290 *
291 * Returns the size of data sent, or negative error value.
292 */
293 ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
294 {
295 struct msghdr msg = { 0 };
296 struct cmsghdr *cmptr;
297 struct iovec iov[1];
298 ssize_t ret = -1;
299 unsigned int sizeof_fds = nb_fd * sizeof(int);
300 char tmp[CMSG_SPACE(sizeof_fds)];
301 char dummy = 0;
302
303 if (nb_fd > LTTCOMM_MAX_SEND_FDS)
304 return -EINVAL;
305
306 msg.msg_control = (caddr_t)tmp;
307 msg.msg_controllen = CMSG_LEN(sizeof_fds);
308
309 cmptr = CMSG_FIRSTHDR(&msg);
310 cmptr->cmsg_level = SOL_SOCKET;
311 cmptr->cmsg_type = SCM_RIGHTS;
312 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
313 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
314 /* Sum of the length of all control messages in the buffer: */
315 msg.msg_controllen = cmptr->cmsg_len;
316
317 iov[0].iov_base = &dummy;
318 iov[0].iov_len = 1;
319 msg.msg_iov = iov;
320 msg.msg_iovlen = 1;
321
322 ret = sendmsg(sock, &msg, 0);
323 if (ret < 0) {
324 perror("sendmsg");
325 }
326 return ret;
327 }
328
329 /*
330 * Recv a message accompanied by fd(s) from a unix socket.
331 *
332 * Returns the size of received data, or negative error value.
333 *
334 * Expect at most "nb_fd" file descriptors. Returns the number of fd
335 * actually received in nb_fd.
336 */
337 ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
338 {
339 struct iovec iov[1];
340 ssize_t ret = 0;
341 struct cmsghdr *cmsg;
342 size_t sizeof_fds = nb_fd * sizeof(int);
343 char recv_fd[CMSG_SPACE(sizeof_fds)];
344 struct msghdr msg = { 0 };
345 char dummy;
346
347 /* Prepare to receive the structures */
348 iov[0].iov_base = &dummy;
349 iov[0].iov_len = 1;
350 msg.msg_iov = iov;
351 msg.msg_iovlen = 1;
352 msg.msg_control = recv_fd;
353 msg.msg_controllen = sizeof(recv_fd);
354
355 ret = recvmsg(sock, &msg, 0);
356 if (ret < 0) {
357 perror("recvmsg fds");
358 goto end;
359 }
360 if (ret != 1) {
361 fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
362 ret, 1);
363 goto end;
364 }
365 if (msg.msg_flags & MSG_CTRUNC) {
366 fprintf(stderr, "Error: Control message truncated.\n");
367 ret = -1;
368 goto end;
369 }
370 cmsg = CMSG_FIRSTHDR(&msg);
371 if (!cmsg) {
372 fprintf(stderr, "Error: Invalid control message header\n");
373 ret = -1;
374 goto end;
375 }
376 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
377 fprintf(stderr, "Didn't received any fd\n");
378 ret = -1;
379 goto end;
380 }
381 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
382 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
383 cmsg->cmsg_len, CMSG_LEN(sizeof_fds));
384 ret = -1;
385 goto end;
386 }
387 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
388 ret = sizeof_fds;
389 end:
390 return ret;
391 }
This page took 0.056612 seconds and 4 git commands to generate.