-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetty.c
51 lines (43 loc) · 930 Bytes
/
getty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "util/util.h"
char *argv0;
void opentty(const char *tty)
{
close(0);
if (open(tty, O_RDWR|O_NOCTTY, 0) != 0)
die("cannot open %s\n", tty);
if (fchown(0, 0, 0) == -1 || fchmod(0, 0600) == -1)
die("cannot change ownership or mode of tty\n");
if (ioctl(0, TIOCSCTTY, 1) == -1)
die("cannot set controlling tty\n");
close(1);
close(2);
if (dup(0) != 1 || dup(0) != 2)
die("cannot setup stdout or stderr\n");
write(0, "\033c", 2);
}
void usage(void)
{
die("usage: %s tty\n", argv0);
}
int main(int argc, char *argv[])
{
char *arg[] = { "/bin/login", NULL };
char *env[] = { "TERM=linux", NULL };
ARGBEGIN {
default:
usage();
} ARGEND;
if (argc != 1)
usage();
else
opentty(argv[0]);
tcflush(0, TCIOFLUSH);
execve(arg[0], arg, env);
warn("cannot execute %s\n", arg[0]);
return 1;
}