738362c1a6f2c533fd43ed38c829e5386e7dcd66
[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/common.h>
31
32 #include "sessiond-comm.h"
33
34 /* For Unix socket */
35 #include "unix.h"
36 /* For Inet socket */
37 #include "inet.h"
38 /* For Inet6 socket */
39 #include "inet6.h"
40
41 static struct lttcomm_net_family net_families[] = {
42 { LTTCOMM_INET, lttcomm_create_inet_sock },
43 { LTTCOMM_INET6, lttcomm_create_inet6_sock },
44 };
45
46 /*
47 * Human readable error message.
48 */
49 static const char *lttcomm_readable_code[] = {
50 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready",
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ENOMEM) ] = "Consumer is out of memory",
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_METADATA) ] = "Error with metadata",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_FATAL) ] = "Fatal error",
67
68 /* Last element */
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code"
70 };
71
72 /*
73 * Return ptr to string representing a human readable error code from the
74 * lttcomm_return_code enum.
75 *
76 * These code MUST be negative in other to treat that as an error value.
77 */
78 LTTNG_HIDDEN
79 const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
80 {
81 code = -code;
82
83 if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY || code > LTTCOMM_NR) {
84 code = LTTCOMM_NR;
85 }
86
87 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)];
88 }
89
90 /*
91 * Create socket from an already allocated lttcomm socket structure and init
92 * sockaddr in the lttcomm sock.
93 */
94 LTTNG_HIDDEN
95 int lttcomm_create_sock(struct lttcomm_sock *sock)
96 {
97 int ret, _sock_type, _sock_proto, domain;
98
99 assert(sock);
100
101 domain = sock->sockaddr.type;
102 if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) {
103 ERR("Create socket of unknown domain %d", domain);
104 ret = -1;
105 goto error;
106 }
107
108 switch (sock->proto) {
109 case LTTCOMM_SOCK_UDP:
110 _sock_type = SOCK_DGRAM;
111 _sock_proto = IPPROTO_UDP;
112 break;
113 case LTTCOMM_SOCK_TCP:
114 _sock_type = SOCK_STREAM;
115 _sock_proto = IPPROTO_TCP;
116 break;
117 default:
118 ret = -1;
119 goto error;
120 }
121
122 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
123 if (ret < 0) {
124 goto error;
125 }
126
127 error:
128 return ret;
129 }
130
131 /*
132 * Return allocated lttcomm socket structure.
133 */
134 LTTNG_HIDDEN
135 struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
136 {
137 struct lttcomm_sock *sock;
138
139 sock = zmalloc(sizeof(struct lttcomm_sock));
140 if (sock == NULL) {
141 PERROR("zmalloc create sock");
142 goto end;
143 }
144
145 sock->proto = proto;
146 sock->fd = -1;
147
148 end:
149 return sock;
150 }
151
152 /*
153 * Return an allocated lttcomm socket structure and copy src content into
154 * the newly created socket.
155 *
156 * This is mostly useful when lttcomm_sock are passed between process where the
157 * fd and ops have to be changed within the correct address space.
158 */
159 LTTNG_HIDDEN
160 struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
161 {
162 struct lttcomm_sock *sock;
163
164 /* Safety net */
165 assert(src);
166
167 sock = lttcomm_alloc_sock(src->proto);
168 if (sock == NULL) {
169 goto alloc_error;
170 }
171
172 lttcomm_copy_sock(sock, src);
173
174 alloc_error:
175 return sock;
176 }
177
178 /*
179 * Create and copy socket from an allocated lttcomm socket structure.
180 *
181 * This is mostly useful when lttcomm_sock are passed between process where the
182 * fd and ops have to be changed within the correct address space.
183 */
184 LTTNG_HIDDEN
185 void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src)
186 {
187 /* Safety net */
188 assert(dst);
189 assert(src);
190
191 dst->proto = src->proto;
192 dst->fd = src->fd;
193 dst->ops = src->ops;
194 /* Copy sockaddr information from original socket */
195 memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr));
196 }
197
198 /*
199 * Init IPv4 sockaddr structure.
200 */
201 LTTNG_HIDDEN
202 int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr,
203 const char *ip, unsigned int port)
204 {
205 int ret;
206
207 assert(sockaddr);
208 assert(ip);
209 assert(port > 0 && port <= 65535);
210
211 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
212
213 sockaddr->type = LTTCOMM_INET;
214 sockaddr->addr.sin.sin_family = AF_INET;
215 sockaddr->addr.sin.sin_port = htons(port);
216 ret = inet_pton(sockaddr->addr.sin.sin_family, ip,
217 &sockaddr->addr.sin.sin_addr);
218 if (ret < 1) {
219 ret = -1;
220 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
221 goto error;
222 }
223 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
224
225 error:
226 return ret;
227 }
228
229 /*
230 * Init IPv6 sockaddr structure.
231 */
232 LTTNG_HIDDEN
233 int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr,
234 const char *ip, unsigned int port)
235 {
236 int ret;
237
238 assert(sockaddr);
239 assert(ip);
240 assert(port > 0 && port <= 65535);
241
242 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
243
244 sockaddr->type = LTTCOMM_INET6;
245 sockaddr->addr.sin6.sin6_family = AF_INET6;
246 sockaddr->addr.sin6.sin6_port = htons(port);
247 ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip,
248 &sockaddr->addr.sin6.sin6_addr);
249 if (ret < 1) {
250 ret = -1;
251 goto error;
252 }
253
254 error:
255 return ret;
256 }
257
258 /*
259 * Return allocated lttcomm socket structure from lttng URI.
260 */
261 LTTNG_HIDDEN
262 struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri)
263 {
264 int ret;
265 int _sock_proto;
266 struct lttcomm_sock *sock = NULL;
267
268 /* Safety net */
269 assert(uri);
270
271 /* Check URI protocol */
272 if (uri->proto == LTTNG_TCP) {
273 _sock_proto = LTTCOMM_SOCK_TCP;
274 } else {
275 ERR("Relayd invalid URI proto: %d", uri->proto);
276 goto alloc_error;
277 }
278
279 sock = lttcomm_alloc_sock(_sock_proto);
280 if (sock == NULL) {
281 goto alloc_error;
282 }
283
284 /* Check destination type */
285 if (uri->dtype == LTTNG_DST_IPV4) {
286 ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4,
287 uri->port);
288 if (ret < 0) {
289 goto error;
290 }
291 } else if (uri->dtype == LTTNG_DST_IPV6) {
292 ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6,
293 uri->port);
294 if (ret < 0) {
295 goto error;
296 }
297 } else {
298 /* Command URI is invalid */
299 ERR("Relayd invalid URI dst type: %d", uri->dtype);
300 goto error;
301 }
302
303 return sock;
304
305 error:
306 lttcomm_destroy_sock(sock);
307 alloc_error:
308 return NULL;
309 }
310
311 /*
312 * Destroy and free lttcomm socket.
313 */
314 LTTNG_HIDDEN
315 void lttcomm_destroy_sock(struct lttcomm_sock *sock)
316 {
317 free(sock);
318 }
319
320 /*
321 * Allocate and return a relayd socket object using a given URI to initialize
322 * it and the major/minor version of the supported protocol.
323 *
324 * On error, NULL is returned.
325 */
326 LTTNG_HIDDEN
327 struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri,
328 uint32_t major, uint32_t minor)
329 {
330 int ret;
331 struct lttcomm_sock *tmp_sock = NULL;
332 struct lttcomm_relayd_sock *rsock = NULL;
333
334 assert(uri);
335
336 rsock = zmalloc(sizeof(*rsock));
337 if (!rsock) {
338 PERROR("zmalloc relayd sock");
339 goto error;
340 }
341
342 /* Allocate socket object from URI */
343 tmp_sock = lttcomm_alloc_sock_from_uri(uri);
344 if (tmp_sock == NULL) {
345 goto error_free;
346 }
347
348 /*
349 * Create socket object which basically sets the ops according to the
350 * socket protocol.
351 */
352 lttcomm_copy_sock(&rsock->sock, tmp_sock);
353 /* Temporary socket pointer not needed anymore. */
354 lttcomm_destroy_sock(tmp_sock);
355 ret = lttcomm_create_sock(&rsock->sock);
356 if (ret < 0) {
357 goto error_free;
358 }
359
360 rsock->major = major;
361 rsock->minor = minor;
362
363 return rsock;
364
365 error_free:
366 free(rsock);
367 error:
368 return NULL;
369 }
This page took 0.038059 seconds and 4 git commands to generate.