1 /* Copyright (c) 2008, 2009 Nicira Networks, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include "extras/ezio/vt.h"
24 #include <sys/ioctl.h>
28 #define THIS_MODULE VLM_vt
31 static bool get_console_fd(int *fd);
34 vt_open(int open_flags)
36 int console_fd, vt_fd;
40 if (!get_console_fd(&console_fd)) {
44 /* Deallocate all unused virtual terminals, so that we don't proliferate an
45 * excess of empty ones over multiple runs. */
46 if (ioctl(console_fd, VT_DISALLOCATE, 0) < 0) {
47 VLOG_WARN("failed to deallocate empty virtual terminals: %s",
51 /* Find a unused virtual terminal. */
52 if (ioctl(console_fd, VT_OPENQRY, &vt) < 0) {
54 VLOG_ERR("failed to find a free virtual terminal: %s",
60 /* Open virtual terminal. */
61 sprintf(name, "/dev/tty%d", vt);
62 vt_fd = open(name, open_flags);
65 VLOG_ERR("failed to open %s: %s", name, strerror(error));
70 /* Activate virtual terminal. */
71 if (ioctl(console_fd, VT_ACTIVATE, vt) < 0
72 || ioctl(console_fd, VT_WAITACTIVE, vt) < 0) {
74 VLOG_ERR("could not activate virtual terminal %d: %s",
82 VLOG_DBG("allocated virtual terminal %d (%s)", vt, name);
91 return !ioctl(fd, KDGKBTYPE, &type) && (type == KB_101 || type == KB_84);
95 open_console(const char *name, int *fdp)
97 *fdp = open(name, O_RDWR | O_NOCTTY);
99 if (is_console(*fdp)) {
108 get_console_fd(int *fdp)
112 if (open_console("/dev/tty", fdp)
113 || open_console("/dev/tty0", fdp)
114 || open_console("/dev/console", fdp)) {
117 for (fd = 0; fd < 3; fd++) {
118 if (is_console(fd)) {
125 VLOG_ERR("unable to obtain a file descriptor for the console");