Fix: print errno message on connect() error
[lttng-tools.git] / src / common / sessiond-comm / inet6.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <errno.h>
28
29 #include <common/common.h>
30
31 #include "inet6.h"
32
33 /*
34 * INET protocol operations.
35 */
36 static const struct lttcomm_proto_ops inet6_ops = {
37 .bind = lttcomm_bind_inet6_sock,
38 .close = lttcomm_close_inet6_sock,
39 .connect = lttcomm_connect_inet6_sock,
40 .accept = lttcomm_accept_inet6_sock,
41 .listen = lttcomm_listen_inet6_sock,
42 .recvmsg = lttcomm_recvmsg_inet6_sock,
43 .sendmsg = lttcomm_sendmsg_inet6_sock,
44 };
45
46 /*
47 * Creates an PF_INET socket.
48 */
49 LTTNG_HIDDEN
50 int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto)
51 {
52 int val = 1, ret;
53
54 /* Create server socket */
55 if ((sock->fd = socket(PF_INET6, type, proto)) < 0) {
56 PERROR("socket inet6");
57 goto error;
58 }
59
60 sock->ops = &inet6_ops;
61
62 /*
63 * Set socket option to reuse the address.
64 */
65 ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
66 if (ret < 0) {
67 PERROR("setsockopt inet6");
68 goto error;
69 }
70
71 return 0;
72
73 error:
74 return -1;
75 }
76
77 /*
78 * Bind socket and return.
79 */
80 LTTNG_HIDDEN
81 int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock)
82 {
83 int ret;
84
85 ret = bind(sock->fd, &sock->sockaddr.addr.sin6,
86 sizeof(sock->sockaddr.addr.sin6));
87 if (ret < 0) {
88 PERROR("bind inet6");
89 }
90
91 return ret;
92 }
93
94 /*
95 * Connect PF_INET socket.
96 */
97 LTTNG_HIDDEN
98 int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock)
99 {
100 int ret, closeret;
101
102 ret = connect(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin6,
103 sizeof(sock->sockaddr.addr.sin6));
104 if (ret < 0) {
105 PERROR("connect inet6");
106 goto error_connect;
107 }
108
109 return ret;
110
111 error_connect:
112 closeret = close(sock->fd);
113 if (closeret) {
114 PERROR("close inet6");
115 }
116
117 return ret;
118 }
119
120 /*
121 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
122 * MUST be bind(2) before.
123 */
124 LTTNG_HIDDEN
125 struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock)
126 {
127 int new_fd;
128 socklen_t len;
129 struct lttcomm_sock *new_sock;
130
131 if (sock->proto == LTTCOMM_SOCK_UDP) {
132 /*
133 * accept(2) does not exist for UDP so simply return the passed socket.
134 */
135 new_sock = sock;
136 goto end;
137 }
138
139 new_sock = lttcomm_alloc_sock(sock->proto);
140 if (new_sock == NULL) {
141 goto error;
142 }
143
144 len = sizeof(new_sock->sockaddr.addr.sin6);
145
146 /* Blocking call */
147 new_fd = accept(sock->fd,
148 (struct sockaddr *) &new_sock->sockaddr.addr.sin6, &len);
149 if (new_fd < 0) {
150 PERROR("accept inet6");
151 goto error;
152 }
153
154 new_sock->fd = new_fd;
155 new_sock->ops = &inet6_ops;
156
157 end:
158 return new_sock;
159
160 error:
161 free(new_sock);
162 return NULL;
163 }
164
165 /*
166 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
167 */
168 LTTNG_HIDDEN
169 int lttcomm_listen_inet6_sock(struct lttcomm_sock *sock, int backlog)
170 {
171 int ret;
172
173 if (sock->proto == LTTCOMM_SOCK_UDP) {
174 /* listen(2) does not exist for UDP so simply return success. */
175 ret = 0;
176 goto end;
177 }
178
179 /* Default listen backlog */
180 if (backlog <= 0) {
181 backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN;
182 }
183
184 ret = listen(sock->fd, backlog);
185 if (ret < 0) {
186 PERROR("listen inet6");
187 }
188
189 end:
190 return ret;
191 }
192
193 /*
194 * Receive data of size len in put that data into the buf param. Using recvmsg
195 * API.
196 *
197 * Return the size of received data.
198 */
199 LTTNG_HIDDEN
200 ssize_t lttcomm_recvmsg_inet6_sock(struct lttcomm_sock *sock, void *buf,
201 size_t len, int flags)
202 {
203 struct msghdr msg;
204 struct iovec iov[1];
205 ssize_t ret = -1;
206 size_t len_last;
207
208 memset(&msg, 0, sizeof(msg));
209
210 iov[0].iov_base = buf;
211 iov[0].iov_len = len;
212 msg.msg_iov = iov;
213 msg.msg_iovlen = 1;
214
215 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6;
216 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
217
218 do {
219 len_last = iov[0].iov_len;
220 ret = recvmsg(sock->fd, &msg, flags);
221 if (ret > 0) {
222 iov[0].iov_base += ret;
223 iov[0].iov_len -= ret;
224 assert(ret <= len_last);
225 }
226 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
227 if (ret < 0) {
228 PERROR("recvmsg inet");
229 } else if (ret > 0) {
230 ret = len;
231 }
232 /* Else ret = 0 meaning an orderly shutdown. */
233
234 return ret;
235 }
236
237 /*
238 * Send buf data of size len. Using sendmsg API.
239 *
240 * Return the size of sent data.
241 */
242 LTTNG_HIDDEN
243 ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, void *buf,
244 size_t len, int flags)
245 {
246 struct msghdr msg;
247 struct iovec iov[1];
248 ssize_t ret = -1;
249
250 memset(&msg, 0, sizeof(msg));
251
252 iov[0].iov_base = buf;
253 iov[0].iov_len = len;
254 msg.msg_iov = iov;
255 msg.msg_iovlen = 1;
256
257 switch (sock->proto) {
258 case LTTCOMM_SOCK_UDP:
259 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6;
260 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
261 break;
262 default:
263 break;
264 }
265
266 do {
267 ret = sendmsg(sock->fd, &msg, flags);
268 } while (ret < 0 && errno == EINTR);
269 if (ret < 0) {
270 /*
271 * Only warn about EPIPE when quiet mode is deactivated.
272 * We consider EPIPE as expected.
273 */
274 if (errno != EPIPE || !lttng_opt_quiet) {
275 PERROR("sendmsg inet6");
276 }
277 }
278
279 return ret;
280 }
281
282 /*
283 * Shutdown cleanly and close.
284 */
285 LTTNG_HIDDEN
286 int lttcomm_close_inet6_sock(struct lttcomm_sock *sock)
287 {
288 int ret;
289
290 /* Don't try to close an invalid marked socket */
291 if (sock->fd == -1) {
292 return 0;
293 }
294
295 ret = close(sock->fd);
296 if (ret) {
297 PERROR("close inet6");
298 }
299
300 /* Mark socket */
301 sock->fd = -1;
302
303 return ret;
304 }
This page took 0.036339 seconds and 5 git commands to generate.