14 #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 std::string commandname;
70 for_( ch, pline.begin(), pline.end() )
78 pinfo.ppid = &*(ch+1);
81 pinfo.puid = &*(ch+1);
84 pinfo.login = &*(ch+1);
87 if ( pinfo.command.empty() )
88 commandname = &*(ch+1);
91 if ( *ch ==
'\n' )
break;
92 do { ++ch; }
while ( *ch !=
'\0' );
95 if ( pinfo.command.empty() )
99 if ( pinfo.command.empty() )
100 pinfo.command = std::move(commandname);
110 inline void addCacheIf( CacheEntry & cache_r,
const std::string & line_r,
bool verbose_r )
116 for_( ch, line_r.c_str(), ch+line_r.size() )
121 if ( *(ch+1) !=
'0' )
134 if ( *ch ==
'\n' )
break;
135 do { ++ch; }
while ( *ch !=
'\0' );
138 if ( !t || !f || !n )
141 if ( !( ( *t ==
'R' && *(t+1) ==
'E' && *(t+2) ==
'G' && *(t+3) ==
'\0' )
142 || ( *t ==
'D' && *(t+1) ==
'E' && *(t+2) ==
'L' && *(t+3) ==
'\0' ) ) )
145 if ( !( ( *f ==
'm' && *(f+1) ==
'e' && *(f+2) ==
'm' && *(f+3) ==
'\0' )
146 || ( *f ==
't' && *(f+1) ==
'x' && *(f+2) ==
't' && *(f+3) ==
'\0' )
147 || ( *f ==
'D' && *(f+1) ==
'E' && *(f+2) ==
'L' && *(f+3) ==
'\0' )
148 || ( *f ==
'l' && *(f+1) ==
't' && *(f+2) ==
'x' && *(f+3) ==
'\0' ) ) )
160 if ( *f ==
'm' || *f ==
'D' )
162 static const char * black[] = {
175 cache_r.second.insert( n );
184 struct FilterRunsInLXC
186 bool operator()( pid_t pid_r )
const 187 {
return( nsIno( pid_r,
"pid" ) !=
pidNS || nsIno( pid_r,
"mnt" ) !=
mntNS ); }
190 :
pidNS( nsIno(
"self",
"pid" ) )
191 ,
mntNS( nsIno(
"self",
"mnt" ) )
194 static inline ino_t nsIno(
const std::string & pid_r,
const std::string & ns_r )
195 {
return PathInfo(
"/proc/"+pid_r+
"/ns/"+ns_r).ino(); }
197 static inline ino_t nsIno( pid_t pid_r,
const std::string & ns_r )
198 {
return nsIno(
asString(pid_r), ns_r ); }
205 void lsofdebug(
const Pathname & file_r )
207 std::ifstream infile( file_r.c_str() );
208 USR << infile << endl;
209 std::vector<std::string> fields;
211 for( iostr::EachLine in( infile ); in; in.next() )
213 std::string field( *in );
214 if ( field[0] ==
'f' || field[0] ==
'p' )
216 if ( !fields.empty() )
219 std::string line(
str::join( fields,
"\n" ) );
220 for (
char & c : line )
221 {
if ( c ==
'\n' ) c =
'\0'; }
222 line.push_back(
'\n' );
224 size_t sze = cache.second.size();
225 addCacheIf( cache, line,
false );
226 if ( sze != cache.second.size() )
227 USR << fields << endl;
231 if ( field[0] ==
'p' )
233 fields.push_back( field );
235 else if ( !fields.empty() )
237 fields.push_back( field );
250 static const char* argv[] =
252 "lsof",
"-n",
"-FpcuLRftkn0", NULL
258 std::map<pid_t,CacheEntry> cachemap;
260 FilterRunsInLXC runsInLXC;
264 if ( line[0] ==
'p' )
267 if ( !runsInLXC( cachepid ) )
268 cachemap[cachepid].first.swap( line );
274 addCacheIf( cachemap[cachepid], line, verbose_r );
278 int ret = prog.
close();
290 std::vector<ProcInfo> data;
291 for (
const auto & cached : cachemap )
293 addDataIf( data, cached.second );
315 static const str::regex rx(
"[0-9]+:name=systemd:/system.slice/(.*/)?(.*).service$" );
319 [&](
int num_r, std::string line_r )->
bool 338 return dumpRange( str <<
"CheckAccessDeleted ",
350 if ( obj.
pid.empty() )
351 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'.
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
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.