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$");
29
30#include <sys/types.h>
31#include <sys/sysctl.h>
32#include <sys/ioctl.h>
33#include <sys/ttycom.h>
34
35#include <err.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43#define DEVDIR	"/dev/"
44
45static void __dead2
46usage(void)
47{
48
49	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
50	    "usage: conscontrol [list]",
51	    "       conscontrol mute on | off",
52	    "       conscontrol add | delete console",
53	    "       conscontrol set | unset console");
54	exit(1);
55}
56
57static void
58consstatus(void)
59{
60	int mute;
61	size_t len;
62	char *buf, *p, *avail;
63
64	len = sizeof(mute);
65	if (sysctlbyname("kern.consmute", &mute, &len, NULL, 0) == -1)
66		err(1, "kern.consmute retrieval failed");
67	if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
68		err(1, "kern.console estimate failed");
69	if ((buf = malloc(len)) == NULL)
70		errx(1, "kern.console malloc failed");
71	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
72		err(1, "kern.console retrieval failed");
73	if ((avail = strchr(buf, '/')) == NULL)
74		errx(1, "kern.console format not understood");
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}
87
88static void
89consmute(const char *onoff)
90{
91	int mute;
92	size_t len;
93
94	if (strcmp(onoff, "on") == 0)
95		mute = 1;
96	else if (strcmp(onoff, "off") == 0)
97		mute = 0;
98	else
99		usage();
100	len = sizeof(mute);
101	if (sysctlbyname("kern.consmute", NULL, NULL, &mute, len) == -1)
102		err(1, "could not change console muting");
103}
104
105/*
106 * The name we supply to the sysctls should be an entry in /dev.  If
107 * the user has specified the full pathname in /dev, DTRT.  If he
108 * specifies a name in some other directory, it's an error.
109 */
110
111static char*
112stripdev(char *devnam)
113{
114	if (memcmp (devnam, DEVDIR, strlen(DEVDIR)) == 0)
115		return (&devnam[strlen(DEVDIR)]);	    /* remove /dev */
116	else if (strchr (devnam, '/')) {
117		fprintf(stderr, "Not a device in /dev: %s\n", devnam);
118		return (NULL);				    /* end of string */
119	} else
120		return (devnam);			    /* passed */
121}
122
123static void
124consadd(char *devnam)
125{
126	size_t len;
127
128	devnam = stripdev(devnam);
129	if (devnam == NULL)
130		return;
131	len = strlen(devnam);
132	if (sysctlbyname("kern.console", NULL, NULL, devnam, len) == -1)
133		err(1, "could not add %s as a console", devnam);
134}
135
136static void
137consdel(char *devnam)
138{
139	char *buf;
140	size_t len;
141
142	devnam = stripdev(devnam);
143	if (devnam == NULL)
144		return;
145	len = strlen(devnam) + sizeof("-");
146	if ((buf = malloc(len)) == NULL)
147		errx(1, "malloc failed");
148	buf[0] = '-';
149	strcpy(buf + 1, devnam);
150	if (sysctlbyname("kern.console", NULL, NULL, buf, len) == -1)
151		err(1, "could not remove %s as a console", devnam);
152	free(buf);
153}
154
155static void
156consset(char *devnam, int flag)
157{
158	int ttyfd;
159
160	ttyfd = open(devnam, O_RDONLY);
161	if (ttyfd == -1)
162		err(1, "opening %s", devnam);
163	if (ioctl(ttyfd, TIOCCONS, &flag) == -1)
164		err(1, "could not %s %s as virtual console",
165		    flag ? "set" : "unset", devnam);
166	close(ttyfd);
167}
168
169int
170main(int argc, char **argv)
171{
172
173	if (getopt(argc, argv, "") != -1)
174		usage();
175	argc -= optind;
176	argv += optind;
177
178	if (argc > 0 && strcmp(argv[0], "list") != 0) {
179		if (argc != 2)
180			usage();
181		else if (strcmp(argv[0], "mute") == 0)
182			consmute(argv[1]);
183		else if (strcmp(argv[0], "add") == 0)
184			consadd(argv[1]);
185		else if (strcmp(argv[0], "delete") == 0)
186			consdel(argv[1]);
187		else if (strcmp(argv[0], "set") == 0)
188			consset(argv[1], 1);
189		else if (strcmp(argv[0], "unset") == 0)
190			consset(argv[1], 0);
191		else
192			usage();
193	}
194	consstatus();
195	exit(0);
196}
197