13 #include <unordered_set>
30 #undef ZYPP_BASE_LOGGER_LOGGROUP
31 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::misc"
50 typedef std::pair<std::string,std::unordered_set<std::string>> CacheEntry;
56 inline void addDataIf( std::vector<CheckAccessDeleted::ProcInfo> & data_r,
const CacheEntry & cache_r )
58 const auto & filelist( cache_r.second );
60 if ( filelist.empty() )
64 data_r.push_back( CheckAccessDeleted::ProcInfo() );
65 CheckAccessDeleted::ProcInfo & pinfo( data_r.back() );
66 pinfo.files.insert( pinfo.files.begin(), filelist.begin(), filelist.end() );
68 const std::string & pline( cache_r.first );
69 for_( ch, pline.begin(), pline.end() )
77 pinfo.ppid = &*(ch+1);
80 pinfo.puid = &*(ch+1);
83 pinfo.login = &*(ch+1);
86 pinfo.command = &*(ch+1);
89 if ( *ch ==
'\n' )
break;
90 do { ++ch; }
while ( *ch !=
'\0' );
93 if ( pinfo.command.size() == 15 )
97 if ( ! command.empty() )
98 pinfo.command = command.basename();
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 ); }
188 :
pidNS( nsIno(
"self",
"pid" ) )
191 static inline ino_t nsIno(
const std::string & pid_r,
const std::string & ns_r )
192 {
return PathInfo(
"/proc/"+pid_r+
"/ns/"+ns_r).ino(); }
194 static inline ino_t nsIno( pid_t pid_r,
const std::string & ns_r )
195 {
return nsIno(
asString(pid_r), ns_r ); }
207 using target::rpm::librpmDb;
212 : _wasBlocked( librpmDb::isBlocked() )
213 {
if ( _wasBlocked ) librpmDb::unblockAccess(); }
215 {
if ( _wasBlocked ) librpmDb::blockAccess(); }
220 librpmDb::db_const_iterator it;
221 return( it.findPackage(
"lsof" ) && it->tag_edition() < Edition(
"4.90") && !it->tag_provides().count( Capability(
"backported-option-Ki") ) );
232 static const char* argv[] = {
"lsof",
"-n",
"-FpcuLRftkn0",
"-K",
"i", NULL };
239 std::map<pid_t,CacheEntry> cachemap;
241 FilterRunsInLXC runsInLXC;
245 if ( line[0] ==
'p' )
248 if ( !runsInLXC( cachepid ) )
249 cachemap[cachepid].first.swap( line );
255 addCacheIf( cachemap[cachepid], line, verbose_r );
259 int ret = prog.
close();
271 std::vector<ProcInfo> data;
272 for (
const auto & cached : cachemap )
274 addDataIf( data, cached.second );
296 static const str::regex rx(
"[0-9]+:name=systemd:/system.slice/(.*/)?(.*).service$" );
300 [&](
int num_r, std::string line_r )->
bool
319 return dumpRange( str <<
"CheckAccessDeleted ",
331 if ( obj.
pid.empty() )
332 return str <<
"<NoProc>";
std::string asString(const Patch::Category &obj)
Data about one running process accessing deleted files.
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
std::vector< ProcInfo > _data
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
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
std::ostream & operator<<(std::ostream &str, const CheckAccessDeleted &obj)
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).
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
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
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.