list.c revision 141575
1
2/*
3 * list.c
4 *
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 *    copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 *    Communications, Inc. trademarks, including the mark "WHISTLE
16 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 *    such appears in the above copyright notice or in the software.
18 *
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35 * OF SUCH DAMAGE.
36 *
37 * $FreeBSD: head/usr.sbin/ngctl/list.c 141575 2005-02-09 16:17:33Z ru $
38 */
39
40#include "ngctl.h"
41
42#define UNNAMED		"<unnamed>"
43
44static int ListCmd(int ac, char **av);
45
46const struct ngcmd list_cmd = {
47	ListCmd,
48	"list [-ln]",
49	"Show information about all nodes",
50	"The list command shows information about every node that currently"
51	" exists in the netgraph system. The optional -n argument limits"
52	" this list to only those nodes with a global name assignment."
53	" The optional -l argument provides verbose output that includes"
54	" hook information as well.",
55	{ "ls" }
56};
57
58static int
59ListCmd(int ac, char **av)
60{
61	struct ng_mesg *resp;
62	struct namelist *nlist;
63	struct nodeinfo *ninfo;
64	int list_hooks = 0;
65	int named_only = 0;
66	int ch, rtn = CMDRTN_OK;
67
68	/* Get options */
69	optind = 1;
70	while ((ch = getopt(ac, av, "ln")) != EOF) {
71		switch (ch) {
72		case 'l':
73			list_hooks = 1;
74			break;
75		case 'n':
76			named_only = 1;
77			break;
78		case '?':
79		default:
80			return(CMDRTN_USAGE);
81			break;
82		}
83	}
84	ac -= optind;
85	av += optind;
86
87	/* Get arguments */
88	switch (ac) {
89	case 0:
90		break;
91	default:
92		return(CMDRTN_USAGE);
93	}
94
95	/* Get list of nodes */
96	if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE,
97	    named_only ? NGM_LISTNAMES : NGM_LISTNODES, NULL, 0) < 0) {
98		warn("send msg");
99		return(CMDRTN_ERROR);
100	}
101	if (NgAllocRecvMsg(csock, &resp, NULL) < 0) {
102		warn("recv msg");
103		return(CMDRTN_ERROR);
104	}
105
106	/* Show each node */
107	nlist = (struct namelist *) resp->data;
108	printf("There are %d total %snodes:\n",
109	    nlist->numnames, named_only ? "named " : "");
110	ninfo = nlist->nodeinfo;
111	if (list_hooks) {
112		char	path[NG_PATHSIZ];
113		char	*argv[2] = { "show", path };
114
115		while (nlist->numnames > 0) {
116			snprintf(path, sizeof(path),
117			    "[%lx]:", (u_long)ninfo->id);
118			if ((rtn = (*show_cmd.func)(2, argv)) != CMDRTN_OK)
119				break;
120			ninfo++;
121			nlist->numnames--;
122		}
123	} else {
124		while (nlist->numnames > 0) {
125			if (!*ninfo->name)
126				snprintf(ninfo->name, sizeof(ninfo->name),
127				    "%s", UNNAMED);
128			printf("  Name: %-15s Type: %-15s ID: %08x   "
129			    "Num hooks: %d\n",
130			    ninfo->name, ninfo->type, ninfo->id, ninfo->hooks);
131			ninfo++;
132			nlist->numnames--;
133		}
134	}
135
136	/* Done */
137	free(resp);
138	return (rtn);
139}
140
141