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