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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "child-handler.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <vector>
#include <string>
#include <cstdlib>
#include <vespa/log/log.h>
LOG_SETUP(".child-handler");
ChildHandler::ChildHandler() : _childRunning(false) {}
namespace {
void
runSplunk(const vespalib::string &prefix, std::vector<const char *> args)
{
vespalib::string path = prefix + "/bin/splunk";
args.insert(args.begin(), path.c_str());
std::string dbg = "";
for (const char *arg : args) {
dbg.append(" '");
dbg.append(arg);
dbg.append("'");
}
LOG(info, "trigger splunk with command: %s", dbg.c_str());
args.push_back(nullptr);
fflush(stdout);
pid_t child = fork();
if (child == -1) {
perror("fork()");
return;
}
if (child == 0) {
vespalib::string env = "SPLUNK_HOME=" + prefix;
char *cenv = const_cast<char *>(env.c_str()); // safe cast
putenv(cenv);
LOG(debug, "added to environment: '%s'", cenv);
char **cargv = const_cast<char **>(args.data()); // safe cast
execv(cargv[0], cargv);
// if execv fails:
perror(cargv[0]);
std::_Exit(1);
}
LOG(debug, "child running with pid %d", (int)child);
int waitStatus = 0;
if (waitpid(child, &waitStatus, 0) == -1) {
perror("waitpid()");
return;
}
if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) {
// all OK
LOG(debug, "child ran ok, exit status 0");
return;
}
if (WIFEXITED(waitStatus)) {
LOG(warning, "failed triggering splunk (exit status %d)",
WEXITSTATUS(waitStatus));
} else if (WIFSIGNALED(waitStatus)) {
LOG(warning, "failed triggering splunk (exit on signal %d)",
WTERMSIG(waitStatus));
} else {
LOG(warning, "failed triggering splunk (abnormal exit status %d)",
waitStatus);
}
}
} // namespace <unnamed>
void
ChildHandler::startChild(const vespalib::string &prefix)
{
LOG(debug, "startChild '%s'", prefix.c_str());
if (_childRunning && prefix == _runningPrefix) {
runSplunk(prefix, {"restart"});
} else {
if (_childRunning) {
runSplunk(_runningPrefix, {"stop"});
} else {
// it is possible that splunk was already running anyway, so
// make sure we restart it to get new config activated:
runSplunk(prefix, {"stop"});
}
sleep(1);
runSplunk(prefix, {"start", "--answer-yes", "--no-prompt", "--accept-license"});
_childRunning = true;
_runningPrefix = prefix;
}
}
void ChildHandler::stopChild() {
if (_runningPrefix != "") {
LOG(debug, "stopChild '%s'", _runningPrefix.c_str());
runSplunk(_runningPrefix, {"stop"});
}
_childRunning = false;
}
void
ChildHandler::stopChild(const vespalib::string &prefix) {
stopChild();
_runningPrefix = prefix;
stopChild();
}
|