Add lttng-error.h containing every API err. code
[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
990570ed 30#include <common/defaults.h>
4c462e79 31#include <common/error.h>
990570ed 32
10a8a223 33#include "sessiond-comm.h"
fac6795d 34
6364a07a
DG
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
42struct lttcomm_net_family net_families[] = {
43 { LTTCOMM_INET, lttcomm_create_inet_sock },
44 { LTTCOMM_INET6, lttcomm_create_inet6_sock },
45};
46
fac6795d
DG
47/*
48 * Human readable error message.
49 */
50static const char *lttcomm_readable_code[] = {
f73fabfd
DG
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE",
80e327fa 65
f73fabfd
DG
66 /* Last element */
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code"
fac6795d
DG
68};
69
70/*
917216f6
DG
71 * Return ptr to string representing a human readable error code from the
72 * lttcomm_return_code enum.
fac6795d 73 *
917216f6 74 * These code MUST be negative in other to treat that as an error value.
fac6795d
DG
75 */
76const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
77{
f73fabfd 78 code = -code;
fac6795d 79
f73fabfd
DG
80 if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY && code > LTTCOMM_NR) {
81 code = LTTCOMM_NR;
fac6795d
DG
82 }
83
f73fabfd 84 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)];
fac6795d 85}
6364a07a
DG
86
87/*
de5e9086
DG
88 * Create socket from an already allocated lttcomm socket structure and init
89 * sockaddr in the lttcomm sock.
6364a07a 90 */
de5e9086 91int lttcomm_create_sock(struct lttcomm_sock *sock)
6364a07a 92{
de5e9086 93 int ret, _sock_type, _sock_proto, domain;
6364a07a 94
de5e9086
DG
95 assert(sock);
96
97 domain = sock->sockaddr.type;
98 if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) {
99 ERR("Create socket of unknown domain %d", domain);
100 ret = -1;
101 goto error;
6364a07a
DG
102 }
103
de5e9086
DG
104 switch (sock->proto) {
105 case LTTCOMM_SOCK_UDP:
106 _sock_type = SOCK_DGRAM;
107 _sock_proto = IPPROTO_UDP;
108 break;
109 case LTTCOMM_SOCK_TCP:
110 _sock_type = SOCK_STREAM;
111 _sock_proto = IPPROTO_TCP;
112 break;
113 default:
114 ret = -1;
115 goto error;
116 }
6364a07a 117
de5e9086
DG
118 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
119 if (ret < 0) {
120 goto error;
121 }
122
123error:
124 return ret;
6364a07a
DG
125}
126
127/*
de5e9086 128 * Return allocated lttcomm socket structure.
6364a07a 129 */
de5e9086 130struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
6364a07a 131{
de5e9086 132 struct lttcomm_sock *sock;
6364a07a 133
de5e9086
DG
134 sock = zmalloc(sizeof(struct lttcomm_sock));
135 if (sock == NULL) {
136 PERROR("zmalloc create sock");
137 goto end;
6364a07a
DG
138 }
139
140 sock->proto = proto;
de5e9086 141 sock->fd = -1;
6364a07a 142
de5e9086
DG
143end:
144 return sock;
6364a07a
DG
145}
146
147/*
de5e9086
DG
148 * Return an allocated lttcomm socket structure and copy src content into
149 * the newly created socket.
150 *
151 * This is mostly useful when lttcomm_sock are passed between process where the
152 * fd and ops have to be changed within the correct address space.
6364a07a 153 */
de5e9086 154struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
6364a07a 155{
6364a07a
DG
156 struct lttcomm_sock *sock;
157
de5e9086
DG
158 /* Safety net */
159 assert(src);
160
161 sock = lttcomm_alloc_sock(src->proto);
6364a07a
DG
162 if (sock == NULL) {
163 goto alloc_error;
164 }
165
de5e9086 166 lttcomm_copy_sock(sock, src);
6364a07a 167
de5e9086 168alloc_error:
6364a07a 169 return sock;
de5e9086 170}
6364a07a 171
de5e9086
DG
172/*
173 * Create and copy socket from an allocated lttcomm socket structure.
174 *
175 * This is mostly useful when lttcomm_sock are passed between process where the
176 * fd and ops have to be changed within the correct address space.
177 */
178void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src)
179{
180 /* Safety net */
181 assert(dst);
182 assert(src);
183
184 dst->proto = src->proto;
185 dst->fd = src->fd;
186 dst->ops = src->ops;
187 /* Copy sockaddr information from original socket */
188 memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr));
6364a07a
DG
189}
190
191/*
192 * Init IPv4 sockaddr structure.
193 */
194int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr,
195 const char *ip, unsigned int port)
196{
197 int ret;
198
199 assert(sockaddr);
200 assert(ip);
201 assert(port > 0 && port <= 65535);
202
203 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
204
205 sockaddr->type = LTTCOMM_INET;
206 sockaddr->addr.sin.sin_family = AF_INET;
207 sockaddr->addr.sin.sin_port = htons(port);
208 ret = inet_pton(sockaddr->addr.sin.sin_family, ip,
209 &sockaddr->addr.sin.sin_addr);
210 if (ret < 1) {
211 ret = -1;
de5e9086 212 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
6364a07a
DG
213 goto error;
214 }
215 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
216
217error:
218 return ret;
219}
220
221/*
222 * Init IPv6 sockaddr structure.
223 */
224int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr,
225 const char *ip, unsigned int port)
226{
227 int ret;
228
229 assert(sockaddr);
230 assert(ip);
231 assert(port > 0 && port <= 65535);
232
233 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
234
235 sockaddr->type = LTTCOMM_INET6;
236 sockaddr->addr.sin6.sin6_family = AF_INET6;
237 sockaddr->addr.sin6.sin6_port = htons(port);
238 ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip,
239 &sockaddr->addr.sin6.sin6_addr);
240 if (ret < 1) {
241 ret = -1;
242 goto error;
243 }
244
245error:
246 return ret;
247}
de5e9086
DG
248
249/*
250 * Return allocated lttcomm socket structure from lttng URI.
251 */
252struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri)
253{
254 int ret;
255 int _sock_proto;
256 struct lttcomm_sock *sock = NULL;
257
258 /* Safety net */
259 assert(uri);
260
261 /* Check URI protocol */
262 if (uri->proto == LTTNG_TCP) {
263 _sock_proto = LTTCOMM_SOCK_TCP;
264 } else {
265 ERR("Relayd invalid URI proto: %d", uri->proto);
266 goto alloc_error;
267 }
268
269 sock = lttcomm_alloc_sock(_sock_proto);
270 if (sock == NULL) {
271 goto alloc_error;
272 }
273
274 /* Check destination type */
275 if (uri->dtype == LTTNG_DST_IPV4) {
276 ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4,
277 uri->port);
278 if (ret < 0) {
279 goto error;
280 }
281 } else if (uri->dtype == LTTNG_DST_IPV6) {
282 ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6,
283 uri->port);
284 if (ret < 0) {
285 goto error;
286 }
287 } else {
288 /* Command URI is invalid */
289 ERR("Relayd invalid URI dst type: %d", uri->dtype);
290 goto error;
291 }
292
293 return sock;
294
295error:
296 lttcomm_destroy_sock(sock);
297alloc_error:
298 return NULL;
299}
300
301/*
302 * Destroy and free lttcomm socket.
303 */
304void lttcomm_destroy_sock(struct lttcomm_sock *sock)
305{
306 if (sock != NULL) {
307 free(sock);
308 }
309}
This page took 0.072068 seconds and 4 git commands to generate.