aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/server_socket.cpp
blob: 33764fc153c9f27b7bb1686d7f67feea988c658e (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "server_socket.h"
#include "socket_spec.h"
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <chrono>
#include <thread>

#include <vespa/log/log.h>
LOG_SETUP(".vespalib.net.server_socket");

namespace vespalib {

namespace {

SocketHandle adjust_blocking(SocketHandle handle, bool value) {
    if (handle.valid() && handle.set_blocking(value)) {
        return handle;
    } else {
        return SocketHandle();
    }
}

bool is_blocked(int err) { return ((err == EWOULDBLOCK) || (err == EAGAIN)); }

bool is_socket(const vespalib::string &path) {
    struct stat info;
    if (path.empty() || (lstat(path.c_str(), &info) != 0)) {
        return false;
    }
    return S_ISSOCK(info.st_mode);
}

}

void
ServerSocket::cleanup()
{
    if (valid() && is_socket(_path)) {
        unlink(_path.c_str());
    }
}

ServerSocket::ServerSocket(const SocketSpec &spec)
    : _handle(adjust_blocking(spec.server_address().listen(), false)),
      _path(spec.path()),
      _blocking(true),
      _shutdown(false)
{
    if (!_handle.valid() && is_socket(_path)) {
        if (!spec.client_address().connect_async().valid()) {
            LOG(warning, "removing old socket: '%s'", _path.c_str());
            unlink(_path.c_str());
            _handle = spec.server_address().listen();
        }
    }
    if (!_handle.valid()) {
        LOG(warning, "listen failed: '%s'", spec.spec().c_str());
    }
}

ServerSocket::ServerSocket(const vespalib::string &spec)
    : ServerSocket(SocketSpec(spec))
{
}

ServerSocket::ServerSocket(int port)
    : ServerSocket(SocketSpec::from_port(port))
{
}

ServerSocket::ServerSocket(ServerSocket &&rhs)
    : _handle(std::move(rhs._handle)),
      _path(std::move(rhs._path)),
      _blocking(rhs._blocking),
      _shutdown(rhs._shutdown.load(std::memory_order_acquire))
{
    rhs._path.clear();
}

ServerSocket &
ServerSocket::operator=(ServerSocket &&rhs)
{
    cleanup();
    _handle = std::move(rhs._handle);
    _path = std::move(rhs._path);
    _blocking = rhs._blocking;
    _shutdown.store(rhs._shutdown.load(std::memory_order_acquire), std::memory_order_release);
    rhs._path.clear();
    return *this;
}

SocketAddress
ServerSocket::address() const
{
    return SocketAddress::address_of(_handle.get());
}

void
ServerSocket::shutdown()
{
    _shutdown.store(true, std::memory_order_release);
    _handle.shutdown();
}

SocketHandle
ServerSocket::accept()
{
    if (!_blocking) {
        return adjust_blocking(_handle.accept(), true);
    } else {
        for (;;) {
            if (_shutdown.load(std::memory_order_acquire)) {
                errno = EIO;
                return SocketHandle();
            }
            SocketHandle res = _handle.accept();
            if (res.valid() || !is_blocked(errno)) {
                return adjust_blocking(std::move(res), true);
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
}

} // namespace vespalib