Advertisement
Jakowlew

Kill xorg v1

Mar 25th, 2024
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. bool Session::stopX()
  2.     {
  3.         if (!m_X) {
  4.             spdlog::info("!m_X");
  5.             return false;  
  6.         }
  7.  
  8.         const auto pgidOpt = m_X->pgid();
  9.         if (!pgidOpt) {
  10.             spdlog::info("pgid is empy for m_X");
  11.             return false; // TODO: kill at least m_X group
  12.         }
  13.         const auto pgid = *pgidOpt;
  14.         spdlog::info("current group is {}", pgid);
  15.  
  16.         const auto pidsByName = [](std::string const & name) {
  17.             PROCTAB *proc = openproc(PROC_FILLCOM | PROC_FILLSTATUS);
  18.             proc_t procInfo{};
  19.  
  20.             struct process
  21.             {
  22.                 int pid;
  23.                 int ppid;
  24.             };
  25.  
  26.             std::vector<std::tuple<int, int>> pids;
  27.             while (readproc(proc, &procInfo) != nullptr)
  28.             {
  29.                 if (procInfo.cmdline && std::strstr(procInfo.cmdline[0], name.c_str()) != nullptr)
  30.                 {
  31.                     const int pid = procInfo.tid != 0 ? procInfo.tid : procInfo.tgid;
  32.                     const int ppid = procInfo.ppid;
  33.                     // closeproc(proc);
  34.                     pids.emplace_back(pid, ppid);
  35.                     // procInfo.
  36.                     spdlog::info("found process {} with pid={} ppid={}", procInfo.cmdline[0], pid, ppid);
  37.                 }
  38.             }
  39.             closeproc(proc);
  40.  
  41.             return pids;
  42.         };
  43.  
  44.         auto xinitPids = pidsByName("xinit");
  45.         decltype(xinitPids) xinitInThisGroup;
  46.         std::copy_if(
  47.             xinitPids.cbegin(),
  48.             xinitPids.cend(),
  49.             std::back_inserter(xinitInThisGroup),
  50.             [=](auto pids) {
  51.                 const auto [pid, ppid] = pids;
  52.                 const auto xinitPgid = getpgid(pid);
  53.                 if (xinitPgid == -1) {
  54.                     spdlog::warn("getpgid failed for xinit (pid={})", pid);
  55.                     return false;
  56.                 }
  57.  
  58.                 return xinitPgid == pgid;
  59.             }
  60.         );
  61.         if (xinitInThisGroup.empty()) {
  62.             spdlog::warn("no xinit in pgid={}", pgid);
  63.             return false; // TODO: kill at least m_X group
  64.         }
  65.         if (xinitInThisGroup.size() > 1) {
  66.             spdlog::warn("more than on xinit with pgid={} found. only first will be processd", pgid);
  67.         }
  68.  
  69.         // We got xinit pid.
  70.         // Now find Xorg proces with ppid=xinitPid and kill its process group
  71.         const auto xinitPid = std::get<0>(xinitInThisGroup.front());
  72.         spdlog::info("found xinit (pid={}) with pgid={}", xinitPid, pgid);
  73.  
  74.         auto xorgPids = pidsByName("Xorg");
  75.         decltype(xorgPids) xorgChildrenOfXinit;
  76.         std::copy_if(
  77.             xorgPids.cbegin(),
  78.             xorgPids.cend(),
  79.             std::back_inserter(xorgChildrenOfXinit),
  80.             [=](auto pids) {
  81.                 const auto [pid, ppid] = pids;
  82.                 return xinitPid == ppid;
  83.             }
  84.         );
  85.         // TODO: Check for empy xorgs
  86.         for (auto xorg : xorgChildrenOfXinit) {
  87.             const auto [pid, ppid] = xorg;
  88.             const auto xorgPgid = getpgid(pid);
  89.             spdlog::info("found Xorg (pid={}) with pgid={}", pid, xorgPgid);
  90.             if (kill(-xorgPgid, SIGKILL) == -1) {
  91.                 spdlog::warn("failed to kill Xorg");
  92.             }
  93.         }
  94.         return true;
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement