server.py
237 lines
| 7.0 KiB
| text/x-python
|
PythonLexer
/ mercurial / server.py
|
r30506 | # server.py - utility and factory of server | ||
# | ||||
|
r47575 | # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com> | ||
|
r30506 | # | ||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
import os | ||||
from .i18n import _ | ||||
|
r43355 | from .pycompat import open | ||
|
r30506 | |||
from . import ( | ||||
|
r30513 | chgserver, | ||
|
r32005 | cmdutil, | ||
|
r30507 | commandserver, | ||
|
r30506 | error, | ||
|
r30509 | hgweb, | ||
|
r32530 | pycompat, | ||
|
r30506 | util, | ||
) | ||||
r47669 | from .utils import ( | |||
procutil, | ||||
urlutil, | ||||
) | ||||
|
r43346 | |||
|
r37137 | |||
|
r43346 | def runservice( | ||
opts, | ||||
parentfn=None, | ||||
initfn=None, | ||||
runfn=None, | ||||
logfile=None, | ||||
runargs=None, | ||||
appendpid=False, | ||||
): | ||||
|
r30506 | '''Run a command as a service.''' | ||
|
r37232 | postexecargs = {} | ||
|
r43347 | if opts[b'daemon_postexec']: | ||
for inst in opts[b'daemon_postexec']: | ||||
if inst.startswith(b'unlink:'): | ||||
postexecargs[b'unlink'] = inst[7:] | ||||
elif inst.startswith(b'chdir:'): | ||||
postexecargs[b'chdir'] = inst[6:] | ||||
elif inst != b'none': | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'invalid value for --daemon-postexec: %s') % inst | ||
|
r43346 | ) | ||
|
r37232 | |||
|
r37229 | # When daemonized on Windows, redirect stdout/stderr to the lockfile (which | ||
# gets cleaned up after the child is up and running), so that the parent can | ||||
# read and print the error if this child dies early. See 594dd384803c. On | ||||
# other platforms, the child can write to the parent's stdio directly, until | ||||
# it is redirected prior to runfn(). | ||||
|
r43347 | if pycompat.iswindows and opts[b'daemon_postexec']: | ||
if b'unlink' in postexecargs and os.path.exists( | ||||
postexecargs[b'unlink'] | ||||
): | ||||
|
r37232 | procutil.stdout.flush() | ||
procutil.stderr.flush() | ||||
|
r37229 | |||
|
r43346 | fd = os.open( | ||
|
r43347 | postexecargs[b'unlink'], os.O_WRONLY | os.O_APPEND | os.O_BINARY | ||
|
r43346 | ) | ||
|
r37232 | try: | ||
|
r37233 | os.dup2(fd, procutil.stdout.fileno()) | ||
os.dup2(fd, procutil.stderr.fileno()) | ||||
|
r37232 | finally: | ||
os.close(fd) | ||||
|
r37229 | |||
|
r30506 | def writepid(pid): | ||
|
r43347 | if opts[b'pid_file']: | ||
|
r30506 | if appendpid: | ||
|
r43347 | mode = b'ab' | ||
|
r30506 | else: | ||
|
r43347 | mode = b'wb' | ||
fp = open(opts[b'pid_file'], mode) | ||||
fp.write(b'%d\n' % pid) | ||||
|
r30506 | fp.close() | ||
|
r43347 | if opts[b'daemon'] and not opts[b'daemon_postexec']: | ||
|
r30506 | # Signal child process startup with file removal | ||
|
r43347 | lockfd, lockpath = pycompat.mkstemp(prefix=b'hg-service-') | ||
|
r30506 | os.close(lockfd) | ||
try: | ||||
if not runargs: | ||||
|
r37138 | runargs = procutil.hgcmd() + pycompat.sysargv[1:] | ||
|
r43347 | runargs.append(b'--daemon-postexec=unlink:%s' % lockpath) | ||
|
r30506 | # Don't pass --cwd to the child process, because we've already | ||
# changed directory. | ||||
|
r38806 | for i in pycompat.xrange(1, len(runargs)): | ||
|
r43347 | if runargs[i].startswith(b'--cwd='): | ||
|
r30506 | del runargs[i] | ||
break | ||||
|
r43347 | elif runargs[i].startswith(b'--cwd'): | ||
|
r43346 | del runargs[i : i + 2] | ||
|
r30506 | break | ||
|
r43346 | |||
|
r30506 | def condfn(): | ||
return not os.path.exists(lockpath) | ||||
|
r43346 | |||
|
r37138 | pid = procutil.rundetached(runargs, condfn) | ||
|
r30506 | if pid < 0: | ||
|
r37229 | # If the daemonized process managed to write out an error msg, | ||
# report it. | ||||
if pycompat.iswindows and os.path.exists(lockpath): | ||||
|
r43347 | with open(lockpath, b'rb') as log: | ||
|
r37229 | for line in log: | ||
procutil.stderr.write(line) | ||||
|
r43347 | raise error.Abort(_(b'child process failed to start')) | ||
|
r30506 | writepid(pid) | ||
finally: | ||||
|
r31548 | util.tryunlink(lockpath) | ||
|
r30506 | if parentfn: | ||
return parentfn(pid) | ||||
else: | ||||
return | ||||
if initfn: | ||||
initfn() | ||||
|
r43347 | if not opts[b'daemon']: | ||
|
r37138 | writepid(procutil.getpid()) | ||
|
r30506 | |||
|
r43347 | if opts[b'daemon_postexec']: | ||
|
r30506 | try: | ||
os.setsid() | ||||
except AttributeError: | ||||
pass | ||||
|
r37229 | |||
|
r43347 | if b'chdir' in postexecargs: | ||
os.chdir(postexecargs[b'chdir']) | ||||
|
r37138 | procutil.hidewindow() | ||
|
r37137 | procutil.stdout.flush() | ||
procutil.stderr.flush() | ||||
|
r30506 | |||
nullfd = os.open(os.devnull, os.O_RDWR) | ||||
logfilefd = nullfd | ||||
if logfile: | ||||
|
r43346 | logfilefd = os.open( | ||
logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND, 0o666 | ||||
) | ||||
|
r37233 | os.dup2(nullfd, procutil.stdin.fileno()) | ||
os.dup2(logfilefd, procutil.stdout.fileno()) | ||||
os.dup2(logfilefd, procutil.stderr.fileno()) | ||||
|
r43346 | stdio = ( | ||
procutil.stdin.fileno(), | ||||
procutil.stdout.fileno(), | ||||
procutil.stderr.fileno(), | ||||
) | ||||
|
r37233 | if nullfd not in stdio: | ||
|
r30506 | os.close(nullfd) | ||
|
r37233 | if logfile and logfilefd not in stdio: | ||
|
r30506 | os.close(logfilefd) | ||
|
r37229 | # Only unlink after redirecting stdout/stderr, so Windows doesn't | ||
# complain about a sharing violation. | ||||
|
r43347 | if b'unlink' in postexecargs: | ||
os.unlink(postexecargs[b'unlink']) | ||||
|
r37229 | |||
|
r30506 | if runfn: | ||
return runfn() | ||||
|
r30507 | |||
|
r43346 | |||
|
r30507 | _cmdservicemap = { | ||
|
r43347 | b'chgunix': chgserver.chgunixservice, | ||
b'pipe': commandserver.pipeservice, | ||||
b'unix': commandserver.unixforkingservice, | ||||
|
r30507 | } | ||
|
r43346 | |||
|
r30510 | def _createcmdservice(ui, repo, opts): | ||
|
r43347 | mode = opts[b'cmdserver'] | ||
|
r30507 | try: | ||
|
r40858 | servicefn = _cmdservicemap[mode] | ||
|
r30507 | except KeyError: | ||
|
r43347 | raise error.Abort(_(b'unknown mode %s') % mode) | ||
|
r40859 | commandserver.setuplogging(ui, repo) | ||
|
r40858 | return servicefn(ui, repo, opts) | ||
|
r30509 | |||
|
r43346 | |||
|
r30510 | def _createhgwebservice(ui, repo, opts): | ||
|
r30509 | # this way we can check if something was given in the command-line | ||
|
r43347 | if opts.get(b'port'): | ||
r47669 | opts[b'port'] = urlutil.getport(opts.get(b'port')) | |||
|
r30509 | |||
|
r32291 | alluis = {ui} | ||
|
r30509 | if repo: | ||
baseui = repo.baseui | ||||
alluis.update([repo.baseui, repo.ui]) | ||||
else: | ||||
baseui = ui | ||||
|
r43347 | webconf = opts.get(b'web_conf') or opts.get(b'webdir_conf') | ||
|
r30509 | if webconf: | ||
|
r43347 | if opts.get(b'subrepos'): | ||
raise error.Abort(_(b'--web-conf cannot be used with --subrepos')) | ||||
|
r32005 | |||
|
r30509 | # load server settings (e.g. web.port) to "copied" ui, which allows | ||
# hgwebdir to reload webconf cleanly | ||||
servui = ui.copy() | ||||
|
r43347 | servui.readconfig(webconf, sections=[b'web']) | ||
|
r30509 | alluis.add(servui) | ||
|
r43347 | elif opts.get(b'subrepos'): | ||
|
r32005 | servui = ui | ||
# If repo is None, hgweb.createapp() already raises a proper abort | ||||
# message as long as webconf is None. | ||||
if repo: | ||||
webconf = dict() | ||||
|
r43347 | cmdutil.addwebdirpath(repo, b"", webconf) | ||
|
r30509 | else: | ||
servui = ui | ||||
|
r43346 | optlist = ( | ||
|
r43347 | b"name templates style address port prefix ipv6" | ||
b" accesslog errorlog certificate encoding" | ||||
|
r43346 | ) | ||
|
r30509 | for o in optlist.split(): | ||
|
r43347 | val = opts.get(o, b'') | ||
if val in (None, b''): # should check against default options instead | ||||
|
r30509 | continue | ||
for u in alluis: | ||||
|
r43347 | u.setconfig(b"web", o, val, b'serve') | ||
|
r30509 | |||
app = hgweb.createapp(baseui, repo, webconf) | ||||
return hgweb.httpservice(servui, app, opts) | ||||
|
r30510 | |||
|
r43346 | |||
|
r30510 | def createservice(ui, repo, opts): | ||
|
r43347 | if opts[b"cmdserver"]: | ||
|
r30510 | return _createcmdservice(ui, repo, opts) | ||
else: | ||||
return _createhgwebservice(ui, repo, opts) | ||||