[ovs-dev] [PATCH] Ensure that daemon.py's monitor process has no controlling tty

Ben Pfaff blp at nicira.com
Thu Dec 13 22:03:56 UTC 2012


On Thu, Dec 13, 2012 at 01:25:39PM -0800, Gordon Good wrote:
> From: Gordon Good <ggood at nicira.com>
> 
> If daemon.py is used with the --monitor option to create a
> monitoring process, that monitoring process ends up inheriting
> the process group and session of the parent process. This
> change causes setsid() to be called for the monitor process
> as well as the daemon process.
> 
> Signed-off-by: Gordon Good <ggood at vmware.com>

The C implementation has the same issue.  And I don't think that we
want to create two new sessions, one for the monitor, one for the
daemon.  That doesn't seem necessary (is it?).

So here's what I'd propose instead.

--8<--------------------------cut here-------------------------->8--

From: Ben Pfaff <blp at nicira.com>
Date: Thu, 13 Dec 2012 14:01:23 -0800
Subject: [PATCH] daemon: Start monitor process, not daemon process, in new session.

To keep control+C and other signals in the initiating session from killing
the monitor process, we need to put the monitor process into its own
session.  However, until this point, we've only done that for the daemon
processes that the monitor started, which means that control+C would kill
the monitor but not the daemons that it launched.

I don't know of a benefit to putting the monitor and daemon processes in
different sessions, as opposed to one new session for both of them, so
this change does the latter.

daemonize_post_detach() is called from one additional context where we'd
want to be in a new session, the worker_start() function, but that function
is documented as to be called after daemonize_start(), in which case we
will (after this commit) already have called setsid(), so no additional
change is required there.

Bug #14280.
Reported-by: Gordon Good <ggood at nicira.com>
Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 lib/daemon.c         |    3 ++-
 python/ovs/daemon.py |    5 +++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/daemon.c b/lib/daemon.c
index 25f2db7..4849196 100644
--- a/lib/daemon.c
+++ b/lib/daemon.c
@@ -492,7 +492,9 @@ daemonize_start(void)
             /* Running in parent process. */
             exit(0);
         }
+
         /* Running in daemon or monitor process. */
+        setsid();
     }
 
     if (monitor) {
@@ -546,7 +548,6 @@ void
 daemonize_post_detach(void)
 {
     if (detach) {
-        setsid();
         if (chdir_) {
             ignore(chdir("/"));
         }
diff --git a/python/ovs/daemon.py b/python/ovs/daemon.py
index 650d250..f74d7f0 100644
--- a/python/ovs/daemon.py
+++ b/python/ovs/daemon.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2010, 2011 Nicira, Inc.
+# Copyright (c) 2010, 2011, 2012 Nicira, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -362,7 +362,9 @@ def daemonize_start():
         if _fork_and_wait_for_startup() > 0:
             # Running in parent process.
             sys.exit(0)
+
         # Running in daemon or monitor process.
+        os.setsid()
 
     if _monitor:
         saved_daemonize_fd = _daemonize_fd
@@ -384,7 +386,6 @@ def daemonize_complete():
     _fork_notify_startup(_daemonize_fd)
 
     if _detach:
-        os.setsid()
         if _chdir:
             os.chdir("/")
         _close_standard_fds()
-- 
1.7.2.5




More information about the dev mailing list