inotify: use cmdutil.service instead of local daemonizing code
authorNicolas Dumazet <nicdumz.commits@gmail.com>
Sun, 16 Aug 2009 11:11:37 +0900
changeset 9514 7c01599dd340
parent 9513 ae88c721f916
child 9515 f7d85980261c
inotify: use cmdutil.service instead of local daemonizing code
hgext/inotify/__init__.py
hgext/inotify/client.py
hgext/inotify/server.py
tests/test-inotify-issue1208.out
--- a/hgext/inotify/__init__.py	Sun Aug 16 11:30:24 2009 +0900
+++ b/hgext/inotify/__init__.py	Sun Aug 16 11:11:37 2009 +0900
@@ -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
--- a/hgext/inotify/client.py	Sun Aug 16 11:30:24 2009 +0900
+++ b/hgext/inotify/client.py	Sun Aug 16 11:11:37 2009 +0900
@@ -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.
--- a/hgext/inotify/server.py	Sun Aug 16 11:30:24 2009 +0900
+++ b/hgext/inotify/server.py	Sun Aug 16 11:11:37 2009 +0900
@@ -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
-
-    m = master(ui, dirstate, root)
-    sys.stdout.flush()
-    sys.stderr.flush()
+                self.master = master(ui, dirstate, root, timeout)
+            except AlreadyStartedException, inst:
+                raise util.Abort(str(inst))
 
-    pid = os.fork()
-    if pid:
-        return pid
-
-    closefds(pollable.instances.keys())
-    os.setsid()
-
-    fd = os.open('/dev/null', os.O_RDONLY)
-    os.dup2(fd, 0)
-    if fd > 0:
-        os.close(fd)
+        def run(self):
+            try:
+                self.master.run()
+            finally:
+                self.master.shutdown()
 
-    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)
+    runargs = None
+    if 'inserve' not in sys.argv:
+        runargs = [sys.argv[0], 'inserve', '-R', root]
 
-    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)
--- a/tests/test-inotify-issue1208.out	Sun Aug 16 11:30:24 2009 +0900
+++ b/tests/test-inotify-issue1208.out	Sun Aug 16 11:11:37 2009 +0900
@@ -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