13 #include <unordered_set>
27 #undef ZYPP_BASE_LOGGER_LOGGROUP
28 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::misc"
47 typedef std::pair<std::string,std::unordered_set<std::string>> CacheEntry;
53 inline void addDataIf( std::vector<CheckAccessDeleted::ProcInfo> & data_r,
const CacheEntry & cache_r )
55 const auto & filelist( cache_r.second );
57 if ( filelist.empty() )
61 data_r.push_back( CheckAccessDeleted::ProcInfo() );
62 CheckAccessDeleted::ProcInfo & pinfo( data_r.back() );
63 pinfo.files.insert( pinfo.files.begin(), filelist.begin(), filelist.end() );
65 const std::string & pline( cache_r.first );
66 for_( ch, pline.begin(), pline.end() )
74 pinfo.ppid = &*(ch+1);
77 pinfo.puid = &*(ch+1);
80 pinfo.login = &*(ch+1);
83 pinfo.command = &*(ch+1);
86 if ( *ch ==
'\n' )
break;
87 do { ++ch; }
while ( *ch !=
'\0' );
90 if ( pinfo.command.size() == 15 )
94 if ( ! command.empty() )
95 pinfo.command = command.basename();
106 inline void addCacheIf( CacheEntry & cache_r,
const std::string & line_r,
bool verbose_r )
112 for_( ch, line_r.c_str(), ch+line_r.size() )
117 if ( *(ch+1) !=
'0' )
130 if ( *ch ==
'\n' )
break;
131 do { ++ch; }
while ( *ch !=
'\0' );
134 if ( !t || !f || !n )
137 if ( !( ( *t ==
'R' && *(t+1) ==
'E' && *(t+2) ==
'G' && *(t+3) ==
'\0' )
138 || ( *t ==
'D' && *(t+1) ==
'E' && *(t+2) ==
'L' && *(t+3) ==
'\0' ) ) )
141 if ( !( ( *f ==
'm' && *(f+1) ==
'e' && *(f+2) ==
'm' && *(f+3) ==
'\0' )
142 || ( *f ==
't' && *(f+1) ==
'x' && *(f+2) ==
't' && *(f+3) ==
'\0' )
143 || ( *f ==
'D' && *(f+1) ==
'E' && *(f+2) ==
'L' && *(f+3) ==
'\0' )
144 || ( *f ==
'l' && *(f+1) ==
't' && *(f+2) ==
'x' && *(f+3) ==
'\0' ) ) )
156 if ( *f ==
'm' || *f ==
'D' )
158 static const char * black[] = {
170 cache_r.second.insert( n );
179 struct FilterRunsInLXC
181 bool operator()( pid_t pid_r )
const
182 {
return( nsIno( pid_r,
"pid" ) !=
pidNS ); }
185 :
pidNS( nsIno(
"self",
"pid" ) )
188 static inline ino_t nsIno(
const std::string & pid_r,
const std::string & ns_r )
189 {
return PathInfo(
"/proc/"+pid_r+
"/ns/"+ns_r).ino(); }
191 static inline ino_t nsIno( pid_t pid_r,
const std::string & ns_r )
192 {
return nsIno(
asString(pid_r), ns_r ); }
204 using target::rpm::librpmDb;
209 : _wasBlocked( librpmDb::isBlocked() )
210 {
if ( _wasBlocked ) librpmDb::unblockAccess(); }
212 {
if ( _wasBlocked ) librpmDb::blockAccess(); }
217 librpmDb::db_const_iterator it;
218 return( it.findPackage(
"lsof" ) && it->tag_edition() < Edition(
"4.90") && !it->tag_provides().count( Capability(
"backported-option-Ki") ) );
229 static const char* argv[] = {
"lsof",
"-n",
"-FpcuLRftkn0",
"-K",
"i", NULL };
236 std::map<pid_t,CacheEntry> cachemap;
238 FilterRunsInLXC runsInLXC;
242 if ( line[0] ==
'p' )
245 if ( !runsInLXC( cachepid ) )
246 cachemap[cachepid].first.swap( line );
252 addCacheIf( cachemap[cachepid], line, verbose_r );
256 int ret = prog.
close();
268 std::vector<ProcInfo> data;
269 for (
const auto & cached : cachemap )
271 addDataIf( data, cached.second );
280 p.
command = command_r.basename();
302 return std::string();
306 static const Pathname initD(
"/etc/init.d" );
309 if ( pi.isFile() && pi.isX() )
313 std::string alt(
command+
"d" );
314 PathInfo pi( initD/alt );
315 if ( pi.isFile() && pi.isX() )
318 if ( *
command.rbegin() ==
'd' )
321 alt.erase( alt.size()-1 );
322 PathInfo pi( initD/alt );
324 if ( pi.isFile() && pi.isX() )
327 return std::string();
337 return dumpRange( str <<
"CheckAccessDeleted ",
349 if ( obj.
pid.empty() )
350 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.
#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 an /etc/init.d/ 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.
std::string pid
process ID
std::ostream & dumpRange(std::ostream &str, _Iterator begin, _Iterator 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 ...
std::string puid
process user ID
std::string receiveLine()
Read one line from the input stream.
_It strtonum(const C_Str &str)
Parsing numbers from string.
std::ostream & dumpRangeLine(std::ostream &str, _Iterator begin, _Iterator end)
Print range defined by iterators (single line style).
std::string numstring(char n, int w=0)
std::string asString(const Patch::SeverityFlag &obj)
int close()
Wait for the progamm to complete.
#define arrayBegin(A)
Simple C-array iterator.
Base class for Exception.
Check for running processes which access deleted executables or libraries.
const_iterator end() const
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
static std::string findService(const char *command_r)
Guess if command was started by an /etc/init.d/ script.
std::vector< std::string > files
list of deleted executables or libraries accessed
std::string ppid
parent process ID