]> Pileus Git - mkinit/blob - src/gettyd.c
Add getty daemon
[mkinit] / src / gettyd.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <termios.h>
10 #include <fcntl.h>
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/ioctl.h>
15 #include <sys/wait.h>
16 #include <sys/epoll.h>
17 #include <sys/signalfd.h>
18
19 /* TTY data */
20 typedef struct tty_t {
21         char         *path;
22         int           fd;
23         int           pid;
24         struct tty_t *next;
25 } tty_t;
26
27 /* Local Data */
28 static int running;
29 static int epoll;
30 static int sigs;
31 static tty_t *ttys;
32
33 /* Helper functions */
34 void error(const char *fmt, ...)
35 {
36         va_list ap;
37         va_start(ap, fmt);
38         fprintf(stderr, "Error ");
39         vfprintf(stderr, fmt, ap);
40         fprintf(stderr, ": %s\n", strerror(errno));
41         va_end(ap);
42         if (!running)
43                 exit(1);
44 }
45
46 int add_poll(int fd, void *ptr)
47 {
48         struct epoll_event ctl = {
49                 .events = EPOLLIN,
50                 .data.ptr = ptr,
51         };
52         return epoll_ctl(epoll, EPOLL_CTL_ADD, fd, &ctl);
53 }
54
55 int del_poll(int fd)
56 {
57         return epoll_ctl(epoll, EPOLL_CTL_DEL, fd, NULL);
58 }
59
60 static int start_tty(tty_t *tty)
61 {
62         const char *prompt = "[Press enter to login]";
63         struct termios attr;
64
65         tty->fd = open(tty->path, O_RDWR|O_NOCTTY|O_NONBLOCK|O_CLOEXEC, 0);
66         if (tty->fd < 0)
67                 return tty->fd;
68         tcgetattr(tty->fd, &attr);
69         attr.c_lflag &= ~ECHO;
70         tcsetattr(tty->fd, TCSANOW, &attr);
71         write(tty->fd, "\033c", 2);
72         write(tty->fd, "\033[?1c", 5);
73         write(tty->fd, prompt, strlen(prompt));
74         return add_poll(tty->fd, tty);
75 }
76
77 static void read_tty(tty_t *tty)
78 {
79         int flags;
80         char ch, login;
81         while (read(tty->fd, &ch, 1) == 1)
82                 if (ch == '\n' || ch == '\r')
83                         login = 1;
84         if (!login)
85                 return;
86         if ((tty->pid = fork()) < 0)
87                 return;
88         if (tty->pid == 0) {
89                 if (putenv("TERM=linux"))
90                         error("setting environment");
91                 if (setsid() < 0)
92                         error("setting sid");
93                 if (write(tty->fd, "\033c", 2) < 0)
94                         error("resetting tty");
95                 if ((flags = fcntl(tty->fd, F_GETFL)) < 0)
96                         error("getting fd flags");
97                 if (fcntl(tty->fd, F_SETFL, flags&~O_NONBLOCK) < 0)
98                         error("setting blocking flags");
99                 if (ioctl(tty->fd, TIOCSCTTY, 0) < 0)
100                         error("setting ctty");
101                 if (dup2(tty->fd, 0) != 0)
102                         error("setting stdin");
103                 if (dup2(tty->fd, 1) != 1)
104                         error("setting stdout");
105                 if (dup2(tty->fd, 2) != 2)
106                         error("setting stderr");
107                 if (execl("/bin/login", "login", NULL) < 0)
108                         error("execing login program");
109         }
110         del_poll(tty->fd);
111         close(tty->fd);
112 }
113
114 void on_child(void)
115 {
116         struct signalfd_siginfo info;
117         if (read(sigs, &info, sizeof(info)) != sizeof(info))
118                 return;
119         if (info.ssi_signo != SIGCHLD)
120                 return;
121
122         int status;
123         pid_t pid;
124         tty_t *tty;
125         while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
126                 for (tty = ttys; tty; tty = tty->next) {
127                         if (pid == tty->pid) {
128                                 tty->pid = 0;
129                                 start_tty(tty);
130                         }
131                 }
132         }
133 }
134
135 /* Main */
136 int main(int argc, char **argv)
137 {
138         int i, count;
139         tty_t *tty;
140         sigset_t mask;
141
142         /* Check arguments */
143         if (argc <= 1) {
144                 printf("usage: gettyd <tty> ...\n");
145                 return 0;
146         }
147
148         /* Setup */
149         sigemptyset(&mask);
150         sigaddset(&mask, SIGCHLD);
151         sigaddset(&mask, SIGHUP);
152         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
153                 error("blocking signals");
154         if ((sigs = signalfd(-1, &mask, SFD_CLOEXEC)) < 0)
155                 error("creating signal fd");
156         if ((epoll = epoll_create1(EPOLL_CLOEXEC)) < 0)
157                 error("creating epoll");
158         if (add_poll(sigs, &sigs) < 0)
159                 error("adding signal epoll");
160
161         /* Open TTYs */
162         for (i = 1; i < argc; i++) {
163                 if (!(tty = malloc(sizeof(tty_t))))
164                         error("allocating memory");
165                 if (asprintf(&tty->path, "/dev/%s", argv[i]) < 0)
166                         error("allocating path name");
167                 if (start_tty(tty) < 0)
168                         error("starting tty '%s'", tty->path);
169                 tty->next = ttys;
170                 ttys = tty;
171         }
172
173         /* Main loop */
174         running = 1;
175         while (1) {
176                 struct epoll_event event;
177                 errno = 0;
178                 count = epoll_wait(epoll, &event, 1, -1);
179                 if (errno == EINTR)
180                         continue;
181                 if (count < 0)
182                         continue;
183                 if (event.data.ptr == &sigs)
184                         on_child();
185                 else
186                         read_tty(event.data.ptr);
187         }
188
189         return 0;
190 }