10 #include <sys/times.h>
16 char result[PATH_MAX];
17 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
19 Path path(std::string(result, count));
22 return makeUnexpected<Path>(
"Unknown error");
27 const std::string& from,
28 const std::string& subject,
29 const std::string& message) {
31 FILE* mailpipe = popen(
"/usr/bin/sendmail -t",
"w");
32 if (mailpipe ==
nullptr) {
35 fprintf(mailpipe,
"To: %s\n", to.c_str());
36 fprintf(mailpipe,
"From: %s\n", from.c_str());
37 fprintf(mailpipe,
"Subject: %s\n\n", subject.c_str());
38 fwrite(message.c_str(), 1, message.size(), mailpipe);
39 fwrite(
".\n", 1, 2, mailpipe);
45 std::string command =
"notify-send \"" + title +
"\" \"" + message +
"\"";
46 if (system(command.c_str())) {
54 std::string command =
"curl --data 'key=" + key +
"&title=" + title +
"&msg=" + message +
55 "' https://api.simplepush.io/send > /dev/null 2> /dev/null";
56 if (system(command.c_str())) {
65 return makeUnexpected<std::string>(
"Invalid path");
68 std::string command =
"cd " + pathToGitRoot.
native() +
" && git rev-parse HEAD~" + std::to_string(prev);
70 FILE* pipe = popen(command.c_str(),
"r");
71 auto f =
finally([pipe] { pclose(pipe); });
76 if (fgets(&buffer[0], 128, pipe) != NULL) {
82 if ((n = result.find(
"\n")) != std::string::npos) {
83 result = result.substr(0, n);
86 if (result.size() != 40) {
87 return makeUnexpected<std::string>(
88 "Returned git SHA has incorrent length (" + std::to_string(result.size()) +
")");
89 }
else if (result.find_first_not_of(
"0123456789abcdef") != std::string::npos) {
90 return makeUnexpected<std::string>(
"Returned git SHA contains invalid characters");
106 struct tms timeSample;
109 lastCpu = times(&timeSample);
110 lastSysCpu = timeSample.tms_stime;
111 lastUserCpu = timeSample.tms_utime;
113 file = fopen(
"/proc/cpuinfo",
"r");
115 while (fgets(line, 128, file) != NULL) {
116 if (strncmp(line,
"processor", 9) == 0)
127 now = times(&timeSample);
128 if (now <= lastCpu || timeSample.tms_stime < lastSysCpu || timeSample.tms_utime < lastUserCpu) {
132 usage = (timeSample.tms_stime - lastSysCpu) + (timeSample.tms_utime - lastUserCpu);
133 usage.
value() /= (now - lastCpu);
134 usage.
value() /= numProcessors;
137 lastSysCpu = timeSample.tms_stime;
138 lastUserCpu = timeSample.tms_utime;
151 bool debuggerPresent =
false;
153 int status_fd = open(
"/proc/self/status", O_RDONLY);
154 if (status_fd == -1) {
158 std::size_t num_read = read(status_fd, buf,
sizeof(buf));
161 static const char TracerPid[] =
"TracerPid:";
165 tracer_pid = strstr(buf, TracerPid);
167 debuggerPresent = atoi(tracer_pid +
sizeof(TracerPid) - 1) != 0;
170 return debuggerPresent;
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Wraps a functor and executes it once the wrapper goes out of scope.
uint32_t Size
Integral type used to index arrays (by default).
#define NAMESPACE_SPH_END
const NothingType NOTHING
const SuccessTag SUCCESS
Global constant for successful outcome.
INLINE Outcome makeFailed(TArgs &&... args)
Constructs failed object with error message.
Optional< Float > getUsage()
Wrapper of type that either contains a value of given type, or an error message.
INLINE Type & value()
Returns the reference to the stored value.
Object representing a path on a filesystem.
std::string native() const
Returns the native version of the path.
Path parentPath() const
Returns the parent directory. If the path is empty or root, return empty path.
Array with fixed number of allocated elements.
bool pathExists(const Path &path)
Checks if a file or directory exists (or more precisely, if a file or directory is accessible).