Repository navigation
Expand file tree
/
Copy pathdaemon_control.cpp
More file actions
351 lines (321 loc) · 12.2 KB
/
Copy pathdaemon_control.cpp
File metadata and controls
351 lines (321 loc) · 12.2 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Daemon / service control implementation. See include/ragger/daemon_control.h.
* Moved verbatim out of main.cpp (the launchctl/systemctl service plumbing).
*/
#include "daemon_control.h"
#include "lang.h"
#include "Logger.h"
#include <chrono>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <format>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include <pwd.h>
#include <unistd.h>
#include <sys/wait.h>
namespace fs = std::filesystem;
namespace ragger {
// Wrappers around launchctl (macOS) or systemctl --user (Linux) that
// operate on the user-level service installed by install-bin.sh.
// The daemon process itself is still `ragger serve` (what launchd/systemd run).
//
// Design: idempotent + friendly.
// start → "already running" if it is, else launch it
// stop → "not running" if it isn't, else stop it
// restart → cycle it whether up or down
// status → one-line summary (running pid / stopped / not loaded)
// -----------------------------------------------------------------------
namespace {
/// Run a shell command, return captured stdout. Silent on failure (returns "").
std::string capture_output(const std::string &cmd) {
FILE *pipe = popen((cmd + " 2>/dev/null").c_str(), "r");
if (!pipe) return "";
std::string out;
char buf[4096];
while (std::fgets(buf, sizeof(buf), pipe)) out += buf;
pclose(pipe);
return out;
}
/// Run a shell command, discard output, return true iff exit code 0.
bool run_quiet(const std::string &cmd) {
int rc = std::system((cmd + " >/dev/null 2>&1").c_str());
return WIFEXITED(rc) && WEXITSTATUS(rc) == 0;
}
/// Kill every `ragger` process owned by the current user except this one.
/// Used by `stop` and `restart` to sweep MCP servers and stray CLI
/// invocations — MCP is typically launched by a client (OpenClaw, Claude
/// Desktop) and has no service manager of its own, so we have to reap it here.
/// Sends SIGTERM, waits briefly, then SIGKILL to any survivors.
/// Returns the number of processes signaled.
int kill_other_ragger_instances() {
pid_t self = getpid();
auto out = capture_output(
"pgrep -u " + std::to_string(getuid()) + " -x ragger");
std::vector<pid_t> pids;
std::istringstream iss(out);
std::string line;
while (std::getline(iss, line)) {
try {
pid_t pid = std::stoi(line);
if (pid != self && pid > 1) pids.push_back(pid);
}
catch (...) {
/* skip malformed */
}
}
if (pids.empty()) return 0;
for (pid_t p: pids) ::kill(p, SIGTERM);
std::this_thread::sleep_for(std::chrono::milliseconds(400));
for (pid_t p: pids) {
if (::kill(p, 0) == 0) ::kill(p, SIGKILL); // still alive → force
}
return static_cast<int>(pids.size());
}
} // anonymous namespace
int daemon_control(const std::string &action) {
struct passwd *pw = getpwuid(getuid());
std::string home = pw ? pw->pw_dir : (std::getenv("HOME") ? std::getenv("HOME") : "");
if (home.empty()) {
Diskerror::Logger::error(ragger::lang::ERR_HOME_NOT_FOUND);
return 1;
}
#if defined(__APPLE__)
const std::string label = "com.diskerror.ragger";
const std::string plist = home + "/Library/LaunchAgents/" + label + ".plist";
const std::string target = "gui/" + std::to_string(getuid()) + "/" + label;
const std::string domain = "gui/" + std::to_string(getuid());
// Loaded = bootstrapped into the gui domain. Running = has a live pid.
auto is_loaded = [&]() { return run_quiet("launchctl print " + target); };
auto get_pid = [&]() -> std::string {
if (!is_loaded()) return "";
auto out = capture_output("launchctl print " + target);
auto pos = out.find("\n\tpid = ");
if (pos == std::string::npos) pos = out.find("\n pid = ");
if (pos == std::string::npos) return "";
auto s = out.find('=', pos) + 1;
while (s < out.size() && out[s] == ' ') ++s;
auto e = out.find('\n', s);
return out.substr(s, e - s);
};
auto is_running = [&]() { return !get_pid().empty(); };
auto ensure_plist = [&]() -> bool {
if (fs::exists(plist)) return true;
Diskerror::Logger::error(std::format(ragger::lang::ERR_PLIST_NOT_FOUND, plist));
return false;
};
if (action == "start") {
if (!ensure_plist()) return 1;
if (is_running()) {
std::cout << std::format(ragger::lang::MSG_ALREADY_RUNNING, get_pid()) << "\n";
return 0;
}
if (is_loaded()) {
if (!run_quiet("launchctl kickstart " + target)) {
Diskerror::Logger::error(ragger::lang::ERR_LAUNCHCTL_KICKSTART);
return 1;
}
}
else {
if (!run_quiet("launchctl bootstrap " + domain + " " + plist)) {
Diskerror::Logger::error(ragger::lang::ERR_LAUNCHCTL_BOOTSTRAP);
return 1;
}
}
auto pid = get_pid();
if (!pid.empty())
std::cout << std::format(ragger::lang::MSG_STARTED_PID, pid) << "\n";
else
std::cout << ragger::lang::MSG_STARTED << "\n";
return 0;
}
if (action == "stop") {
bool daemon_was_running = is_loaded() && is_running();
if (daemon_was_running) {
if (!run_quiet("launchctl bootout " + target)) {
Diskerror::Logger::error(ragger::lang::ERR_LAUNCHCTL_BOOTOUT);
return 1;
}
}
// Sweep any other ragger processes this user owns (MCP servers
// launched by clients, stray CLI runs, etc.).
int extras = kill_other_ragger_instances();
if (daemon_was_running && extras > 0) {
if (extras == 1)
std::cout << std::format(ragger::lang::MSG_STOPPED_EXTRA_1, extras) << "\n";
else
std::cout << std::format(ragger::lang::MSG_STOPPED_EXTRA_N, extras) << "\n";
}
else if (daemon_was_running) {
std::cout << ragger::lang::MSG_STOPPED << "\n";
}
else if (extras > 0) {
if (extras == 1)
std::cout << std::format(ragger::lang::MSG_EXTRAS_ONLY_1, extras) << "\n";
else
std::cout << std::format(ragger::lang::MSG_EXTRAS_ONLY_N, extras) << "\n";
}
else {
std::cout << ragger::lang::MSG_NOT_RUNNING << "\n";
}
return 0;
}
if (action == "restart") {
if (!ensure_plist()) return 1;
bool was_running = is_running();
if (is_loaded()) {
run_quiet("launchctl bootout " + target);
// bootout returns before launchd completes teardown — wait for
// the domain to actually release the label (up to 3s).
for (int i = 0; i < 30 && is_loaded(); ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
// Sweep MCP + stray instances before relaunching the daemon.
// (Clients will need to reconnect their MCP subprocess themselves.)
kill_other_ragger_instances();
if (!run_quiet("launchctl bootstrap " + domain + " " + plist)) {
Diskerror::Logger::critical(ragger::lang::ERR_LAUNCHCTL_BOOTSTRAP);
return 1;
}
auto pid = get_pid();
if (!pid.empty()) {
if (was_running)
std::cout << std::format(ragger::lang::MSG_RESTARTED_PID, pid) << "\n";
else
std::cout << std::format(ragger::lang::MSG_STARTED_PID, pid) << "\n";
}
else {
std::cout << (was_running
? ragger::lang::MSG_RESTARTED
: ragger::lang::MSG_STARTED) << "\n";
}
return 0;
}
if (action == "status") {
if (!is_loaded()) {
std::cout << ragger::lang::MSG_NOT_LOADED << "\n";
return 0;
}
auto pid = get_pid();
if (pid.empty()) {
std::cout << ragger::lang::MSG_LOADED_NOT_RUNNING << "\n";
return 0;
}
std::cout << std::format(ragger::lang::MSG_RUNNING_PID, pid) << "\n";
return 0;
}
Diskerror::Logger::critical(std::format(ragger::lang::ERR_UNKNOWN_ACTION, action));
return 1;
#elif defined(__linux__)
const std::string unit = "ragger.service";
auto is_active = [&]() {
return run_quiet("systemctl --user is-active --quiet " + unit);
};
auto main_pid = [&]() -> std::string {
auto out = capture_output(
"systemctl --user show -p MainPID --value " + unit);
// trim trailing newline/whitespace
while (!out.empty() && (out.back() == '\n' || out.back() == ' '))
out.pop_back();
if (out == "0" || out.empty()) return "";
return out;
};
if (action == "start") {
if (is_active()) {
auto pid = main_pid();
if (!pid.empty())
std::cout << std::format(ragger::lang::MSG_ALREADY_RUNNING, pid) << "\n";
else
std::cout << ragger::lang::MSG_IS_RUNNING << "\n";
return 0;
}
if (!run_quiet("systemctl --user start " + unit)) {
Diskerror::Logger::critical(ragger::lang::ERR_SYSTEMCTL_START);
return 1;
}
auto pid = main_pid();
if (!pid.empty())
std::cout << std::format(ragger::lang::MSG_STARTED_PID, pid) << "\n";
else
std::cout << ragger::lang::MSG_STARTED << "\n";
return 0;
}
if (action == "stop") {
bool daemon_was_running = is_active();
if (daemon_was_running) {
if (!run_quiet("systemctl --user stop " + unit)) {
Diskerror::Logger::critical(ragger::lang::ERR_SYSTEMCTL_STOP);
return 1;
}
}
int extras = kill_other_ragger_instances();
if (daemon_was_running && extras > 0) {
if (extras == 1)
std::cout << std::format(ragger::lang::MSG_STOPPED_EXTRA_1, extras) << "\n";
else
std::cout << std::format(ragger::lang::MSG_STOPPED_EXTRA_N, extras) << "\n";
}
else if (daemon_was_running) {
std::cout << ragger::lang::MSG_STOPPED << "\n";
}
else if (extras > 0) {
if (extras == 1)
std::cout << std::format(ragger::lang::MSG_EXTRAS_ONLY_1, extras) << "\n";
else
std::cout << std::format(ragger::lang::MSG_EXTRAS_ONLY_N, extras) << "\n";
}
else {
std::cout << ragger::lang::MSG_NOT_RUNNING << "\n";
}
return 0;
}
if (action == "restart") {
bool was_active = is_active();
// Stop daemon and sweep MCP + strays before relaunching.
if (was_active) run_quiet("systemctl --user stop " + unit);
kill_other_ragger_instances();
if (!run_quiet("systemctl --user start " + unit)) {
Diskerror::Logger::critical(ragger::lang::ERR_SYSTEMCTL_START);
return 1;
}
auto pid = main_pid();
if (!pid.empty()) {
if (was_active)
std::cout << std::format(ragger::lang::MSG_RESTARTED_PID, pid) << "\n";
else
std::cout << std::format(ragger::lang::MSG_STARTED_PID, pid) << "\n";
}
else {
std::cout << (was_active
? ragger::lang::MSG_RESTARTED
: ragger::lang::MSG_STARTED) << "\n";
}
return 0;
}
if (action == "status") {
if (is_active()) {
auto pid = main_pid();
if (!pid.empty())
std::cout << std::format(ragger::lang::MSG_RUNNING_PID, pid) << "\n";
else
std::cout << ragger::lang::MSG_IS_RUNNING << "\n";
}
else {
std::cout << ragger::lang::MSG_NOT_RUNNING << "\n";
}
return 0;
}
Diskerror::Logger::critical(std::format(ragger::lang::ERR_UNKNOWN_ACTION, action));
return 1;
#else
Diskerror::Logger::error(ragger::lang::ERR_DAEMON_UNSUPPORTED);
return 1;
#endif
}
} // namespace ragger