[PATCH 2 of 2] inotify: use cmdutil.service instead of local daemonizing code

Nicolas Dumazet nicdumz at gmail.com
Sat Aug 15 22:06:19 CDT 2009


# HG changeset patch
# User Nicolas Dumazet <nicdumz.commits at gmail.com>
# Date 1250388697 -32400
# Node ID ca0fbff12650044810b7ec6c870235fd8c3dc7d5
# Parent  f9a8cb7a99d7772d33c0a288f3227b1ddff41546
inotify: use cmdutil.service instead of local daemonizing code

diff --git a/hgext/inotify/__init__.py b/hgext/inotify/__init__.py
--- a/hgext/inotify/__init__.py
+++ b/hgext/inotify/__init__.py
@@ -17,28 +17,7 @@
 
 def serve(ui, repo, **opts):
     '''start an inotify server for this repository'''
-    timeout = opts.get('timeout')
-    if timeout:
-        timeout = float(timeout) * 1e3
-
-    class service(object):
-        def init(self):
-            try:
-                self.master = server.master(ui, repo.dirstate,
-                                            repo.root, timeout)
-            except server.AlreadyStartedException, inst:
-                raise util.Abort(str(inst))
-
-        def run(self):
-            try:
-                self.master.run()
-            finally:
-                self.master.shutdown()
-
-    service = service()
-    logfile = ui.config('inotify', 'log')
-    cmdutil.service(opts, initfn=service.init, runfn=service.run,
-                    logfile=logfile)
+    server.start(ui, repo.dirstate, repo.root, opts)
 
 def debuginotify(ui, repo, **opts):
     '''debugging information for inotify extension
diff --git a/hgext/inotify/client.py b/hgext/inotify/client.py
--- a/hgext/inotify/client.py
+++ b/hgext/inotify/client.py
@@ -34,7 +34,8 @@
                 self.ui.debug(_('(starting inotify server)\n'))
                 try:
                     try:
-                        server.start(self.ui, self.dirstate, self.root)
+                        server.start(self.ui, self.dirstate, self.root,
+                                     dict(daemon=True, daemon_pipefds=''))
                     except server.AlreadyStartedException, inst:
                         # another process may have started its own
                         # inotify server while this one was starting.
diff --git a/hgext/inotify/server.py b/hgext/inotify/server.py
--- a/hgext/inotify/server.py
+++ b/hgext/inotify/server.py
@@ -7,7 +7,7 @@
 # GNU General Public License version 2, incorporated herein by reference.
 
 from mercurial.i18n import _
-from mercurial import osutil, util
+from mercurial import cmdutil, osutil, util
 import common
 import errno, os, select, socket, stat, struct, sys, tempfile, time
 
@@ -823,52 +823,29 @@
             sys.exit(0)
         pollable.run()
 
-def start(ui, dirstate, root):
-    def closefds(ignore):
-        # (from python bug #1177468)
-        # close all inherited file descriptors
-        # Python 2.4.1 and later use /dev/urandom to seed the random module's RNG
-        # a file descriptor is kept internally as os._urandomfd (created on demand
-        # the first time os.urandom() is called), and should not be closed
-        try:
-            os.urandom(4)
-            urandom_fd = getattr(os, '_urandomfd', None)
-        except AttributeError:
-            urandom_fd = None
-        ignore.append(urandom_fd)
-        for fd in range(3, 256):
-            if fd in ignore:
-                continue
+def start(ui, dirstate, root, opts):
+    timeout = opts.get('timeout')
+    if timeout:
+        timeout = float(timeout) * 1e3
+
+    class service(object):
+        def init(self):
             try:
-                os.close(fd)
-            except OSError:
-                pass
+                self.master = master(ui, dirstate, root, timeout)
+            except AlreadyStartedException, inst:
+                raise util.Abort(str(inst))
 
-    m = master(ui, dirstate, root)
-    sys.stdout.flush()
-    sys.stderr.flush()
+        def run(self):
+            try:
+                self.master.run()
+            finally:
+                self.master.shutdown()
 
-    pid = os.fork()
-    if pid:
-        return pid
+    runargs = None
+    if 'inserve' not in sys.argv:
+        runargs = ['hg', 'inserve']
 
-    closefds(pollable.instances.keys())
-    os.setsid()
-
-    fd = os.open('/dev/null', os.O_RDONLY)
-    os.dup2(fd, 0)
-    if fd > 0:
-        os.close(fd)
-
-    fd = os.open(ui.config('inotify', 'log', '/dev/null'),
-                 os.O_RDWR | os.O_CREAT | os.O_TRUNC)
-    os.dup2(fd, 1)
-    os.dup2(fd, 2)
-    if fd > 2:
-        os.close(fd)
-
-    try:
-        m.run()
-    finally:
-        m.shutdown()
-        os._exit(0)
+    service = service()
+    logfile = ui.config('inotify', 'log')
+    cmdutil.service(opts, initfn=service.init, runfn=service.run,
+                    logfile=logfile, runargs=runargs)
diff --git a/tests/test-inotify-issue1208.out b/tests/test-inotify-issue1208.out
--- a/tests/test-inotify-issue1208.out
+++ b/tests/test-inotify-issue1208.out
@@ -1,5 +1,5 @@
 % fail
-could not talk to new inotify server: No such file or directory
+abort: could not start server: File exists
 abort: could not start server: File exists
 % inserve
 % status


More information about the Mercurial-devel mailing list