ia64/xen-unstable

changeset 4136:095b6204d7f7

bitkeeper revision 1.1236.25.20 (423576ddv67ZIGUBXLyQsmLCTyT6NA)

Bunch of little xcs fixes.

Signed-off-by: andrew.warfield@cl.cam.ac.uk
author akw27@arcadians.cl.cam.ac.uk
date Mon Mar 14 11:34:53 2005 +0000 (2005-03-14)
parents b5337b34feb7
children a4136a26fa6d 63bff8d75218 e97b2a505ee8
files tools/misc/xend tools/xcs/evtchn.c tools/xcs/xcs.c
line diff
     1.1 --- a/tools/misc/xend	Mon Mar 14 09:04:55 2005 +0000
     1.2 +++ b/tools/misc/xend	Mon Mar 14 11:34:53 2005 +0000
     1.3 @@ -22,11 +22,13 @@
     1.4  import os
     1.5  import sys
     1.6  import socket
     1.7 +import signal
     1.8  import time
     1.9  
    1.10 -XCS_PATH = "/var/lib/xen/xcs_socket"
    1.11 -XCS_EXEC = "/usr/sbin/xcs"
    1.12 -XCS_LOGFILE = "/var/log/xcs.log"
    1.13 +XCS_PATH    = "/var/lib/xen/xcs_socket"
    1.14 +XCS_EXEC    = "/usr/sbin/xcs"
    1.15 +XCS_PIDFILE = "/var/run/xcs.pid"
    1.16 +XCS_ARGS    = (XCS_EXEC, "-p", XCS_PIDFILE)
    1.17  
    1.18  # Default install path for Xen binary packages.
    1.19  sys.path = [ '/usr/lib/python' ] + sys.path
    1.20 @@ -98,37 +100,21 @@ def check_user():
    1.21  
    1.22  def xcs_running():
    1.23      """ See if the control switch is running.
    1.24 -    """
    1.25 -    ret = 1
    1.26 -    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    1.27 +    """	
    1.28      try:
    1.29 -        s.connect( (XCS_PATH) )
    1.30 -    except:
    1.31 -        ret = 0
    1.32 -    s.close()
    1.33 -    return (ret)
    1.34 +	xcs_pidfile = open(XCS_PIDFILE)
    1.35 +    except IOError:
    1.36 +	return(0)
    1.37 +    xcs_pidfile.close()
    1.38 +    return 1
    1.39      
    1.40 -def main():
    1.41 -    try:
    1.42 -        check_logging()
    1.43 -        check_twisted_version()
    1.44 -        check_user()
    1.45 -    except CheckError:
    1.46 -        sys.exit(1)
    1.47 -    
    1.48 +def start_xcs():
    1.49      if (not xcs_running()):
    1.50          if os.fork():
    1.51 -            time.sleep(0.5) # let xcs start
    1.52 +            time.sleep(0.1) # let xcs start
    1.53          else:
    1.54              try:
    1.55 -                logfile = os.open(XCS_LOGFILE, 
    1.56 -                                  os.O_WRONLY|os.O_APPEND|os.O_CREAT)
    1.57 -                os.close(1)
    1.58 -                os.dup(logfile)
    1.59 -                os.close(2)
    1.60 -                os.dup(logfile)
    1.61 -                os.close(logfile)
    1.62 -                os.execlp(XCS_EXEC, XCS_EXEC)
    1.63 +                os.execvp(XCS_EXEC, XCS_ARGS)
    1.64              except:
    1.65                  hline()
    1.66                  msg("Tried to start xcs, but failed. Is it installed?")
    1.67 @@ -139,7 +125,25 @@ def main():
    1.68              msg("Failed to start the control interface switch.")
    1.69              hline()
    1.70              raise CheckError("xcs not running")
    1.71 -        
    1.72 +            
    1.73 +def stop_xcs():
    1.74 +    try:
    1.75 +	xcs_pidfile = open(XCS_PIDFILE)
    1.76 +    except IOError:
    1.77 +	return
    1.78 +    xcs_pid = int(xcs_pidfile.read().strip())
    1.79 +    os.kill(xcs_pid, signal.SIGTERM)
    1.80 +    xcs_pidfile.close()
    1.81 +    
    1.82 +            
    1.83 +def main():
    1.84 +    try:
    1.85 +        check_logging()
    1.86 +        check_twisted_version()
    1.87 +        check_user()
    1.88 +    except CheckError:
    1.89 +        sys.exit(1)
    1.90 +    
    1.91      daemon = SrvDaemon.instance()
    1.92      if not sys.argv[1:]:
    1.93          print 'usage: %s {start|stop|restart}' % sys.argv[0]
    1.94 @@ -147,12 +151,17 @@ def main():
    1.95          pid, status = os.wait()
    1.96          return status >> 8
    1.97      elif sys.argv[1] == 'start':
    1.98 +        start_xcs()
    1.99          return daemon.start()
   1.100      elif sys.argv[1] == 'trace_start':
   1.101 +        start_xcs()
   1.102          return daemon.start(trace=1)
   1.103      elif sys.argv[1] == 'stop':
   1.104 +        stop_xcs()
   1.105          return daemon.stop()
   1.106      elif sys.argv[1] == 'restart':
   1.107 +        stop_xcs()
   1.108 +        start_xcs()
   1.109          return daemon.stop() or daemon.start()
   1.110      elif sys.argv[1] == 'status':
   1.111          return daemon.status()
     2.1 --- a/tools/xcs/evtchn.c	Mon Mar 14 09:04:55 2005 +0000
     2.2 +++ b/tools/xcs/evtchn.c	Mon Mar 14 11:34:53 2005 +0000
     2.3 @@ -95,8 +95,6 @@ int evtchn_open(void)
     2.4              goto reopen;
     2.5          return -errno;
     2.6      }
     2.7 -    /*set_cloexec(evtchn_fd); -- no longer required*/
     2.8 -printf("Eventchan_fd is %d\n", evtchn_fd);
     2.9      return evtchn_fd;
    2.10  }
    2.11  
     3.1 --- a/tools/xcs/xcs.c	Mon Mar 14 09:04:55 2005 +0000
     3.2 +++ b/tools/xcs/xcs.c	Mon Mar 14 11:34:53 2005 +0000
     3.3 @@ -75,6 +75,7 @@
     3.4  #include <errno.h>
     3.5  #include <malloc.h>
     3.6  #include <fcntl.h>
     3.7 +#include <ctype.h>
     3.8  #include "xcs.h"
     3.9  
    3.10  #undef fd_max
    3.11 @@ -250,7 +251,7 @@ static int listen_socket (char *listen_p
    3.12          close (s);
    3.13          return -1;
    3.14      }
    3.15 -    printf ("accepting connections on path %s\n", listen_path);
    3.16 +    DPRINTF ("accepting connections on path %s\n", listen_path);
    3.17      listen (s, 10);
    3.18      return s;
    3.19  }
    3.20 @@ -628,13 +629,66 @@ void gc_ufd_list( unbound_fd_t **ufd )
    3.21      }
    3.22  }
    3.23  
    3.24 +void daemonize_xcs(void)
    3.25 +{
    3.26 +    
    3.27 +    /* detach from our controlling tty so that a shell does hang waiting for
    3.28 +       stopped jobs. */
    3.29 +    
    3.30 +    pid_t pid = fork();
    3.31 +    int fd;
    3.32 +
    3.33 +    if (pid == -1) {
    3.34 +	    perror("fork()");
    3.35 +    } else if (pid) {
    3.36 +	    exit(0);
    3.37 +    }
    3.38 +
    3.39 +    fd = open("/var/log/xcs.log", O_WRONLY | O_APPEND | O_CREAT);
    3.40 +    if ( fd == -1 ) {
    3.41 +        fprintf(stderr, "xcs couldn't open logfile.  Directing all output to "
    3.42 +                "/dev/null instead.\n");
    3.43 +        fd = open("/dev/null", O_WRONLY);
    3.44 +    }
    3.45 +    
    3.46 +    setsid();
    3.47 +    close(2);
    3.48 +    close(1);
    3.49 +    close(0);
    3.50 +    dup(fd);
    3.51 +    dup(fd);
    3.52 +}
    3.53 +
    3.54 +
    3.55 +static char *pidfilename = NULL;
    3.56 +void cleanup(int sig)
    3.57 +{
    3.58 +    /* throw away our pidfile if we created one. */
    3.59 +    if ( pidfilename != NULL ) 
    3.60 +        unlink(pidfilename);
    3.61 +    exit(0);
    3.62 +}
    3.63 +
    3.64  int main (int argc, char *argv[])
    3.65  {
    3.66      int listen_fd, evtchn_fd;
    3.67      unbound_fd_t *unbound_fd_list = NULL, **ufd;
    3.68      struct timeval timeout = { XCS_GC_INTERVAL, 0 };
    3.69      connection_t **con;
    3.70 +    int c, daemonize;
    3.71 +    FILE *pidfile;
    3.72 +    struct stat s;
    3.73 +    
    3.74 +    daemonize = 1;
    3.75 +    pidfile = NULL;
    3.76  
    3.77 +    signal(SIGHUP, cleanup);
    3.78 +    signal(SIGTERM, cleanup);
    3.79 +    signal(SIGINT, cleanup);
    3.80 +    
    3.81 +    /* Do a bunch of stuff before potentially daemonizing so we can 
    3.82 +     * print error messages sanely before redirecting output. */
    3.83 +    
    3.84      /* Initialize xc and event connections. */
    3.85      if (ctrl_chan_init() != 0)
    3.86      {
    3.87 @@ -648,35 +702,61 @@ int main (int argc, char *argv[])
    3.88          exit(-1);
    3.89      }
    3.90     
    3.91 +    /* Bind listen_fd to the client socket. */
    3.92 +    listen_fd = listen_socket(XCS_SUN_PATH);
    3.93 +     
    3.94 +    while ((c = getopt (argc, argv, "ip:")) != -1)
    3.95 +    {
    3.96 +        switch (c)
    3.97 +        {
    3.98 +        case 'i': /* interactive */
    3.99 +            daemonize = 0;
   3.100 +            break;
   3.101 +        case 'p': /* pid file */
   3.102 +            pidfilename = optarg;
   3.103 +            break;          
   3.104 +        case '?':
   3.105 +            if (isprint (optopt))
   3.106 +                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
   3.107 +            else
   3.108 +                fprintf (stderr,
   3.109 +                    "Bad option character `\\x%x'.\n", optopt);
   3.110 +            break;
   3.111 +        }    
   3.112 +    }
   3.113 +    
   3.114 +    if ( pidfilename != NULL )
   3.115 +    {
   3.116 +        if ( stat(pidfilename, &s) == 0 )
   3.117 +        {
   3.118 +            fprintf(stderr, "Thre specified pid file (%s) already exists.\n"
   3.119 +                    "Is another instance of xcs running?\n", pidfilename);
   3.120 +            exit(-1);
   3.121 +        }
   3.122 +
   3.123 +        pidfile = fopen(pidfilename, "w");
   3.124 +        if (pidfile == NULL)
   3.125 +        {
   3.126 +            fprintf(stderr, "Error openning pidfile (%s).\n", pidfilename);
   3.127 +            exit(-1);
   3.128 +        }
   3.129 +    }
   3.130 +        
   3.131 +    if (daemonize == 1) 
   3.132 +        daemonize_xcs();
   3.133 +    
   3.134 +    if (pidfile != NULL)
   3.135 +    {
   3.136 +        fprintf(pidfile, "%d", getpid());
   3.137 +        fclose(pidfile); 
   3.138 +    }
   3.139 +    
   3.140 +    
   3.141      /* Initialize control interfaces, bindings. */
   3.142      init_interfaces();
   3.143      init_bindings();
   3.144      
   3.145 -    listen_fd = listen_socket(XCS_SUN_PATH);
   3.146     
   3.147 -    /* detach from our controlling tty so that a shell does hang waiting for
   3.148 -       stopped jobs. */
   3.149 -    /* we should use getopt() here */
   3.150 -
   3.151 -    if (!(argc == 2 && !strcmp(argv[1], "-i"))) {
   3.152 -	pid_t pid = fork();
   3.153 -	int fd;
   3.154 -
   3.155 -	if (pid == -1) {
   3.156 -		perror("fork()");
   3.157 -	} else if (pid) {
   3.158 -		exit(0);
   3.159 -	}
   3.160 -
   3.161 -    	setsid();
   3.162 -	close(2);
   3.163 -	close(1);
   3.164 -	close(0);
   3.165 -	fd = open("/dev/null", O_RDWR);
   3.166 -	dup(fd);
   3.167 -	dup(fd);
   3.168 -    }
   3.169 - 
   3.170      for (;;)
   3.171      {
   3.172          int n = 0, ret;