2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
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);
44 cmd_debug(const char *hostname)
46 struct sockaddr_in lsin;
47 struct hammer2_iocom iocom;
53 * Acquire socket and set options
55 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
56 fprintf(stderr, "cmd_debug: socket(): %s\n",
62 * Connect to the target
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);
70 hen = gethostbyname2(hostname, AF_INET);
72 if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
74 "Cannot resolve %s\n", hostname);
78 bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
81 if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
83 fprintf(stderr, "debug: connect failed: %s\n",
89 * Run the session. The remote end transmits our prompt.
91 hammer2_iocom_init(&iocom, fd, 0);
92 printf("debug: connected\n");
94 msg = hammer2_allocmsg(&iocom, HAMMER2_DBG_SHELL, 0);
95 hammer2_ioq_write(msg);
97 hammer2_iocom_core(&iocom, debug_recv, debug_send, debug_tty);
98 fprintf(stderr, "debug: disconnected\n");
104 * Callback from hammer2_iocom_core() when messages might be present
109 debug_recv(hammer2_iocom_t *iocom)
113 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
114 (msg = hammer2_ioq_read(iocom)) != NULL) {
116 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
117 case HAMMER2_LNK_ERROR:
118 fprintf(stderr, "Link Error: %d\n",
119 msg->any.head.error);
121 case HAMMER2_DBG_SHELL:
123 * We send the commands, not accept them.
125 hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
126 hammer2_freemsg(msg);
128 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
130 * A reply from the remote is data we copy to stdout.
133 msg->aux_data[msg->aux_size - 1] = 0;
134 write(1, msg->aux_data, strlen(msg->aux_data));
136 write(1, "debug> ", 7);
138 hammer2_freemsg(msg);
141 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
142 fprintf(stderr, "Unknown message: %08x\n",
144 hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
148 if (iocom->ioq_rx.error) {
149 fprintf(stderr, "node_master_recv: comm error %d\n",
150 iocom->ioq_rx.error);
155 * Callback from hammer2_iocom_core() when messages might be transmittable
160 debug_send(hammer2_iocom_t *iocom)
162 hammer2_iocom_flush(iocom);
167 debug_tty(hammer2_iocom_t *iocom)
173 if (fgets(buf, sizeof(buf), stdin) != NULL) {
175 if (len && buf[len - 1] == '\n')
178 msg = hammer2_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
179 bcopy(buf, msg->aux_data, len);
180 hammer2_ioq_write(msg);
183 * Set EOF flag without setting any error code for normal
186 iocom->flags |= HAMMER2_IOCOMF_EOF;
191 * This is called from the master node to process a received debug
192 * shell command. We process the command, outputting the results,
193 * then finish up by outputting another prompt.
196 hammer2_debug_remote(hammer2_msg_t *msg)
198 /* hammer2_iocom_t *iocom = msg->iocom; */
201 msg->aux_data[msg->aux_size - 1] = 0;
202 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
204 * A reply just prints out the string. No newline is added
205 * (it is expected to be embedded if desired).
208 write(2, msg->aux_data, strlen(msg->aux_data));
209 hammer2_freemsg(msg);
212 * Otherwise this is a command which we must process.
213 * When we are finished we generate a final reply.
215 hammer2_debug_parse(msg, msg->aux_data);
216 hammer2_replymsg(msg, 0);
221 hammer2_debug_parse(hammer2_msg_t *msg, char *cmdbuf)
223 /* hammer2_iocom_t *iocom = msg->iocom; */
224 char *cmd = strsep(&cmdbuf, " \t");
226 if (cmd == NULL || *cmd == 0) {
228 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
229 msg_printf(msg, "help Command help\n");
231 msg_printf(msg, "Unrecognized command: %s\n", cmd);
236 * Returns text debug output to the original defined by (msg). (msg) is
237 * not modified and stays intact.
240 msg_printf(hammer2_msg_t *msg, const char *ctl, ...)
242 /* hammer2_iocom_t *iocom = msg->iocom; */
249 vsnprintf(buf, sizeof(buf), ctl, va);
251 len = strlen(buf) + 1;
253 rmsg = hammer2_allocreply(msg, HAMMER2_DBG_SHELL, len);
254 bcopy(buf, rmsg->aux_data, len);
256 hammer2_ioq_write(rmsg);