summaryrefslogtreecommitdiffstats
path: root/tdm/backend/getfd.c
diff options
context:
space:
mode:
authorTimothy Pearson <kb9vqf@pearsoncomputing.net>2013-08-08 14:36:50 -0500
committerTimothy Pearson <kb9vqf@pearsoncomputing.net>2013-08-08 14:36:50 -0500
commit960cc8392da291eaf6c90d2101b721f6c20488f3 (patch)
tree63ecdf7a203cd721c0652bad2023a3e9f53f995c /tdm/backend/getfd.c
parent3a84ecae5ded7370a7dfee43f25dd21269d65a7c (diff)
downloadtdebase-960cc8392da291eaf6c90d2101b721f6c20488f3.tar.gz
tdebase-960cc8392da291eaf6c90d2101b721f6c20488f3.zip
Update tdehw-based media manager for new mount API
Allow active VT to be queried from tdm Only display media notification messages on active VT Hide logout action buttons if logout process unstalls
Diffstat (limited to 'tdm/backend/getfd.c')
-rw-r--r--tdm/backend/getfd.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/tdm/backend/getfd.c b/tdm/backend/getfd.c
new file mode 100644
index 000000000..3632161d3
--- /dev/null
+++ b/tdm/backend/getfd.c
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <linux/kd.h>
+#include "getfd.h"
+
+/*
+ * getfd.c
+ *
+ * Get an fd for use with kbd/console ioctls.
+ * We try several things because opening /dev/console will fail
+ * if someone else used X (which does a chown on /dev/console).
+ */
+
+static int
+is_a_console(int fd) {
+ char arg;
+
+ arg = 0;
+ return (ioctl(fd, KDGKBTYPE, &arg) == 0
+ && ((arg == KB_101) || (arg == KB_84)));
+}
+
+static int
+open_a_console(const char *fnam) {
+ int fd;
+
+ /*
+ * For ioctl purposes we only need some fd and permissions
+ * do not matter. But setfont:activatemap() does a write.
+ */
+ fd = open(fnam, O_RDWR);
+ if (fd < 0 && errno == EACCES)
+ fd = open(fnam, O_WRONLY);
+ if (fd < 0 && errno == EACCES)
+ fd = open(fnam, O_RDONLY);
+ if (fd < 0)
+ return -1;
+ if (!is_a_console(fd)) {
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+
+int getfd() {
+ int fd;
+
+ fd = open_a_console("/dev/tty");
+ if (fd >= 0)
+ return fd;
+
+ fd = open_a_console("/dev/tty0");
+ if (fd >= 0)
+ return fd;
+
+ fd = open_a_console("/dev/vc/0");
+ if (fd >= 0)
+ return fd;
+
+ fd = open_a_console("/dev/console");
+ if (fd >= 0)
+ return fd;
+
+ for (fd = 0; fd < 3; fd++)
+ if (is_a_console(fd))
+ return fd;
+
+ // "Couldnt get a file descriptor referring to the console
+ return -1;
+}
+