Commit 1a96592b authored by Jeremy Hylton's avatar Jeremy Hylton

Remove last reference to string module.

parent 8eaa6efb
...@@ -13,8 +13,9 @@ ...@@ -13,8 +13,9 @@
############################################################################## ##############################################################################
"""Start the server storage. """Start the server storage.
$Id: start.py,v 1.35 2002/07/16 18:30:03 jeremy Exp $ $Id: start.py,v 1.36 2002/07/25 16:47:02 jeremy Exp $
""" """
from __future__ import nested_scopes
import sys, os, getopt import sys, os, getopt
import types import types
...@@ -23,7 +24,7 @@ def directory(p, n=1): ...@@ -23,7 +24,7 @@ def directory(p, n=1):
d = p d = p
while n: while n:
d = os.path.split(d)[0] d = os.path.split(d)[0]
if not d or d=='.': if not d or d == '.':
d = os.getcwd() d = os.getcwd()
n -= 1 n -= 1
return d return d
...@@ -43,10 +44,65 @@ def get_storage(m, n, cache={}): ...@@ -43,10 +44,65 @@ def get_storage(m, n, cache={}):
cache[(d, m)] = im cache[(d, m)] = im
return getattr(im, n) return getattr(im, n)
def set_uid(uid):
"""Try to set uid and gid based on -u argument.
This will only work if this script is run by root.
"""
try:
import pwd
except ImportError:
LOG('ZEO Server', INFO, ("Can't set uid to %s."
"pwd module is not available." % uid))
return
try:
gid = None
try:
UID = int(UID)
except: # conversion could raise all sorts of errors
uid = pwd.getpwnam(UID)[2]
gid = pwd.getpwnam(UID)[3]
else:
uid = pwd.getpwuid(UID)[2]
gid = pwd.getpwuid(UID)[3]
if gid is not None:
try:
os.setgid(gid)
except OSError:
pass
try:
os.setuid(uid)
except OSError:
pass
except KeyError:
LOG('ZEO Server', ERROR, ("can't find UID %s" % UID))
def setup_signals(storages):
try:
import signal
except ImportError:
return
try:
xfsz = signal.SIFXFSZ
except AttributeError:
pass
else:
signal.signal(xfsz, signal.SIG_IGN)
signal.signal(signal.SIGTERM, lambda sig, frame: shutdown(storages))
signal.signal(signal.SIGINT, lambda sig, frame: shutdown(storages, 0))
try:
signal.signal(signal.SIGHUP, rotate_logs_handler)
except:
pass
def main(argv): def main(argv):
me = argv[0] me = argv[0]
sys.path.insert(0, directory(me, 2)) sys.path.insert(0, directory(me, 2))
global LOG, INFO, ERROR
from zLOG import LOG, INFO, ERROR, PANIC
# XXX hack for profiling support # XXX hack for profiling support
global unix, storages, zeo_pid, asyncore global unix, storages, zeo_pid, asyncore
...@@ -137,21 +193,21 @@ def main(argv): ...@@ -137,21 +193,21 @@ def main(argv):
prof = None prof = None
detailed = 0 detailed = 0
for o, v in opts: for o, v in opts:
if o=='-p': if o =='-p':
port = int(v) port = int(v)
elif o=='-h': elif o =='-h':
host = v host = v
elif o=='-U': elif o =='-U':
unix = v unix = v
elif o=='-u': elif o =='-u':
UID = v UID = v
elif o=='-D': elif o =='-D':
debug = 1 debug = 1
elif o=='-d': elif o =='-d':
detailed = 1 detailed = 1
elif o=='-s': elif o =='-s':
Z = 0 Z = 0
elif o=='-P': elif o =='-P':
prof = v prof = v
if prof: if prof:
...@@ -165,7 +221,7 @@ def main(argv): ...@@ -165,7 +221,7 @@ def main(argv):
if args: if args:
if len(args) > 1: if len(args) > 1:
print usage print usage
print 'Unrecognizd arguments: ', string.join(args[1:]) print 'Unrecognizd arguments: ', " ".join(args[1:])
sys.exit(1) sys.exit(1)
fs = args[0] fs = args[0]
...@@ -175,41 +231,8 @@ def main(argv): ...@@ -175,41 +231,8 @@ def main(argv):
if detailed: if detailed:
os.environ['STUPID_LOG_SEVERITY'] = '-300' os.environ['STUPID_LOG_SEVERITY'] = '-300'
from zLOG import LOG, INFO, ERROR set_uid(uid)
# Try to set uid to "-u" -provided uid.
# Try to set gid to "-u" user's primary group.
# This will only work if this script is run by root.
try:
import pwd
try:
try:
UID = int(UID)
except:
pass
gid = None
if isinstance(UID, types.StringType):
uid = pwd.getpwnam(UID)[2]
gid = pwd.getpwnam(UID)[3]
elif isinstance(UID, types.IntType):
uid = pwd.getpwuid(UID)[2]
gid = pwd.getpwuid(UID)[3]
else:
raise KeyError
try:
if gid is not None:
try:
os.setgid(gid)
except OSError:
pass
os.setuid(uid)
except OSError:
pass
except KeyError:
LOG('ZEO Server', ERROR, ("can't find UID %s" % UID))
except:
pass
if Z: if Z:
try: try:
import posix import posix
...@@ -223,13 +246,13 @@ def main(argv): ...@@ -223,13 +246,13 @@ def main(argv):
import ZEO.StorageServer, asyncore import ZEO.StorageServer, asyncore
storages={} storages = {}
for o, v in opts: for o, v in opts:
if o=='-S': if o == '-S':
n, m = string.split(v,'=') n, m = v.split("=", 1)
if string.find(m,':'): if m.find(":") >= 0:
# we got an attribute name # we got an attribute name
m, a = string.split(m,':') m, a = m.split(':')
else: else:
# attribute name must be same as storage name # attribute name must be same as storage name
a=n a=n
...@@ -240,23 +263,7 @@ def main(argv): ...@@ -240,23 +263,7 @@ def main(argv):
storages['1']=ZODB.FileStorage.FileStorage(fs) storages['1']=ZODB.FileStorage.FileStorage(fs)
# Try to set up a signal handler # Try to set up a signal handler
try: setup_signals(storages)
import signal
try:
signal.signal(signal.SIFXFSZ, signal.SIG_IGN)
except AttributeError:
pass
signal.signal(signal.SIGTERM,
lambda sig, frame, s=storages: shutdown(s))
signal.signal(signal.SIGINT,
lambda sig, frame, s=storages: shutdown(s, 0))
try:
signal.signal(signal.SIGHUP, rotate_logs_handler)
except:
pass
except:
pass
items = storages.items() items = storages.items()
items.sort() items.sort()
...@@ -279,8 +286,7 @@ def main(argv): ...@@ -279,8 +286,7 @@ def main(argv):
# Log startup exception and tell zdaemon not to restart us. # Log startup exception and tell zdaemon not to restart us.
info = sys.exc_info() info = sys.exc_info()
try: try:
import zLOG LOG("z2", PANIC, "Startup exception", error=info)
zLOG.LOG("z2", zLOG.PANIC, "Startup exception", error=info)
except: except:
pass pass
...@@ -288,8 +294,20 @@ def main(argv): ...@@ -288,8 +294,20 @@ def main(argv):
traceback.print_exception(*info) traceback.print_exception(*info)
sys.exit(0) sys.exit(0)
asyncore.loop() try:
asyncore.loop()
except SystemExit:
raise
except:
info = sys.exc_info()
try:
LOG("ZEO Server", PANIC, "Unexpected error", error=info)
except:
pass
import traceback
traceback.print_exception(*info)
sys.exit(1)
def rotate_logs(): def rotate_logs():
import zLOG import zLOG
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment