libzypp: zypp-core/ShutdownLock.cc Source File

libzypp 17.38.15

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
8zypp::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
30zypp::ShutdownLock::~ShutdownLock()
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}
ExternalProgram.h
LogTools.h
MIL
#define MIL
Definition Logger.h:130
WAR
#define WAR
Definition Logger.h:131
ShutdownLock_p.h
shared_ptr
zypp::ExternalProgramWithSeperatePgid::ExternalProgramWithSeperatePgid
ExternalProgramWithSeperatePgid(const char *const *argv, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
Definition ExternalProgram.h:319
zypp::ExternalProgram::Discard_Stderr
@ Discard_Stderr
Definition ExternalProgram.h:76
zypp::ShutdownLock::~ShutdownLock
~ShutdownLock()
Definition ShutdownLock.cc:30
zypp::ShutdownLock::_prog
shared_ptr< ExternalProgramWithSeperatePgid > _prog
Definition ShutdownLock_p.h:42
zypp::ShutdownLock::ShutdownLock
ShutdownLock(const std::string &who, const std::string &reason)
Definition ShutdownLock.cc:8
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39