Skip to content

Systemd: Add support for MAINPID= and EXTEND_TIMEOUT_USEC #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Systemd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const std::string isWatchdog = std::string("WATCHDOG=1");
// systemd string prefix to publish information to systemd
const std::string isStatusPrefix = std::string("STATUS=");

// systemd string prefix to publish the main PID
const std::string mainPidPrefix = std::string("MAINPID=");

// systemd string prefix to publish timeout extension
const std::string extendTimeoutPrefix = std::string("EXTEND_TIMEOUT_USEC=");

// Multiplier to convert between milliseconds and nanoseconds
const uint64_t msToNano = 1000000;

Expand Down Expand Up @@ -75,6 +81,19 @@ void notifyStatus(std::string status) {
sd_pid_notify(getpid(), 0, statusPrefix.append(status).c_str());
}

// Sends the specified PID of the service to systemd in case the service manager did not fork off the process itself
void sendMainPid(int pid) {
std::string mainPidStr = std::string(mainPidPrefix);
sd_pid_notify(getpid(), 0, mainPidStr.append(std::to_string(pid)).c_str());
}

// Allows the kdb process to extend the amount of time taken to transition the process (e.g. during process start, before ready to check TP log file)
// @param extendTimeUs The time extension required in microseconds
void extendTimeout(int extendTimeUs) {
std::string timeoutStr = std::string(extendTimeoutPrefix);
sd_pid_notify(getpid(), 0, timeoutStr.append(std::to_string(extendTimeUs)).c_str());
}

} // namespace kdbsystemd


Expand Down Expand Up @@ -117,3 +136,31 @@ extern "C" K sendStatus(K status) {
kdbsystemd::notifyStatus(statusStr);
return kb(1);
}

extern "C" K sendMainPid(K intOrNullArg) {
int pid = getpid();

if(intOrNullArg->t != -KI)
pid = intOrNullArg->i;

kdbsystemd::sendMainPid(pid);
return kb(1);
}

extern "C" K extendTimeout(K timespan) {
int timeoutUs;

if(timespan->t != -KN)
return krr((char*) "[lib-kdbsystemd] Incorrect type for timeout extension. Must be timespan");

if(timespan->j == nj)
return krr((char*) "[lib-kdbsystemd] Cannot specify null timeout extension");

timeoutUs = timespan->j / 1000;

if(timeoutUs <= 0)
return krr((char*) "[lib-kdbsystemd] Incorrect value for start timeout extension. Must be greater than 0 us");

kdbsystemd::extendTimeout(timeoutUs);
return kb(1);
}