direct-io.hg

changeset 15211:edf407a3dd70

Add persistent guest & hv logging in xenconsoled.

* The --log command line argument takes one of 4 values

- none - no logging (the default)
- hv - log all hypervisor messages
- guest - log all guest messages
- both - log all guest & hypervisor messages

* The --log-dir command line argument takes a path to specify where
to store logfiles. If omitted it defaults to /var/log/xen/console

* The hypervisor logfile is $LOGDIR/hypervisor.log

* The guest logfile is $LOGDIR/guest-[NAME].log

* If receiving a SIGHUP it will close & re-open all log files to
enable logrotate to do its magic

* Fixes the permissions of /var/run/xenconsoled.pid

* Adds a --pid-file command line argument to override the default
location of pid file (this is not really related to logging, but
since I was in that code...)

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
author kfraser@localhost.localdomain
date Mon Jun 04 14:49:12 2007 +0100 (2007-06-04)
parents da3185b03113
children fc8e40692690
files tools/console/daemon/io.c tools/console/daemon/main.c tools/console/daemon/utils.c
line diff
     1.1 --- a/tools/console/daemon/io.c	Mon Jun 04 14:40:12 2007 +0100
     1.2 +++ b/tools/console/daemon/io.c	Mon Jun 04 14:49:12 2007 +0100
     1.3 @@ -43,6 +43,14 @@
     1.4  /* Each 10 bits takes ~ 3 digits, plus one, plus one for nul terminator. */
     1.5  #define MAX_STRLEN(x) ((sizeof(x) * CHAR_BIT + CHAR_BIT-1) / 10 * 3 + 2)
     1.6  
     1.7 +extern int log_reload;
     1.8 +extern int log_guest;
     1.9 +extern int log_hv;
    1.10 +extern char *log_dir;
    1.11 +
    1.12 +static int log_hv_fd = -1;
    1.13 +static int xc_handle = -1;
    1.14 +
    1.15  struct buffer
    1.16  {
    1.17  	char *data;
    1.18 @@ -56,6 +64,7 @@ struct domain
    1.19  {
    1.20  	int domid;
    1.21  	int tty_fd;
    1.22 +	int log_fd;
    1.23  	bool is_dead;
    1.24  	struct buffer buffer;
    1.25  	struct domain *next;
    1.26 @@ -102,6 +111,19 @@ static void buffer_append(struct domain 
    1.27  	intf->out_cons = cons;
    1.28  	xc_evtchn_notify(dom->xce_handle, dom->local_port);
    1.29  
    1.30 +	/* Get the data to the logfile as early as possible because if
    1.31 +	 * no one is listening on the console pty then it will fill up
    1.32 +	 * and handle_tty_write will stop being called.
    1.33 +	 */
    1.34 +	if (dom->log_fd != -1) {
    1.35 +		int len = write(dom->log_fd,
    1.36 +				buffer->data + buffer->size - size,
    1.37 +				size);
    1.38 +		if (len < 0)
    1.39 +			dolog(LOG_ERR, "Write to log failed on domain %d: %d (%s)\n",
    1.40 +			      dom->domid, errno, strerror(errno));
    1.41 +	}
    1.42 +
    1.43  	if (buffer->max_capacity &&
    1.44  	    buffer->size > buffer->max_capacity) {
    1.45  		/* Discard the middle of the data. */
    1.46 @@ -143,6 +165,53 @@ static bool domain_is_valid(int domid)
    1.47  	return ret;
    1.48  }
    1.49  
    1.50 +static int create_hv_log(void)
    1.51 +{
    1.52 +	char logfile[PATH_MAX];
    1.53 +	int fd;
    1.54 +	snprintf(logfile, PATH_MAX-1, "%s/hypervisor.log", log_dir);
    1.55 +	logfile[PATH_MAX-1] = '\0';
    1.56 +
    1.57 +	fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0644);
    1.58 +	if (fd == -1)
    1.59 +		dolog(LOG_ERR, "Failed to open log %s: %d (%s)",
    1.60 +		      logfile, errno, strerror(errno));
    1.61 +	return fd;
    1.62 +}
    1.63 +
    1.64 +static int create_domain_log(struct domain *dom)
    1.65 +{
    1.66 +	char logfile[PATH_MAX];
    1.67 +	char *namepath, *data, *s;
    1.68 +	int fd, len;
    1.69 +
    1.70 +	namepath = xs_get_domain_path(xs, dom->domid);
    1.71 +	s = realloc(namepath, strlen(namepath) + 6);
    1.72 +	if (s == NULL) {
    1.73 +		free(namepath);
    1.74 +		return -1;
    1.75 +	}
    1.76 +	strcat(namepath, "/name");
    1.77 +	data = xs_read(xs, XBT_NULL, namepath, &len);
    1.78 +	if (!data)
    1.79 +		return -1;
    1.80 +	if (!len) {
    1.81 +		free(data);
    1.82 +		return -1;
    1.83 +	}
    1.84 +
    1.85 +	snprintf(logfile, PATH_MAX-1, "%s/guest-%s.log", log_dir, data);
    1.86 +	free(data);
    1.87 +	logfile[PATH_MAX-1] = '\0';
    1.88 +
    1.89 +	fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0644);
    1.90 +	if (fd == -1)
    1.91 +		dolog(LOG_ERR, "Failed to open log %s: %d (%s)",
    1.92 +		      logfile, errno, strerror(errno));
    1.93 +	return fd;
    1.94 +}
    1.95 +
    1.96 +
    1.97  static int domain_create_tty(struct domain *dom)
    1.98  {
    1.99  	char *path;
   1.100 @@ -324,6 +393,9 @@ static int domain_create_ring(struct dom
   1.101  		}
   1.102  	}
   1.103  
   1.104 +	if (log_guest)
   1.105 +		dom->log_fd = create_domain_log(dom);
   1.106 +
   1.107   out:
   1.108  	return err;
   1.109  }
   1.110 @@ -351,6 +423,7 @@ static bool watch_domain(struct domain *
   1.111  	return success;
   1.112  }
   1.113  
   1.114 +
   1.115  static struct domain *create_domain(int domid)
   1.116  {
   1.117  	struct domain *dom;
   1.118 @@ -382,6 +455,8 @@ static struct domain *create_domain(int 
   1.119  	strcat(dom->conspath, "/console");
   1.120  
   1.121  	dom->tty_fd = -1;
   1.122 +	dom->log_fd = -1;
   1.123 +
   1.124  	dom->is_dead = false;
   1.125  	dom->buffer.data = 0;
   1.126  	dom->buffer.consumed = 0;
   1.127 @@ -443,6 +518,10 @@ static void cleanup_domain(struct domain
   1.128  		close(d->tty_fd);
   1.129  		d->tty_fd = -1;
   1.130  	}
   1.131 +	if (d->log_fd != -1) {
   1.132 +		close(d->log_fd);
   1.133 +		d->log_fd = -1;
   1.134 +	}
   1.135  
   1.136  	free(d->buffer.data);
   1.137  	d->buffer.data = NULL;
   1.138 @@ -604,13 +683,54 @@ static void handle_xs(void)
   1.139  	free(vec);
   1.140  }
   1.141  
   1.142 +static void handle_hv_logs(void)
   1.143 +{
   1.144 +	char buffer[1024*16];
   1.145 +	char *bufptr = buffer;
   1.146 +	unsigned int size = sizeof(buffer);
   1.147 +	if (xc_readconsolering(xc_handle, &bufptr, &size, 1) == 0) {
   1.148 +		int len = write(log_hv_fd, buffer, size);
   1.149 +		if (len < 0)
   1.150 +			dolog(LOG_ERR, "Failed to write hypervisor log: %d (%s)",
   1.151 +			      errno, strerror(errno));
   1.152 +	}
   1.153 +}
   1.154 +
   1.155 +static void handle_log_reload(void)
   1.156 +{
   1.157 +	if (log_guest) {
   1.158 +		struct domain *d;
   1.159 +		for (d = dom_head; d; d = d->next) {
   1.160 +			if (d->log_fd != -1)
   1.161 +				close(d->log_fd);
   1.162 +			d->log_fd = create_domain_log(d);
   1.163 +		}
   1.164 +	}
   1.165 +
   1.166 +	if (log_hv) {
   1.167 +		if (log_hv_fd != -1)
   1.168 +			close(log_hv_fd);
   1.169 +		log_hv_fd = create_hv_log();
   1.170 +	}
   1.171 +}
   1.172 +
   1.173  void handle_io(void)
   1.174  {
   1.175  	fd_set readfds, writefds;
   1.176  	int ret;
   1.177  
   1.178 -	do {
   1.179 +	if (log_hv) {
   1.180 +		xc_handle = xc_interface_open();
   1.181 +		if (xc_handle == -1)
   1.182 +			dolog(LOG_ERR, "Failed to open xc handle: %d (%s)",
   1.183 +			      errno, strerror(errno));
   1.184 +		else
   1.185 +			log_hv_fd = create_hv_log();
   1.186 +	}
   1.187 +
   1.188 +	for (;;) {
   1.189  		struct domain *d, *n;
   1.190 +		struct timeval timeout = { 1, 0 }; /* Read HV logs every 1 second */
   1.191  		int max_fd = -1;
   1.192  
   1.193  		FD_ZERO(&readfds);
   1.194 @@ -636,7 +756,30 @@ void handle_io(void)
   1.195  			}
   1.196  		}
   1.197  
   1.198 -		ret = select(max_fd + 1, &readfds, &writefds, 0, NULL);
   1.199 +		/* XXX I wish we didn't have to busy wait for hypervisor logs
   1.200 +		 * but there's no obvious way to get event channel notifications
   1.201 +		 * for new HV log data as we can with guest */
   1.202 +		ret = select(max_fd + 1, &readfds, &writefds, 0, log_hv_fd != -1 ? &timeout : NULL);
   1.203 +
   1.204 +		if (ret == -1) {
   1.205 +			if (errno == EINTR) {
   1.206 +				if (log_reload) {
   1.207 +					handle_log_reload();
   1.208 +					log_reload = 0;
   1.209 +				}
   1.210 +				continue;
   1.211 +			}
   1.212 +			dolog(LOG_ERR, "Failure in select: %d (%s)",
   1.213 +			      errno, strerror(errno));
   1.214 +			break;
   1.215 +		}
   1.216 +
   1.217 +		/* Check for timeout */
   1.218 +		if (ret == 0) {
   1.219 +			if (log_hv_fd != -1)
   1.220 +				handle_hv_logs();
   1.221 +			continue;
   1.222 +		}
   1.223  
   1.224  		if (FD_ISSET(xs_fileno(xs), &readfds))
   1.225  			handle_xs();
   1.226 @@ -656,7 +799,12 @@ void handle_io(void)
   1.227  			if (d->is_dead)
   1.228  				cleanup_domain(d);
   1.229  		}
   1.230 -	} while (ret > -1);
   1.231 +	}
   1.232 +
   1.233 +	if (log_hv_fd != -1)
   1.234 +		close(log_hv_fd);
   1.235 +	if (xc_handle != -1)
   1.236 +		xc_interface_close(xc_handle);
   1.237  }
   1.238  
   1.239  /*
     2.1 --- a/tools/console/daemon/main.c	Mon Jun 04 14:40:12 2007 +0100
     2.2 +++ b/tools/console/daemon/main.c	Mon Jun 04 14:49:12 2007 +0100
     2.3 @@ -23,6 +23,8 @@
     2.4  #include <stdio.h>
     2.5  #include <errno.h>
     2.6  #include <unistd.h>
     2.7 +#include <string.h>
     2.8 +#include <signal.h>
     2.9  #include <sys/types.h>
    2.10  
    2.11  #include "xenctrl.h"
    2.12 @@ -30,9 +32,19 @@
    2.13  #include "utils.h"
    2.14  #include "io.h"
    2.15  
    2.16 +int log_reload = 0;
    2.17 +int log_guest = 0;
    2.18 +int log_hv = 0;
    2.19 +char *log_dir = NULL;
    2.20 +
    2.21 +static void handle_hup(int sig)
    2.22 +{
    2.23 +        log_reload = 1;
    2.24 +}
    2.25 +
    2.26  static void usage(char *name)
    2.27  {
    2.28 -	printf("Usage: %s [-h] [-V] [-v] [-i]\n", name);
    2.29 +	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH]\n", name);
    2.30  }
    2.31  
    2.32  static void version(char *name)
    2.33 @@ -48,6 +60,9 @@ int main(int argc, char **argv)
    2.34  		{ "version", 0, 0, 'V' },
    2.35  		{ "verbose", 0, 0, 'v' },
    2.36  		{ "interactive", 0, 0, 'i' },
    2.37 +		{ "log", 1, 0, 'l' },
    2.38 +		{ "log-dir", 1, 0, 'r' },
    2.39 +		{ "pid-file", 1, 0, 'p' },
    2.40  		{ 0 },
    2.41  	};
    2.42  	bool is_interactive = false;
    2.43 @@ -55,6 +70,7 @@ int main(int argc, char **argv)
    2.44  	int syslog_option = LOG_CONS;
    2.45  	int syslog_mask = LOG_WARNING;
    2.46  	int opt_ind = 0;
    2.47 +	char *pidfile = NULL;
    2.48  
    2.49  	while ((ch = getopt_long(argc, argv, sopts, lopts, &opt_ind)) != -1) {
    2.50  		switch (ch) {
    2.51 @@ -71,6 +87,22 @@ int main(int argc, char **argv)
    2.52  		case 'i':
    2.53  			is_interactive = true;
    2.54  			break;
    2.55 +		case 'l':
    2.56 +		        if (!strcmp(optarg, "all")) {
    2.57 +			      log_hv = 1;
    2.58 +			      log_guest = 1;
    2.59 +			} else if (!strcmp(optarg, "hv")) {
    2.60 +			      log_hv = 1;
    2.61 +			} else if (!strcmp(optarg, "guest")) {
    2.62 +			      log_guest = 1;
    2.63 +			}
    2.64 +			break;
    2.65 +		case 'r':
    2.66 +		        log_dir = strdup(optarg);
    2.67 +			break;
    2.68 +		case 'p':
    2.69 +		        pidfile = strdup(optarg);
    2.70 +			break;
    2.71  		case '?':
    2.72  			fprintf(stderr,
    2.73  				"Try `%s --help' for more information\n",
    2.74 @@ -79,16 +111,22 @@ int main(int argc, char **argv)
    2.75  		}
    2.76  	}
    2.77  
    2.78 +	if (!log_dir) {
    2.79 +		log_dir = strdup("/var/log/xen/console");
    2.80 +	}
    2.81 +
    2.82  	if (geteuid() != 0) {
    2.83  		fprintf(stderr, "%s requires root to run.\n", argv[0]);
    2.84  		exit(EPERM);
    2.85  	}
    2.86  
    2.87 +	signal(SIGHUP, handle_hup);
    2.88 +
    2.89  	openlog("xenconsoled", syslog_option, LOG_DAEMON);
    2.90  	setlogmask(syslog_mask);
    2.91  
    2.92  	if (!is_interactive) {
    2.93 -		daemonize("/var/run/xenconsoled.pid");
    2.94 +		daemonize(pidfile ? pidfile : "/var/run/xenconsoled.pid");
    2.95  	}
    2.96  
    2.97  	if (!xen_setup())
    2.98 @@ -99,6 +137,18 @@ int main(int argc, char **argv)
    2.99  	handle_io();
   2.100  
   2.101  	closelog();
   2.102 +	free(log_dir);
   2.103 +	free(pidfile);
   2.104  
   2.105  	return 0;
   2.106  }
   2.107 +
   2.108 +/*
   2.109 + * Local variables:
   2.110 + *  c-file-style: "linux"
   2.111 + *  indent-tabs-mode: t
   2.112 + *  c-indent-level: 8
   2.113 + *  c-basic-offset: 8
   2.114 + *  tab-width: 8
   2.115 + * End:
   2.116 + */
     3.1 --- a/tools/console/daemon/utils.c	Mon Jun 04 14:40:12 2007 +0100
     3.2 +++ b/tools/console/daemon/utils.c	Mon Jun 04 14:49:12 2007 +0100
     3.3 @@ -86,7 +86,7 @@ void daemonize(const char *pidfile)
     3.4  	if (chdir("/") < 0)
     3.5  		exit (1);
     3.6  
     3.7 -	fd = open(pidfile, O_RDWR | O_CREAT);
     3.8 +	fd = open(pidfile, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR);
     3.9  	if (fd == -1) {
    3.10  		exit(1);
    3.11  	}