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
40 static void shell_recv(hammer2_iocom_t *iocom);
41 static void shell_send(hammer2_iocom_t *iocom);
42 static void shell_tty(hammer2_iocom_t *iocom);
43 static void hammer2_shell_parse(hammer2_msg_t *msg, char *cmdbuf);
45 /************************************************************************
47 ************************************************************************/
50 cmd_shell(const char *hostname)
52 struct sockaddr_in lsin;
53 struct hammer2_iocom iocom;
59 * Acquire socket and set options
61 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
62 fprintf(stderr, "cmd_debug: socket(): %s\n",
68 * Connect to the target
70 bzero(&lsin, sizeof(lsin));
71 lsin.sin_family = AF_INET;
72 lsin.sin_addr.s_addr = 0;
73 lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
76 hen = gethostbyname2(hostname, AF_INET);
78 if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
80 "Cannot resolve %s\n", hostname);
84 bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
87 if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
89 fprintf(stderr, "debug: connect failed: %s\n",
95 * Run the session. The remote end transmits our prompt.
97 hammer2_iocom_init(&iocom, fd, 0);
98 printf("debug: connected\n");
100 msg = hammer2_allocmsg(&iocom, HAMMER2_DBG_SHELL, 0);
101 hammer2_ioq_write(msg);
103 hammer2_iocom_core(&iocom, shell_recv, shell_send, shell_tty);
104 fprintf(stderr, "debug: disconnected\n");
110 * Callback from hammer2_iocom_core() when messages might be present
115 shell_recv(hammer2_iocom_t *iocom)
119 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
120 (msg = hammer2_ioq_read(iocom)) != NULL) {
122 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
123 case HAMMER2_LNK_ERROR:
124 fprintf(stderr, "Link Error: %d\n",
125 msg->any.head.error);
127 case HAMMER2_DBG_SHELL:
129 * We send the commands, not accept them.
131 hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
132 hammer2_freemsg(msg);
134 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
136 * A reply from the remote is data we copy to stdout.
139 msg->aux_data[msg->aux_size - 1] = 0;
140 write(1, msg->aux_data, strlen(msg->aux_data));
142 write(1, "debug> ", 7);
144 hammer2_freemsg(msg);
147 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
148 fprintf(stderr, "Unknown message: %08x\n",
150 hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
154 if (iocom->ioq_rx.error) {
155 fprintf(stderr, "node_master_recv: comm error %d\n",
156 iocom->ioq_rx.error);
161 * Callback from hammer2_iocom_core() when messages might be transmittable
166 shell_send(hammer2_iocom_t *iocom)
168 hammer2_iocom_flush(iocom);
173 shell_tty(hammer2_iocom_t *iocom)
179 if (fgets(buf, sizeof(buf), stdin) != NULL) {
181 if (len && buf[len - 1] == '\n')
184 msg = hammer2_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
185 bcopy(buf, msg->aux_data, len);
186 hammer2_ioq_write(msg);
189 * Set EOF flag without setting any error code for normal
192 iocom->flags |= HAMMER2_IOCOMF_EOF;
197 * This is called from the master node to process a received debug
198 * shell command. We process the command, outputting the results,
199 * then finish up by outputting another prompt.
202 hammer2_shell_remote(hammer2_msg_t *msg)
204 /* hammer2_iocom_t *iocom = msg->iocom; */
207 msg->aux_data[msg->aux_size - 1] = 0;
208 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
210 * A reply just prints out the string. No newline is added
211 * (it is expected to be embedded if desired).
214 write(2, msg->aux_data, strlen(msg->aux_data));
215 hammer2_freemsg(msg);
218 * Otherwise this is a command which we must process.
219 * When we are finished we generate a final reply.
221 hammer2_shell_parse(msg, msg->aux_data);
222 hammer2_replymsg(msg, 0);
227 hammer2_shell_parse(hammer2_msg_t *msg, char *cmdbuf)
229 /* hammer2_iocom_t *iocom = msg->iocom; */
230 char *cmd = strsep(&cmdbuf, " \t");
232 if (cmd == NULL || *cmd == 0) {
234 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
235 msg_printf(msg, "help Command help\n");
237 msg_printf(msg, "Unrecognized command: %s\n", cmd);
242 * Returns text debug output to the original defined by (msg). (msg) is
243 * not modified and stays intact.
246 msg_printf(hammer2_msg_t *msg, const char *ctl, ...)
248 /* hammer2_iocom_t *iocom = msg->iocom; */
255 vsnprintf(buf, sizeof(buf), ctl, va);
257 len = strlen(buf) + 1;
259 rmsg = hammer2_allocreply(msg, HAMMER2_DBG_SHELL, len);
260 bcopy(buf, rmsg->aux_data, len);
262 hammer2_ioq_write(rmsg);
265 /************************************************************************
267 ************************************************************************/
269 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
270 static void tabprintf(int tab, const char *ctl, ...);
273 cmd_show(const char *devpath)
275 hammer2_blockref_t broot;
278 fd = open(devpath, O_RDONLY);
283 bzero(&broot, sizeof(broot));
284 broot.type = HAMMER2_BREF_TYPE_VOLUME;
285 broot.data_off = 0 | HAMMER2_PBUFRADIX;
286 show_bref(fd, 0, 0, &broot);
293 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
295 hammer2_media_data_t media;
296 hammer2_blockref_t *bscan;
302 const char *type_str;
306 case HAMMER2_BREF_TYPE_EMPTY:
309 case HAMMER2_BREF_TYPE_INODE:
312 case HAMMER2_BREF_TYPE_INDIRECT:
315 case HAMMER2_BREF_TYPE_DATA:
318 case HAMMER2_BREF_TYPE_VOLUME:
322 type_str = "unknown";
327 tabprintf(tab, "%s.%-3d %016jx/%-2d mir=%016jx mod=%016jx ",
329 bref->key, bref->keybits,
330 bref->mirror_tid, bref->modify_tid);
333 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
334 if (bytes > sizeof(media)) {
335 printf("(bad block size %zd)\n", bytes);
338 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
339 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
340 if (read(fd, &media, bytes) != (ssize_t)bytes) {
341 printf("(media read failed)\n");
351 case HAMMER2_BREF_TYPE_EMPTY:
354 case HAMMER2_BREF_TYPE_INODE:
356 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
359 bscan = &media.ipdata.u.blockset.blockref[0];
360 bcount = HAMMER2_SET_COUNT;
362 tabprintf(tab, "filename \"%s\"\n", media.ipdata.filename);
363 tabprintf(tab, "version %d\n", media.ipdata.version);
364 tabprintf(tab, "uflags 0x%08x\n",
365 media.ipdata.uflags);
366 if (media.ipdata.rmajor || media.ipdata.rminor) {
367 tabprintf(tab, "rmajor %d\n",
368 media.ipdata.rmajor);
369 tabprintf(tab, "rminor %d\n",
370 media.ipdata.rminor);
372 tabprintf(tab, "ctime %s\n",
373 hammer2_time64_to_str(media.ipdata.ctime, &str));
374 tabprintf(tab, "mtime %s\n",
375 hammer2_time64_to_str(media.ipdata.mtime, &str));
376 tabprintf(tab, "atime %s\n",
377 hammer2_time64_to_str(media.ipdata.atime, &str));
378 tabprintf(tab, "btime %s\n",
379 hammer2_time64_to_str(media.ipdata.btime, &str));
380 tabprintf(tab, "uid %s\n",
381 hammer2_uuid_to_str(&media.ipdata.uid, &str));
382 tabprintf(tab, "gid %s\n",
383 hammer2_uuid_to_str(&media.ipdata.gid, &str));
384 tabprintf(tab, "type %s\n",
385 hammer2_iptype_to_str(media.ipdata.type));
386 tabprintf(tab, "opflgs 0x%02x\n",
387 media.ipdata.op_flags);
388 tabprintf(tab, "capflgs 0x%04x\n",
389 media.ipdata.cap_flags);
390 tabprintf(tab, "mode %-7o\n",
392 tabprintf(tab, "inum 0x%016jx\n",
394 tabprintf(tab, "size %ju\n",
395 (uintmax_t)media.ipdata.size);
396 tabprintf(tab, "nlinks %ju\n",
397 (uintmax_t)media.ipdata.nlinks);
398 tabprintf(tab, "iparent 0x%016jx\n",
399 (uintmax_t)media.ipdata.iparent);
400 tabprintf(tab, "name_key 0x%016jx\n",
401 (uintmax_t)media.ipdata.name_key);
402 tabprintf(tab, "name_len %u\n",
403 media.ipdata.name_len);
404 tabprintf(tab, "ncopies %u\n",
405 media.ipdata.ncopies);
406 tabprintf(tab, "compalg %u\n",
407 media.ipdata.comp_algo);
408 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
409 tabprintf(tab, "pfs_type %u (%s)\n",
410 media.ipdata.pfs_type,
411 hammer2_pfstype_to_str(media.ipdata.pfs_type));
412 tabprintf(tab, "pfs_inum 0x%016jx\n",
413 (uintmax_t)media.ipdata.pfs_inum);
414 tabprintf(tab, "pfs_id %s\n",
415 hammer2_uuid_to_str(&media.ipdata.pfs_id,
417 tabprintf(tab, "pfs_fsid %s\n",
418 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
421 tabprintf(tab, "data_quota %ju\n",
422 (uintmax_t)media.ipdata.data_quota);
423 tabprintf(tab, "data_count %ju\n",
424 (uintmax_t)media.ipdata.data_count);
425 tabprintf(tab, "inode_quota %ju\n",
426 (uintmax_t)media.ipdata.inode_quota);
427 tabprintf(tab, "inode_count %ju\n",
428 (uintmax_t)media.ipdata.inode_count);
429 tabprintf(tab, "attr_tid 0x%016jx\n",
430 (uintmax_t)media.ipdata.attr_tid);
431 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
432 tabprintf(tab, "dirent_tid %016jx\n",
433 (uintmax_t)media.ipdata.dirent_tid);
436 case HAMMER2_BREF_TYPE_INDIRECT:
437 bscan = &media.npdata.blockref[0];
438 bcount = bytes / sizeof(hammer2_blockref_t);
442 case HAMMER2_BREF_TYPE_DATA:
443 if (VerboseOpt >= 2) {
450 case HAMMER2_BREF_TYPE_VOLUME:
451 bscan = &media.voldata.sroot_blockset.blockref[0];
452 bcount = HAMMER2_SET_COUNT;
460 for (i = 0; i < bcount; ++i) {
461 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
466 show_bref(fd, tab, i, &bscan[i]);
471 if (bref->type == HAMMER2_BREF_TYPE_INODE)
472 tabprintf(tab, "} (%s.%d, \"%s\")\n",
473 type_str, bi, media.ipdata.filename);
475 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
481 tabprintf(int tab, const char *ctl, ...)
485 printf("%*.*s", tab, tab, "");