ia64/xen-unstable

view tools/xcs/xcsdump.c @ 3966:0b9ff7354a95

bitkeeper revision 1.1236.1.47 (4225ff68LvKUt73k78dKjnXxsnQt7A)

Fix tools build. Clean up barrier defs.
Signed-off-by: Jerone Young <jyoung5@us.ibm.com>
Signed-off-by: Keir Fraser <keir.fraser@cl.cam.ac.uk>
author kaf24@scramble.cl.cam.ac.uk
date Wed Mar 02 18:01:12 2005 +0000 (2005-03-02)
parents d8cf61e99a52
children a6914c2c15cf 720d41954256
line source
1 /* xcsdump.c
2 *
3 * little tool to sniff control messages.
4 *
5 * Copyright (c) 2004, Andrew Warfield
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include <ctype.h>
16 #include <xc.h>
17 #include <xen/xen.h>
18 #include <xen/io/domain_controller.h>
19 #include "xcs_proto.h"
20 #include "xcs.h"
22 static int xcs_ctrl_fd = -1; /* connection to the xcs server. */
23 static int xcs_data_fd = -1; /* connection to the xcs server. */
25 int sock_connect(char *path)
26 {
27 struct sockaddr_un addr;
28 int ret, len, fd;
30 fd = socket(AF_UNIX, SOCK_STREAM, 0);
31 if (fd < 0)
32 {
33 printf("error creating xcs socket!\n");
34 return -1;
35 }
37 addr.sun_family = AF_UNIX;
38 strcpy(addr.sun_path, path);
39 len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
41 ret = connect(fd, (struct sockaddr *)&addr, len);
42 if (ret < 0)
43 {
44 printf("error connecting to xcs!\n");
45 return -1;
46 }
48 return fd;
49 }
51 void sock_disconnect(int *fd)
52 {
53 close(*fd);
54 *fd = -1;
55 }
57 void xcs_read(int fd, xcs_msg_t *msg)
58 {
59 int ret;
61 ret = read(fd, msg, sizeof(xcs_msg_t));
62 if (ret != sizeof(xcs_msg_t)) {
63 printf("read error\n");
64 exit(-1);
65 }
66 }
68 void xcs_send(int fd, xcs_msg_t *msg)
69 {
70 int ret;
72 ret = send(fd, msg, sizeof(xcs_msg_t), 0);
73 if (ret != sizeof(xcs_msg_t) )
74 {
75 printf("send error\n");
76 exit(-1);
77 }
78 }
81 int main(int argc, char* argv[])
82 {
83 int ret, i;
84 xcs_msg_t msg;
85 control_msg_t *cmsg;
86 int verbose = 0;
88 if (argc > 1)
89 if ((strlen(argv[1]) >=2) && (strncmp(argv[1], "-v", 2) == 0))
90 verbose = 1;
92 ret = sock_connect(XCS_SUN_PATH);
93 if (ret < 0)
94 {
95 printf("connect failed!\n");
96 exit(-1);
97 }
98 xcs_ctrl_fd = ret;
100 memset(&msg, 0, sizeof(msg));
101 msg.type = XCS_CONNECT_CTRL;
102 xcs_send(xcs_ctrl_fd, &msg);
103 xcs_read(xcs_ctrl_fd, &msg);
104 if (msg.result != XCS_RSLT_OK)
105 {
106 printf("Error connecting control channel\n");
107 exit(-1);
108 }
110 ret = sock_connect(XCS_SUN_PATH);
111 if (ret < 0)
112 {
113 printf("connect failed!\n");
114 exit(-1);
115 }
116 xcs_data_fd = ret;
118 msg.type = XCS_CONNECT_DATA;
119 /* session id is set from before... */
120 xcs_send(xcs_data_fd, &msg);
121 xcs_read(xcs_data_fd, &msg);
122 if (msg.result != XCS_RSLT_OK)
123 {
124 printf("Error connecting data channel\n");
125 exit(-1);
126 }
128 msg.type = XCS_MSG_BIND;
129 msg.u.bind.port = PORT_WILDCARD;
130 msg.u.bind.type = TYPE_WILDCARD;
131 xcs_send(xcs_ctrl_fd, &msg);
132 xcs_read(xcs_ctrl_fd, &msg);
133 if (msg.result != XCS_RSLT_OK)
134 {
135 printf("Error binding.\n");
136 exit(-1);
137 }
140 while (1)
141 {
142 xcs_read(xcs_data_fd, &msg);
143 cmsg = &msg.u.control.msg;
145 for (i=0; i<60; i++)
146 if ((!isprint(cmsg->msg[i])) && (cmsg->msg[i] != '\0'))
147 cmsg->msg[i] = '.';
148 cmsg->msg[59] = '\0';
150 switch (msg.type)
151 {
152 case XCS_REQUEST:
153 printf("[REQUEST ] : (dom:%u port:%d) (type:(%d,%d) len %d) \n",
154 msg.u.control.remote_dom,
155 msg.u.control.local_port,
156 msg.u.control.msg.type,
157 msg.u.control.msg.subtype,
158 msg.u.control.msg.length);
159 if (verbose)
160 printf(" : %s\n", msg.u.control.msg.msg);
161 break;
162 case XCS_RESPONSE:
163 printf("[RESPONSE] : (dom:%u port:%d) (type:(%d,%d) len %d) \n",
164 msg.u.control.remote_dom,
165 msg.u.control.local_port,
166 msg.u.control.msg.type,
167 msg.u.control.msg.subtype,
168 msg.u.control.msg.length);
169 if (verbose)
170 printf(" : %s\n", msg.u.control.msg.msg);
171 break;
172 case XCS_VIRQ:
173 printf("[VIRQ ] : %d\n", msg.u.control.local_port);
174 default:
175 printf("[UNKNOWN ]\n");
176 }
177 }
179 return(0);
180 }