ia64/xen-unstable

changeset 7768:975aa9e4def3

Fix the log reopening by moving the code with all the races out of the signal
handler and into the main loop. The "self-pipe trick" is used to awaken the
select() call on SIGHUP.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Nov 10 16:20:27 2005 +0100 (2005-11-10)
parents 4982559d8fde
children 10e6946477b8
files tools/xenstore/xenstored_core.c
line diff
     1.1 --- a/tools/xenstore/xenstored_core.c	Thu Nov 10 15:16:01 2005 +0100
     1.2 +++ b/tools/xenstore/xenstored_core.c	Thu Nov 10 16:20:27 2005 +0100
     1.3 @@ -56,6 +56,7 @@ extern int eventchn_fd; /* in xenstored_
     1.4  static bool verbose;
     1.5  LIST_HEAD(connections);
     1.6  static int tracefd = -1;
     1.7 +static int reopen_log_pipe[2];
     1.8  static char *tracefile = NULL;
     1.9  static TDB_CONTEXT *tdb_ctx;
    1.10  
    1.11 @@ -243,21 +244,35 @@ void trace(const char *fmt, ...)
    1.12  	talloc_free(str);
    1.13  }
    1.14  
    1.15 -void reopen_log()
    1.16 +
    1.17 +/**
    1.18 + * Signal handler for SIGHUP, which requests that the trace log is reopened
    1.19 + * (in the main loop).  A single byte is written to reopen_log_pipe, to awaken
    1.20 + * the select() in the main loop.
    1.21 + */
    1.22 +static void trigger_reopen_log(int signal __attribute__((unused)))
    1.23  {
    1.24 -	if (!tracefile)
    1.25 -		return;
    1.26 +	char c = 'A';
    1.27 +	write(reopen_log_pipe[1], &c, 1);
    1.28 +}
    1.29 +
    1.30  
    1.31 -	if (tracefd > 0)
    1.32 -		close(tracefd);
    1.33 -	tracefd = open(tracefile, O_WRONLY|O_CREAT|O_APPEND, 0600);
    1.34 -	if (tracefd < 0) {
    1.35 -		perror("Could not open tracefile");
    1.36 -		return;
    1.37 +static void reopen_log()
    1.38 +{
    1.39 +	if (tracefile) {
    1.40 +		if (tracefd > 0)
    1.41 +			close(tracefd);
    1.42 +
    1.43 +		tracefd = open(tracefile, O_WRONLY|O_CREAT|O_APPEND, 0600);
    1.44 +
    1.45 +		if (tracefd < 0)
    1.46 +			perror("Could not open tracefile");
    1.47 +		else
    1.48 +			write(tracefd, "\n***\n", strlen("\n***\n"));
    1.49  	}
    1.50 -	write(tracefd, "\n***\n", strlen("\n***\n"));
    1.51  }
    1.52  
    1.53 +
    1.54  static bool write_messages(struct connection *conn)
    1.55  {
    1.56  	int ret;
    1.57 @@ -331,29 +346,33 @@ static int destroy_conn(void *_conn)
    1.58  	return 0;
    1.59  }
    1.60  
    1.61 +
    1.62 +static void set_fd(int fd, fd_set *set, int *max)
    1.63 +{
    1.64 +	FD_SET(fd, set);
    1.65 +	if (fd > *max)
    1.66 +		*max = fd;
    1.67 +}
    1.68 +
    1.69 +
    1.70  static int initialize_set(fd_set *inset, fd_set *outset, int sock, int ro_sock)
    1.71  {
    1.72  	struct connection *i;
    1.73 -	int max;
    1.74 +	int max = -1;
    1.75  
    1.76  	FD_ZERO(inset);
    1.77  	FD_ZERO(outset);
    1.78 -	FD_SET(sock, inset);
    1.79 -	max = sock;
    1.80 -	FD_SET(ro_sock, inset);
    1.81 -	if (ro_sock > max)
    1.82 -		max = ro_sock;
    1.83 -	FD_SET(eventchn_fd, inset);
    1.84 -	if (eventchn_fd > max)
    1.85 -		max = eventchn_fd;
    1.86 +
    1.87 +	set_fd(sock,               inset, &max);
    1.88 +	set_fd(ro_sock,            inset, &max);
    1.89 +	set_fd(eventchn_fd,        inset, &max);
    1.90 +	set_fd(reopen_log_pipe[0], inset, &max);
    1.91  	list_for_each_entry(i, &connections, list) {
    1.92  		if (i->domain)
    1.93  			continue;
    1.94 -		FD_SET(i->fd, inset);
    1.95 +		set_fd(i->fd, inset, &max);
    1.96  		if (!list_empty(&i->out_list))
    1.97  			FD_SET(i->fd, outset);
    1.98 -		if (i->fd > max)
    1.99 -			max = i->fd;
   1.100  	}
   1.101  	return max;
   1.102  }
   1.103 @@ -1570,6 +1589,10 @@ int main(int argc, char *argv[])
   1.104  	    || listen(*ro_sock, 1) != 0)
   1.105  		barf_perror("Could not listen on sockets");
   1.106  
   1.107 +	if (pipe(reopen_log_pipe)) {
   1.108 +		barf_perror("pipe");
   1.109 +	}
   1.110 +
   1.111  	/* Setup the database */
   1.112  	setup_structure();
   1.113  
   1.114 @@ -1592,7 +1615,7 @@ int main(int argc, char *argv[])
   1.115  		close(STDERR_FILENO);
   1.116  	}
   1.117  
   1.118 -	signal(SIGHUP, reopen_log);
   1.119 +	signal(SIGHUP, trigger_reopen_log);
   1.120  
   1.121  #ifdef TESTING
   1.122  	signal(SIGUSR1, stop_failtest);
   1.123 @@ -1612,6 +1635,12 @@ int main(int argc, char *argv[])
   1.124  			barf_perror("Select failed");
   1.125  		}
   1.126  
   1.127 +		if (FD_ISSET(reopen_log_pipe[0], &inset)) {
   1.128 +			char c;
   1.129 +			read(reopen_log_pipe[0], &c, 1);
   1.130 +			reopen_log();
   1.131 +		}
   1.132 +
   1.133  		if (FD_ISSET(*sock, &inset))
   1.134  			accept_connection(*sock, true);
   1.135