Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / sbin / hammer2 / cmd_debug.c
1 /*
2  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "hammer2.h"
37
38 static void debug_recv(hammer2_iocom_t *iocom);
39 static void debug_send(hammer2_iocom_t *iocom);
40 static void debug_tty(hammer2_iocom_t *iocom);
41 static void hammer2_debug_parse(hammer2_msg_t *msg, char *cmdbuf);
42
43 int
44 cmd_debug(void)
45 {
46         struct sockaddr_in lsin;
47         struct hammer2_iocom iocom;
48         hammer2_msg_t *msg;
49         int fd;
50
51         /*
52          * Acquire socket and set options
53          */
54         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
55                 fprintf(stderr, "cmd_debug: socket(): %s\n",
56                         strerror(errno));
57                 return 1;
58         }
59
60         /*
61          * Connect to the target
62          */
63         bzero(&lsin, sizeof(lsin));
64         lsin.sin_family = AF_INET;
65         lsin.sin_addr.s_addr = 0;
66         lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
67         if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
68                 close(fd);
69                 fprintf(stderr, "debug: connect failed: %s\n",
70                         strerror(errno));
71                 return 0;
72         }
73
74         /*
75          * Run the session.  The remote end transmits our prompt.
76          */
77         hammer2_iocom_init(&iocom, fd, 0);
78         printf("debug: connected\n");
79
80         msg = hammer2_allocmsg(&iocom, HAMMER2_DBG_SHELL, 0);
81         hammer2_ioq_write(msg);
82
83         hammer2_iocom_core(&iocom, debug_recv, debug_send, debug_tty);
84         fprintf(stderr, "debug: disconnected\n");
85         close(fd);
86         return 0;
87 }
88
89 /*
90  * Callback from hammer2_iocom_core() when messages might be present
91  * on the socket.
92  */
93 static
94 void
95 debug_recv(hammer2_iocom_t *iocom)
96 {
97         hammer2_msg_t *msg;
98
99         while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
100                (msg = hammer2_ioq_read(iocom)) != NULL) {
101
102                 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
103                 case HAMMER2_LNK_ERROR:
104                         fprintf(stderr, "Link Error: %d\n",
105                                 msg->any.head.error);
106                         break;
107                 case HAMMER2_DBG_SHELL:
108                         /*
109                          * We send the commands, not accept them.
110                          */
111                         hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
112                         hammer2_freemsg(msg);
113                         break;
114                 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
115                         /*
116                          * A reply from the remote is data we copy to stdout.
117                          */
118                         if (msg->aux_size) {
119                                 msg->aux_data[msg->aux_size - 1] = 0;
120                                 write(1, msg->aux_data, strlen(msg->aux_data));
121                         } else {
122                                 write(1, "debug> ", 7);
123                         }
124                         hammer2_freemsg(msg);
125                         break;
126                 default:
127                         assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
128                         fprintf(stderr, "Unknown message: %08x\n",
129                                 msg->any.head.cmd);
130                         hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
131                         break;
132                 }
133         }
134         if (iocom->ioq_rx.error) {
135                 fprintf(stderr, "node_master_recv: comm error %d\n",
136                         iocom->ioq_rx.error);
137         }
138 }
139
140 /*
141  * Callback from hammer2_iocom_core() when messages might be transmittable
142  * to the socket.
143  */
144 static
145 void
146 debug_send(hammer2_iocom_t *iocom)
147 {
148         hammer2_iocom_flush(iocom);
149 }
150
151 static
152 void
153 debug_tty(hammer2_iocom_t *iocom)
154 {
155         hammer2_msg_t *msg;
156         char buf[256];
157         size_t len;
158
159         if (fgets(buf, sizeof(buf), stdin) != NULL) {
160                 len = strlen(buf);
161                 if (len && buf[len - 1] == '\n')
162                         buf[--len] = 0;
163                 ++len;
164                 msg = hammer2_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
165                 bcopy(buf, msg->aux_data, len);
166                 hammer2_ioq_write(msg);
167         } else {
168                 /*
169                  * Set EOF flag without setting any error code for normal
170                  * EOF.
171                  */
172                 iocom->flags |= HAMMER2_IOCOMF_EOF;
173         }
174 }
175
176 /*
177  * This is called from the master node to process a received debug
178  * shell command.  We process the command, outputting the results,
179  * then finish up by outputting another prompt.
180  */
181 void
182 hammer2_debug_remote(hammer2_msg_t *msg)
183 {
184         /* hammer2_iocom_t *iocom = msg->iocom; */
185
186         if (msg->aux_data)
187                 msg->aux_data[msg->aux_size - 1] = 0;
188         if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
189                 /*
190                  * A reply just prints out the string.  No newline is added
191                  * (it is expected to be embedded if desired).
192                  */
193                 if (msg->aux_data)
194                         write(2, msg->aux_data, strlen(msg->aux_data));
195                 hammer2_freemsg(msg);
196         } else {
197                 /*
198                  * Otherwise this is a command which we must process.
199                  * When we are finished we generate a final reply.
200                  */
201                 hammer2_debug_parse(msg, msg->aux_data);
202                 hammer2_replymsg(msg, 0);
203         }
204 }
205
206 static void
207 hammer2_debug_parse(hammer2_msg_t *msg, char *cmdbuf)
208 {
209         /* hammer2_iocom_t *iocom = msg->iocom; */
210         char *cmd = strsep(&cmdbuf, " \t");
211
212         if (cmd == NULL || *cmd == 0) {
213                 ;
214         } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
215                 msg_printf(msg, "help        Command help\n");
216         } else {
217                 msg_printf(msg, "Unrecognized command: %s\n", cmd);
218         }
219 }
220
221 /*
222  * Returns text debug output to the original defined by (msg).  (msg) is
223  * not modified and stays intact.
224  */
225 void
226 msg_printf(hammer2_msg_t *msg, const char *ctl, ...)
227 {
228         /* hammer2_iocom_t *iocom = msg->iocom; */
229         hammer2_msg_t *rmsg;
230         va_list va;
231         char buf[1024];
232         size_t len;
233
234         va_start(va, ctl);
235         vsnprintf(buf, sizeof(buf), ctl, va);
236         va_end(va);
237         len = strlen(buf) + 1;
238
239         rmsg = hammer2_allocreply(msg, HAMMER2_DBG_SHELL, len);
240         bcopy(buf, rmsg->aux_data, len);
241
242         hammer2_ioq_write(rmsg);
243 }