conscontrol.c revision 85378
1/*-
2 * Copyright (c) 2001 Jonathan Lemon <jlemon@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sbin/conscontrol/conscontrol.c 85378 2001-10-23 20:36:43Z jlemon $
27 */
28
29#include <errno.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/types.h>
34#include <sys/sysctl.h>
35
36static void __dead2
37usage(void)
38{
39	const char *pname = getprogname();
40
41	fprintf(stderr, "usage: %s [list]\n", pname);
42	fprintf(stderr, "       %s mute on|off\n", pname);
43	fprintf(stderr, "       %s add|delete console\n", pname);
44	exit(1);
45}
46
47#define CONSBUFSIZE	32
48
49static void
50constatus(void)
51{
52	int mute;
53	size_t len;
54	char *buf, *p, *avail;
55
56	len = sizeof(mute);
57	if (sysctlbyname("kern.consmute", &mute, &len, NULL, 0) == -1)
58		goto fail;
59
60	len = 0;
61alloc:
62	len += CONSBUFSIZE;
63	buf = malloc(len);
64	if (buf == NULL)
65		err(1, "Could not malloc sysctl buffer");
66
67	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1) {
68		if (errno == ENOMEM) {
69			free(buf);
70			goto alloc;
71		}
72		goto fail;
73	}
74	avail = strchr(buf, '/');
75	p = avail;
76	*avail++ = '\0';
77	if (p != buf)
78		*--p = '\0';			/* remove trailing ',' */
79	p = avail + strlen(avail);
80	if (p != avail)
81		*--p = '\0';			/* remove trailing ',' */
82	printf("Configured: %s\n", buf);
83	printf(" Available: %s\n", avail);
84	printf("    Muting: %s\n", mute ? "on" : "off");
85	free(buf);
86	return;
87fail:
88	err(1, "Could not get console information");
89}
90
91static void
92consmute(const char *onoff)
93{
94	int mute;
95	size_t len;
96
97	if (strcmp(onoff, "on") == 0)
98		mute = 1;
99	else if (strcmp(onoff, "off") == 0)
100		mute = 0;
101	else
102		usage();
103	len = sizeof(mute);
104	if (sysctlbyname("kern.consmute", NULL, NULL, &mute, len) == -1)
105		err(1, "Could not change console muting");
106}
107
108static void
109consadd(char *devname)
110{
111	size_t len;
112
113	len = strlen(devname);
114	if (sysctlbyname("kern.console", NULL, NULL, devname, len) == -1)
115		err(1, "Could not add %s as a console", devname);
116}
117
118#define MAXDEVNAME	32
119
120static void
121consdel(const char *devname)
122{
123	char buf[MAXDEVNAME];
124	size_t len;
125
126	snprintf(buf, MAXDEVNAME, "-%s", devname);
127	len = strlen(buf);
128	if (sysctlbyname("kern.console", NULL, NULL, &buf, len) == -1)
129		err(1, "Could not remove %s as a console", devname);
130}
131
132int
133main(int argc, char **argv)
134{
135
136	if (argc < 2 || strcmp(argv[1], "list") == 0)
137		goto done;
138	if (argc < 3)
139		usage();
140	if (strcmp(argv[1], "mute") == 0)
141		consmute(argv[2]);
142	else if (strcmp(argv[1], "add") == 0)
143		consadd(argv[2]);
144	else if (strcmp(argv[1], "delete") == 0)
145		consdel(argv[2]);
146	else
147		usage();
148done:
149	constatus();
150}
151