ia64/xen-unstable

view tools/misc/xen_log.c @ 1372:5a6113c65ead

bitkeeper revision 1.891.1.12 (40a248b0WTGoOa9206iWkyGN0mTPNw)

Allow forcing of IRQ trigger-type to edge or level
(NB. DANGEROUS!).
author kaf24@scramble.cl.cam.ac.uk
date Wed May 12 15:54:24 2004 +0000 (2004-05-12)
parents 4d18dbf45e16
children
line source
2 #include <netinet/in.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <syslog.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <string.h>
14 #include "dom0_defs.h"
15 #include "mem_defs.h"
17 #define SILENT_ERRORS_FROM_XEN
18 #define SYSLOG 1
19 #define SYSLOGTO LOG_LOCAL5
21 int logoutput;
23 void stripit(char *str)
24 {
25 int i;
26 for ( i = 0; str[i]; i++ )
27 if ( (str[i] == '\n') || (str[i] == '\r') )
28 str[i] = '\0';
29 }
31 void errexit(char *str)
32 {
33 if ( logoutput == SYSLOG )
34 {
35 stripit(str);
36 syslog(LOG_ERR, "%s failed: %d (%m)", str, errno);
37 }
38 else
39 {
40 printf("%s", str);
41 }
42 exit(1);
43 }
45 void log(char *str)
46 {
47 if ( logoutput == SYSLOG )
48 {
49 stripit(str);
50 syslog(LOG_INFO, "%s", str);
51 }
52 else
53 {
54 printf("%s", str);
55 }
56 }
58 void process(void)
59 {
60 dom0_op_t op;
61 unsigned char buf[208], obuf[224];
62 struct sockaddr_in addr, from;
63 int fromlen = sizeof(from);
64 int len, fd = socket(PF_INET, SOCK_DGRAM, 0);
65 unsigned short int lastport = 0, curport = 0;
67 if ( fd < 0 )
68 errexit("could not open datagram socket");
70 memset(&addr, 0, sizeof(addr));
71 addr.sin_addr.s_addr = htonl(0xa9fe0100); /* 169.254.1.0 */
72 addr.sin_port = htons(666);
73 addr.sin_family = AF_INET;
75 if ( bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0 )
76 errexit("could not bind to local address and port");
78 op.cmd = DOM0_GETDOMAININFO;
80 while ( (len = recvfrom(fd, buf, sizeof(buf), 0,
81 (struct sockaddr *)&from, &fromlen)) >= 0 )
82 {
83 curport = ntohs(from.sin_port);
84 if ( lastport != curport )
85 {
86 op.u.getdomaininfo.domain = (int)curport;
87 if ( do_dom0_op(&op) < 0 )
88 log("Error resolving domain name\n");
89 else
90 lastport = curport;
91 }
93 sprintf(obuf, "[%s] %s", op.u.getdomaininfo.name, buf);
94 log(obuf);
96 fromlen = sizeof(from);
97 }
98 }
100 void closeall(int fd)
101 {
102 int fdlimit = sysconf(_SC_OPEN_MAX);
104 while (fd < fdlimit)
105 close(fd++);
106 }
108 int daemon(int nochdir, int noclose)
109 {
110 switch (fork())
111 {
112 case 0: break;
113 case -1: return -1;
114 default: _exit(0);
115 }
117 if (setsid() < 0)
118 return -1;
120 switch (fork())
121 {
122 case 0: break;
123 case -1: return -1;
124 default: _exit(0);
125 }
127 if (!nochdir)
128 chdir("/");
130 if (!noclose)
131 {
132 closeall(0);
133 open("/dev/null",O_RDWR);
134 dup(0); dup(0);
135 }
137 return 0;
138 }
140 int main(int argc, char **argv)
141 {
142 int c;
144 logoutput = 0;
145 opterr = 0;
147 while ( (c = getopt (argc, argv, "dh")) != -1 )
148 {
149 switch(c)
150 {
151 case 'd':
152 logoutput = SYSLOG;
153 if (daemon(0,0) < 0)
154 {
155 errno = 2;
156 errexit("daemon");
157 } else {
158 openlog("xen_log", LOG_PID, SYSLOGTO);
159 }
160 break;
161 case 'h':
162 printf("Usage: xen_log [options]\n");
163 printf("Capture and display output of xen domains.\n\n");
164 printf(" -d Daemonize and send output to syslog.\n");
165 exit(0);
166 break;
167 }
168 }
170 process();
172 return 0;
173 }