Show More
@@ -1,169 +1,169 | |||
|
1 | 1 | # server.py - utility and factory of server |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import os |
|
11 | 11 | import tempfile |
|
12 | 12 | |
|
13 | 13 | from .i18n import _ |
|
14 | 14 | |
|
15 | 15 | from . import ( |
|
16 | 16 | chgserver, |
|
17 | 17 | cmdutil, |
|
18 | 18 | commandserver, |
|
19 | 19 | error, |
|
20 | 20 | hgweb, |
|
21 | 21 | pycompat, |
|
22 | 22 | util, |
|
23 | 23 | ) |
|
24 | 24 | |
|
25 | 25 | def runservice(opts, parentfn=None, initfn=None, runfn=None, logfile=None, |
|
26 | 26 | runargs=None, appendpid=False): |
|
27 | 27 | '''Run a command as a service.''' |
|
28 | 28 | |
|
29 | 29 | def writepid(pid): |
|
30 | 30 | if opts['pid_file']: |
|
31 | 31 | if appendpid: |
|
32 | 32 | mode = 'ab' |
|
33 | 33 | else: |
|
34 | 34 | mode = 'wb' |
|
35 | 35 | fp = open(opts['pid_file'], mode) |
|
36 | fp.write(str(pid).encode('latin1') + '\n') | |
|
36 | fp.write('%d\n' % pid) | |
|
37 | 37 | fp.close() |
|
38 | 38 | |
|
39 | 39 | if opts['daemon'] and not opts['daemon_postexec']: |
|
40 | 40 | # Signal child process startup with file removal |
|
41 | 41 | lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-') |
|
42 | 42 | os.close(lockfd) |
|
43 | 43 | try: |
|
44 | 44 | if not runargs: |
|
45 | 45 | runargs = util.hgcmd() + pycompat.sysargv[1:] |
|
46 | 46 | runargs.append('--daemon-postexec=unlink:%s' % lockpath) |
|
47 | 47 | # Don't pass --cwd to the child process, because we've already |
|
48 | 48 | # changed directory. |
|
49 | 49 | for i in xrange(1, len(runargs)): |
|
50 | 50 | if runargs[i].startswith('--cwd='): |
|
51 | 51 | del runargs[i] |
|
52 | 52 | break |
|
53 | 53 | elif runargs[i].startswith('--cwd'): |
|
54 | 54 | del runargs[i:i + 2] |
|
55 | 55 | break |
|
56 | 56 | def condfn(): |
|
57 | 57 | return not os.path.exists(lockpath) |
|
58 | 58 | pid = util.rundetached(runargs, condfn) |
|
59 | 59 | if pid < 0: |
|
60 | 60 | raise error.Abort(_('child process failed to start')) |
|
61 | 61 | writepid(pid) |
|
62 | 62 | finally: |
|
63 | 63 | util.tryunlink(lockpath) |
|
64 | 64 | if parentfn: |
|
65 | 65 | return parentfn(pid) |
|
66 | 66 | else: |
|
67 | 67 | return |
|
68 | 68 | |
|
69 | 69 | if initfn: |
|
70 | 70 | initfn() |
|
71 | 71 | |
|
72 | 72 | if not opts['daemon']: |
|
73 | 73 | writepid(util.getpid()) |
|
74 | 74 | |
|
75 | 75 | if opts['daemon_postexec']: |
|
76 | 76 | try: |
|
77 | 77 | os.setsid() |
|
78 | 78 | except AttributeError: |
|
79 | 79 | pass |
|
80 | 80 | for inst in opts['daemon_postexec']: |
|
81 | 81 | if inst.startswith('unlink:'): |
|
82 | 82 | lockpath = inst[7:] |
|
83 | 83 | os.unlink(lockpath) |
|
84 | 84 | elif inst.startswith('chdir:'): |
|
85 | 85 | os.chdir(inst[6:]) |
|
86 | 86 | elif inst != 'none': |
|
87 | 87 | raise error.Abort(_('invalid value for --daemon-postexec: %s') |
|
88 | 88 | % inst) |
|
89 | 89 | util.hidewindow() |
|
90 | 90 | util.stdout.flush() |
|
91 | 91 | util.stderr.flush() |
|
92 | 92 | |
|
93 | 93 | nullfd = os.open(os.devnull, os.O_RDWR) |
|
94 | 94 | logfilefd = nullfd |
|
95 | 95 | if logfile: |
|
96 | 96 | logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND) |
|
97 | 97 | os.dup2(nullfd, 0) |
|
98 | 98 | os.dup2(logfilefd, 1) |
|
99 | 99 | os.dup2(logfilefd, 2) |
|
100 | 100 | if nullfd not in (0, 1, 2): |
|
101 | 101 | os.close(nullfd) |
|
102 | 102 | if logfile and logfilefd not in (0, 1, 2): |
|
103 | 103 | os.close(logfilefd) |
|
104 | 104 | |
|
105 | 105 | if runfn: |
|
106 | 106 | return runfn() |
|
107 | 107 | |
|
108 | 108 | _cmdservicemap = { |
|
109 | 109 | 'chgunix': chgserver.chgunixservice, |
|
110 | 110 | 'pipe': commandserver.pipeservice, |
|
111 | 111 | 'unix': commandserver.unixforkingservice, |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | def _createcmdservice(ui, repo, opts): |
|
115 | 115 | mode = opts['cmdserver'] |
|
116 | 116 | try: |
|
117 | 117 | return _cmdservicemap[mode](ui, repo, opts) |
|
118 | 118 | except KeyError: |
|
119 | 119 | raise error.Abort(_('unknown mode %s') % mode) |
|
120 | 120 | |
|
121 | 121 | def _createhgwebservice(ui, repo, opts): |
|
122 | 122 | # this way we can check if something was given in the command-line |
|
123 | 123 | if opts.get('port'): |
|
124 | 124 | opts['port'] = util.getport(opts.get('port')) |
|
125 | 125 | |
|
126 | 126 | alluis = {ui} |
|
127 | 127 | if repo: |
|
128 | 128 | baseui = repo.baseui |
|
129 | 129 | alluis.update([repo.baseui, repo.ui]) |
|
130 | 130 | else: |
|
131 | 131 | baseui = ui |
|
132 | 132 | webconf = opts.get('web_conf') or opts.get('webdir_conf') |
|
133 | 133 | if webconf: |
|
134 | 134 | if opts.get('subrepos'): |
|
135 | 135 | raise error.Abort(_('--web-conf cannot be used with --subrepos')) |
|
136 | 136 | |
|
137 | 137 | # load server settings (e.g. web.port) to "copied" ui, which allows |
|
138 | 138 | # hgwebdir to reload webconf cleanly |
|
139 | 139 | servui = ui.copy() |
|
140 | 140 | servui.readconfig(webconf, sections=['web']) |
|
141 | 141 | alluis.add(servui) |
|
142 | 142 | elif opts.get('subrepos'): |
|
143 | 143 | servui = ui |
|
144 | 144 | |
|
145 | 145 | # If repo is None, hgweb.createapp() already raises a proper abort |
|
146 | 146 | # message as long as webconf is None. |
|
147 | 147 | if repo: |
|
148 | 148 | webconf = dict() |
|
149 | 149 | cmdutil.addwebdirpath(repo, "", webconf) |
|
150 | 150 | else: |
|
151 | 151 | servui = ui |
|
152 | 152 | |
|
153 | 153 | optlist = ("name templates style address port prefix ipv6" |
|
154 | 154 | " accesslog errorlog certificate encoding") |
|
155 | 155 | for o in optlist.split(): |
|
156 | 156 | val = opts.get(o, '') |
|
157 | 157 | if val in (None, ''): # should check against default options instead |
|
158 | 158 | continue |
|
159 | 159 | for u in alluis: |
|
160 | 160 | u.setconfig("web", o, val, 'serve') |
|
161 | 161 | |
|
162 | 162 | app = hgweb.createapp(baseui, repo, webconf) |
|
163 | 163 | return hgweb.httpservice(servui, app, opts) |
|
164 | 164 | |
|
165 | 165 | def createservice(ui, repo, opts): |
|
166 | 166 | if opts["cmdserver"]: |
|
167 | 167 | return _createcmdservice(ui, repo, opts) |
|
168 | 168 | else: |
|
169 | 169 | return _createhgwebservice(ui, repo, opts) |
General Comments 0
You need to be logged in to leave comments.
Login now