conscontrol.c revision 125923
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sbin/conscontrol/conscontrol.c 125923 2004-02-17 04:51:06Z grog $");
29
30#include <sys/types.h>
31#include <sys/sysctl.h>
32
33#include <err.h>
34#include <errno.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39
40#define DEVDIR	"/dev/"
41
42static void __dead2
43usage(void)
44{
45
46	(void)fprintf(stderr, "%s\n%s\n%s\n",
47	    "usage: conscontrol [list]",
48	    "       conscontrol mute on | off",
49	    "       conscontrol add | delete console");
50	exit(1);
51}
52
53static void
54consstatus(void)
55{
56	int mute;
57	size_t len;
58	char *buf, *p, *avail;
59
60	len = sizeof(mute);
61	if (sysctlbyname("kern.consmute", &mute, &len, NULL, 0) == -1)
62		err(1, "kern.consmute retrieval failed");
63	if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
64		err(1, "kern.console estimate failed");
65	if ((buf = malloc(len)) == NULL)
66		errx(1, "kern.console malloc failed");
67	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
68		err(1, "kern.console retrieval failed");
69	if ((avail = strchr(buf, '/')) == NULL)
70		errx(1, "kern.console format not understood");
71	p = avail;
72	*avail++ = '\0';
73	if (p != buf)
74		*--p = '\0';			/* remove trailing ',' */
75	p = avail + strlen(avail);
76	if (p != avail)
77		*--p = '\0';			/* remove trailing ',' */
78	printf("Configured: %s\n", buf);
79	printf(" Available: %s\n", avail);
80	printf("    Muting: %s\n", mute ? "on" : "off");
81	free(buf);
82}
83
84static void
85consmute(const char *onoff)
86{
87	int mute;
88	size_t len;
89
90	if (strcmp(onoff, "on") == 0)
91		mute = 1;
92	else if (strcmp(onoff, "off") == 0)
93		mute = 0;
94	else
95		usage();
96	len = sizeof(mute);
97	if (sysctlbyname("kern.consmute", NULL, NULL, &mute, len) == -1)
98		err(1, "could not change console muting");
99}
100
101/*
102 * The name we supply to the sysctls should be an entry in /dev.  If
103 * the user has specified the full pathname in /dev, DTRT.  If he
104 * specifies a name in some other directory, it's an error.
105 */
106
107static char*
108stripdev(char *devnam)
109{
110	if (memcmp (devnam, DEVDIR, strlen(DEVDIR)) == 0)
111		return (&devnam[strlen(DEVDIR)]);	    /* remove /dev */
112	else if (strchr (devnam, '/')) {
113		fprintf(stderr, "Not a device in /dev: %s\n", devnam);
114		return (NULL);				    /* end of string */
115	} else
116		return (devnam);			    /* passed */
117}
118
119static void
120consadd(char *devnam)
121{
122	size_t len;
123
124	devnam = stripdev(devnam);
125	if (devnam == NULL)
126		return;
127	len = strlen(devnam);
128	if (sysctlbyname("kern.console", NULL, NULL, devnam, len) == -1)
129		err(1, "could not add %s as a console", devnam);
130}
131
132static void
133consdel(char *devnam)
134{
135	char *buf;
136	size_t len;
137
138	devnam = stripdev(devnam);
139	if (devnam == NULL)
140		return;
141	len = strlen(devnam) + sizeof("-");
142	if ((buf = malloc(len)) == NULL)
143		errx(1, "malloc failed");
144	buf[0] = '-';
145	strcpy(buf + 1, devnam);
146	if (sysctlbyname("kern.console", NULL, NULL, buf, len) == -1)
147		err(1, "could not remove %s as a console", devnam);
148	free(buf);
149}
150
151int
152main(int argc, char **argv)
153{
154
155	if (getopt(argc, argv, "") != -1)
156		usage();
157	argc -= optind;
158	argv += optind;
159
160	if (argc > 0 && strcmp(argv[0], "list") != 0) {
161		if (argc != 2)
162			usage();
163		if (strcmp(argv[0], "mute") == 0)
164			consmute(argv[1]);
165		else if (strcmp(argv[0], "add") == 0)
166			consadd(argv[1]);
167		else if (strcmp(argv[0], "delete") == 0)
168			consdel(argv[1]);
169		else
170			usage();
171	}
172	consstatus();
173	exit(0);
174}
175