From 7b4baeb16cc47bf8cc30904dbebd71f198d8bc77 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Mon, 10 Jan 2011 19:59:45 +0100
Subject: [PATCH] utils: Use function to disable fork

Use a function instead of a variable written by another module.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/daemon.py                 |  3 ++-
 lib/utils/__init__.py         | 15 ++++++++++++---
 test/ganeti.utils_unittest.py |  7 ++++---
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/lib/daemon.py b/lib/daemon.py
index 99f7dd322..15b89bf35 100644
--- a/lib/daemon.py
+++ b/lib/daemon.py
@@ -620,7 +620,8 @@ def GenericMain(daemon_name, optionparser,
                             metavar="SSL_CERT_PATH")
 
   # Disable the use of fork(2) if the daemon uses threads
-  utils.no_fork = multithreaded
+  if multithreaded:
+    utils.DisableFork()
 
   options, args = optionparser.parse_args()
 
diff --git a/lib/utils/__init__.py b/lib/utils/__init__.py
index 06b5e87d7..061456fa0 100644
--- a/lib/utils/__init__.py
+++ b/lib/utils/__init__.py
@@ -62,7 +62,7 @@ from ganeti.utils.hash import * # pylint: disable-msg=W0401
 
 
 #: when set to True, L{RunCmd} is disabled
-no_fork = False
+_no_fork = False
 
 _RANDOM_UUID_FILE = "/proc/sys/kernel/random/uuid"
 
@@ -93,6 +93,15 @@ _SHELLPARAM_REGEX = re.compile(r"^[-a-zA-Z0-9._+/:%@]+$")
 _ASN1_TIME_REGEX = re.compile(r"^(\d+)([-+]\d\d)(\d\d)$")
 
 
+def DisableFork():
+  """Disables the use of fork(2).
+
+  """
+  global _no_fork # pylint: disable-msg=W0603
+
+  _no_fork = True
+
+
 class RunResult(object):
   """Holds the result of running external programs.
 
@@ -203,7 +212,7 @@ def RunCmd(cmd, env=None, output=None, cwd="/", reset_env=False,
   @raise errors.ProgrammerError: if we call this when forks are disabled
 
   """
-  if no_fork:
+  if _no_fork:
     raise errors.ProgrammerError("utils.RunCmd() called with fork() disabled")
 
   if output and interactive:
@@ -318,7 +327,7 @@ def StartDaemon(cmd, env=None, cwd="/", output=None, output_fd=None,
   @raise errors.ProgrammerError: if we call this when forks are disabled
 
   """
-  if no_fork:
+  if _no_fork:
     raise errors.ProgrammerError("utils.StartDaemon() called with fork()"
                                  " disabled")
 
diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py
index 65431f840..a62feb52b 100755
--- a/test/ganeti.utils_unittest.py
+++ b/test/ganeti.utils_unittest.py
@@ -366,12 +366,13 @@ class TestRunCmd(testutils.GanetiTestCase):
 
   def testNoFork(self):
     """Test that nofork raise an error"""
-    assert not utils.no_fork
-    utils.no_fork = True
+    self.assertFalse(utils._no_fork)
+    utils.DisableFork()
     try:
+      self.assertTrue(utils._no_fork)
       self.assertRaises(errors.ProgrammerError, RunCmd, ["true"])
     finally:
-      utils.no_fork = False
+      utils._no_fork = False
 
   def testWrongParams(self):
     """Test wrong parameters"""
-- 
GitLab