xref: /freebsd/sbin/conscontrol/conscontrol.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 Jonathan Lemon <jlemon@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
34 #include <sys/ioctl.h>
35 #include <sys/ttycom.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #define DEVDIR	"/dev/"
46 
47 static void __dead2
48 usage(void)
49 {
50 
51 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
52 	    "usage: conscontrol [list]",
53 	    "       conscontrol mute on | off",
54 	    "       conscontrol add | delete console",
55 	    "       conscontrol set | unset console");
56 	exit(1);
57 }
58 
59 static void
60 consstatus(void)
61 {
62 	int mute;
63 	size_t len;
64 	char *buf, *p, *avail;
65 
66 	len = sizeof(mute);
67 	if (sysctlbyname("kern.consmute", &mute, &len, NULL, 0) == -1)
68 		err(1, "kern.consmute retrieval failed");
69 	if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
70 		err(1, "kern.console estimate failed");
71 	if ((buf = malloc(len)) == NULL)
72 		errx(1, "kern.console malloc failed");
73 	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
74 		err(1, "kern.console retrieval failed");
75 	if ((avail = strchr(buf, '/')) == NULL)
76 		errx(1, "kern.console format not understood");
77 	p = avail;
78 	*avail++ = '\0';
79 	if (p != buf)
80 		*--p = '\0';			/* remove trailing ',' */
81 	p = avail + strlen(avail);
82 	if (p != avail)
83 		*--p = '\0';			/* remove trailing ',' */
84 	printf("Configured: %s\n", buf);
85 	printf(" Available: %s\n", avail);
86 	printf("    Muting: %s\n", mute ? "on" : "off");
87 	free(buf);
88 }
89 
90 static void
91 consmute(const char *onoff)
92 {
93 	int mute;
94 	size_t len;
95 
96 	if (strcmp(onoff, "on") == 0)
97 		mute = 1;
98 	else if (strcmp(onoff, "off") == 0)
99 		mute = 0;
100 	else
101 		usage();
102 	len = sizeof(mute);
103 	if (sysctlbyname("kern.consmute", NULL, NULL, &mute, len) == -1)
104 		err(1, "could not change console muting");
105 }
106 
107 /*
108  * The name we supply to the sysctls should be an entry in /dev.  If
109  * the user has specified the full pathname in /dev, DTRT.  If he
110  * specifies a name in some other directory, it's an error.
111  */
112 
113 static char*
114 stripdev(char *devnam)
115 {
116 	if (memcmp (devnam, DEVDIR, strlen(DEVDIR)) == 0)
117 		return (&devnam[strlen(DEVDIR)]);	    /* remove /dev */
118 	else if (strchr (devnam, '/')) {
119 		fprintf(stderr, "Not a device in /dev: %s\n", devnam);
120 		return (NULL);				    /* end of string */
121 	} else
122 		return (devnam);			    /* passed */
123 }
124 
125 static void
126 consadd(char *devnam)
127 {
128 	size_t len;
129 
130 	devnam = stripdev(devnam);
131 	if (devnam == NULL)
132 		return;
133 	len = strlen(devnam);
134 	if (sysctlbyname("kern.console", NULL, NULL, devnam, len) == -1)
135 		err(1, "could not add %s as a console", devnam);
136 }
137 
138 static void
139 consdel(char *devnam)
140 {
141 	char *buf;
142 	size_t len;
143 
144 	devnam = stripdev(devnam);
145 	if (devnam == NULL)
146 		return;
147 	len = strlen(devnam) + sizeof("-");
148 	if ((buf = malloc(len)) == NULL)
149 		errx(1, "malloc failed");
150 	buf[0] = '-';
151 	strcpy(buf + 1, devnam);
152 	if (sysctlbyname("kern.console", NULL, NULL, buf, len) == -1)
153 		err(1, "could not remove %s as a console", devnam);
154 	free(buf);
155 }
156 
157 static void
158 consset(char *devnam, int flag)
159 {
160 	int ttyfd;
161 
162 	ttyfd = open(devnam, O_RDONLY);
163 	if (ttyfd == -1)
164 		err(1, "opening %s", devnam);
165 	if (ioctl(ttyfd, TIOCCONS, &flag) == -1)
166 		err(1, "could not %s %s as virtual console",
167 		    flag ? "set" : "unset", devnam);
168 	close(ttyfd);
169 }
170 
171 int
172 main(int argc, char **argv)
173 {
174 
175 	if (getopt(argc, argv, "") != -1)
176 		usage();
177 	argc -= optind;
178 	argv += optind;
179 
180 	if (argc > 0 && strcmp(argv[0], "list") != 0) {
181 		if (argc != 2)
182 			usage();
183 		else if (strcmp(argv[0], "mute") == 0)
184 			consmute(argv[1]);
185 		else if (strcmp(argv[0], "add") == 0)
186 			consadd(argv[1]);
187 		else if (strcmp(argv[0], "delete") == 0)
188 			consdel(argv[1]);
189 		else if (strcmp(argv[0], "set") == 0)
190 			consset(argv[1], 1);
191 		else if (strcmp(argv[0], "unset") == 0)
192 			consset(argv[1], 0);
193 		else
194 			usage();
195 	}
196 	consstatus();
197 	exit(0);
198 }
199