aee9458b44660d1a728b726eee79379277f69230
[lttng-tools.git] / src / common / sessiond-comm / 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
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
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
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #include <common/defaults.h>
31 #include <common/error.h>
32
33 #include "sessiond-comm.h"
34
35 /* For Unix socket */
36 #include "unix.h"
37 /* For Inet socket */
38 #include "inet.h"
39 /* For Inet6 socket */
40 #include "inet6.h"
41
42 struct lttcomm_net_family net_families[] = {
43 { LTTCOMM_INET, lttcomm_create_inet_sock },
44 { LTTCOMM_INET6, lttcomm_create_inet6_sock },
45 };
46
47 /*
48 * Human readable error message.
49 */
50 static const char *lttcomm_readable_code[] = {
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_DIR_FAIL) ] = "Create directory failed",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed",
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected",
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist",
68 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket",
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found",
70 [ LTTCOMM_ERR_INDEX(LTTCOMM_EPERM) ] = "Permission denied",
71 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available",
72 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_VERSION) ] = "Kernel tracer version is not compatible",
73 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists",
74 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed",
75 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_EXIST) ] = "Kernel channel already exists",
76 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed",
77 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found",
78 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed",
79 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed",
80 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed",
81 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed",
82 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed",
83 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed",
84 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed",
85 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed",
86 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed",
87 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed",
88 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed",
89 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist",
90 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found",
91 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed",
92 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CALIBRATE_FAIL) ] = "UST calibration failed",
93 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_VERSION) ] = "UST tracer version is not compatible",
94 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed",
95 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed",
96 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST) ] = "UST channel already exist",
97 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found",
98 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed",
99 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed",
100 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_FAIL) ] = "Add UST context failed",
101 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_ENABLE_FAIL) ] = "Enable UST event failed",
102 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DISABLE_FAIL) ] = "Disable UST event failed",
103 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_META_FAIL) ] = "Opening metadata failed",
104 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_START_FAIL) ] = "Starting UST trace failed",
105 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STOP_FAIL) ] = "Stoping UST trace failed",
106 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER64_FAIL) ] = "64-bit UST consumer start failed",
107 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER32_FAIL) ] = "32-bit UST consumer start failed",
108 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STREAM_FAIL) ] = "UST create stream failed",
109 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_FAIL) ] = "UST trace directory creation failed",
110 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_EXIST) ] = "UST trace directory already exist",
111 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_NO_SESSION) ] = "No UST session found",
112 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_LIST_FAIL) ] = "Listing UST events failed",
113 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_EXIST) ] = "UST event already exist",
114 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND)] = "UST event not found",
115 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST)] = "UST context already exist",
116 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_INVAL)] = "UST invalid context",
117 [ LTTCOMM_ERR_INDEX(LTTCOMM_NEED_ROOT_SESSIOND) ] = "Tracing the kernel requires a root lttng-sessiond daemon and \"tracing\" group user membership",
118 [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STARTED) ] = "Tracing already started",
119 [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STOPPED) ] = "Tracing already stopped",
120 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_ENOSYS) ] = "Kernel event type not supported",
121
122 [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready",
123 [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds",
124 [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds",
125 [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command",
126 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread",
127 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd",
128 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up",
129 [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally",
130 [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error",
131 [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile",
132 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF",
133 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL",
134 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM",
135 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE",
136 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found",
137 [ LTTCOMM_ERR_INDEX(LTTCOMM_INVALID) ] = "Invalid parameter",
138 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_USTCONSUMERD) ] = "No UST consumer detected",
139 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_KERNCONSUMERD) ] = "No kernel consumer detected",
140 [ LTTCOMM_ERR_INDEX(LTTCOMM_EVENT_EXIST_LOGLEVEL) ] = "Event already enabled with different loglevel",
141 [ LTTCOMM_ERR_INDEX(LTTCOMM_URL_DATA_MISS) ] = "Missing data path URL",
142 [ LTTCOMM_ERR_INDEX(LTTCOMM_URL_CTRL_MISS) ] = "Missing control data path URL",
143 [ LTTCOMM_ERR_INDEX(LTTCOMM_ENABLE_CONSUMER_FAIL) ] = "Enabling consumer failed",
144 [ LTTCOMM_ERR_INDEX(LTTCOMM_RELAYD_SESSION_FAIL) ] = "Unable to create session on lttng-relayd",
145 [ LTTCOMM_ERR_INDEX(LTTCOMM_RELAYD_VERSION_FAIL) ] = "Relay daemon not compatible",
146 [ LTTCOMM_ERR_INDEX(LTTCOMM_FILTER_INVAL) ] = "Invalid filter bytecode",
147 [ LTTCOMM_ERR_INDEX(LTTCOMM_FILTER_NOMEM) ] = "Not enough memory for filter bytecode",
148 [ LTTCOMM_ERR_INDEX(LTTCOMM_FILTER_EXIST) ] = "Filter already exist",
149 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_CONSUMER) ] = "Consumer not found for tracing session",
150 };
151
152 /*
153 * Return ptr to string representing a human readable error code from the
154 * lttcomm_return_code enum.
155 *
156 * These code MUST be negative in other to treat that as an error value.
157 */
158 const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
159 {
160 int tmp_code = -code;
161
162 if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) {
163 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)];
164 }
165
166 return "Unknown error code";
167 }
168
169 /*
170 * Create socket from an already allocated lttcomm socket structure and init
171 * sockaddr in the lttcomm sock.
172 */
173 int lttcomm_create_sock(struct lttcomm_sock *sock)
174 {
175 int ret, _sock_type, _sock_proto, domain;
176
177 assert(sock);
178
179 domain = sock->sockaddr.type;
180 if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) {
181 ERR("Create socket of unknown domain %d", domain);
182 ret = -1;
183 goto error;
184 }
185
186 switch (sock->proto) {
187 case LTTCOMM_SOCK_UDP:
188 _sock_type = SOCK_DGRAM;
189 _sock_proto = IPPROTO_UDP;
190 break;
191 case LTTCOMM_SOCK_TCP:
192 _sock_type = SOCK_STREAM;
193 _sock_proto = IPPROTO_TCP;
194 break;
195 default:
196 ret = -1;
197 goto error;
198 }
199
200 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
201 if (ret < 0) {
202 goto error;
203 }
204
205 error:
206 return ret;
207 }
208
209 /*
210 * Return allocated lttcomm socket structure.
211 */
212 struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
213 {
214 struct lttcomm_sock *sock;
215
216 sock = zmalloc(sizeof(struct lttcomm_sock));
217 if (sock == NULL) {
218 PERROR("zmalloc create sock");
219 goto end;
220 }
221
222 sock->proto = proto;
223 sock->fd = -1;
224
225 end:
226 return sock;
227 }
228
229 /*
230 * Return an allocated lttcomm socket structure and copy src content into
231 * the newly created socket.
232 *
233 * This is mostly useful when lttcomm_sock are passed between process where the
234 * fd and ops have to be changed within the correct address space.
235 */
236 struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
237 {
238 struct lttcomm_sock *sock;
239
240 /* Safety net */
241 assert(src);
242
243 sock = lttcomm_alloc_sock(src->proto);
244 if (sock == NULL) {
245 goto alloc_error;
246 }
247
248 lttcomm_copy_sock(sock, src);
249
250 alloc_error:
251 return sock;
252 }
253
254 /*
255 * Create and copy socket from an allocated lttcomm socket structure.
256 *
257 * This is mostly useful when lttcomm_sock are passed between process where the
258 * fd and ops have to be changed within the correct address space.
259 */
260 void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src)
261 {
262 /* Safety net */
263 assert(dst);
264 assert(src);
265
266 dst->proto = src->proto;
267 dst->fd = src->fd;
268 dst->ops = src->ops;
269 /* Copy sockaddr information from original socket */
270 memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr));
271 }
272
273 /*
274 * Init IPv4 sockaddr structure.
275 */
276 int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr,
277 const char *ip, unsigned int port)
278 {
279 int ret;
280
281 assert(sockaddr);
282 assert(ip);
283 assert(port > 0 && port <= 65535);
284
285 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
286
287 sockaddr->type = LTTCOMM_INET;
288 sockaddr->addr.sin.sin_family = AF_INET;
289 sockaddr->addr.sin.sin_port = htons(port);
290 ret = inet_pton(sockaddr->addr.sin.sin_family, ip,
291 &sockaddr->addr.sin.sin_addr);
292 if (ret < 1) {
293 ret = -1;
294 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
295 goto error;
296 }
297 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
298
299 error:
300 return ret;
301 }
302
303 /*
304 * Init IPv6 sockaddr structure.
305 */
306 int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr,
307 const char *ip, unsigned int port)
308 {
309 int ret;
310
311 assert(sockaddr);
312 assert(ip);
313 assert(port > 0 && port <= 65535);
314
315 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
316
317 sockaddr->type = LTTCOMM_INET6;
318 sockaddr->addr.sin6.sin6_family = AF_INET6;
319 sockaddr->addr.sin6.sin6_port = htons(port);
320 ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip,
321 &sockaddr->addr.sin6.sin6_addr);
322 if (ret < 1) {
323 ret = -1;
324 goto error;
325 }
326
327 error:
328 return ret;
329 }
330
331 /*
332 * Return allocated lttcomm socket structure from lttng URI.
333 */
334 struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri)
335 {
336 int ret;
337 int _sock_proto;
338 struct lttcomm_sock *sock = NULL;
339
340 /* Safety net */
341 assert(uri);
342
343 /* Check URI protocol */
344 if (uri->proto == LTTNG_TCP) {
345 _sock_proto = LTTCOMM_SOCK_TCP;
346 } else {
347 ERR("Relayd invalid URI proto: %d", uri->proto);
348 goto alloc_error;
349 }
350
351 sock = lttcomm_alloc_sock(_sock_proto);
352 if (sock == NULL) {
353 goto alloc_error;
354 }
355
356 /* Check destination type */
357 if (uri->dtype == LTTNG_DST_IPV4) {
358 ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4,
359 uri->port);
360 if (ret < 0) {
361 goto error;
362 }
363 } else if (uri->dtype == LTTNG_DST_IPV6) {
364 ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6,
365 uri->port);
366 if (ret < 0) {
367 goto error;
368 }
369 } else {
370 /* Command URI is invalid */
371 ERR("Relayd invalid URI dst type: %d", uri->dtype);
372 goto error;
373 }
374
375 return sock;
376
377 error:
378 lttcomm_destroy_sock(sock);
379 alloc_error:
380 return NULL;
381 }
382
383 /*
384 * Destroy and free lttcomm socket.
385 */
386 void lttcomm_destroy_sock(struct lttcomm_sock *sock)
387 {
388 if (sock != NULL) {
389 free(sock);
390 }
391 }
This page took 0.057827 seconds and 4 git commands to generate.