# HG changeset patch # User Nicolas Dumazet # Date 2009-08-16 02:11:37 # Node ID 7c01599dd3402fe1cb410f5a03203720024d9ee5 # Parent ae88c721f9166b338ea0d2f6e37cdf01bb1ff25e 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 @@ from client import client, QueryFailed 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 @@ def start_server(function): 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 @@ class master(object): 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) 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