##// END OF EJS Templates
inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet -
r9514:7c01599d default
parent child Browse files
Show More
@@ -17,28 +17,7 b' from client import client, QueryFailed'
17
17
18 def serve(ui, repo, **opts):
18 def serve(ui, repo, **opts):
19 '''start an inotify server for this repository'''
19 '''start an inotify server for this repository'''
20 timeout = opts.get('timeout')
20 server.start(ui, repo.dirstate, repo.root, opts)
21 if timeout:
22 timeout = float(timeout) * 1e3
23
24 class service(object):
25 def init(self):
26 try:
27 self.master = server.master(ui, repo.dirstate,
28 repo.root, timeout)
29 except server.AlreadyStartedException, inst:
30 raise util.Abort(str(inst))
31
32 def run(self):
33 try:
34 self.master.run()
35 finally:
36 self.master.shutdown()
37
38 service = service()
39 logfile = ui.config('inotify', 'log')
40 cmdutil.service(opts, initfn=service.init, runfn=service.run,
41 logfile=logfile)
42
21
43 def debuginotify(ui, repo, **opts):
22 def debuginotify(ui, repo, **opts):
44 '''debugging information for inotify extension
23 '''debugging information for inotify extension
@@ -34,7 +34,8 b' def start_server(function):'
34 self.ui.debug('(starting inotify server)\n')
34 self.ui.debug('(starting inotify server)\n')
35 try:
35 try:
36 try:
36 try:
37 server.start(self.ui, self.dirstate, self.root)
37 server.start(self.ui, self.dirstate, self.root,
38 dict(daemon=True, daemon_pipefds=''))
38 except server.AlreadyStartedException, inst:
39 except server.AlreadyStartedException, inst:
39 # another process may have started its own
40 # another process may have started its own
40 # inotify server while this one was starting.
41 # inotify server while this one was starting.
@@ -7,7 +7,7 b''
7 # GNU General Public License version 2, incorporated herein by reference.
7 # GNU General Public License version 2, incorporated herein by reference.
8
8
9 from mercurial.i18n import _
9 from mercurial.i18n import _
10 from mercurial import osutil, util
10 from mercurial import cmdutil, osutil, util
11 import common
11 import common
12 import errno, os, select, socket, stat, struct, sys, tempfile, time
12 import errno, os, select, socket, stat, struct, sys, tempfile, time
13
13
@@ -823,52 +823,29 b' class master(object):'
823 sys.exit(0)
823 sys.exit(0)
824 pollable.run()
824 pollable.run()
825
825
826 def start(ui, dirstate, root):
826 def start(ui, dirstate, root, opts):
827 def closefds(ignore):
827 timeout = opts.get('timeout')
828 # (from python bug #1177468)
828 if timeout:
829 # close all inherited file descriptors
829 timeout = float(timeout) * 1e3
830 # Python 2.4.1 and later use /dev/urandom to seed the random module's RNG
830
831 # a file descriptor is kept internally as os._urandomfd (created on demand
831 class service(object):
832 # the first time os.urandom() is called), and should not be closed
832 def init(self):
833 try:
834 os.urandom(4)
835 urandom_fd = getattr(os, '_urandomfd', None)
836 except AttributeError:
837 urandom_fd = None
838 ignore.append(urandom_fd)
839 for fd in range(3, 256):
840 if fd in ignore:
841 continue
842 try:
833 try:
843 os.close(fd)
834 self.master = master(ui, dirstate, root, timeout)
844 except OSError:
835 except AlreadyStartedException, inst:
845 pass
836 raise util.Abort(str(inst))
846
847 m = master(ui, dirstate, root)
848 sys.stdout.flush()
849 sys.stderr.flush()
850
837
851 pid = os.fork()
838 def run(self):
852 if pid:
839 try:
853 return pid
840 self.master.run()
854
841 finally:
855 closefds(pollable.instances.keys())
842 self.master.shutdown()
856 os.setsid()
857
858 fd = os.open('/dev/null', os.O_RDONLY)
859 os.dup2(fd, 0)
860 if fd > 0:
861 os.close(fd)
862
843
863 fd = os.open(ui.config('inotify', 'log', '/dev/null'),
844 runargs = None
864 os.O_RDWR | os.O_CREAT | os.O_TRUNC)
845 if 'inserve' not in sys.argv:
865 os.dup2(fd, 1)
846 runargs = [sys.argv[0], 'inserve', '-R', root]
866 os.dup2(fd, 2)
867 if fd > 2:
868 os.close(fd)
869
847
870 try:
848 service = service()
871 m.run()
849 logfile = ui.config('inotify', 'log')
872 finally:
850 cmdutil.service(opts, initfn=service.init, runfn=service.run,
873 m.shutdown()
851 logfile=logfile, runargs=runargs)
874 os._exit(0)
@@ -1,5 +1,5 b''
1 % fail
1 % fail
2 could not talk to new inotify server: No such file or directory
2 abort: could not start server: File exists
3 abort: could not start server: File exists
3 abort: could not start server: File exists
4 % inserve
4 % inserve
5 % status
5 % status
General Comments 0
You need to be logged in to leave comments. Login now