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