Introduce LTTNG_NETWORK_SOCKET_TIMEOUT env. var
[lttng-tools.git] / src / common / sessiond-comm / inet.c
CommitLineData
6364a07a
DG
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
90e535ef 29#include <common/common.h>
6364a07a
DG
30
31#include "inet.h"
32
33/*
34 * INET protocol operations.
35 */
36static const struct lttcomm_proto_ops inet_ops = {
37 .bind = lttcomm_bind_inet_sock,
38 .close = lttcomm_close_inet_sock,
39 .connect = lttcomm_connect_inet_sock,
40 .accept = lttcomm_accept_inet_sock,
41 .listen = lttcomm_listen_inet_sock,
42 .recvmsg = lttcomm_recvmsg_inet_sock,
43 .sendmsg = lttcomm_sendmsg_inet_sock,
44};
45
46/*
47 * Creates an PF_INET socket.
48 */
90e535ef 49LTTNG_HIDDEN
6364a07a
DG
50int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto)
51{
de5e9086 52 int val = 1, ret;
6364a07a
DG
53
54 /* Create server socket */
55 if ((sock->fd = socket(PF_INET, type, proto)) < 0) {
56 PERROR("socket inet");
57 goto error;
58 }
59
60 sock->ops = &inet_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 inet");
68 goto error;
69 }
70
71 return 0;
72
73error:
74 return -1;
75}
76
77/*
78 * Bind socket and return.
79 */
90e535ef 80LTTNG_HIDDEN
6364a07a
DG
81int lttcomm_bind_inet_sock(struct lttcomm_sock *sock)
82{
83 int ret;
84
85 ret = bind(sock->fd, &sock->sockaddr.addr.sin,
86 sizeof(sock->sockaddr.addr.sin));
87 if (ret < 0) {
88 PERROR("bind inet");
89 }
90
91 return ret;
92}
93
94/*
95 * Connect PF_INET socket.
96 */
90e535ef 97LTTNG_HIDDEN
6364a07a
DG
98int lttcomm_connect_inet_sock(struct lttcomm_sock *sock)
99{
100 int ret, closeret;
101
102 ret = connect(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin,
103 sizeof(sock->sockaddr.addr.sin));
104 if (ret < 0) {
82c05d47 105 PERROR("connect");
6364a07a
DG
106 goto error_connect;
107 }
108
109 return ret;
110
111error_connect:
112 closeret = close(sock->fd);
113 if (closeret) {
114 PERROR("close inet");
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 */
90e535ef 124LTTNG_HIDDEN
6364a07a
DG
125struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock)
126{
127 int new_fd;
88a5db70 128 socklen_t len;
6364a07a
DG
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
de5e9086 139 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
140 if (new_sock == NULL) {
141 goto error;
142 }
143
88a5db70
DG
144 len = sizeof(new_sock->sockaddr.addr.sin);
145
6364a07a
DG
146 /* Blocking call */
147 new_fd = accept(sock->fd, (struct sockaddr *) &new_sock->sockaddr.addr.sin,
148 &len);
149 if (new_fd < 0) {
150 PERROR("accept inet");
151 goto error;
152 }
153
154 new_sock->fd = new_fd;
de5e9086 155 new_sock->ops = &inet_ops;
6364a07a
DG
156
157end:
158 return new_sock;
159
160error:
161 free(new_sock);
162 return NULL;
163}
164
165/*
166 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
167 */
90e535ef 168LTTNG_HIDDEN
6364a07a
DG
169int lttcomm_listen_inet_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 inet");
187 }
188
189end:
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 */
90e535ef 199LTTNG_HIDDEN
6364a07a
DG
200ssize_t lttcomm_recvmsg_inet_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;
7c5aef62 206 size_t len_last;
6364a07a
DG
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.sin;
216 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
217
6364a07a 218 do {
7c5aef62 219 len_last = iov[0].iov_len;
6364a07a 220 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62
DG
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));
6364a07a
DG
227 if (ret < 0) {
228 PERROR("recvmsg inet");
7c5aef62
DG
229 } else if (ret > 0) {
230 ret = len;
6364a07a 231 }
7c5aef62 232 /* Else ret = 0 meaning an orderly shutdown. */
6364a07a
DG
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 */
90e535ef 242LTTNG_HIDDEN
6364a07a
DG
243ssize_t lttcomm_sendmsg_inet_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.sin;
260 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
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 inet");
276 }
277 }
278
279 return ret;
280}
281
282/*
283 * Shutdown cleanly and close.
284 */
90e535ef 285LTTNG_HIDDEN
6364a07a
DG
286int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
287{
6e742359 288 int ret;
6364a07a 289
de5e9086 290 /* Don't try to close an invalid marked socket */
6364a07a
DG
291 if (sock->fd == -1) {
292 return 0;
293 }
294
6e742359
DG
295 ret = close(sock->fd);
296 if (ret) {
6364a07a
DG
297 PERROR("close inet");
298 }
299
300 /* Mark socket */
301 sock->fd = -1;
302
303 return ret;
304}
This page took 0.058608 seconds and 4 git commands to generate.