xref: /freebsd/usr.sbin/mpsutil/mpsutil.c (revision d37eb51047221dc3322b34db1038ff3aa533883f)
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/param.h>
35 #include <sys/errno.h>
36 #include <err.h>
37 #include <inttypes.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "mpsutil.h"
44 
45 SET_DECLARE(MPS_DATASET(top), struct mpsutil_command);
46 SET_DECLARE(MPS_DATASET(usage), struct mpsutil_usage);
47 
48 int mps_unit;
49 int is_mps;
50 
51 static void
52 usage(void)
53 {
54 	struct mpsutil_usage **cmd;
55 	const char *args, *desc;
56 
57 	fprintf(stderr, "usage: %s [-u unit] <command> ...\n\n", getprogname());
58 	fprintf(stderr, "Commands include:\n");
59 	SET_FOREACH(cmd, MPS_DATASET(usage)) {
60 		if (*cmd == NULL) {
61 			fprintf(stderr, "\n");
62 		} else {
63 			(*cmd)->handler(&args, &desc);
64 			if (strncmp((*cmd)->set, "top", 3) == 0)
65 				fprintf(stderr, "%-16s %-28s%s\n",
66 				    (*cmd)->name, args, desc);
67 			else
68 				fprintf(stderr, "%-5s %-10s %-28s%s\n",
69 				    (*cmd)->set, (*cmd)->name, args, desc);
70 		}
71 	}
72 	exit(1);
73 }
74 
75 static int
76 version(int ac, char **av)
77 {
78 
79 	printf("%s: version %s", MPSUTIL_VERSION, getprogname());
80 #ifdef DEBUG
81 	printf(" (DEBUG)");
82 #endif
83 	printf("\n");
84 	return (0);
85 }
86 
87 MPS_COMMAND(top, version, version, "", "Version number")
88 
89 int
90 main(int ac, char **av)
91 {
92 	struct mpsutil_command **cmd;
93 	uintmax_t unit;
94 	char *end;
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 			if (strncmp(optarg, _PATH_DEV, strlen(_PATH_DEV)) == 0) {
103 				optarg += strlen(_PATH_DEV);
104 				if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) != 0)
105 					errx(1, "Invalid device: %s", optarg);
106 			}
107 			if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) == 0)
108 				optarg += 3;
109 			unit = strtoumax(optarg, &end, 10);
110 			if (*end != '\0' || unit == UINTMAX_MAX || unit > INT_MAX)
111 				errx(1, "Invalid unit: %s", optarg);
112 			mps_unit = unit;
113 			break;
114 		case 'h':
115 		case '?':
116 			usage();
117 			return (1);
118 		}
119 	}
120 
121 	av += optind;
122 	ac -= optind;
123 
124 	/* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
125 	if (ac == 0) {
126 		usage();
127 		return (1);
128 	}
129 
130 	SET_FOREACH(cmd, MPS_DATASET(top)) {
131 		if (strcmp((*cmd)->name, av[0]) == 0) {
132 			if ((*cmd)->handler(ac, av))
133 				return (1);
134 			else
135 				return (0);
136 		}
137 	}
138 	warnx("Unknown command %s.", av[0]);
139 	return (1);
140 }
141 
142 int
143 mps_table_handler(struct mpsutil_command **start, struct mpsutil_command **end,
144     int ac, char **av)
145 {
146 	struct mpsutil_command **cmd;
147 
148 	if (ac < 2) {
149 		warnx("The %s command requires a sub-command.", av[0]);
150 		return (EINVAL);
151 	}
152 	for (cmd = start; cmd < end; cmd++) {
153 		if (strcmp((*cmd)->name, av[1]) == 0)
154 			return ((*cmd)->handler(ac - 1, av + 1));
155 	}
156 
157 	warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
158 	return (ENOENT);
159 }
160 
161 void
162 hexdump(const void *ptr, int length, const char *hdr, int flags)
163 {
164 	int i, j, k;
165 	int cols;
166 	const unsigned char *cp;
167 	char delim;
168 
169 	if ((flags & HD_DELIM_MASK) != 0)
170 		delim = (flags & HD_DELIM_MASK) >> 8;
171 	else
172 		delim = ' ';
173 
174 	if ((flags & HD_COLUMN_MASK) != 0)
175 		cols = flags & HD_COLUMN_MASK;
176 	else
177 		cols = 16;
178 
179 	cp = ptr;
180 	for (i = 0; i < length; i+= cols) {
181 		if (hdr != NULL)
182 			printf("%s", hdr);
183 
184 		if ((flags & HD_OMIT_COUNT) == 0)
185 			printf("%04x  ", i);
186 
187 		if ((flags & HD_OMIT_HEX) == 0) {
188 			for (j = 0; j < cols; j++) {
189 				if (flags & HD_REVERSED)
190 					k = i + (cols - 1 - j);
191 				else
192 					k = i + j;
193 				if (k < length)
194 					printf("%c%02x", delim, cp[k]);
195 				else
196 					printf("   ");
197 			}
198 		}
199 
200 		if ((flags & HD_OMIT_CHARS) == 0) {
201 			printf("  |");
202 			for (j = 0; j < cols; j++) {
203 				if (flags & HD_REVERSED)
204 					k = i + (cols - 1 - j);
205 				else
206 					k = i + j;
207 				if (k >= length)
208 					printf(" ");
209 				else if (cp[k] >= ' ' && cp[k] <= '~')
210 					printf("%c", cp[k]);
211 				else
212 					printf(".");
213 			}
214 			printf("|");
215 		}
216 		printf("\n");
217 	}
218 }
219 
220 #define PCHAR(c) { if (retval < tmpsz) { *outbuf++ = (c); retval++; } }
221 
222 int
223 mps_parse_flags(uintmax_t num, const char *q, char *outbuf, int tmpsz)
224 {
225 	int n, tmp, retval = 0;
226 
227 	if (num == 0)
228 		return (retval);
229 
230 	/* %b conversion flag format. */
231 	tmp = retval;
232 	while (*q) {
233 		n = *q++;
234 		if (num & (1 << (n - 1))) {
235 			PCHAR(retval != tmp ?  ',' : '<');
236 			for (; (n = *q) > ' '; ++q)
237 				PCHAR(n);
238 		} else
239 			for (; *q > ' '; ++q)
240 				continue;
241 	}
242 	if (retval != tmp)
243 		PCHAR('>');
244 
245 	return (retval);
246 }
247 
248