Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Session::stopX()
- {
- if (!m_X) {
- spdlog::info("!m_X");
- return false;
- }
- const auto pgidOpt = m_X->pgid();
- if (!pgidOpt) {
- spdlog::info("pgid is empy for m_X");
- return false; // TODO: kill at least m_X group
- }
- const auto pgid = *pgidOpt;
- spdlog::info("current group is {}", pgid);
- const auto pidsByName = [](std::string const & name) {
- PROCTAB *proc = openproc(PROC_FILLCOM | PROC_FILLSTATUS);
- proc_t procInfo{};
- struct process
- {
- int pid;
- int ppid;
- };
- std::vector<std::tuple<int, int>> pids;
- while (readproc(proc, &procInfo) != nullptr)
- {
- if (procInfo.cmdline && std::strstr(procInfo.cmdline[0], name.c_str()) != nullptr)
- {
- const int pid = procInfo.tid != 0 ? procInfo.tid : procInfo.tgid;
- const int ppid = procInfo.ppid;
- // closeproc(proc);
- pids.emplace_back(pid, ppid);
- // procInfo.
- spdlog::info("found process {} with pid={} ppid={}", procInfo.cmdline[0], pid, ppid);
- }
- }
- closeproc(proc);
- return pids;
- };
- auto xinitPids = pidsByName("xinit");
- decltype(xinitPids) xinitInThisGroup;
- std::copy_if(
- xinitPids.cbegin(),
- xinitPids.cend(),
- std::back_inserter(xinitInThisGroup),
- [=](auto pids) {
- const auto [pid, ppid] = pids;
- const auto xinitPgid = getpgid(pid);
- if (xinitPgid == -1) {
- spdlog::warn("getpgid failed for xinit (pid={})", pid);
- return false;
- }
- return xinitPgid == pgid;
- }
- );
- if (xinitInThisGroup.empty()) {
- spdlog::warn("no xinit in pgid={}", pgid);
- return false; // TODO: kill at least m_X group
- }
- if (xinitInThisGroup.size() > 1) {
- spdlog::warn("more than on xinit with pgid={} found. only first will be processd", pgid);
- }
- // We got xinit pid.
- // Now find Xorg proces with ppid=xinitPid and kill its process group
- const auto xinitPid = std::get<0>(xinitInThisGroup.front());
- spdlog::info("found xinit (pid={}) with pgid={}", xinitPid, pgid);
- auto xorgPids = pidsByName("Xorg");
- decltype(xorgPids) xorgChildrenOfXinit;
- std::copy_if(
- xorgPids.cbegin(),
- xorgPids.cend(),
- std::back_inserter(xorgChildrenOfXinit),
- [=](auto pids) {
- const auto [pid, ppid] = pids;
- return xinitPid == ppid;
- }
- );
- // TODO: Check for empy xorgs
- for (auto xorg : xorgChildrenOfXinit) {
- const auto [pid, ppid] = xorg;
- const auto xorgPgid = getpgid(pid);
- spdlog::info("found Xorg (pid={}) with pgid={}", pid, xorgPgid);
- if (kill(-xorgPgid, SIGKILL) == -1) {
- spdlog::warn("failed to kill Xorg");
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement