@@ -233,29 +233,31 @@ def iter_proc_map():
233233 yield pid , inode , pathname
234234
235235
236+ def get_fileobj (pid , inode , pathname ):
237+ logging .debug ("path: %s" , pathname )
238+ # If mapped file exists and has the same inode
239+ if os .path .isfile (pathname ) and os .stat (pathname ).st_ino == int (inode ):
240+ fileobj = open (pathname , 'rb' )
241+ # If file exists only as a mapped to the memory
242+ else :
243+ fileobj = open_mmapped (pid , inode )
244+ logging .warning ("Library `%s` was gathered from memory." , pathname )
245+ return fileobj
246+
247+
236248def iter_proc_lib ():
237249 cache = {}
238250 for pid , inode , pathname in iter_proc_map ():
239251 if inode not in cache :
240- logging .debug ("path: %s" , pathname )
241- # If mapped file exists and has the same inode
242- if os .path .isfile (pathname ) and os .stat (pathname ).st_ino == int (inode ):
243- fileobj = open (pathname , 'rb' )
244- # If file exists only as a mapped to the memory
245- else :
246- fileobj = open_mmapped (pid , inode )
247- logging .warning ("Library `%s` was gathered from memory." , pathname )
248-
249252 try :
250- cache [inode ] = get_build_id (fileobj )
253+ with get_fileobj (pid , inode , pathname ) as fileobj :
254+ cache [inode ] = get_build_id (fileobj )
251255 except (NotAnELFException , BuildIDParsingException ) as err :
252256 logging .info ("Cat't read buildID from {0}: {1}" .format (pathname , err ))
253257 cache [inode ] = None
254258 except Exception as err :
255259 logging .error ("Cat't read buildID from {0}: {1}" .format (pathname , err ))
256260 cache [inode ] = None
257- finally :
258- fileobj .close ()
259261 build_id = cache [inode ]
260262 yield pid , os .path .basename (pathname ), build_id
261263
@@ -293,6 +295,8 @@ def main():
293295 "such issues with no process downtime. To find out more, please,"
294296 " visit https://lp.kernelcare.com/kernelcare-early-access?" )
295297
298+ return 0 if not failed else 1
299+
296300
297301if __name__ == '__main__' :
298302 exit (main ())
0 commit comments