xref: /dragonfly/usr.sbin/mpsutil/mpsutil.c (revision 7b1120e5)
1 /*-
2  * Copyright (c) 2015 Netflix, Inc.
3  * All rights reserved.
4  * Written by: Scott Long <scottl@freebsd.org>
5  *
6  * Copyright (c) 2008 Yahoo!, Inc.
7  * All rights reserved.
8  * Written by: John Baldwin <jhb@FreeBSD.org>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __RCSID("$FreeBSD: head/usr.sbin/mpsutil/mpsutil.c 330790 2018-03-12 05:03:32Z scottl $");
37 
38 #include <sys/param.h>
39 #include <sys/errno.h>
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include "mpsutil.h"
46 
47 SET_DECLARE(MPS_DATASET(top), struct mpsutil_command);
48 SET_DECLARE(MPS_DATASET(usage), struct mpsutil_usage);
49 
50 int mps_unit;
51 int is_mps;
52 
53 static void
54 usage(void)
55 {
56 	struct mpsutil_usage **cmd;
57 	const char *args, *desc;
58 
59 	fprintf(stderr, "usage: %s [-u unit] <command> ...\n\n", getprogname());
60 	fprintf(stderr, "Commands include:\n");
61 	SET_FOREACH(cmd, MPS_DATASET(usage)) {
62 		if (*cmd == NULL) {
63 			fprintf(stderr, "\n");
64 		} else {
65 			(*cmd)->handler(&args, &desc);
66 			if (strncmp((*cmd)->set, "top", 3) == 0)
67 				fprintf(stderr, "%s %-30s\t%s\n",
68 				    (*cmd)->name, args, desc);
69 			else
70 				fprintf(stderr, "%s %s %-30s\t%s\n",
71 				    (*cmd)->set, (*cmd)->name, args, desc);
72 		}
73 	}
74 	exit(1);
75 }
76 
77 static int
78 version(int ac, char **av)
79 {
80 
81 	printf("%s: version %s", MPSUTIL_VERSION, getprogname());
82 #ifdef DEBUG
83 	printf(" (DEBUG)");
84 #endif
85 	printf("\n");
86 	return (0);
87 }
88 
89 MPS_COMMAND(top, version, version, "", "version")
90 
91 int
92 main(int ac, char **av)
93 {
94 	struct mpsutil_command **cmd;
95 	int ch;
96 
97 	is_mps = !strcmp(getprogname(), "mpsutil");
98 
99 	while ((ch = getopt(ac, av, "u:h?")) != -1) {
100 		switch (ch) {
101 		case 'u':
102 			mps_unit = atoi(optarg);
103 			break;
104 		case 'h':
105 		case '?':
106 			usage();
107 			return (1);
108 		}
109 	}
110 
111 	av += optind;
112 	ac -= optind;
113 
114 	/* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
115 	if (ac == 0) {
116 		usage();
117 		return (1);
118 	}
119 
120 	SET_FOREACH(cmd, MPS_DATASET(top)) {
121 		if (strcmp((*cmd)->name, av[0]) == 0) {
122 			if ((*cmd)->handler(ac, av))
123 				return (1);
124 			else
125 				return (0);
126 		}
127 	}
128 	warnx("Unknown command %s.", av[0]);
129 	return (1);
130 }
131 
132 int
133 mps_table_handler(struct mpsutil_command **start, struct mpsutil_command **end,
134     int ac, char **av)
135 {
136 	struct mpsutil_command **cmd;
137 
138 	if (ac < 2) {
139 		warnx("The %s command requires a sub-command.", av[0]);
140 		return (EINVAL);
141 	}
142 	for (cmd = start; cmd < end; cmd++) {
143 		if (strcmp((*cmd)->name, av[1]) == 0)
144 			return ((*cmd)->handler(ac - 1, av + 1));
145 	}
146 
147 	warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
148 	return (ENOENT);
149 }
150 
151 void
152 hexdump(const void *ptr, int length, const char *hdr, int flags)
153 {
154 	int i, j, k;
155 	int cols;
156 	const unsigned char *cp;
157 	char delim;
158 
159 	if ((flags & HD_DELIM_MASK) != 0)
160 		delim = (flags & HD_DELIM_MASK) >> 8;
161 	else
162 		delim = ' ';
163 
164 	if ((flags & HD_COLUMN_MASK) != 0)
165 		cols = flags & HD_COLUMN_MASK;
166 	else
167 		cols = 16;
168 
169 	cp = ptr;
170 	for (i = 0; i < length; i+= cols) {
171 		if (hdr != NULL)
172 			printf("%s", hdr);
173 
174 		if ((flags & HD_OMIT_COUNT) == 0)
175 			printf("%04x  ", i);
176 
177 		if ((flags & HD_OMIT_HEX) == 0) {
178 			for (j = 0; j < cols; j++) {
179 				if (flags & HD_REVERSED)
180 					k = i + (cols - 1 - j);
181 				else
182 					k = i + j;
183 				if (k < length)
184 					printf("%c%02x", delim, cp[k]);
185 				else
186 					printf("   ");
187 			}
188 		}
189 
190 		if ((flags & HD_OMIT_CHARS) == 0) {
191 			printf("  |");
192 			for (j = 0; j < cols; j++) {
193 				if (flags & HD_REVERSED)
194 					k = i + (cols - 1 - j);
195 				else
196 					k = i + j;
197 				if (k >= length)
198 					printf(" ");
199 				else if (cp[k] >= ' ' && cp[k] <= '~')
200 					printf("%c", cp[k]);
201 				else
202 					printf(".");
203 			}
204 			printf("|");
205 		}
206 		printf("\n");
207 	}
208 }
209 
210 #define PCHAR(c) { if (retval < tmpsz) { *outbuf++ = (c); retval++; } }
211 
212 int
213 mps_parse_flags(uintmax_t num, const char *q, char *outbuf, int tmpsz)
214 {
215 	int n, tmp, retval = 0;
216 
217 	if (num == 0)
218 		return (retval);
219 
220 	/* %b conversion flag format. */
221 	tmp = retval;
222 	while (*q) {
223 		n = *q++;
224 		if (num & (1 << (n - 1))) {
225 			PCHAR(retval != tmp ?  ',' : '<');
226 			for (; (n = *q) > ' '; ++q)
227 				PCHAR(n);
228 		} else
229 			for (; *q > ' '; ++q)
230 				continue;
231 	}
232 	if (retval != tmp)
233 		PCHAR('>');
234 
235 	return (retval);
236 }
237 
238