zhengzheng / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Linux Process.get_open_files(): race condition + possible incorrect results #272

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I realized there are two potential bugs in Process.get_connections() code on 
Linux:
http://code.google.com/p/psutil/source/browse/tags/release-0.4.1/psutil/_pslinux
.py#582

    @wrap_exceptions
    def get_open_files(self):
        retlist = []
        files = os.listdir("/proc/%s/fd" % self.pid)
        for fd in files:
            file = "/proc/%s/fd/%s" % (self.pid, fd)
            if os.path.islink(file):
                file = os.readlink(file)
                if file.startswith("socket:["):
                    continue
                if file.startswith("pipe:["):
                    continue
                if file == "[]":
                    continue
                if os.path.isfile(file) and not file in retlist:
                    ntuple = ntuple_openfile(file, int(fd))
                    retlist.append(ntuple)
        return retlist

At line 589 we have a race condition in case "file" disappears (os.readlink() 
fails with ENOENT which gets translated to NoSuchProcess).
At line 596, before os.path.isfile(file), we should check that "file" is an 
absolute path.
If not we cannot tell whether "file" is valid or not because the check gets 
made against process's current working directory.

Original issue reported on code.google.com by g.rodola on 1 Jun 2012 at 8:41

GoogleCodeExporter commented 9 years ago
Fixed in r1328.

Original comment by g.rodola on 1 Jun 2012 at 9:11

GoogleCodeExporter commented 9 years ago
0.5.0 is finally out. Closing out as fixed.

Original comment by g.rodola on 27 Jun 2012 at 6:54

GoogleCodeExporter commented 9 years ago
Updated csets after the SVN -> Mercurial migration:
r1328 == revision ???

Original comment by g.rodola on 2 Mar 2013 at 12:08