libzypp  17.31.31
ShutdownLock.cc
Go to the documentation of this file.
1 #include "ShutdownLock_p.h"
2 
3 #include <zypp-core/base/LogTools.h>
4 #include <zypp-core/ExternalProgram.h>
5 #include <iostream>
6 #include <signal.h>
7 
8 zypp::ShutdownLock::ShutdownLock(const std::string &who, const std::string &reason)
9 {
10  try {
11  MIL << "Try to acquire an inhibitor lock..." << endl;
12  std::string whoStr = str::form("--who=%s", who.c_str());
13  std::string whyStr = str::form("--why=%s", reason.c_str());
14 
15  const char* argv[] =
16  {
17  "/usr/bin/systemd-inhibit",
18  "--what=sleep:shutdown:idle",
19  whoStr.c_str(),
20  "--mode=block",
21  whyStr.c_str(),
22  "/usr/bin/cat",
23  NULL
24  };
25  _prog = shared_ptr<ExternalProgramWithSeperatePgid>( new ExternalProgramWithSeperatePgid( argv, ExternalProgram::Discard_Stderr ) );
26  } catch (...) {
27  }
28 }
29 
31 {
32  if (_prog) {
33  MIL << "Terminate inhibitor lock: pid " << _prog->getpid() << endl;
34  _prog->kill( SIGTERM );
35  if ( !_prog->waitForExit( 10 * 1000 ) ) {
36  // do a real kill if the app does not close in 10 seconds
37  WAR << "systemd-inhibit did not respond to SIGTERM, killing it" << std::endl;
38  _prog->kill();
39  } else {
40  _prog->close();
41  }
42  }
43 }
#define MIL
Definition: Logger.h:96
bool kill()
Kill the program.
ExternalProgramWithStderr & _prog
Definition: PluginScript.cc:79
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
#define WAR
Definition: Logger.h:97
ExternalProgram extended to change the progress group ID after forking.
int close()
Wait for the progamm to complete.
ShutdownLock(const std::string &who, const std::string &reason)
Definition: ShutdownLock.cc:8
shared_ptr< ExternalProgramWithSeperatePgid > _prog
pid_t getpid()
return pid