Move stream file rotation functions to utils
[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",
80e327fa 64
f73fabfd
DG
65 /* Last element */
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code"
fac6795d
DG
67};
68
69/*
917216f6
DG
70 * Return ptr to string representing a human readable error code from the
71 * lttcomm_return_code enum.
fac6795d 72 *
917216f6 73 * These code MUST be negative in other to treat that as an error value.
fac6795d 74 */
90e535ef 75LTTNG_HIDDEN
fac6795d
DG
76const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
77{
f73fabfd 78 code = -code;
fac6795d 79
c617c0c6 80 if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY || code > LTTCOMM_NR) {
f73fabfd 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 */
90e535ef 91LTTNG_HIDDEN
de5e9086 92int lttcomm_create_sock(struct lttcomm_sock *sock)
6364a07a 93{
de5e9086 94 int ret, _sock_type, _sock_proto, domain;
6364a07a 95
de5e9086
DG
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;
6364a07a
DG
103 }
104
de5e9086
DG
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 }
6364a07a 118
de5e9086
DG
119 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
120 if (ret < 0) {
121 goto error;
122 }
123
124error:
125 return ret;
6364a07a
DG
126}
127
128/*
de5e9086 129 * Return allocated lttcomm socket structure.
6364a07a 130 */
90e535ef 131LTTNG_HIDDEN
de5e9086 132struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
6364a07a 133{
de5e9086 134 struct lttcomm_sock *sock;
6364a07a 135
de5e9086
DG
136 sock = zmalloc(sizeof(struct lttcomm_sock));
137 if (sock == NULL) {
138 PERROR("zmalloc create sock");
139 goto end;
6364a07a
DG
140 }
141
142 sock->proto = proto;
de5e9086 143 sock->fd = -1;
6364a07a 144
de5e9086
DG
145end:
146 return sock;
6364a07a
DG
147}
148
149/*
de5e9086
DG
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.
6364a07a 155 */
90e535ef 156LTTNG_HIDDEN
de5e9086 157struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
6364a07a 158{
6364a07a
DG
159 struct lttcomm_sock *sock;
160
de5e9086
DG
161 /* Safety net */
162 assert(src);
163
164 sock = lttcomm_alloc_sock(src->proto);
6364a07a
DG
165 if (sock == NULL) {
166 goto alloc_error;
167 }
168
de5e9086 169 lttcomm_copy_sock(sock, src);
6364a07a 170
de5e9086 171alloc_error:
6364a07a 172 return sock;
de5e9086 173}
6364a07a 174
de5e9086
DG
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 */
90e535ef 181LTTNG_HIDDEN
de5e9086
DG
182void 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));
6364a07a
DG
193}
194
195/*
196 * Init IPv4 sockaddr structure.
197 */
90e535ef 198LTTNG_HIDDEN
6364a07a
DG
199int 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;
de5e9086 217 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
6364a07a
DG
218 goto error;
219 }
220 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
221
222error:
223 return ret;
224}
225
226/*
227 * Init IPv6 sockaddr structure.
228 */
90e535ef 229LTTNG_HIDDEN
6364a07a
DG
230int 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
251error:
252 return ret;
253}
de5e9086
DG
254
255/*
256 * Return allocated lttcomm socket structure from lttng URI.
257 */
90e535ef 258LTTNG_HIDDEN
de5e9086
DG
259struct 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
302error:
303 lttcomm_destroy_sock(sock);
304alloc_error:
305 return NULL;
306}
307
308/*
309 * Destroy and free lttcomm socket.
310 */
90e535ef 311LTTNG_HIDDEN
de5e9086
DG
312void lttcomm_destroy_sock(struct lttcomm_sock *sock)
313{
0e428499 314 free(sock);
de5e9086 315}
6151a90f
JD
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 */
323struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri,
324 uint32_t major, uint32_t minor)
325{
326 int ret;
327 struct lttcomm_sock *tmp_sock = NULL;
328 struct lttcomm_relayd_sock *rsock = NULL;
329
330 assert(uri);
331
332 rsock = zmalloc(sizeof(*rsock));
333 if (!rsock) {
334 PERROR("zmalloc relayd sock");
335 goto error;
336 }
337
338 /* Allocate socket object from URI */
339 tmp_sock = lttcomm_alloc_sock_from_uri(uri);
340 if (tmp_sock == NULL) {
341 goto error_free;
342 }
343
344 /*
345 * Create socket object which basically sets the ops according to the
346 * socket protocol.
347 */
348 lttcomm_copy_sock(&rsock->sock, tmp_sock);
349 /* Temporary socket pointer not needed anymore. */
350 lttcomm_destroy_sock(tmp_sock);
351 ret = lttcomm_create_sock(&rsock->sock);
352 if (ret < 0) {
353 goto error_free;
354 }
355
356 rsock->major = major;
357 rsock->minor = minor;
358
359 return rsock;
360
361error_free:
362 free(rsock);
363error:
364 return NULL;
365}
This page took 0.056379 seconds and 4 git commands to generate.