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 for_( ch, pline.begin(), pline.end() )
76 pinfo.ppid = &*(ch+1);
79 pinfo.puid = &*(ch+1);
82 pinfo.login = &*(ch+1);
85 pinfo.command = &*(ch+1);
88 if ( *ch ==
'\n' )
break;
89 do { ++ch; }
while ( *ch !=
'\0' );
92 if ( pinfo.command.size() == 15 )
96 if ( ! command.empty() )
97 pinfo.command = command.basename();
108 inline void addCacheIf( CacheEntry & cache_r,
const std::string & line_r,
bool verbose_r )
114 for_( ch, line_r.c_str(), ch+line_r.size() )
119 if ( *(ch+1) !=
'0' )
132 if ( *ch ==
'\n' )
break;
133 do { ++ch; }
while ( *ch !=
'\0' );
136 if ( !t || !f || !n )
139 if ( !( ( *t ==
'R' && *(t+1) ==
'E' && *(t+2) ==
'G' && *(t+3) ==
'\0' )
140 || ( *t ==
'D' && *(t+1) ==
'E' && *(t+2) ==
'L' && *(t+3) ==
'\0' ) ) )
143 if ( !( ( *f ==
'm' && *(f+1) ==
'e' && *(f+2) ==
'm' && *(f+3) ==
'\0' )
144 || ( *f ==
't' && *(f+1) ==
'x' && *(f+2) ==
't' && *(f+3) ==
'\0' )
145 || ( *f ==
'D' && *(f+1) ==
'E' && *(f+2) ==
'L' && *(f+3) ==
'\0' )
146 || ( *f ==
'l' && *(f+1) ==
't' && *(f+2) ==
'x' && *(f+3) ==
'\0' ) ) )
158 if ( *f ==
'm' || *f ==
'D' )
160 static const char * black[] = {
172 cache_r.second.insert( n );
181 struct FilterRunsInLXC
183 bool operator()( pid_t pid_r )
const
184 {
return( nsIno( pid_r,
"pid" ) !=
pidNS || nsIno( pid_r,
"mnt" ) !=
mntNS ); }
187 :
pidNS( nsIno(
"self",
"pid" ) )
188 ,
mntNS( nsIno(
"self",
"mnt" ) )
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 ); }
209 static const char* argv[] =
211 "lsof",
"-n",
"-FpcuLRftkn0", NULL
217 std::map<pid_t,CacheEntry> cachemap;
219 FilterRunsInLXC runsInLXC;
223 if ( line[0] ==
'p' )
226 if ( !runsInLXC( cachepid ) )
227 cachemap[cachepid].first.swap( line );
233 addCacheIf( cachemap[cachepid], line, verbose_r );
237 int ret = prog.
close();
249 std::vector<ProcInfo> data;
250 for (
const auto & cached : cachemap )
252 addDataIf( data, cached.second );
274 static const str::regex rx(
"[0-9]+:name=systemd:/system.slice/(.*/)?(.*).service$" );
278 [&](
int num_r, std::string line_r )->
bool
297 return dumpRange( str <<
"CheckAccessDeleted ",
309 if ( obj.
pid.empty() )
310 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.