xref: /freebsd/sbin/conscontrol/conscontrol.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
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 
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 static void __dead2
41 usage(void)
42 {
43 
44 	(void)fprintf(stderr, "%s\n%s\n%s\n",
45 	    "usage: conscontrol [list]",
46 	    "       conscontrol mute on | off",
47 	    "       conscontrol add | delete console");
48 	exit(1);
49 }
50 
51 static void
52 consstatus(void)
53 {
54 	int mute;
55 	size_t len;
56 	char *buf, *p, *avail;
57 
58 	len = sizeof(mute);
59 	if (sysctlbyname("kern.consmute", &mute, &len, NULL, 0) == -1)
60 		err(1, "kern.consmute retrieval failed");
61 	if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
62 		err(1, "kern.console estimate failed");
63 	if ((buf = malloc(len)) == NULL)
64 		errx(1, "kern.console malloc failed");
65 	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
66 		err(1, "kern.console retrieval failed");
67 	if ((avail = strchr(buf, '/')) == NULL)
68 		errx(1, "kern.console format not understood");
69 	p = avail;
70 	*avail++ = '\0';
71 	if (p != buf)
72 		*--p = '\0';			/* remove trailing ',' */
73 	p = avail + strlen(avail);
74 	if (p != avail)
75 		*--p = '\0';			/* remove trailing ',' */
76 	printf("Configured: %s\n", buf);
77 	printf(" Available: %s\n", avail);
78 	printf("    Muting: %s\n", mute ? "on" : "off");
79 	free(buf);
80 }
81 
82 static void
83 consmute(const char *onoff)
84 {
85 	int mute;
86 	size_t len;
87 
88 	if (strcmp(onoff, "on") == 0)
89 		mute = 1;
90 	else if (strcmp(onoff, "off") == 0)
91 		mute = 0;
92 	else
93 		usage();
94 	len = sizeof(mute);
95 	if (sysctlbyname("kern.consmute", NULL, NULL, &mute, len) == -1)
96 		err(1, "could not change console muting");
97 }
98 
99 static void
100 consadd(char *devnam)
101 {
102 	size_t len;
103 
104 	len = strlen(devnam);
105 	if (sysctlbyname("kern.console", NULL, NULL, devnam, len) == -1)
106 		err(1, "could not add %s as a console", devnam);
107 }
108 
109 static void
110 consdel(const char *devnam)
111 {
112 	char *buf;
113 	size_t len;
114 
115 	len = strlen(devnam) + sizeof("-");
116 	if ((buf = malloc(len)) == NULL)
117 		errx(1, "malloc failed");
118 	buf[0] = '-';
119 	strcpy(buf + 1, devnam);
120 	if (sysctlbyname("kern.console", NULL, NULL, buf, len) == -1)
121 		err(1, "could not remove %s as a console", devnam);
122 	free(buf);
123 }
124 
125 int
126 main(int argc, char **argv)
127 {
128 
129 	if (getopt(argc, argv, "") != -1)
130 		usage();
131 	argc -= optind;
132 	argv += optind;
133 
134 	if (argc > 0 && strcmp(argv[0], "list") != 0) {
135 		if (argc != 2)
136 			usage();
137 		if (strcmp(argv[0], "mute") == 0)
138 			consmute(argv[1]);
139 		else if (strcmp(argv[0], "add") == 0)
140 			consadd(argv[1]);
141 		else if (strcmp(argv[0], "delete") == 0)
142 			consdel(argv[1]);
143 		else
144 			usage();
145 	}
146 	consstatus();
147 	exit(0);
148 }
149