Fix: add missing enum lttcomm_return_code entries
[lttng-tools.git] / src / common / sessiond-comm / sessiond-comm.c
CommitLineData
826d496d 1/*
917216f6
DG
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fac6795d 4 *
d14d33bf
AM
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.
fac6795d 8 *
917216f6
DG
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.
fac6795d 13 *
d14d33bf
AM
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.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
1e307fab 20#include <assert.h>
fac6795d
DG
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
fac6795d
DG
25#include <sys/stat.h>
26#include <sys/types.h>
fac6795d 27#include <unistd.h>
3bd1e081 28#include <errno.h>
fac6795d 29
90e535ef 30#include <common/common.h>
990570ed 31
10a8a223 32#include "sessiond-comm.h"
fac6795d 33
6364a07a
DG
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
32dd26fb 41static struct lttcomm_net_family net_families[] = {
6364a07a
DG
42 { LTTCOMM_INET, lttcomm_create_inet_sock },
43 { LTTCOMM_INET6, lttcomm_create_inet6_sock },
44};
45
fac6795d
DG
46/*
47 * Human readable error message.
48 */
49static const char *lttcomm_readable_code[] = {
f73fabfd
DG
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",
40727660
MD
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",
80e327fa 67
f73fabfd
DG
68 /* Last element */
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code"
fac6795d
DG
70};
71
72/*
917216f6
DG
73 * Return ptr to string representing a human readable error code from the
74 * lttcomm_return_code enum.
fac6795d 75 *
917216f6 76 * These code MUST be negative in other to treat that as an error value.
fac6795d 77 */
90e535ef 78LTTNG_HIDDEN
fac6795d
DG
79const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
80{
f73fabfd 81 code = -code;
fac6795d 82
c617c0c6 83 if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY || code > LTTCOMM_NR) {
f73fabfd 84 code = LTTCOMM_NR;
fac6795d
DG
85 }
86
f73fabfd 87 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)];
fac6795d 88}
6364a07a
DG
89
90/*
de5e9086
DG
91 * Create socket from an already allocated lttcomm socket structure and init
92 * sockaddr in the lttcomm sock.
6364a07a 93 */
90e535ef 94LTTNG_HIDDEN
de5e9086 95int lttcomm_create_sock(struct lttcomm_sock *sock)
6364a07a 96{
de5e9086 97 int ret, _sock_type, _sock_proto, domain;
6364a07a 98
de5e9086
DG
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;
6364a07a
DG
106 }
107
de5e9086
DG
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 }
6364a07a 121
de5e9086
DG
122 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
123 if (ret < 0) {
124 goto error;
125 }
126
127error:
128 return ret;
6364a07a
DG
129}
130
131/*
de5e9086 132 * Return allocated lttcomm socket structure.
6364a07a 133 */
90e535ef 134LTTNG_HIDDEN
de5e9086 135struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
6364a07a 136{
de5e9086 137 struct lttcomm_sock *sock;
6364a07a 138
de5e9086
DG
139 sock = zmalloc(sizeof(struct lttcomm_sock));
140 if (sock == NULL) {
141 PERROR("zmalloc create sock");
142 goto end;
6364a07a
DG
143 }
144
145 sock->proto = proto;
de5e9086 146 sock->fd = -1;
6364a07a 147
de5e9086
DG
148end:
149 return sock;
6364a07a
DG
150}
151
152/*
de5e9086
DG
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.
6364a07a 158 */
90e535ef 159LTTNG_HIDDEN
de5e9086 160struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
6364a07a 161{
6364a07a
DG
162 struct lttcomm_sock *sock;
163
de5e9086
DG
164 /* Safety net */
165 assert(src);
166
167 sock = lttcomm_alloc_sock(src->proto);
6364a07a
DG
168 if (sock == NULL) {
169 goto alloc_error;
170 }
171
de5e9086 172 lttcomm_copy_sock(sock, src);
6364a07a 173
de5e9086 174alloc_error:
6364a07a 175 return sock;
de5e9086 176}
6364a07a 177
de5e9086
DG
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 */
90e535ef 184LTTNG_HIDDEN
de5e9086
DG
185void 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));
6364a07a
DG
196}
197
198/*
199 * Init IPv4 sockaddr structure.
200 */
90e535ef 201LTTNG_HIDDEN
6364a07a
DG
202int 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;
de5e9086 220 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
6364a07a
DG
221 goto error;
222 }
223 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
224
225error:
226 return ret;
227}
228
229/*
230 * Init IPv6 sockaddr structure.
231 */
90e535ef 232LTTNG_HIDDEN
6364a07a
DG
233int 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
254error:
255 return ret;
256}
de5e9086
DG
257
258/*
259 * Return allocated lttcomm socket structure from lttng URI.
260 */
90e535ef 261LTTNG_HIDDEN
de5e9086
DG
262struct 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
305error:
306 lttcomm_destroy_sock(sock);
307alloc_error:
308 return NULL;
309}
310
311/*
312 * Destroy and free lttcomm socket.
313 */
90e535ef 314LTTNG_HIDDEN
de5e9086
DG
315void lttcomm_destroy_sock(struct lttcomm_sock *sock)
316{
0e428499 317 free(sock);
de5e9086 318}
6151a90f
JD
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 */
bc182241 326LTTNG_HIDDEN
6151a90f
JD
327struct 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
365error_free:
366 free(rsock);
367error:
368 return NULL;
369}
This page took 0.060987 seconds and 5 git commands to generate.