Add lttng-error.h containing every API err. code
[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 #include <assert.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #include <common/defaults.h>
31 #include <common/error.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 struct lttcomm_net_family net_families[] = {
43 { LTTCOMM_INET, lttcomm_create_inet_sock },
44 { LTTCOMM_INET6, lttcomm_create_inet6_sock },
45 };
46
47 /*
48 * Human readable error message.
49 */
50 static const char *lttcomm_readable_code[] = {
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",
65
66 /* Last element */
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code"
68 };
69
70 /*
71 * Return ptr to string representing a human readable error code from the
72 * lttcomm_return_code enum.
73 *
74 * These code MUST be negative in other to treat that as an error value.
75 */
76 const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
77 {
78 code = -code;
79
80 if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY && code > LTTCOMM_NR) {
81 code = LTTCOMM_NR;
82 }
83
84 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)];
85 }
86
87 /*
88 * Create socket from an already allocated lttcomm socket structure and init
89 * sockaddr in the lttcomm sock.
90 */
91 int lttcomm_create_sock(struct lttcomm_sock *sock)
92 {
93 int ret, _sock_type, _sock_proto, domain;
94
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;
102 }
103
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 }
117
118 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
119 if (ret < 0) {
120 goto error;
121 }
122
123 error:
124 return ret;
125 }
126
127 /*
128 * Return allocated lttcomm socket structure.
129 */
130 struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
131 {
132 struct lttcomm_sock *sock;
133
134 sock = zmalloc(sizeof(struct lttcomm_sock));
135 if (sock == NULL) {
136 PERROR("zmalloc create sock");
137 goto end;
138 }
139
140 sock->proto = proto;
141 sock->fd = -1;
142
143 end:
144 return sock;
145 }
146
147 /*
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.
153 */
154 struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
155 {
156 struct lttcomm_sock *sock;
157
158 /* Safety net */
159 assert(src);
160
161 sock = lttcomm_alloc_sock(src->proto);
162 if (sock == NULL) {
163 goto alloc_error;
164 }
165
166 lttcomm_copy_sock(sock, src);
167
168 alloc_error:
169 return sock;
170 }
171
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 */
178 void 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));
189 }
190
191 /*
192 * Init IPv4 sockaddr structure.
193 */
194 int 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;
212 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
213 goto error;
214 }
215 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
216
217 error:
218 return ret;
219 }
220
221 /*
222 * Init IPv6 sockaddr structure.
223 */
224 int 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
245 error:
246 return ret;
247 }
248
249 /*
250 * Return allocated lttcomm socket structure from lttng URI.
251 */
252 struct 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
295 error:
296 lttcomm_destroy_sock(sock);
297 alloc_error:
298 return NULL;
299 }
300
301 /*
302 * Destroy and free lttcomm socket.
303 */
304 void lttcomm_destroy_sock(struct lttcomm_sock *sock)
305 {
306 if (sock != NULL) {
307 free(sock);
308 }
309 }
This page took 0.061669 seconds and 4 git commands to generate.