Cleanup: use LTTNG_HIDDEN in lttng-ctl filter lib
[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
29#include <common/defaults.h>
30#include <common/error.h>
31
32#include "inet.h"
33
34/*
35 * INET protocol operations.
36 */
37static const struct lttcomm_proto_ops inet_ops = {
38 .bind = lttcomm_bind_inet_sock,
39 .close = lttcomm_close_inet_sock,
40 .connect = lttcomm_connect_inet_sock,
41 .accept = lttcomm_accept_inet_sock,
42 .listen = lttcomm_listen_inet_sock,
43 .recvmsg = lttcomm_recvmsg_inet_sock,
44 .sendmsg = lttcomm_sendmsg_inet_sock,
45};
46
47/*
48 * Creates an PF_INET socket.
49 */
32dd26fb 50__attribute__((visibility("hidden")))
6364a07a
DG
51int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto)
52{
de5e9086 53 int val = 1, ret;
6364a07a
DG
54
55 /* Create server socket */
56 if ((sock->fd = socket(PF_INET, type, proto)) < 0) {
57 PERROR("socket inet");
58 goto error;
59 }
60
61 sock->ops = &inet_ops;
62
63 /*
64 * Set socket option to reuse the address.
65 */
66 ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
67 if (ret < 0) {
68 PERROR("setsockopt inet");
69 goto error;
70 }
71
72 return 0;
73
74error:
75 return -1;
76}
77
78/*
79 * Bind socket and return.
80 */
32dd26fb 81__attribute__((visibility("hidden")))
6364a07a
DG
82int lttcomm_bind_inet_sock(struct lttcomm_sock *sock)
83{
84 int ret;
85
86 ret = bind(sock->fd, &sock->sockaddr.addr.sin,
87 sizeof(sock->sockaddr.addr.sin));
88 if (ret < 0) {
89 PERROR("bind inet");
90 }
91
92 return ret;
93}
94
95/*
96 * Connect PF_INET socket.
97 */
32dd26fb 98__attribute__((visibility("hidden")))
6364a07a
DG
99int lttcomm_connect_inet_sock(struct lttcomm_sock *sock)
100{
101 int ret, closeret;
102
103 ret = connect(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin,
104 sizeof(sock->sockaddr.addr.sin));
105 if (ret < 0) {
106 /*
107 * Don't print message on connect error, because connect is used in
108 * normal execution to detect if sessiond is alive.
109 */
110 goto error_connect;
111 }
112
113 return ret;
114
115error_connect:
116 closeret = close(sock->fd);
117 if (closeret) {
118 PERROR("close inet");
119 }
120
121 return ret;
122}
123
124/*
125 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
126 * MUST be bind(2) before.
127 */
32dd26fb 128__attribute__((visibility("hidden")))
6364a07a
DG
129struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock)
130{
131 int new_fd;
88a5db70 132 socklen_t len;
6364a07a
DG
133 struct lttcomm_sock *new_sock;
134
135 if (sock->proto == LTTCOMM_SOCK_UDP) {
136 /*
137 * accept(2) does not exist for UDP so simply return the passed socket.
138 */
139 new_sock = sock;
140 goto end;
141 }
142
de5e9086 143 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
144 if (new_sock == NULL) {
145 goto error;
146 }
147
88a5db70
DG
148 len = sizeof(new_sock->sockaddr.addr.sin);
149
6364a07a
DG
150 /* Blocking call */
151 new_fd = accept(sock->fd, (struct sockaddr *) &new_sock->sockaddr.addr.sin,
152 &len);
153 if (new_fd < 0) {
154 PERROR("accept inet");
155 goto error;
156 }
157
158 new_sock->fd = new_fd;
de5e9086 159 new_sock->ops = &inet_ops;
6364a07a
DG
160
161end:
162 return new_sock;
163
164error:
165 free(new_sock);
166 return NULL;
167}
168
169/*
170 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
171 */
32dd26fb 172__attribute__((visibility("hidden")))
6364a07a
DG
173int lttcomm_listen_inet_sock(struct lttcomm_sock *sock, int backlog)
174{
175 int ret;
176
177 if (sock->proto == LTTCOMM_SOCK_UDP) {
178 /* listen(2) does not exist for UDP so simply return success. */
179 ret = 0;
180 goto end;
181 }
182
183 /* Default listen backlog */
184 if (backlog <= 0) {
185 backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN;
186 }
187
188 ret = listen(sock->fd, backlog);
189 if (ret < 0) {
190 PERROR("listen inet");
191 }
192
193end:
194 return ret;
195}
196
197/*
198 * Receive data of size len in put that data into the buf param. Using recvmsg
199 * API.
200 *
201 * Return the size of received data.
202 */
32dd26fb 203__attribute__((visibility("hidden")))
6364a07a
DG
204ssize_t lttcomm_recvmsg_inet_sock(struct lttcomm_sock *sock, void *buf,
205 size_t len, int flags)
206{
207 struct msghdr msg;
208 struct iovec iov[1];
209 ssize_t ret = -1;
7c5aef62 210 size_t len_last;
6364a07a
DG
211
212 memset(&msg, 0, sizeof(msg));
213
214 iov[0].iov_base = buf;
215 iov[0].iov_len = len;
216 msg.msg_iov = iov;
217 msg.msg_iovlen = 1;
218
219 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin;
220 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
221
6364a07a 222 do {
7c5aef62 223 len_last = iov[0].iov_len;
6364a07a 224 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62
DG
225 if (ret > 0) {
226 iov[0].iov_base += ret;
227 iov[0].iov_len -= ret;
228 assert(ret <= len_last);
229 }
230 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
6364a07a
DG
231 if (ret < 0) {
232 PERROR("recvmsg inet");
7c5aef62
DG
233 } else if (ret > 0) {
234 ret = len;
6364a07a 235 }
7c5aef62 236 /* Else ret = 0 meaning an orderly shutdown. */
6364a07a
DG
237
238 return ret;
239}
240
241/*
242 * Send buf data of size len. Using sendmsg API.
243 *
244 * Return the size of sent data.
245 */
32dd26fb 246__attribute__((visibility("hidden")))
6364a07a
DG
247ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, void *buf,
248 size_t len, int flags)
249{
250 struct msghdr msg;
251 struct iovec iov[1];
252 ssize_t ret = -1;
253
254 memset(&msg, 0, sizeof(msg));
255
256 iov[0].iov_base = buf;
257 iov[0].iov_len = len;
258 msg.msg_iov = iov;
259 msg.msg_iovlen = 1;
260
261 switch (sock->proto) {
262 case LTTCOMM_SOCK_UDP:
263 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin;
264 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
265 break;
266 default:
267 break;
268 }
269
270 do {
271 ret = sendmsg(sock->fd, &msg, flags);
272 } while (ret < 0 && errno == EINTR);
273 if (ret < 0) {
274 /*
275 * Only warn about EPIPE when quiet mode is deactivated.
276 * We consider EPIPE as expected.
277 */
278 if (errno != EPIPE || !lttng_opt_quiet) {
279 PERROR("sendmsg inet");
280 }
281 }
282
283 return ret;
284}
285
286/*
287 * Shutdown cleanly and close.
288 */
32dd26fb 289__attribute__((visibility("hidden")))
6364a07a
DG
290int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
291{
6e742359 292 int ret;
6364a07a 293
de5e9086 294 /* Don't try to close an invalid marked socket */
6364a07a
DG
295 if (sock->fd == -1) {
296 return 0;
297 }
298
6e742359
DG
299 ret = close(sock->fd);
300 if (ret) {
6364a07a
DG
301 PERROR("close inet");
302 }
303
304 /* Mark socket */
305 sock->fd = -1;
306
307 return ret;
308}
This page took 0.055178 seconds and 4 git commands to generate.