summaryrefslogtreecommitdiff
path: root/distrib/special/sysctl/sysctl.c
blob: c622b690edd87f2dd266b90ae10170ee1a542e8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*	$OpenBSD: sysctl.c,v 1.6 2010/04/03 21:32:53 miod Exp $	*/

/*
 * Copyright (c) 2009 Theo de Raadt <deraadt@openbsd.org>
 * Copyright (c) 2007 Kenneth R. Westerback <krw@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/uio.h>

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct var {
	char *name;
	int (*print)(struct var *);
	int nmib;
	int mib[3];
};

int	pstring(struct var *);
int	pint(struct var *);

struct var vars[] = {
	{ "kern.osrelease", pstring, 2,
	    { CTL_KERN, KERN_OSRELEASE }},
	{ "hw.machine", pstring, 2,
	    { CTL_HW, HW_MACHINE }},
	{ "hw.model", pstring, 2,
	    { CTL_HW, HW_MODEL }},
	{ "hw.product", pstring, 2,
	    { CTL_HW, HW_PRODUCT }},
	{ "hw.disknames", pstring, 2,
	    { CTL_HW, HW_DISKNAMES }},
	{ "hw.ncpufound", pint, 2,
	    { CTL_HW, HW_NCPUFOUND }},
};

int	nflag;
char	*name;

int
pint(struct var *v)
{
	int n;
	size_t len = sizeof(n);

	if (sysctl(v->mib, v->nmib, &n, &len, NULL, 0) != -1) {
		if (nflag == 0)
			printf("%s=", v->name);
		printf("%d\n", n);
		return (0);
	}
	return (1);
}

int
pstring(struct var *v)
{
	struct iovec iov[2];
	char *p;
	size_t len;

	if (sysctl(v->mib, v->nmib, NULL, &len, NULL, 0) != -1)
		if ((p = malloc(len)) != NULL)
			if (sysctl(v->mib, v->nmib, p, &len, NULL, 0) != -1) {
				if (nflag == 0)
					printf("%s=", v->name);
				fwrite(p, len, 1, stdout);
				printf("\n");
				return (0);
			}
	return (1);
}

int
main(int argc, char *argv[])
{
	int ch, i;

	while ((ch = getopt(argc, argv, "n")) != -1) {
		switch (ch) {
		case 'n':
			nflag = 1;
			break;
		default:
			fprintf(stderr, "usage: sysctl [-n] name\n");
			exit(1);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0) {
		for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++)
			(vars[i].print)(&vars[i]);
		exit(0);
	}

	while (argc--) {
		name = *argv++;

		for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
			if (strcmp(name, vars[i].name) == 0) {
				(vars[i].print)(&vars[i]);
				break;
			}
		}
	}

	exit(0);
}