aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/socket_utils.cpp
blob: 78f1193828563cceac82344d238c63bcdf73feaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "socket_utils.h"
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <cassert>

namespace vespalib::socketutils {

void set_blocking(int fd, bool blocking)
{
    int flags = fcntl(fd, F_GETFL, 0);
    if (blocking) {
        flags &= ~O_NONBLOCK;
    } else {
        flags |= O_NONBLOCK;
    }
    int res = fcntl(fd, F_SETFL, flags);
    assert(res == 0);
}

void nonblocking_pipe(int pipefd[2])
{
    int res = pipe(pipefd);
    assert(res == 0);
    set_blocking(pipefd[0], false);
    set_blocking(pipefd[1], false);
}

void nonblocking_socketpair(int domain, int type, int protocol, int socketfd[2])
{
    int res = socketpair(domain, type, protocol, socketfd);
    assert(res == 0);
    set_blocking(socketfd[0], false);
    set_blocking(socketfd[1], false);
}

}