14 #include <unordered_set>
34 #undef ZYPP_BASE_LOGGER_LOGGROUP
35 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::misc"
54 typedef std::pair<std::string,std::unordered_set<std::string>> CacheEntry;
62 struct FilterRunsInContainer
77 Type in_our_root(
const Pathname &path )
const {
79 const PathInfo procInfoStat( path );
82 if ( procInfoStat.error() )
return IGNORE;
85 if ( procInfoStat.nlink() == 0 )
90 if ( linkTarget.empty() )
return IGNORE;
93 const PathInfo linkStat( linkTarget );
96 if ( !linkStat.isExist() )
100 if ( linkStat.ino() != procInfoStat.ino())
104 if ( linkStat.dev() != procInfoStat.dev() )
116 bool operator()(
const pid_t pid )
const {
119 const Pathname pidDir = Pathname(
"/proc") /
asString(pid);
120 const Pathname exeFile = pidDir /
"exe";
122 auto res = in_our_root( exeFile );
124 return res == CONTAINER;
130 std::unordered_set<std::string> tested;
133 filesystem::dirForEach( pidDir /
"map_files", [
this, &tested, &res ](
const Pathname & dir_r,
const char *
const & name_r ){
136 constexpr
bool contloop =
true;
137 constexpr
bool stoploop =
false;
139 const Pathname entryName = dir_r / name_r;
143 if ( linkTarget.empty() || !tested.insert( linkTarget.asString() ).second )
return contloop;
146 const auto mappedFileType = in_our_root( entryName );
149 if ( mappedFileType > IGNORE ) {
150 res = mappedFileType;
160 return res == CONTAINER;
163 FilterRunsInContainer() {}
174 using target::rpm::librpmDb;
179 : _wasBlocked( librpmDb::isBlocked() )
180 {
if ( _wasBlocked ) librpmDb::unblockAccess(); }
182 {
if ( _wasBlocked ) librpmDb::blockAccess(); }
187 librpmDb::db_const_iterator it;
188 return( it.findPackage(
"lsof" ) && it->tag_edition() < Edition(
"4.90") && !it->tag_provides().count( Capability(
"backported-option-Ki") ) );
199 bool addDataIf(
const CacheEntry & cache_r, std::vector<std::string> *debMap =
nullptr );
200 void addCacheIf( CacheEntry & cache_r,
const std::string & line_r, std::vector<std::string> *debMap =
nullptr );
205 std::vector<CheckAccessDeleted::ProcInfo>
_data;
225 const auto & filelist( cache_r.second );
227 if ( filelist.empty() )
233 pinfo.
files.insert( pinfo.
files.begin(), filelist.begin(), filelist.end() );
235 const std::string & pline( cache_r.first );
236 std::string commandname;
237 std::ostringstream pLineStr;
238 for_( ch, pline.begin(), pline.end() )
243 pinfo.
pid = &*(ch+1);
245 pLineStr <<&*(ch)<<
'\0';
248 pinfo.
ppid = &*(ch+1);
250 pLineStr <<&*(ch)<<
'\0';
253 pinfo.
puid = &*(ch+1);
255 pLineStr <<&*(ch)<<
'\0';
258 pinfo.
login = &*(ch+1);
260 pLineStr <<&*(ch)<<
'\0';
264 commandname = &*(ch+1);
266 if (!_fromLsofFileMode)
269 pinfo.
command = std::move(commandname);
271 pLineStr <<
'c'<<pinfo.
command<<
'\0';
275 if ( *ch ==
'\n' )
break;
276 do { ++ch; }
while ( *ch !=
'\0' );
282 debMap->front() = pLineStr.str();
301 for_( ch, line_r.c_str(), ch+line_r.size() )
306 if ( *(ch+1) !=
'0' )
319 if ( *ch ==
'\n' )
break;
320 do { ++ch; }
while ( *ch !=
'\0' );
323 if ( !t || !f || !n )
326 if ( !( ( *t ==
'R' && *(t+1) ==
'E' && *(t+2) ==
'G' && *(t+3) ==
'\0' )
327 || ( *t ==
'D' && *(t+1) ==
'E' && *(t+2) ==
'L' && *(t+3) ==
'\0' ) ) )
330 if ( !( ( *f ==
'm' && *(f+1) ==
'e' && *(f+2) ==
'm' && *(f+3) ==
'\0' )
331 || ( *f ==
't' && *(f+1) ==
'x' && *(f+2) ==
't' && *(f+3) ==
'\0' )
332 || ( *f ==
'D' && *(f+1) ==
'E' && *(f+2) ==
'L' && *(f+3) ==
'\0' )
333 || ( *f ==
'l' && *(f+1) ==
't' && *(f+2) ==
'x' && *(f+3) ==
'\0' ) ) )
345 if ( *f ==
'm' || *f ==
'D' )
347 static const char * black[] = {
362 if ( debMap && cache_r.second.find(n) == cache_r.second.end() ) {
363 debMap->push_back(line_r);
365 cache_r.second.insert( n );
371 if ( doCheck_r )
check();
379 FILE *inFile = fopen( lsofOutput_r.
c_str(),
"r" );
394 std::map<pid_t,CacheEntry> cachemap;
399 FilterRunsInContainer runsInLXC;
400 MIL <<
"Silently scanning lsof output..." << endl;
402 for( std::string line = source.
receiveLine( 30 * 1000 ); ! line.empty(); line = source.
receiveLine( 30 * 1000 ) )
405 if ( line[0] ==
'p' )
409 if ( debugEnabled ) {
411 if ( pidMad.empty() )
412 debugMap[cachepid].push_back( line );
416 cachemap[cachepid].first.swap( line );
424 addCacheIf( cachemap[cachepid], line, debugEnabled ? &dbgMap :
nullptr);
432 static const char* argv[] = {
"lsof",
"-n",
"-FpcuLRftkn0",
"-K",
"i", NULL };
440 std::map<pid_t,CacheEntry> cachemap;
450 int ret = prog.
close();
467 std::ofstream debugFileOut;
468 bool debugEnabled =
false;
469 if ( !_debugFile.empty() ) {
470 debugFileOut.open( _debugFile.c_str() );
471 debugEnabled = debugFileOut.is_open();
473 if ( !debugEnabled ) {
474 ERR<<
"Unable to open debug file: "<<_debugFile<<endl;
479 for (
const auto &cached : in )
482 addDataIf( cached.second);
484 std::vector<std::string> *mapPtr =
nullptr;
486 auto dbgInfo = debugMap.find(cached.first);
487 if ( dbgInfo != debugMap.end() )
488 mapPtr = &(dbgInfo->second);
490 if( !addDataIf( cached.second, mapPtr ) )
493 for (
const std::string &dbgLine: dbgInfo->second ) {
494 debugFileOut.write( dbgLine.c_str(), dbgLine.length() );
535 static const str::regex rx(
"[0-9]+:name=systemd:/system.slice/(.*/)?(.*).service$" );
539 [&](
int num_r, std::string line_r )->
bool
570 if ( obj.
pid.empty() )
571 return str <<
"<NoProc>";