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