-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake-daemon.cpp
268 lines (219 loc) · 9.14 KB
/
fake-daemon.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*************************************************************************
* Copyright (C) 2017-2019 Barcelona Supercomputing Center *
* Centro Nacional de Supercomputacion *
* All rights reserved. *
* *
* This file is part of NORNS, a service that allows other programs to *
* start, track and manage asynchronous transfers of data resources *
* between different storage backends. *
* *
* See AUTHORS file in the top level directory for information regarding *
* developers and contributors. *
* *
* This software was developed as part of the EC H2020 funded project *
* NEXTGenIO (Project ID: 671951). *
* www.nextgenio.eu *
* *
* Permission is hereby granted, free of charge, to any person obtaining *
* a copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to *
* the following conditions: *
* *
* The above copyright notice and this permission notice shall be *
* included in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS *
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN *
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN *
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
*************************************************************************/
#ifdef DEBUG_OUTPUT
#include <iostream>
#endif
#include <chrono>
#include "nornsctl.h"
#include "fake-daemon.hpp"
const norns::config::settings fake_daemon::default_cfg(
"test_urd", /* progname */
false, /* daemonize */
false, /* use syslog */
false, /* use console */
{},// "./test_urd.log", /* log file */
0, /* unused */
false, /* dry run */
100, /* dry run duration */
"./test_urd.global.socket", /* global_socket */
"./test_urd.control.socket", /* control_socket */
"127.0.0.1", /* bind address */
42002, /* remote port */
"./test_urd.pid", /* daemon_pidfile */
2, /* api workers */
"./tmp/", /* staging directory */
128,
"./",
{}
);
fake_daemon::fake_daemon() {
// extern const char* norns_api_global_socket;
// extern const char* norns_api_control_socket;
// norns_api_global_socket = "./test_urd.global.socket";
// norns_api_control_socket = "./test_urd.control.socket";
}
fake_daemon::~fake_daemon() {
if(m_running) {
stop();
}
}
void fake_daemon::configure(const bfs::path& config_file,
const fake_daemon_cfg& override_cfg) {
#if 1
// most settings are taken from the default configuration for tests
m_config = default_cfg;
// some settings are taken from the configuration file auto-generated
// for this test (mostly those path-related)
norns::config::settings file_config;
file_config.load_from_file(config_file);
m_config.log_file(file_config.log_file());
m_config.global_socket(file_config.global_socket());
m_config.control_socket(file_config.control_socket());
m_config.pidfile(file_config.pidfile());
m_config.staging_directory(file_config.staging_directory());
m_config.config_file(config_file);
// we allow some settings to be overriden
m_config.dry_run(override_cfg.m_dry_run);
m_config.dry_run_duration(override_cfg.m_dry_run_duration);
// for now default_namespaces is empty
m_config.default_namespaces().clear();
#endif
#if 0
m_config.load_from_file(config_file);
m_config.progname(default_cfg.progname());
m_config.daemonize(default_cfg.daemonize());
m_config.use_syslog(default_cfg.use_syslog());
m_config.use_console(default_cfg.use_console());
m_config.dry_run(override_cfg.m_dry_run);
m_config.dry_run_duration(override_cfg.m_dry_run_duration);
m_config.bind_address(default_cfg.bind_address());
m_config.remote_port(default_cfg.remote_port());
m_config.workers_in_pool(default_cfg.workers_in_pool());
m_config.staging_directory(default_cfg.staging_directory());
m_config.config_file(config_file);
m_config.default_namespaces().clear();
#endif
}
void fake_daemon::configure(const bfs::path& config_file,
const std::string& alias) {
#if 1
// most settings are taken from the configuration file auto-generated
// for this test (mostly those path-related)
m_config.load_from_file(config_file);
m_config.progname(default_cfg.progname() + alias);
m_config.daemonize(default_cfg.daemonize());
m_config.use_console(default_cfg.use_console());
m_config.config_file(config_file);
#endif
#if 0
m_config.load_from_file(config_file);
m_config.progname(default_cfg.progname() + alias);
m_config.daemonize(default_cfg.daemonize());
m_config.use_syslog(default_cfg.use_syslog());
m_config.use_console(default_cfg.use_console());
m_config.dry_run(default_cfg.dry_run());
m_config.dry_run_duration(default_cfg.dry_run_duration());
m_config.bind_address(default_cfg.bind_address());
m_config.remote_port(default_cfg.remote_port());
m_config.workers_in_pool(default_cfg.workers_in_pool());
m_config.staging_directory(default_cfg.staging_directory());
m_config.config_file(config_file);
m_config.default_namespaces().clear();
#endif
}
//extern "C" void __gcov_flush (void);
void fake_daemon::run() {
m_running = true;
m_pid = fork();
switch(m_pid) {
/* child code */
case 0:
{
// disable any signal handlers set by Catch2
// so that we can receive control signals from the parent process
for(auto signum : { SIGINT, SIGILL, SIGFPE,
SIGSEGV, SIGTERM, SIGABRT }) {
struct sigaction sa;
sa.sa_handler = SIG_DFL;
if(::sigaction(signum, &sa, NULL) != 0) {
throw std::runtime_error("Failed to reset Catch2 signal handler");
}
}
m_daemon.configure(m_config);
m_daemon.run();
m_daemon.teardown();
#ifdef DEBUG_OUTPUT
std::cerr << "[" << getpid() << "] exitting...\n";
#endif
// __gcov_flush();
exit(0);
}
/* error code */
case -1:
throw std::runtime_error("Failed to spawn test daemon");
/* parent code */
default:
{
#ifdef DEBUG_OUTPUT
std::cerr << "[" << getpid() << "] daemon process spawned (" << m_pid << ")\n";
#endif
int rv;
int retries = 20;
do {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
rv = nornsctl_send_command(NORNSCTL_CMD_PING, NULL);
} while(rv != NORNS_SUCCESS && --retries != 0);
if(retries == 0) {
// the daemon may be running even if we don't receive a reply,
// try to stop it to avoid leaving a dangling process
stop();
throw std::runtime_error("Failed to ping test daemon");
}
#ifdef DEBUG_OUTPUT
std::cerr << "[" << getpid() << "] daemon process ready\n";
#endif
}
}
}
int fake_daemon::stop() {
if(!m_running) {
return 0;
}
if(m_pid != 0) {
#ifdef DEBUG_OUTPUT
std::cerr << "[" << getpid() << "] Sending SIGTERM to " << m_pid << "\n";
#endif
if(kill(m_pid, SIGTERM) != 0) {
#ifdef DEBUG_OUTPUT
std::cerr << "[" << getpid() << "] Unable to send SIGTERM to daemon process: " << strerror(errno) << "\n";
#endif
return -1;
}
int status;
if(waitpid(m_pid, &status, 0) == -1) {
#ifdef DEBUG_OUTPUT
std::cerr << "[" << getpid() << "] Unable to wait for daemon process\n";
#endif
return -1;
}
#ifdef DEBUG_OUTPUT
std::cerr << "[" << getpid() << "] Daemon process exited with status " << status << "\n";
#endif
m_running = false;
}
return 0;
}