Ondřej Svoboda has uploaded a new change for review.
Change subject: utils: Moved pgrep and getCmdArgs from storage/misc
......................................................................
utils: Moved pgrep and getCmdArgs from storage/misc
They will be used elsewhere to find a running DHCP client
and to read an interface name from its cmdline.
Change-Id: Ia9a933bec0df3f7773c0f8e47fc0bf0492149031
Signed-off-by: Ondřej Svoboda <osvoboda(a)redhat.com>
---
M lib/vdsm/utils.py
M tests/miscTests.py
M tests/utilsTests.py
M vdsm/storage/blockSD.py
M vdsm/storage/misc.py
5 files changed, 86 insertions(+), 84 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/40/23040/1
diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py
index b072bd4..b0eeea8 100644
--- a/lib/vdsm/utils.py
+++ b/lib/vdsm/utils.py
@@ -259,6 +259,48 @@
return stat._make(res[:len(fields)])
+def iteratePids():
+ for path in glob.iglob("/proc/[0-9]*"):
+ pid = os.path.basename(path)
+ yield int(pid)
+
+
+def pgrep(name):
+ res = []
+ for pid in iteratePids():
+ try:
+ pid = int(pid)
+ except ValueError:
+ continue
+
+ try:
+ procName = pidStat(pid).comm
+ if procName == name:
+ res.append(pid)
+ except (OSError, IOError):
+ continue
+ return res
+
+
+def _parseCmdLine(pid):
+ with open("/proc/%d/cmdline" % pid, "rb") as f:
+ return tuple(f.read().split("\0")[:-1])
+
+
+def getCmdArgs(pid):
+ res = tuple()
+ # Sometimes cmdline is empty even though the process is not a zombie.
+ # Retrying seems to solve it.
+ while len(res) == 0:
+ # cmdline is empty for zombie processes
+ if pidStat(pid).state in ("Z", "z"):
+ return tuple()
+
+ res = _parseCmdLine(pid)
+
+ return res
+
+
def convertToStr(val):
varType = type(val)
if varType is float:
diff --git a/tests/miscTests.py b/tests/miscTests.py
index fb1191b..1765a57 100644
--- a/tests/miscTests.py
+++ b/tests/miscTests.py
@@ -58,45 +58,6 @@
return ret, out, err
-class PgrepTests(TestCaseBase):
- def test(self):
- sleepProcs = []
- for i in range(3):
- sleepProcs.append(utils.execCmd([EXT_SLEEP, "3"], sync=False,
- sudo=False))
-
- pids = misc.pgrep("sleep")
- for proc in sleepProcs:
- self.assertTrue(proc.pid in pids, "pid %d was not located by
pgrep"
- % proc.pid)
-
- for proc in sleepProcs:
- proc.kill()
- proc.wait()
-
-
-class GetCmdArgsTests(TestCaseBase):
- def test(self):
- args = [EXT_SLEEP, "4"]
- sproc = utils.execCmd(args, sync=False, sudo=False)
- try:
- self.assertEquals(misc.getCmdArgs(sproc.pid), tuple(args))
- finally:
- sproc.kill()
- sproc.wait()
-
- def testZombie(self):
- args = [EXT_SLEEP, "0"]
- sproc = utils.execCmd(args, sync=False, sudo=False)
- sproc.kill()
- try:
- test = lambda: self.assertEquals(misc.getCmdArgs(sproc.pid),
- tuple())
- utils.retry(AssertionError, test, tries=10, sleep=0.1)
- finally:
- sproc.wait()
-
-
class EventTests(TestCaseBase):
def testEmit(self):
ev = threading.Event()
diff --git a/tests/utilsTests.py b/tests/utilsTests.py
index c5b250d..1399c93 100644
--- a/tests/utilsTests.py
+++ b/tests/utilsTests.py
@@ -27,6 +27,8 @@
from vdsm import utils
import time
+EXT_SLEEP = "sleep"
+
class RetryTests(TestCaseBase):
def testStopCallback(self):
@@ -66,6 +68,45 @@
sproc.wait()
+class PgrepTests(TestCaseBase):
+ def test(self):
+ sleepProcs = []
+ for i in range(3):
+ sleepProcs.append(utils.execCmd([EXT_SLEEP, "3"], sync=False,
+ sudo=False))
+
+ pids = utils.pgrep("sleep")
+ for proc in sleepProcs:
+ self.assertTrue(proc.pid in pids, "pid %d was not located by
pgrep"
+ % proc.pid)
+
+ for proc in sleepProcs:
+ proc.kill()
+ proc.wait()
+
+
+class GetCmdArgsTests(TestCaseBase):
+ def test(self):
+ args = [EXT_SLEEP, "4"]
+ sproc = utils.execCmd(args, sync=False, sudo=False)
+ try:
+ self.assertEquals(utils.getCmdArgs(sproc.pid), tuple(args))
+ finally:
+ sproc.kill()
+ sproc.wait()
+
+ def testZombie(self):
+ args = [EXT_SLEEP, "0"]
+ sproc = utils.execCmd(args, sync=False, sudo=False)
+ sproc.kill()
+ try:
+ test = lambda: self.assertEquals(utils.getCmdArgs(sproc.pid),
+ tuple())
+ utils.retry(AssertionError, test, tries=10, sleep=0.1)
+ finally:
+ sproc.wait()
+
+
class CommandPathTests(TestCaseBase):
def testExisting(self):
cp = utils.CommandPath('sh', 'utter nonsense',
'/bin/sh')
diff --git a/vdsm/storage/blockSD.py b/vdsm/storage/blockSD.py
index 55bd796..5179c5a 100644
--- a/vdsm/storage/blockSD.py
+++ b/vdsm/storage/blockSD.py
@@ -1177,7 +1177,7 @@
@classmethod
def __handleStuckUmount(cls, masterDir):
- umountPids = misc.pgrep("umount")
+ umountPids = utils.pgrep("umount")
try:
masterMount = mount.getMountFromTarget(masterDir)
except OSError as ex:
@@ -1189,7 +1189,7 @@
for umountPid in umountPids:
try:
state = utils.pidStat(umountPid).state
- mountPoint = misc.getCmdArgs(umountPid)[-1]
+ mountPoint = utils.getCmdArgs(umountPid)[-1]
except:
# Process probably exited
continue
diff --git a/vdsm/storage/misc.py b/vdsm/storage/misc.py
index 48020b6..3f2c074 100644
--- a/vdsm/storage/misc.py
+++ b/vdsm/storage/misc.py
@@ -828,48 +828,6 @@
return helper
-def iteratePids():
- for path in glob.iglob("/proc/[0-9]*"):
- pid = os.path.basename(path)
- yield int(pid)
-
-
-def pgrep(name):
- res = []
- for pid in iteratePids():
- try:
- pid = int(pid)
- except ValueError:
- continue
-
- try:
- procName = utils.pidStat(pid).comm
- if procName == name:
- res.append(pid)
- except (OSError, IOError):
- continue
- return res
-
-
-def _parseCmdLine(pid):
- with open("/proc/%d/cmdline" % pid, "rb") as f:
- return tuple(f.read().split("\0")[:-1])
-
-
-def getCmdArgs(pid):
- res = tuple()
- # Sometimes cmdline is empty even though the process is not a zombie.
- # Retrying seems to solve it.
- while len(res) == 0:
- # cmdline is empty for zombie processes
- if utils.pidStat(pid).state in ("Z", "z"):
- return tuple()
-
- res = _parseCmdLine(pid)
-
- return res
-
-
def tmap(func, iterable):
resultsDict = {}
error = [None]
@@ -1008,7 +966,7 @@
def killall(name, signum, group=False):
exception = None
knownPgs = set()
- pidList = pgrep(name)
+ pidList = utils.pgrep(name)
if len(pidList) == 0:
raise OSError(errno.ESRCH,
"Could not find processes named `%s`" % name)
--
To view, visit
http://gerrit.ovirt.org/23040
To unsubscribe, visit
http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia9a933bec0df3f7773c0f8e47fc0bf0492149031
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Ondřej Svoboda <osvoboda(a)redhat.com>