13 #include <unordered_set> 29 #undef ZYPP_BASE_LOGGER_LOGGROUP 30 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::misc" 49 typedef std::pair<std::string,std::unordered_set<std::string>> CacheEntry;
55 inline void addDataIf( std::vector<CheckAccessDeleted::ProcInfo> & data_r,
const CacheEntry & cache_r )
57 const auto & filelist( cache_r.second );
59 if ( filelist.empty() )
63 data_r.push_back( CheckAccessDeleted::ProcInfo() );
64 CheckAccessDeleted::ProcInfo & pinfo( data_r.back() );
65 pinfo.files.insert( pinfo.files.begin(), filelist.begin(), filelist.end() );
67 const std::string & pline( cache_r.first );
68 std::string commandname;
69 for_( ch, pline.begin(), pline.end() )
77 pinfo.ppid = &*(ch+1);
80 pinfo.puid = &*(ch+1);
83 pinfo.login = &*(ch+1);
86 if ( pinfo.command.empty() )
87 commandname = &*(ch+1);
90 if ( *ch ==
'\n' )
break;
91 do { ++ch; }
while ( *ch !=
'\0' );
94 if ( pinfo.command.empty() )
98 if ( pinfo.command.empty() )
99 pinfo.command = std::move(commandname);
109 inline void addCacheIf( CacheEntry & cache_r,
const std::string & line_r,
bool verbose_r )
115 for_( ch, line_r.c_str(), ch+line_r.size() )
120 if ( *(ch+1) !=
'0' )
133 if ( *ch ==
'\n' )
break;
134 do { ++ch; }
while ( *ch !=
'\0' );
137 if ( !t || !f || !n )
140 if ( !( ( *t ==
'R' && *(t+1) ==
'E' && *(t+2) ==
'G' && *(t+3) ==
'\0' )
141 || ( *t ==
'D' && *(t+1) ==
'E' && *(t+2) ==
'L' && *(t+3) ==
'\0' ) ) )
144 if ( !( ( *f ==
'm' && *(f+1) ==
'e' && *(f+2) ==
'm' && *(f+3) ==
'\0' )
145 || ( *f ==
't' && *(f+1) ==
'x' && *(f+2) ==
't' && *(f+3) ==
'\0' )
146 || ( *f ==
'D' && *(f+1) ==
'E' && *(f+2) ==
'L' && *(f+3) ==
'\0' )
147 || ( *f ==
'l' && *(f+1) ==
't' && *(f+2) ==
'x' && *(f+3) ==
'\0' ) ) )
159 if ( *f ==
'm' || *f ==
'D' )
161 static const char * black[] = {
173 cache_r.second.insert( n );
182 struct FilterRunsInLXC
184 bool operator()( pid_t pid_r )
const 185 {
return( nsIno( pid_r,
"pid" ) !=
pidNS || nsIno( pid_r,
"mnt" ) !=
mntNS ); }
188 :
pidNS( nsIno(
"self",
"pid" ) )
189 ,
mntNS( nsIno(
"self",
"mnt" ) )
192 static inline ino_t nsIno(
const std::string & pid_r,
const std::string & ns_r )
193 {
return PathInfo(
"/proc/"+pid_r+
"/ns/"+ns_r).ino(); }
195 static inline ino_t nsIno( pid_t pid_r,
const std::string & ns_r )
196 {
return nsIno(
asString(pid_r), ns_r ); }
210 static const char* argv[] =
212 "lsof",
"-n",
"-FpcuLRftkn0", NULL
218 std::map<pid_t,CacheEntry> cachemap;
220 FilterRunsInLXC runsInLXC;
224 if ( line[0] ==
'p' )
227 if ( !runsInLXC( cachepid ) )
228 cachemap[cachepid].first.swap( line );
234 addCacheIf( cachemap[cachepid], line, verbose_r );
238 int ret = prog.
close();
250 std::vector<ProcInfo> data;
251 for (
const auto & cached : cachemap )
253 addDataIf( data, cached.second );
275 static const str::regex rx(
"[0-9]+:name=systemd:/system.slice/(.*/)?(.*).service$" );
279 [&](
int num_r, std::string line_r )->
bool 298 return dumpRange( str <<
"CheckAccessDeleted ",
310 if ( obj.
pid.empty() )
311 return str <<
"<NoProc>";
Data about one running process accessing deleted files.
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
std::string asString(const DefaultIntegral< Tp, TInitial > &obj)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
const std::string & execError() const
Some detail telling why the execution failed, if it failed.
std::string service() const
Guess if command was started by a systemd service script.
std::string command
process command name
String related utilities and Regular expression matching.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
void remember(const Exception &old_r)
Store an other Exception as history.
size_type check(bool verbose_r=false)
Check for running processes which access deleted executables or libraries.
int simpleParseFile(std::istream &str_r, ParseFlags flags_r, function< bool(int, std::string)> consume_r)
Simple lineparser optionally trimming and skipping comments.
std::string pid
process ID
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
std::list< PublicKeyData > _data
TInt strtonum(const C_Str &str)
Parsing numbers from string.
std::string puid
process user ID
std::string receiveLine()
Read one line from the input stream.
std::string numstring(char n, int w=0)
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.
const_iterator end() const
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.
const_iterator begin() const
std::string login
process login name
std::vector< std::string > files
list of deleted executables or libraries accessed
std::string ppid
parent process ID
static std::string findService(pid_t pid_r)
Guess if pid was started by a systemd service script.