-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmain.cpp
More file actions
208 lines (175 loc) · 6.03 KB
/
Copy pathmain.cpp
File metadata and controls
208 lines (175 loc) · 6.03 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
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
/********************************************************************************
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include <unistd.h>
#include <chrono>
#include <ctime>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
#ifdef __linux__
#include <linux/prctl.h>
#include <sys/prctl.h>
#endif
#include <score/mw/lifecycle/application.h>
#include <score/mw/lifecycle/runapplication.h>
/// @brief CLI configuration options for the not_supervised_application process
struct Config
{
std::int32_t responseTimeInMs{100};
bool crashRequested{false};
std::int32_t crashTimeInMs{1000};
bool failToStart{false};
bool verbose{false};
};
std::string helpSstring =
"Usage:\n\
-r <response time in ms> Worst case response time to SIGTERM signal in milliseconds.\n\
-c <crash time in ms> Simulate crash of the application, after specified time in milliseconds.\n\
-s Simulate failure during start-up of the application.\n\
-v Run in verbose mode.\n";
std::optional<Config> parseOptions(int argc, char* const* argv) noexcept
{
Config config{};
int c;
while ((c = getopt(argc, argv, ":r:c:svh")) != -1)
{
switch (static_cast<char>(c))
{
case 'r':
config.responseTimeInMs = std::stoi(optarg);
break;
case 'c':
config.crashRequested = true;
config.crashTimeInMs = std::stoi(optarg);
break;
case 's':
config.failToStart = true;
break;
case 'h':
std::cout << helpSstring;
return std::nullopt;
case 'v':
config.verbose = true;
break;
case '?':
std::cout << "Unrecognized option: -" << static_cast<char>(optopt) << std::endl;
std::cout << helpSstring;
return std::nullopt;
default:
break;
}
}
return config;
}
void set_process_name()
{
const char* identifier = getenv("PROCESSIDENTIFIER");
if (identifier != nullptr)
{
#ifdef __QNXNTO__
if (pthread_setname_np(pthread_self(), identifier) != 0)
{
std::cerr << "Failed to set QNX thread name" << std::endl;
}
#elif defined(__linux__)
if (prctl(PR_SET_NAME, identifier) < 0)
{
std::cerr << "Failed to set process name to " << identifier << std::endl;
}
#endif
}
}
class LifecycleApp final : public score::mw::lifecycle::Application
{
public:
std::int32_t Initialize(const score::mw::lifecycle::ApplicationContext& appCtx) override
{
set_process_name();
// Build a classic argv for getopt() from ApplicationContext arguments
const auto& args = appCtx.get_arguments();
m_argvStorage.clear();
m_argvStorage.reserve(args.size() + 2);
// Ensure argv[0] exists (getopt expects it)
if (args.empty())
{
m_argvStorage.push_back(const_cast<char*>("LifecycleApp"));
}
else
{
for (const auto& s : args)
{
// NOTE: relies on the underlying storage staying alive during Initialize().
m_argvStorage.push_back(const_cast<char*>(s.data()));
}
}
m_argvStorage.push_back(nullptr);
optind = 1;
const int argcLocal = static_cast<int>(m_argvStorage.size() - 1);
const auto config = parseOptions(argcLocal, m_argvStorage.data());
if (!config)
{
return EXIT_FAILURE;
}
m_config = *config;
if (true == m_config.failToStart)
{
return EXIT_FAILURE;
}
return 0;
}
std::int32_t Run(const score::cpp::stop_token& stopToken) override
{
std::chrono::time_point<std::chrono::steady_clock> startTime = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> runTime;
timespec req{static_cast<time_t>(m_config.responseTimeInMs / 1000),
static_cast<long>((m_config.responseTimeInMs % 1000) * 1000000L)};
auto timeLastVerboseLog = std::chrono::steady_clock::now();
while (!stopToken.stop_requested())
{
if (true == m_config.crashRequested)
{
runTime = std::chrono::steady_clock::now() - startTime;
int timeTillCrash = static_cast<int>(m_config.crashTimeInMs - runTime.count());
if (timeTillCrash < m_config.responseTimeInMs)
{
if (timeTillCrash > 0)
{
timespec crash_req{static_cast<time_t>(timeTillCrash / 1000),
static_cast<long>((timeTillCrash % 1000) * 1000000L)};
nanosleep(&crash_req, nullptr);
}
std::abort();
}
}
if (m_config.verbose)
{
const auto now = std::chrono::steady_clock::now();
if (now - timeLastVerboseLog >= std::chrono::seconds(1))
{
std::cout << "LifecycleApp: Running in verbose mode" << std::endl;
timeLastVerboseLog = now;
}
}
nanosleep(&req, nullptr);
}
return EXIT_SUCCESS;
}
private:
Config m_config{};
std::vector<char*> m_argvStorage{};
};
int main(int argc, char** argv)
{
return score::mw::lifecycle::run_application<LifecycleApp>(argc, argv);
}