ia64/xen-unstable
changeset 829:bfbe6d080b40
bitkeeper revision 1.512 (3f8bcf2a0Vz2xXrdEA4lXWy2qRMaSg)
oops, forgot to click on xen_log to add it to repository.
oops, forgot to click on xen_log to add it to repository.
author | iap10@labyrinth.cl.cam.ac.uk |
---|---|
date | Tue Oct 14 10:25:46 2003 +0000 (2003-10-14) |
parents | cf07f0fdbf52 |
children | c0eefee27220 |
files | .rootkeys tools/misc/xen_log.c |
line diff
1.1 --- a/.rootkeys Mon Oct 13 22:29:25 2003 +0000 1.2 +++ b/.rootkeys Tue Oct 14 10:25:46 2003 +0000 1.3 @@ -183,6 +183,7 @@ 3f5ef5a2ir1kVAthS14Dc5QIRCEFWg tools/mis 1.4 3f5ef5a2dTZP0nnsFoeq2jRf3mWDDg tools/misc/xen-clone.README 1.5 3f1668d4-FUY6Enc7MB3GcwUtfJ5HA tools/misc/xen-mkdevnodes 1.6 3f870808zS6T6iFhqYPGelroZlVfGQ tools/misc/xen_cpuperf.c 1.7 +3f8bcf29ulZIC9rC4wM70H_q4s6VPg tools/misc/xen_log.c 1.8 3f13d81eQ9Vz-h-6RDGFkNR9CRP95g tools/misc/xen_nat_enable 1.9 3f13d81e6Z6806ihYYUw8GVKNkYnuw tools/misc/xen_nat_enable.README 1.10 3f1668d4F29Jsw0aC0bJEIkOBiagiQ tools/misc/xen_read_console.c
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tools/misc/xen_log.c Tue Oct 14 10:25:46 2003 +0000 2.3 @@ -0,0 +1,171 @@ 2.4 +#include <sys/types.h> 2.5 +#include <tcpd.h> 2.6 +#include <sys/socket.h> 2.7 +#include <stdio.h> 2.8 +#include <stdlib.h> 2.9 +#include <syslog.h> 2.10 +#include <errno.h> 2.11 +#include <unistd.h> 2.12 +#include <fcntl.h> 2.13 +#include <signal.h> 2.14 +#include <sys/wait.h> 2.15 +#include <string.h> 2.16 + 2.17 +#include "hypervisor-ifs/dom0_ops.h" 2.18 +#include "dom0_defs.h" 2.19 +#include "mem_defs.h" 2.20 + 2.21 +#define SILENT_ERRORS_FROM_XEN 2.22 +#define SYSLOG 1 2.23 +#define SYSLOGTO LOG_LOCAL5 2.24 + 2.25 +int logoutput; 2.26 + 2.27 +void stripit(char *str) 2.28 +{ 2.29 + register int i; 2.30 + 2.31 + for (i = 0; str[i]; i++) { 2.32 + if (str[i] == '\n') str[i] = '\0'; 2.33 + if (str[i] == '\r') str[i] = '\0'; 2.34 + } 2.35 +} 2.36 + 2.37 +void errexit(char *str) 2.38 +{ 2.39 + if(logoutput == SYSLOG) { 2.40 + stripit(str); 2.41 + syslog(LOG_ERR, "%s failed: %d (%m)", str, errno); 2.42 + } else { 2.43 + printf("%s", str); 2.44 + } 2.45 + exit(1); 2.46 +} 2.47 + 2.48 +void log(char *str) 2.49 +{ 2.50 + if(logoutput == SYSLOG) { 2.51 + stripit(str); 2.52 + syslog(LOG_INFO, "%s", str); 2.53 + } else { 2.54 + printf("%s", str); 2.55 + } 2.56 +} 2.57 + 2.58 +void process() 2.59 +{ 2.60 + dom0_op_t op; 2.61 + unsigned char buf[208], obuf[224]; 2.62 + struct sockaddr_in addr, from; 2.63 + int fromlen = sizeof(from); 2.64 + int len, fd = socket(PF_INET, SOCK_DGRAM, 0); 2.65 + unsigned short int lastport = 0, curport = 0; 2.66 + 2.67 + if ( fd < 0 ) 2.68 + errexit("could not open datagram socket"); 2.69 + 2.70 + memset(&addr, 0, sizeof(addr)); 2.71 + addr.sin_addr.s_addr = htonl(0xa9fe0100); /* 169.254.1.0 */ 2.72 + addr.sin_port = htons(666); 2.73 + addr.sin_family = AF_INET; 2.74 + 2.75 + if ( bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0 ) 2.76 + errexit("could not bind to local address and port"); 2.77 + 2.78 + op.cmd = DOM0_GETDOMAININFO; 2.79 + 2.80 + while ( (len = recvfrom(fd, buf, sizeof(buf), 0, 2.81 + (struct sockaddr *)&from, &fromlen)) >= 0 ) 2.82 + { 2.83 + curport = ntohs(from.sin_port); 2.84 + if(lastport != curport) { 2.85 + op.u.getdominfo.domain = (int)curport; 2.86 + if ( do_dom0_op(&op) < 0 ) 2.87 + log("Error resolving domain name\n"); 2.88 + } else { 2.89 + lastport = curport; 2.90 + } 2.91 + } 2.92 + 2.93 + sprintf(obuf, "[%s] %s", op.u.getdominfo.name, curport, buf); 2.94 + log(obuf); 2.95 + 2.96 + fromlen = sizeof(from); 2.97 + } 2.98 +} 2.99 + 2.100 +void closeall(int fd) 2.101 +{ 2.102 + int fdlimit = sysconf(_SC_OPEN_MAX); 2.103 + 2.104 + while (fd < fdlimit) 2.105 + close(fd++); 2.106 +} 2.107 + 2.108 +int daemon(int nochdir, int noclose) 2.109 +{ 2.110 + switch (fork()) 2.111 + { 2.112 + case 0: break; 2.113 + case -1: return -1; 2.114 + default: _exit(0); 2.115 + } 2.116 + 2.117 + if (setsid() < 0) 2.118 + return -1; 2.119 + 2.120 + switch (fork()) 2.121 + { 2.122 + case 0: break; 2.123 + case -1: return -1; 2.124 + default: _exit(0); 2.125 + } 2.126 + 2.127 + if (!nochdir) 2.128 + chdir("/"); 2.129 + 2.130 + if (!noclose) 2.131 + { 2.132 + closeall(0); 2.133 + open("/dev/null",O_RDWR); 2.134 + dup(0); dup(0); 2.135 + } 2.136 + 2.137 + return 0; 2.138 +} 2.139 + 2.140 +int main(int argc, char **argv) 2.141 +{ 2.142 + logoutput = 0; 2.143 + int c; 2.144 + 2.145 + opterr = 0; 2.146 + 2.147 + while ((c = getopt (argc, argv, "dh")) != -1) 2.148 + { 2.149 + switch(c) 2.150 + { 2.151 + case 'd': 2.152 + logoutput = SYSLOG; 2.153 + if (daemon(0,0) < 0) 2.154 + { 2.155 + errno = 2; 2.156 + errexit("daemon"); 2.157 + } else { 2.158 + openlog("xenolog", LOG_PID, SYSLOGTO); 2.159 + } 2.160 + break; 2.161 + case 'h': 2.162 + printf("Usage: xenolog [options]\n"); 2.163 + printf("Capture and display output of xen domains.\n\n"); 2.164 + printf(" -d Daemonize and send output to syslog.\n"); 2.165 + exit(0); 2.166 + break; 2.167 + } 2.168 + } 2.169 + 2.170 + process(); 2.171 + 2.172 + return 0; 2.173 +} 2.174 +