ia64/xen-unstable

changeset 6033:61cbf8f977ef

Attached are
three patches to fix a problem with Xend starting consoled. consoled
depends on xenstored to be running and xenstored is started on demand in
Xend. The patches change xenstored to manage its own pidfile, and have
xend start actually start up xenstored.
author kaf24@firebug.cl.cam.ac.uk
date Thu Aug 04 18:51:55 2005 +0000 (2005-08-04)
parents ebf05456ee11
children 38e6467df0e6
files tools/misc/xend tools/python/xen/xend/server/SrvDaemon.py tools/xenstore/utils.c
line diff
     1.1 --- a/tools/misc/xend	Thu Aug 04 17:37:09 2005 +0000
     1.2 +++ b/tools/misc/xend	Thu Aug 04 18:51:55 2005 +0000
     1.3 @@ -114,6 +114,14 @@ def stop_xcs():
     1.4          xcs_pidfile.close()
     1.5      except:
     1.6  	return    
     1.7 +
     1.8 +def start_xenstored():
     1.9 +    if os.fork() == 0:
    1.10 +        os.execvp('/usr/sbin/xenstored', ['/usr/sbin/xenstored']);
    1.11 +
    1.12 +def start_consoled():
    1.13 +    if os.fork() == 0:
    1.14 +        os.execvp('/usr/sbin/consoled', ['/usr/sbin/consoled']);
    1.15              
    1.16  def main():
    1.17      try:
    1.18 @@ -130,11 +138,13 @@ def main():
    1.19          return status >> 8
    1.20      elif sys.argv[1] == 'start':
    1.21          start_xcs()
    1.22 -        if os.fork() == 0:
    1.23 -            os.execvp('/usr/sbin/consoled', ['/usr/sbin/consoled']);
    1.24 +        start_xenstored()
    1.25 +        start_consoled()
    1.26          return daemon.start()
    1.27      elif sys.argv[1] == 'trace_start':
    1.28          start_xcs()
    1.29 +        start_xenstored()
    1.30 +        start_consoled()
    1.31          return daemon.start(trace=1)
    1.32      elif sys.argv[1] == 'stop':
    1.33          stop_xcs()
    1.34 @@ -142,6 +152,8 @@ def main():
    1.35      elif sys.argv[1] == 'restart':
    1.36          stop_xcs()
    1.37          start_xcs()
    1.38 +        start_xenstored()
    1.39 +        start_consoled()
    1.40          return daemon.stop() or daemon.start()
    1.41      elif sys.argv[1] == 'status':
    1.42          return daemon.status()
     2.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Thu Aug 04 17:37:09 2005 +0000
     2.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Thu Aug 04 18:51:55 2005 +0000
     2.3 @@ -126,12 +126,8 @@ class Daemon:
     2.4      def cleanup_xend(self, kill=False):
     2.5          return self.cleanup_process(XEND_PID_FILE, "xend", kill)
     2.6  
     2.7 -    def cleanup_xenstored(self, kill=False):
     2.8 -        return self.cleanup_process(XENSTORED_PID_FILE, "xenstored", kill)
     2.9 -
    2.10      def cleanup(self, kill=False):
    2.11          self.cleanup_xend(kill=kill)
    2.12 -        #self.cleanup_xenstored(kill=kill)
    2.13  
    2.14      def status(self):
    2.15          """Returns the status of the xend daemon.
    2.16 @@ -168,31 +164,6 @@ class Daemon:
    2.17              pidfile.close()
    2.18          return pid
    2.19  
    2.20 -    def start_xenstored(self):
    2.21 -        """Fork and exec xenstored, writing its pid to XENSTORED_PID_FILE.
    2.22 -        """
    2.23 -        def mkdirs(p):
    2.24 -            try:
    2.25 -                os.makedirs(p)
    2.26 -            except:
    2.27 -                pass
    2.28 -        mkdirs(XENSTORED_RUN_DIR)
    2.29 -        mkdirs(XENSTORED_LIB_DIR)
    2.30 -        
    2.31 -        pid = self.fork_pid(XENSTORED_PID_FILE)
    2.32 -        if pid:
    2.33 -            # Parent
    2.34 -            log.info("Started xenstored, pid=%d", pid)
    2.35 -        else:
    2.36 -            # Child
    2.37 -            if XEND_DAEMONIZE:
    2.38 -                self.daemonize()
    2.39 -            if XENSTORED_DEBUG:
    2.40 -                os.execl("/usr/sbin/xenstored", "xenstored", "--no-fork",
    2.41 -                         "-T", "/var/log/xenstored-trace.log")
    2.42 -            else:
    2.43 -                os.execl("/usr/sbin/xenstored", "xenstored", "--no-fork")
    2.44 -
    2.45      def daemonize(self):
    2.46          if not XEND_DAEMONIZE: return
    2.47          # Detach from TTY.
    2.48 @@ -223,15 +194,11 @@ class Daemon:
    2.49          4  Insufficient privileges
    2.50          """
    2.51          xend_pid = self.cleanup_xend()
    2.52 -        xenstored_pid = self.cleanup_xenstored()
    2.53  
    2.54          if self.set_user():
    2.55              return 4
    2.56          os.chdir("/")
    2.57  
    2.58 -        if xenstored_pid == 0:
    2.59 -            self.start_xenstored()
    2.60 -
    2.61          if xend_pid > 0:
    2.62              # Trying to run an already-running service is a success.
    2.63              return 0
     3.1 --- a/tools/xenstore/utils.c	Thu Aug 04 17:37:09 2005 +0000
     3.2 +++ b/tools/xenstore/utils.c	Thu Aug 04 18:51:55 2005 +0000
     3.3 @@ -84,6 +84,9 @@ void *malloc_nofail(size_t size)
     3.4  void daemonize(void)
     3.5  {
     3.6  	pid_t pid;
     3.7 +	int fd;
     3.8 +	size_t len;
     3.9 +	char buf[100];
    3.10  
    3.11  	/* Separate from our parent via fork, so init inherits us. */
    3.12  	if ((pid = fork()) < 0)
    3.13 @@ -101,6 +104,18 @@ void daemonize(void)
    3.14  	chdir("/");
    3.15  	/* Discard our parent's old-fashioned umask prejudices. */
    3.16  	umask(0);
    3.17 +
    3.18 +	fd = open("/var/run/xenstored.pid", O_RDWR | O_CREAT);
    3.19 +	if (fd == -1) {
    3.20 +		exit(1);
    3.21 +	}
    3.22 +
    3.23 +	if (lockf(fd, F_TLOCK, 0) == -1) {
    3.24 +		exit(1);
    3.25 +	}
    3.26 +
    3.27 +	len = sprintf(buf, "%d\n", getpid());
    3.28 +	write(fd, buf, len);
    3.29  }
    3.30  
    3.31