Commit | Line | Data |
---|---|---|
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 | ||
32dd26fb | 42 | static struct lttcomm_net_family net_families[] = { |
6364a07a DG |
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 | */ | |
50 | static 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 | 75 | */ |
32dd26fb | 76 | __attribute__((visibility("hidden"))) |
fac6795d DG |
77 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) |
78 | { | |
f73fabfd | 79 | code = -code; |
fac6795d | 80 | |
f73fabfd DG |
81 | if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY && code > LTTCOMM_NR) { |
82 | code = LTTCOMM_NR; | |
fac6795d DG |
83 | } |
84 | ||
f73fabfd | 85 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)]; |
fac6795d | 86 | } |
6364a07a DG |
87 | |
88 | /* | |
de5e9086 DG |
89 | * Create socket from an already allocated lttcomm socket structure and init |
90 | * sockaddr in the lttcomm sock. | |
6364a07a | 91 | */ |
32dd26fb | 92 | __attribute__((visibility("hidden"))) |
de5e9086 | 93 | int lttcomm_create_sock(struct lttcomm_sock *sock) |
6364a07a | 94 | { |
de5e9086 | 95 | int ret, _sock_type, _sock_proto, domain; |
6364a07a | 96 | |
de5e9086 DG |
97 | assert(sock); |
98 | ||
99 | domain = sock->sockaddr.type; | |
100 | if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) { | |
101 | ERR("Create socket of unknown domain %d", domain); | |
102 | ret = -1; | |
103 | goto error; | |
6364a07a DG |
104 | } |
105 | ||
de5e9086 DG |
106 | switch (sock->proto) { |
107 | case LTTCOMM_SOCK_UDP: | |
108 | _sock_type = SOCK_DGRAM; | |
109 | _sock_proto = IPPROTO_UDP; | |
110 | break; | |
111 | case LTTCOMM_SOCK_TCP: | |
112 | _sock_type = SOCK_STREAM; | |
113 | _sock_proto = IPPROTO_TCP; | |
114 | break; | |
115 | default: | |
116 | ret = -1; | |
117 | goto error; | |
118 | } | |
6364a07a | 119 | |
de5e9086 DG |
120 | ret = net_families[domain].create(sock, _sock_type, _sock_proto); |
121 | if (ret < 0) { | |
122 | goto error; | |
123 | } | |
124 | ||
125 | error: | |
126 | return ret; | |
6364a07a DG |
127 | } |
128 | ||
129 | /* | |
de5e9086 | 130 | * Return allocated lttcomm socket structure. |
6364a07a | 131 | */ |
32dd26fb | 132 | __attribute__((visibility("hidden"))) |
de5e9086 | 133 | struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto) |
6364a07a | 134 | { |
de5e9086 | 135 | struct lttcomm_sock *sock; |
6364a07a | 136 | |
de5e9086 DG |
137 | sock = zmalloc(sizeof(struct lttcomm_sock)); |
138 | if (sock == NULL) { | |
139 | PERROR("zmalloc create sock"); | |
140 | goto end; | |
6364a07a DG |
141 | } |
142 | ||
143 | sock->proto = proto; | |
de5e9086 | 144 | sock->fd = -1; |
6364a07a | 145 | |
de5e9086 DG |
146 | end: |
147 | return sock; | |
6364a07a DG |
148 | } |
149 | ||
150 | /* | |
de5e9086 DG |
151 | * Return an allocated lttcomm socket structure and copy src content into |
152 | * the newly created socket. | |
153 | * | |
154 | * This is mostly useful when lttcomm_sock are passed between process where the | |
155 | * fd and ops have to be changed within the correct address space. | |
6364a07a | 156 | */ |
32dd26fb | 157 | __attribute__((visibility("hidden"))) |
de5e9086 | 158 | struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src) |
6364a07a | 159 | { |
6364a07a DG |
160 | struct lttcomm_sock *sock; |
161 | ||
de5e9086 DG |
162 | /* Safety net */ |
163 | assert(src); | |
164 | ||
165 | sock = lttcomm_alloc_sock(src->proto); | |
6364a07a DG |
166 | if (sock == NULL) { |
167 | goto alloc_error; | |
168 | } | |
169 | ||
de5e9086 | 170 | lttcomm_copy_sock(sock, src); |
6364a07a | 171 | |
de5e9086 | 172 | alloc_error: |
6364a07a | 173 | return sock; |
de5e9086 | 174 | } |
6364a07a | 175 | |
de5e9086 DG |
176 | /* |
177 | * Create and copy socket from an allocated lttcomm socket structure. | |
178 | * | |
179 | * This is mostly useful when lttcomm_sock are passed between process where the | |
180 | * fd and ops have to be changed within the correct address space. | |
181 | */ | |
32dd26fb | 182 | __attribute__((visibility("hidden"))) |
de5e9086 DG |
183 | void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src) |
184 | { | |
185 | /* Safety net */ | |
186 | assert(dst); | |
187 | assert(src); | |
188 | ||
189 | dst->proto = src->proto; | |
190 | dst->fd = src->fd; | |
191 | dst->ops = src->ops; | |
192 | /* Copy sockaddr information from original socket */ | |
193 | memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr)); | |
6364a07a DG |
194 | } |
195 | ||
196 | /* | |
197 | * Init IPv4 sockaddr structure. | |
198 | */ | |
32dd26fb | 199 | __attribute__((visibility("hidden"))) |
6364a07a DG |
200 | int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr, |
201 | const char *ip, unsigned int port) | |
202 | { | |
203 | int ret; | |
204 | ||
205 | assert(sockaddr); | |
206 | assert(ip); | |
207 | assert(port > 0 && port <= 65535); | |
208 | ||
209 | memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); | |
210 | ||
211 | sockaddr->type = LTTCOMM_INET; | |
212 | sockaddr->addr.sin.sin_family = AF_INET; | |
213 | sockaddr->addr.sin.sin_port = htons(port); | |
214 | ret = inet_pton(sockaddr->addr.sin.sin_family, ip, | |
215 | &sockaddr->addr.sin.sin_addr); | |
216 | if (ret < 1) { | |
217 | ret = -1; | |
de5e9086 | 218 | ERR("%s with port %d: unrecognized IPv4 address", ip, port); |
6364a07a DG |
219 | goto error; |
220 | } | |
221 | memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero)); | |
222 | ||
223 | error: | |
224 | return ret; | |
225 | } | |
226 | ||
227 | /* | |
228 | * Init IPv6 sockaddr structure. | |
229 | */ | |
32dd26fb | 230 | __attribute__((visibility("hidden"))) |
6364a07a DG |
231 | int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr, |
232 | const char *ip, unsigned int port) | |
233 | { | |
234 | int ret; | |
235 | ||
236 | assert(sockaddr); | |
237 | assert(ip); | |
238 | assert(port > 0 && port <= 65535); | |
239 | ||
240 | memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); | |
241 | ||
242 | sockaddr->type = LTTCOMM_INET6; | |
243 | sockaddr->addr.sin6.sin6_family = AF_INET6; | |
244 | sockaddr->addr.sin6.sin6_port = htons(port); | |
245 | ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip, | |
246 | &sockaddr->addr.sin6.sin6_addr); | |
247 | if (ret < 1) { | |
248 | ret = -1; | |
249 | goto error; | |
250 | } | |
251 | ||
252 | error: | |
253 | return ret; | |
254 | } | |
de5e9086 DG |
255 | |
256 | /* | |
257 | * Return allocated lttcomm socket structure from lttng URI. | |
258 | */ | |
32dd26fb | 259 | __attribute__((visibility("hidden"))) |
de5e9086 DG |
260 | struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri) |
261 | { | |
262 | int ret; | |
263 | int _sock_proto; | |
264 | struct lttcomm_sock *sock = NULL; | |
265 | ||
266 | /* Safety net */ | |
267 | assert(uri); | |
268 | ||
269 | /* Check URI protocol */ | |
270 | if (uri->proto == LTTNG_TCP) { | |
271 | _sock_proto = LTTCOMM_SOCK_TCP; | |
272 | } else { | |
273 | ERR("Relayd invalid URI proto: %d", uri->proto); | |
274 | goto alloc_error; | |
275 | } | |
276 | ||
277 | sock = lttcomm_alloc_sock(_sock_proto); | |
278 | if (sock == NULL) { | |
279 | goto alloc_error; | |
280 | } | |
281 | ||
282 | /* Check destination type */ | |
283 | if (uri->dtype == LTTNG_DST_IPV4) { | |
284 | ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4, | |
285 | uri->port); | |
286 | if (ret < 0) { | |
287 | goto error; | |
288 | } | |
289 | } else if (uri->dtype == LTTNG_DST_IPV6) { | |
290 | ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6, | |
291 | uri->port); | |
292 | if (ret < 0) { | |
293 | goto error; | |
294 | } | |
295 | } else { | |
296 | /* Command URI is invalid */ | |
297 | ERR("Relayd invalid URI dst type: %d", uri->dtype); | |
298 | goto error; | |
299 | } | |
300 | ||
301 | return sock; | |
302 | ||
303 | error: | |
304 | lttcomm_destroy_sock(sock); | |
305 | alloc_error: | |
306 | return NULL; | |
307 | } | |
308 | ||
309 | /* | |
310 | * Destroy and free lttcomm socket. | |
311 | */ | |
32dd26fb | 312 | __attribute__((visibility("hidden"))) |
de5e9086 DG |
313 | void lttcomm_destroy_sock(struct lttcomm_sock *sock) |
314 | { | |
315 | if (sock != NULL) { | |
316 | free(sock); | |
317 | } | |
318 | } |