2 * Copyright (c) 2001 Jonathan Lemon <jlemon@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
32 #include <sys/ioctl.h>
33 #include <sys/ttycom.h>
43 #define DEVDIR "/dev/"
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");
62 char *buf, *p, *avail;
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");
78 *--p = '\0'; /* remove trailing ',' */
79 p = avail + strlen(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");
89 consmute(const char *onoff)
94 if (strcmp(onoff, "on") == 0)
96 else if (strcmp(onoff, "off") == 0)
101 if (sysctlbyname("kern.consmute", NULL, NULL, &mute, len) == -1)
102 err(1, "could not change console muting");
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.
112 stripdev(char *devnam)
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 */
120 return (devnam); /* passed */
124 consadd(char *devnam)
128 devnam = stripdev(devnam);
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);
137 consdel(char *devnam)
142 devnam = stripdev(devnam);
145 len = strlen(devnam) + sizeof("-");
146 if ((buf = malloc(len)) == NULL)
147 errx(1, "malloc failed");
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);
156 consset(char *devnam, int flag)
160 ttyfd = open(devnam, O_RDONLY);
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);
170 main(int argc, char **argv)
173 if (getopt(argc, argv, "") != -1)
178 if (argc > 0 && strcmp(argv[0], "list") != 0) {
181 else if (strcmp(argv[0], "mute") == 0)
183 else if (strcmp(argv[0], "add") == 0)
185 else if (strcmp(argv[0], "delete") == 0)
187 else if (strcmp(argv[0], "set") == 0)
189 else if (strcmp(argv[0], "unset") == 0)