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