debuggers.hg

view tools/misc/xend @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents 9ced12c28e05
children 3ffdb094c2c0
line source
1 #!/usr/bin/env python
2 # -*- mode: python; -*-
3 #============================================================================
4 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
5 # Copyright (C) 2005-2006 XenSource Inc
6 #============================================================================
8 """Xen management daemon.
9 Provides console server and HTTP management api.
11 Run:
12 xend start
14 Restart:
15 xend restart
17 The daemon is stopped with:
18 xend stop
20 The daemon should reconnect to device control interfaces
21 and recover its state when restarted.
23 On Solaris, the daemons are SMF managed, and you should not attempt
24 to start xend by hand.
25 """
26 import fcntl
27 import glob
28 import os
29 import os.path
30 import sys
31 import socket
32 import signal
33 import time
34 import commands
36 from xen.xend.server import SrvDaemon
38 class CheckError(ValueError):
39 pass
41 def hline():
42 print >>sys.stderr, "*" * 70
44 def msg(message):
45 print >>sys.stderr, "*" * 3, message
47 def check_logging():
48 """Check python logging is installed and raise an error if not.
49 Logging is standard from Python 2.3 on.
50 """
51 try:
52 import logging
53 except ImportError:
54 hline()
55 msg("Python logging is not installed.")
56 msg("Use 'make install-logging' at the xen root to install.")
57 msg("")
58 msg("Alternatively download and install from")
59 msg("http://www.red-dove.com/python_logging.html")
60 hline()
61 raise CheckError("logging is not installed")
63 def check_user():
64 """Check that the effective user id is 0 (root).
65 """
66 if os.geteuid() != 0:
67 hline()
68 msg("Xend must be run as root.")
69 hline()
70 raise CheckError("invalid user")
72 def start_daemon(daemon, *args):
73 if os.fork() == 0:
74 os.execvp(daemon, (daemon,) + args)
76 def start_xenstored():
77 pidfname = "/var/run/xenstore.pid"
78 try:
79 f = open(pidfname, "a")
80 try:
81 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
82 rootdir = os.getenv("XENSTORED_ROOTDIR") or "/var/lib/xenstored"
83 for i in glob.glob(rootdir + "/tdb*"):
84 try:
85 os.unlink(i)
86 except:
87 pass
88 os.unlink(pidfname)
89 except:
90 pass
91 f.close()
92 except:
93 pass
94 XENSTORED_TRACE = os.getenv("XENSTORED_TRACE")
95 cmd = "xenstored --pid-file /var/run/xenstore.pid"
96 if XENSTORED_TRACE:
97 cmd += " -T /var/log/xen/xenstored-trace.log"
98 s,o = commands.getstatusoutput(cmd)
100 def start_consoled():
101 XENCONSOLED_TRACE = os.getenv("XENCONSOLED_TRACE")
102 args = ""
103 if XENCONSOLED_TRACE:
104 args += "--log=" + XENCONSOLED_TRACE
105 start_daemon("xenconsoled", args)
107 def start_blktapctrl():
108 start_daemon("blktapctrl", "")
110 def start_xenbackendd():
111 XENBACKENDD_DEBUG = os.getenv("XENBACKENDD_DEBUG")
112 args = ""
113 if XENBACKENDD_DEBUG:
114 args += "-d"
115 if os.uname()[0] == 'NetBSD':
116 start_daemon("xenbackendd", args)
118 def main():
119 try:
120 check_logging()
121 check_user()
122 except CheckError:
123 sys.exit(1)
125 daemon = SrvDaemon.instance()
126 if not sys.argv[1:]:
127 print 'usage: %s {start|stop|reload|restart}' % sys.argv[0]
128 elif sys.argv[1] == 'start':
129 if os.uname()[0] != "SunOS":
130 start_xenstored()
131 start_consoled()
132 start_xenbackendd()
133 start_blktapctrl()
134 return daemon.start()
135 elif sys.argv[1] == 'trace_start':
136 start_xenstored()
137 start_consoled()
138 start_xenbackendd()
139 start_blktapctrl()
140 return daemon.start(trace=1)
141 elif sys.argv[1] == 'stop':
142 return daemon.stop()
143 elif sys.argv[1] == 'reload':
144 return daemon.reloadConfig()
145 elif sys.argv[1] == 'restart':
146 start_xenstored()
147 start_consoled()
148 start_xenbackendd()
149 start_blktapctrl()
150 return daemon.stop() or daemon.start()
151 elif sys.argv[1] == 'status':
152 return daemon.status()
153 else:
154 print 'not an option:', sys.argv[1]
155 return 1
157 if __name__ == '__main__':
158 sys.exit(main())