|
Packit Service |
50ad14 |
#include "config.h"
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#include <stdio.h>
|
|
Packit Service |
50ad14 |
#include <stdlib.h>
|
|
Packit Service |
50ad14 |
#include <unistd.h>
|
|
Packit Service |
50ad14 |
#include <fcntl.h>
|
|
Packit Service |
50ad14 |
#include <errno.h>
|
|
Packit Service |
50ad14 |
#include <sys/ioctl.h>
|
|
Packit Service |
50ad14 |
#include <linux/kd.h>
|
|
Packit Service |
50ad14 |
#include "nls.h"
|
|
Packit Service |
50ad14 |
#include "getfd.h"
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
static char *conspath[] = {
|
|
Packit Service |
50ad14 |
"/proc/self/fd/0",
|
|
Packit Service |
50ad14 |
"/dev/tty",
|
|
Packit Service |
50ad14 |
"/dev/tty0",
|
|
Packit Service |
50ad14 |
"/dev/vc/0",
|
|
Packit Service |
50ad14 |
"/dev/systty",
|
|
Packit Service |
50ad14 |
"/dev/console",
|
|
Packit Service |
50ad14 |
NULL
|
|
Packit Service |
50ad14 |
};
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
/*
|
|
Packit Service |
50ad14 |
* getfd.c
|
|
Packit Service |
50ad14 |
*
|
|
Packit Service |
50ad14 |
* Get an fd for use with kbd/console ioctls.
|
|
Packit Service |
50ad14 |
* We try several things because opening /dev/console will fail
|
|
Packit Service |
50ad14 |
* if someone else used X (which does a chown on /dev/console).
|
|
Packit Service |
50ad14 |
*/
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
static int
|
|
Packit Service |
50ad14 |
is_a_console(int fd)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
char arg;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
arg = 0;
|
|
Packit Service |
50ad14 |
return (isatty(fd) && ioctl(fd, KDGKBTYPE, &arg) == 0 && ((arg == KB_101) || (arg == KB_84)));
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
static int
|
|
Packit Service |
50ad14 |
open_a_console(const char *fnam)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
int fd;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
/*
|
|
Packit Service |
50ad14 |
* For ioctl purposes we only need some fd and permissions
|
|
Packit Service |
50ad14 |
* do not matter. But setfont:activatemap() does a write.
|
|
Packit Service |
50ad14 |
*/
|
|
Packit Service |
50ad14 |
fd = open(fnam, O_RDWR);
|
|
Packit Service |
50ad14 |
if (fd < 0)
|
|
Packit Service |
50ad14 |
fd = open(fnam, O_WRONLY);
|
|
Packit Service |
50ad14 |
if (fd < 0)
|
|
Packit Service |
50ad14 |
fd = open(fnam, O_RDONLY);
|
|
Packit Service |
50ad14 |
if (fd < 0)
|
|
Packit Service |
50ad14 |
return -1;
|
|
Packit Service |
50ad14 |
return fd;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int getfd(const char *fnam)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
int fd, i;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (fnam) {
|
|
Packit Service |
50ad14 |
if ((fd = open_a_console(fnam)) >= 0) {
|
|
Packit Service |
50ad14 |
if (is_a_console(fd))
|
|
Packit Service |
50ad14 |
return fd;
|
|
Packit Service |
50ad14 |
close(fd);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
fprintf(stderr, _("Couldn't open %s\n"), fnam);
|
|
Packit Service |
50ad14 |
exit(1);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
for (i = 0; conspath[i]; i++) {
|
|
Packit Service |
50ad14 |
if ((fd = open_a_console(conspath[i])) >= 0) {
|
|
Packit Service |
50ad14 |
if (is_a_console(fd))
|
|
Packit Service |
50ad14 |
return fd;
|
|
Packit Service |
50ad14 |
close(fd);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
for (fd = 0; fd < 3; fd++)
|
|
Packit Service |
50ad14 |
if (is_a_console(fd))
|
|
Packit Service |
50ad14 |
return fd;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
fprintf(stderr,
|
|
Packit Service |
50ad14 |
_("Couldn't get a file descriptor referring to the console\n"));
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
/* total failure */
|
|
Packit Service |
50ad14 |
exit(1);
|
|
Packit Service |
50ad14 |
}
|