direct-io.hg

changeset 3589:24a15d37eec4

bitkeeper revision 1.1159.212.57 (41fb53cc8wKjvhd8TsN4YHhUYPQCdQ)

Currently xcs is run by xend in xen-unstable by simply making it a child
process. If you launch xend from an ssh session, the session won't exit
because the xcs still is connected to it's controlling terminal.

xcs should daemonize itself to prevent this from happening. This patch
does that along with making the domain id => port mapping dynamically
allocated.

Signed-off-by: Anthony Liguori aliguori@us.ibm.com
author iap10@labyrinth.cl.cam.ac.uk
date Sat Jan 29 09:13:48 2005 +0000 (2005-01-29)
parents abddf91bdfa8
children 96a5638d65e5
files tools/xcs/xcs.c
line diff
     1.1 --- a/tools/xcs/xcs.c	Fri Jan 28 23:26:21 2005 +0000
     1.2 +++ b/tools/xcs/xcs.c	Sat Jan 29 09:13:48 2005 +0000
     1.3 @@ -74,6 +74,8 @@
     1.4  #include <netinet/in.h>
     1.5  #include <arpa/inet.h>
     1.6  #include <errno.h>
     1.7 +#include <malloc.h>
     1.8 +#include <fcntl.h>
     1.9  #include "xcs.h"
    1.10  
    1.11  #undef fd_max
    1.12 @@ -82,14 +84,36 @@
    1.13  /* ------[ Control channel interfaces ]------------------------------------*/
    1.14  
    1.15  static control_channel_t *cc_list[NR_EVENT_CHANNELS];
    1.16 -static int dom_to_port[MAX_DOMS]; /* This should not be a fixed-size array.*/
    1.17 +static int *dom_port_map = 0;
    1.18 +static int dom_port_map_size = 0;
    1.19 +
    1.20 +static void map_dom_to_port(u32 dom, int port)
    1.21 +{
    1.22 +	if (dom >= dom_port_map_size) {
    1.23 +		dom_port_map = (int *)realloc(dom_port_map,
    1.24 +					      (dom + 10) * sizeof(dom_port_map[0]));
    1.25 +
    1.26 +		if (dom_port_map == NULL) {
    1.27 +			perror("realloc(dom_port_map)");
    1.28 +			exit(1);
    1.29 +		}
    1.30 +
    1.31 +		for (; dom_port_map_size < dom + 10; dom_port_map_size++) {
    1.32 +			dom_port_map[dom_port_map_size] = -1;
    1.33 +		}
    1.34 +	}
    1.35 +
    1.36 +	dom_port_map[dom] = port;
    1.37 +}
    1.38 +
    1.39 +static int dom_to_port(u32 dom) {
    1.40 +	if (dom >= dom_port_map_size) return -1;
    1.41 +
    1.42 +	return dom_port_map[dom];
    1.43 +}
    1.44  
    1.45  static void init_interfaces(void)
    1.46  {
    1.47 -    int i;
    1.48 -    
    1.49 -    for (i = 0; i < MAX_DOMS; i++)
    1.50 -        dom_to_port[i] = -1;
    1.51      memset(cc_list, 0, sizeof cc_list);
    1.52  }
    1.53  
    1.54 @@ -99,9 +123,9 @@ static control_channel_t *add_interface(
    1.55      control_channel_t *cc=NULL, *oldcc;
    1.56      int ret;
    1.57      
    1.58 -    if (cc_list[dom_to_port[dom]] != NULL)
    1.59 +    if (cc_list[dom_to_port(dom)] != NULL)
    1.60      {
    1.61 -        return(cc_list[dom_to_port[dom]]);
    1.62 +        return(cc_list[dom_to_port(dom)]);
    1.63      }
    1.64      
    1.65      if (cc_list[local_port] == NULL) 
    1.66 @@ -133,13 +157,13 @@ static control_channel_t *add_interface(
    1.67          {
    1.68              DPRINTF("CC conflict! (port: %d, old dom: %u, new dom: %u)\n",
    1.69                      cc->local_port, oldcc->remote_dom, cc->remote_dom);
    1.70 -            dom_to_port[oldcc->remote_dom] = -1;
    1.71 +            map_dom_to_port(oldcc->remote_dom, -1);
    1.72              ctrl_chan_free(cc_list[cc->local_port]);
    1.73          }
    1.74      }
    1.75       
    1.76      cc_list[cc->local_port] = cc;
    1.77 -    dom_to_port[cc->remote_dom] = cc->local_port;
    1.78 +    map_dom_to_port(cc->remote_dom, cc->local_port);
    1.79      cc->type = CC_TYPE_INTERDOMAIN;
    1.80      cc->ref_count = 0;
    1.81      return cc;
    1.82 @@ -436,7 +460,7 @@ void handle_data_message( connection_t *
    1.83          if ( cmsg->remote_dom > MAX_DOMS )
    1.84              break;
    1.85          
    1.86 -        port = dom_to_port[cmsg->remote_dom];
    1.87 +        port = dom_to_port(cmsg->remote_dom);
    1.88          if (port == -1) break;
    1.89          cc = cc_list[port];
    1.90          if ((cc != NULL) && ( cc->type == CC_TYPE_INTERDOMAIN ))
    1.91 @@ -455,7 +479,7 @@ void handle_data_message( connection_t *
    1.92          if ( cmsg->remote_dom > MAX_DOMS )
    1.93              break;
    1.94          
    1.95 -        port = dom_to_port[cmsg->remote_dom];
    1.96 +        port = dom_to_port(cmsg->remote_dom);
    1.97          if (port == -1) break;
    1.98          cc = cc_list[port];
    1.99          if ((cc != NULL) && ( cc->type == CC_TYPE_INTERDOMAIN ))
   1.100 @@ -627,7 +651,30 @@ int main (int argc, char*argv[])
   1.101      init_bindings();
   1.102      
   1.103      listen_fd = listen_socket(XCS_TCP_PORT);
   1.104 -    
   1.105 +   
   1.106 +    /* detach from our controlling tty so that a shell does hang waiting for
   1.107 +       stopped jobs. */
   1.108 +    /* we should use getopt() here */
   1.109 +
   1.110 +    if (!(argc == 2 && !strcmp(argv[1], "-i"))) {
   1.111 +	pid_t pid = fork();
   1.112 +	int fd;
   1.113 +
   1.114 +	if (pid == -1) {
   1.115 +		perror("fork()");
   1.116 +	} else if (pid) {
   1.117 +		exit(0);
   1.118 +	}
   1.119 +
   1.120 +    	setsid();
   1.121 +	close(2);
   1.122 +	close(1);
   1.123 +	close(0);
   1.124 +	fd = open("/dev/null", O_RDWR);
   1.125 +	dup(fd);
   1.126 +	dup(fd);
   1.127 +    }
   1.128 + 
   1.129      for (;;)
   1.130      {
   1.131          int n, ret;