Commit a9211d79 authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[trac606] added a new option for bob, --pid-file, to record the PID of bob himself.

mainly intended for testing purposes, so disabled by default.
for testing this feature bind10_test.py is renamed to bind10_test.py.in
and is auto generated by the configure script.
parent 48211526
......@@ -712,6 +712,7 @@ AC_OUTPUT([doc/version.ent
src/bin/stats/tests/stats_test
src/bin/bind10/bind10.py
src/bin/bind10/tests/bind10_test
src/bin/bind10/tests/bind10_test.py
src/bin/bind10/run_bind10.sh
src/bin/bindctl/run_bindctl.sh
src/bin/bindctl/bindctl-source.py
......
......@@ -785,6 +785,36 @@ def process_rename(option, opt_str, value, parser):
"""Function that renames the process if it is requested by a option."""
isc.util.process.rename(value)
def dump_pid(pid_file):
"""
Dump the PID of the current process to the specified file. If the given
file is None this function does nothing. If the file already exists,
the existing content will be removed. If a system error happens in
creating or writing to the file, the corresponding exception will be
propagated to the caller.
"""
if pid_file is None:
return
unlink_pid_file(pid_file)
f = open(pid_file, "w")
f.write('%d\n' % os.getpid())
f.close()
def unlink_pid_file(pid_file):
"""
Remove the given file, which is basically expected to be the PID file
created by dump_pid(). If the specified may or may not exist; if it
doesn't this function does nothing. Other system level errors in removing
the file will be propagated as the corresponding exception.
"""
if pid_file is None:
return
try:
os.unlink(pid_file)
except OSError as error:
if error.errno is not errno.ENOENT:
raise
def main():
global options
global boss_of_bind
......@@ -805,6 +835,9 @@ def main():
parser.add_option("--pretty-name", type="string", action="callback",
callback=process_rename,
help="Set the process name (displayed in ps, top, ...)")
parser.add_option("--pid-file", dest="pid_file", type="string",
default=None,
help="file to dump the PID of the BIND 10 process")
(options, args) = parser.parse_args()
if args:
parser.print_help()
......@@ -865,6 +898,7 @@ def main():
sys.stderr.write("[bind10] Error on startup: %s\n" % startup_result)
sys.exit(1)
sys.stdout.write("[bind10] BIND 10 started\n")
dump_pid(options.pid_file)
# send "bind10.boot_time" to b10-stats
time.sleep(1) # wait a second
......@@ -918,6 +952,7 @@ def main():
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
boss_of_bind.shutdown()
sys.stdout.write("[bind10] BIND 10 exiting\n");
unlink_pid_file(options.pid_file)
sys.exit(0)
if __name__ == "__main__":
......
from bind10 import ProcessInfo, BoB
from bind10 import ProcessInfo, BoB, dump_pid, unlink_pid_file
# XXX: environment tests are currently disabled, due to the preprocessor
# setup that we have now complicating the environment
......@@ -412,5 +412,51 @@ class TestStartStopProcessesBob(unittest.TestCase):
bob.config_handler({'start_auth': True, 'start_resolver': True})
class TestPIDFile(unittest.TestCase):
def setUp(self):
self.pid_file = '@builddir@' + os.sep + 'bind10.pid'
if os.path.exists(self.pid_file):
os.unlink(self.pid_file)
def tearDown(self):
if os.path.exists(self.pid_file):
os.unlink(self.pid_file)
def check_pid_file(self):
# dump PID to the file, and confirm the content is correct
dump_pid(self.pid_file)
my_pid = os.getpid()
self.assertEqual(my_pid, int(open(self.pid_file, "r").read()))
def test_dump_pid(self):
self.check_pid_file()
# make sure any existing content will be removed
open(self.pid_file, "w").write('dummy data\n')
self.check_pid_file()
def test_dump_pid_notexist(self):
dummy_data = 'dummy_data\n'
open(self.pid_file, "w").write(dummy_data)
dump_pid('no_such_pid_file')
# the original content of the file should be intact.
self.assertEqual(dummy_data, open(self.pid_file, "r").read())
def test_dump_pid_with_none(self):
# Check the behavior of dump_pid() and unlink_pid_file() with None.
# This should be no-op.
dump_pid(None)
self.assertFalse(os.path.exists(self.pid_file))
dummy_data = 'dummy_data\n'
open(self.pid_file, "w").write(dummy_data)
unlink_pid_file(None)
self.assertEqual(dummy_data, open(self.pid_file, "r").read())
def test_dump_pid_failure(self):
# the attempt to open file will fail, which should result in exception.
self.assertRaises(IOError, dump_pid,
'nonexistent_dir' + os.sep + 'bind10.pid')
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment