hammer2 - Early messaging infrastructure
[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_iocom_t *iocom,
42                                 hammer2_msg_t *msg, char *cmdbuf);
43
44 int
45 cmd_debug(void)
46 {
47         struct sockaddr_in lsin;
48         struct hammer2_iocom iocom;
49         hammer2_msg_t *msg;
50         int fd;
51
52         /*
53          * Acquire socket and set options
54          */
55         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
56                 fprintf(stderr, "cmd_debug: socket(): %s\n",
57                         strerror(errno));
58                 return 1;
59         }
60
61         /*
62          * Connect to the target
63          */
64         bzero(&lsin, sizeof(lsin));
65         lsin.sin_family = AF_INET;
66         lsin.sin_addr.s_addr = 0;
67         lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
68         if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
69                 close(fd);
70                 fprintf(stderr, "debug: connect failed: %s\n",
71                         strerror(errno));
72                 return 0;
73         }
74
75         /*
76          * Run the session.  The remote end transmits our prompt.
77          */
78         hammer2_iocom_init(&iocom, fd, 0);
79         printf("debug: connected\n");
80
81         msg = hammer2_iocom_allocmsg(&iocom, HAMMER2_DBG_SHELL, 0);
82         hammer2_ioq_write(&iocom, msg);
83
84         hammer2_iocom_core(&iocom, debug_recv, debug_send, debug_tty);
85         fprintf(stderr, "debug: disconnected\n");
86         close(fd);
87         return 0;
88 }
89
90 /*
91  * Callback from hammer2_iocom_core() when messages might be present
92  * on the socket.
93  */
94 static
95 void
96 debug_recv(hammer2_iocom_t *iocom)
97 {
98         hammer2_msg_t *msg;
99
100         while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
101                (msg = hammer2_ioq_read(iocom)) != NULL) {
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_iocom_freemsg(iocom, msg);
112                         break;
113                 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
114                         /*
115                          * A reply from the remote is data we copy to stdout.
116                          */
117                         if (msg->aux_size) {
118                                 msg->aux_data[msg->aux_size - 1] = 0;
119                                 write(1, msg->aux_data, strlen(msg->aux_data));
120                         }
121                         hammer2_iocom_freemsg(iocom, msg);
122                         break;
123                 default:
124                         assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
125                         fprintf(stderr, "Unknown message: %08x\n",
126                                 msg->any.head.cmd);
127                         hammer2_ioq_reply_term(iocom, msg,
128                                                HAMMER2_MSG_ERR_UNKNOWN);
129                         break;
130                 }
131         }
132         if (iocom->ioq_rx.error) {
133                 fprintf(stderr, "node_master_recv: comm error %d\n",
134                         iocom->ioq_rx.error);
135         }
136 }
137
138 /*
139  * Callback from hammer2_iocom_core() when messages might be transmittable
140  * to the socket.
141  */
142 static
143 void
144 debug_send(hammer2_iocom_t *iocom)
145 {
146         hammer2_ioq_write(iocom, NULL);
147 }
148
149 static
150 void
151 debug_tty(hammer2_iocom_t *iocom)
152 {
153         hammer2_msg_t *msg;
154         char buf[256];
155         size_t len;
156
157         if (fgets(buf, sizeof(buf), stdin) != NULL) {
158                 len = strlen(buf);
159                 if (len && buf[len - 1] == '\n')
160                         buf[--len] = 0;
161                 ++len;
162                 msg = hammer2_iocom_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
163                 bcopy(buf, msg->aux_data, len);
164                 hammer2_ioq_write(iocom, msg);
165         } else {
166                 /*
167                  * Set EOF flag without setting any error code for normal
168                  * EOF.
169                  */
170                 iocom->flags |= HAMMER2_IOCOMF_EOF;
171         }
172 }
173
174 /*
175  * This is called from the master node to process a received debug
176  * shell command.  We process the command, outputting the results,
177  * then finish up by outputting another prompt.
178  */
179 void
180 hammer2_debug_remote(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
181 {
182         if (msg->aux_data)
183                 msg->aux_data[msg->aux_size - 1] = 0;
184         if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
185                 /*
186                  * A reply just prints out the string.  No newline is added
187                  * (it is expected to be embedded if desired).
188                  */
189                 if (msg->aux_data)
190                         write(2, msg->aux_data, strlen(msg->aux_data));
191                 hammer2_iocom_freemsg(iocom, msg);
192         } else {
193                 /*
194                  * Otherwise this is a command which we must process.
195                  * When we are finished we generate a final reply.
196                  */
197                 hammer2_debug_parse(iocom, msg, msg->aux_data);
198                 iocom_printf(iocom, msg, "debug> ");
199                 hammer2_iocom_freemsg(iocom, msg);
200         }
201 }
202
203 static void
204 hammer2_debug_parse(hammer2_iocom_t *iocom, hammer2_msg_t *msg, char *cmdbuf)
205 {
206         char *cmd = strsep(&cmdbuf, " \t");
207
208         if (cmd == NULL || *cmd == 0) {
209                 ;
210         } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
211                 iocom_printf(iocom, msg,
212                              "help        Command help\n"
213                 );
214         } else {
215                 iocom_printf(iocom, msg, "Unrecognized command: %s\n", cmd);
216         }
217 }
218
219 void
220 iocom_printf(hammer2_iocom_t *iocom, hammer2_msg_t *msg, const char *ctl, ...)
221 {
222         hammer2_msg_t *rmsg;
223         va_list va;
224         char buf[1024];
225         size_t len;
226
227         va_start(va, ctl);
228         vsnprintf(buf, sizeof(buf), ctl, va);
229         va_end(va);
230         len = strlen(buf) + 1;
231
232         rmsg = hammer2_iocom_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
233         bcopy(buf, rmsg->aux_data, len);
234         rmsg->any.head = msg->any.head;
235         rmsg->any.head.aux_icrc = 0;
236
237         hammer2_ioq_reply(iocom, rmsg);
238 }