-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
183 lines (148 loc) · 4.57 KB
/
main.cpp
File metadata and controls
183 lines (148 loc) · 4.57 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "api_server.h"
#include "backend_client.h"
#include "event_log.h"
#include "socketd_protocol.h"
#include <csignal>
#include <cstdio>
#include <cstdint>
#include <iostream>
#include <string>
namespace {
using droidspaces::socketd::ApiServer;
using droidspaces::socketd::BackendClient;
using droidspaces::socketd::CapabilitiesResult;
using droidspaces::socketd::TcpListenConfig;
using droidspaces::socketd::parse_tcp_listen_endpoint;
using droidspaces::socketd::SocketdEventAttributes;
using droidspaces::socketd::record_socketd_event;
constexpr std::uint32_t kRequiredBackendCapabilities =
DS_SOCKETD_CAP_PROTOCOL_V1 |
DS_SOCKETD_CAP_PING |
DS_SOCKETD_CAP_CAPABILITIES;
void print_usage(const char* argv0) {
std::cerr
<< "Usage:\n"
<< " " << argv0 << " --listen-tcp [PORT]\n"
<< " " << argv0 << " --listen-tcp [ADDR:PORT]\n"
<< "\n"
<< "Examples:\n"
<< " " << argv0 << " --listen-tcp\n"
<< " " << argv0 << " --listen-tcp 2375\n"
<< " " << argv0 << " --listen-tcp 127.0.0.1:2375\n"
<< " " << argv0 << " --listen-tcp 0.0.0.0:2375\n";
}
bool check_backend(std::string& error) {
BackendClient backend;
if (!backend.ping(error)) {
error = "backend PING failed: " + error;
return false;
}
CapabilitiesResult caps {};
if (!backend.capabilities(caps, error)) {
error = "backend CAPABILITIES failed: " + error;
return false;
}
if ((caps.mask & kRequiredBackendCapabilities) !=
kRequiredBackendCapabilities) {
error = "backend is missing required base capabilities";
return false;
}
// To tty
std::cerr << "socketd: backend handshake OK, capabilities mask: 0x"
<< std::hex
<< caps.mask
<< std::dec
<< '\n';
// To API
const std::string caps_text = "0x" + [&caps]() {
char buffer[32] {};
std::snprintf(buffer, sizeof(buffer), "%x", caps.mask);
return std::string(buffer);
}();
const SocketdEventAttributes attrs[] = {
{"name", "droidspaces-backend"},
{"component", "socketd"},
{"capabilities", caps_text},
};
record_socketd_event("daemon",
"connect",
"droidspaces-backend",
attrs,
sizeof(attrs) / sizeof(attrs[0]));
return true;
}
} // namespace
int main(int argc, char** argv) {
/*
* Avoid process termination if a TCP peer disappears while a response
* is being written.
*/
(void)std::signal(SIGPIPE, SIG_IGN);
bool listen_tcp = false;
TcpListenConfig tcp_config {};
for (int i = 1; i < argc; ++i) {
const std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
print_usage(argv[0]);
return 0;
}
if (arg == "--listen-tcp") {
if (listen_tcp) {
std::cerr << "socketd: --listen-tcp specified more than once\n";
return 2;
}
listen_tcp = true;
if (i + 1 < argc) {
const std::string next = argv[i + 1];
if (!next.empty() && next[0] != '-') {
std::string parse_error;
if (!parse_tcp_listen_endpoint(next, tcp_config, parse_error)) {
std::cerr << "socketd: " << parse_error << '\n';
return 2;
}
++i;
}
}
continue;
}
if (arg.rfind("--listen-tcp=", 0) == 0) {
if (listen_tcp) {
std::cerr << "socketd: --listen-tcp specified more than once\n";
return 2;
}
listen_tcp = true;
const std::string value = arg.substr(std::string("--listen-tcp=").size());
std::string parse_error;
if (!parse_tcp_listen_endpoint(value, tcp_config, parse_error)) {
std::cerr << "socketd: " << parse_error << '\n';
return 2;
}
continue;
}
std::cerr << "socketd: unknown argument: " << arg << '\n';
print_usage(argv[0]);
return 2;
}
if (!listen_tcp) {
std::cerr << "socketd: no listener configured\n";
print_usage(argv[0]);
return 2;
}
std::string error;
if (!check_backend(error)) {
/*
* Keep the HTTP listener available even when the privileged backend is
* absent. This lets the socketd HTTP/static WebUI surface be tested in
* isolation. Backend-dependent Docker API routes still fail normally when
* they attempt their per-request BackendClient RPC.
*/
std::cerr << "socketd: warning: " << error << '\n';
std::cerr << "socketd: warning: continuing without backend handshake\n";
}
ApiServer server(tcp_config);
if (!server.run(error)) {
std::cerr << "socketd: server failed: " << error << '\n';
return 1;
}
return 0;
}