14 #include <unordered_set> 33 #undef ZYPP_BASE_LOGGER_LOGGROUP 34 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::misc" 53 typedef std::pair<std::string,std::unordered_set<std::string>> CacheEntry;
61 struct FilterRunsInLXC
63 bool operator()( pid_t pid_r )
const 64 {
return( nsIno( pid_r,
"pid" ) !=
pidNS ); }
67 :
pidNS( nsIno(
"self",
"pid" ) )
70 static inline ino_t nsIno(
const std::string & pid_r,
const std::string & ns_r )
71 {
return PathInfo(
"/proc/"+pid_r+
"/ns/"+ns_r).ino(); }
73 static inline ino_t nsIno( pid_t pid_r,
const std::string & ns_r )
74 {
return nsIno(
asString(pid_r), ns_r ); }
86 using target::rpm::librpmDb;
91 : _wasBlocked( librpmDb::isBlocked() )
92 {
if ( _wasBlocked ) librpmDb::unblockAccess(); }
94 {
if ( _wasBlocked ) librpmDb::blockAccess(); }
99 librpmDb::db_const_iterator it;
100 return( it.findPackage(
"lsof" ) && it->tag_edition() < Edition(
"4.90") && !it->tag_provides().count( Capability(
"backported-option-Ki") ) );
111 bool addDataIf(
const CacheEntry & cache_r, std::vector<std::string> *debMap =
nullptr );
112 void addCacheIf( CacheEntry & cache_r,
const std::string & line_r, std::vector<std::string> *debMap =
nullptr );
117 std::vector<CheckAccessDeleted::ProcInfo>
_data;
137 const auto & filelist( cache_r.second );
139 if ( filelist.empty() )
145 pinfo.
files.insert( pinfo.files.begin(), filelist.begin(), filelist.end() );
147 const std::string & pline( cache_r.first );
148 std::string commandname;
149 std::ostringstream pLineStr;
150 for_( ch, pline.begin(), pline.end() )
155 pinfo.pid = &*(ch+1);
157 pLineStr <<&*(ch)<<
'\0';
160 pinfo.ppid = &*(ch+1);
162 pLineStr <<&*(ch)<<
'\0';
165 pinfo.puid = &*(ch+1);
167 pLineStr <<&*(ch)<<
'\0';
170 pinfo.login = &*(ch+1);
172 pLineStr <<&*(ch)<<
'\0';
175 if ( pinfo.command.empty() ) {
176 commandname = &*(ch+1);
178 if (!_fromLsofFileMode)
180 if ( pinfo.command.empty() )
181 pinfo.command = std::move(commandname);
183 pLineStr <<
'c'<<pinfo.command<<
'\0';
187 if ( *ch ==
'\n' )
break;
188 do { ++ch; }
while ( *ch !=
'\0' );
194 debMap->front() = pLineStr.str();
213 for_( ch, line_r.c_str(), ch+line_r.size() )
218 if ( *(ch+1) !=
'0' )
231 if ( *ch ==
'\n' )
break;
232 do { ++ch; }
while ( *ch !=
'\0' );
235 if ( !t || !f || !n )
238 if ( !( ( *t ==
'R' && *(t+1) ==
'E' && *(t+2) ==
'G' && *(t+3) ==
'\0' )
239 || ( *t ==
'D' && *(t+1) ==
'E' && *(t+2) ==
'L' && *(t+3) ==
'\0' ) ) )
242 if ( !( ( *f ==
'm' && *(f+1) ==
'e' && *(f+2) ==
'm' && *(f+3) ==
'\0' )
243 || ( *f ==
't' && *(f+1) ==
'x' && *(f+2) ==
't' && *(f+3) ==
'\0' )
244 || ( *f ==
'D' && *(f+1) ==
'E' && *(f+2) ==
'L' && *(f+3) ==
'\0' )
245 || ( *f ==
'l' && *(f+1) ==
't' && *(f+2) ==
'x' && *(f+3) ==
'\0' ) ) )
257 if ( *f ==
'm' || *f ==
'D' )
259 static const char * black[] = {
273 if ( debMap && cache_r.second.find(n) == cache_r.second.end() ) {
274 debMap->push_back(line_r);
276 cache_r.second.insert( n );
282 if ( doCheck_r )
check();
290 FILE *inFile = fopen( lsofOutput_r.
c_str(),
"r" );
305 std::map<pid_t,CacheEntry> cachemap;
310 FilterRunsInLXC runsInLXC;
314 if ( line[0] ==
'p' )
318 if ( debugEnabled ) {
320 if ( pidMad.empty() )
321 debugMap[cachepid].push_back( line );
325 cachemap[cachepid].first.swap( line );
333 addCacheIf( cachemap[cachepid], line, debugEnabled ? &dbgMap :
nullptr);
341 static const char* argv[] = {
"lsof",
"-n",
"-FpcuLRftkn0",
"-K",
"i", NULL };
351 int ret = prog.
close();
368 std::ofstream debugFileOut;
369 bool debugEnabled =
false;
370 if ( !_debugFile.empty() ) {
371 debugFileOut.open( _debugFile.c_str() );
372 debugEnabled = debugFileOut.is_open();
374 if ( !debugEnabled ) {
375 ERR<<
"Unable to open debug file: "<<_debugFile<<endl;
380 for (
const auto &cached : in )
383 addDataIf( cached.second);
385 std::vector<std::string> *mapPtr =
nullptr;
387 auto dbgInfo = debugMap.find(cached.first);
388 if ( dbgInfo != debugMap.end() )
389 mapPtr = &(dbgInfo->second);
391 if( !addDataIf( cached.second, mapPtr ) )
394 for (
const std::string &dbgLine: dbgInfo->second ) {
395 debugFileOut.write( dbgLine.c_str(), dbgLine.length() );
436 static const str::regex rx(
"[0-9]+:name=systemd:/system.slice/(.*/)?(.*).service$" );
440 [&](
int num_r, std::string line_r )->
bool 471 if ( obj.
pid.empty() )
472 return str <<
"<NoProc>";
bool addDataIf(const CacheEntry &cache_r, std::vector< std::string > *debMap=nullptr)
Add cache to data if the process is accessing deleted files.
Data about one running process accessing deleted files.
Bidirectional stream to external data.
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
std::map< pid_t, CacheEntry > filterInput(externalprogram::ExternalDataSource &source)
std::string asString(const DefaultIntegral< Tp, TInitial > &obj)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
std::map< pid_t, std::vector< std::string > > debugMap
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
void setDebugOutputFile(const Pathname &filename_r)
Writes all filtered process entries that make it into the final set into a file specified by filename...
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\ ", const std::string &sep="\ ", const std::string &sfx="\, const std::string &extro="}")
Print range defined by iterators (multiline style).
const char * c_str() const
String representation.
std::string command
process command name
String related utilities and Regular expression matching.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
std::ostream & operator<<(std::ostream &str, const CheckAccessDeleted &obj)
void remember(const Exception &old_r)
Store an other Exception as history.
CheckAccessDeleted::Impl * clone() const
bool empty() const
Test for an empty path.
size_type check(bool verbose_r=false)
Check for running processes which access deleted executables or libraries.
RWCOW_pointer< Impl > _pimpl
const_iterator begin() const
int simpleParseFile(std::istream &str_r, ParseFlags flags_r, function< bool(int, std::string)> consume_r)
Simple lineparser optionally trimming and skipping comments.
const std::string & execError() const
Some detail telling why the execution failed, if it failed.
std::string pid
process ID
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
std::list< PublicKeyData > _data
std::vector< CheckAccessDeleted::ProcInfo > _data
TInt strtonum(const C_Str &str)
Parsing numbers from string.
std::string puid
process user ID
const_iterator end() const
std::string receiveLine()
Read one line from the input stream.
std::string numstring(char n, int w=0)
CheckAccessDeleted::size_type createProcInfo(const std::map< pid_t, CacheEntry > &in)
int close()
Wait for the progamm to complete.
#define arrayBegin(A)
Simple C-array iterator.
Regular expression match result.
Base class for Exception.
Check for running processes which access deleted executables or libraries.
CheckAccessDeleted(bool doCheck_r=true)
Default ctor performs check immediately.
std::ostream & dumpRangeLine(std::ostream &str, TIterator begin, TIterator end)
Print range defined by iterators (single line style).
bool regex_match(const std::string &s, smatch &matches, const regex ®ex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Easy-to use interface to the ZYPP dependency resolver.
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
std::vector< ProcInfo >::const_iterator const_iterator
std::string login
process login name
void addCacheIf(CacheEntry &cache_r, const std::string &line_r, std::vector< std::string > *debMap=nullptr)
Add file to cache if it refers to a deleted executable or library file:
std::vector< std::string > files
list of deleted executables or libraries accessed
std::string ppid
parent process ID
std::string service() const
Guess if command was started by a systemd service script.
static std::string findService(pid_t pid_r)
Guess if pid was started by a systemd service script.