2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Jonathan Lemon <jlemon@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
34 #include <sys/ioctl.h>
35 #include <sys/ttycom.h>
45 #define DEVDIR "/dev/"
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");
64 char *buf, *p, *avail;
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");
80 *--p = '\0'; /* remove trailing ',' */
81 p = avail + strlen(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");
91 consmute(const char *onoff)
96 if (strcmp(onoff, "on") == 0)
98 else if (strcmp(onoff, "off") == 0)
103 if (sysctlbyname("kern.consmute", NULL, NULL, &mute, len) == -1)
104 err(1, "could not change console muting");
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.
114 stripdev(char *devnam)
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 */
122 return (devnam); /* passed */
126 consadd(char *devnam)
130 devnam = stripdev(devnam);
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);
139 consdel(char *devnam)
144 devnam = stripdev(devnam);
147 len = strlen(devnam) + sizeof("-");
148 if ((buf = malloc(len)) == NULL)
149 errx(1, "malloc failed");
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);
158 consset(char *devnam, int flag)
162 ttyfd = open(devnam, O_RDONLY);
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);
172 main(int argc, char **argv)
175 if (getopt(argc, argv, "") != -1)
180 if (argc > 0 && strcmp(argv[0], "list") != 0) {
183 else if (strcmp(argv[0], "mute") == 0)
185 else if (strcmp(argv[0], "add") == 0)
187 else if (strcmp(argv[0], "delete") == 0)
189 else if (strcmp(argv[0], "set") == 0)
191 else if (strcmp(argv[0], "unset") == 0)