2018-07-06 04:40:49 +03:00
|
|
|
/*
|
|
|
|
* runs a proper tty on a serial port
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2018-08-02 07:13:27 +03:00
|
|
|
#include <pty.h>
|
2018-08-02 12:48:45 +03:00
|
|
|
#include <sys/wait.h>
|
2018-08-02 07:13:27 +03:00
|
|
|
#include <sys/fswait.h>
|
2018-07-06 04:40:49 +03:00
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
int fd_master, fd_slave, fd_serial;
|
|
|
|
char * file = "/dev/ttyS0";
|
|
|
|
|
|
|
|
if (getuid() != 0) {
|
|
|
|
fprintf(stderr, "%s: only root can do that\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
if (argc > 1) {
|
|
|
|
file = argv[1];
|
|
|
|
}
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
|
|
|
|
fd_serial = open(file, O_RDWR);
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
pid_t child = fork();
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
if (!child) {
|
|
|
|
dup2(fd_slave, 0);
|
|
|
|
dup2(fd_slave, 1);
|
|
|
|
dup2(fd_slave, 2);
|
2018-08-01 04:03:34 +03:00
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
system("ttysize -q");
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
char * tokens[] = {"/bin/login",NULL};
|
|
|
|
execvp(tokens[0], tokens);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
int fds[2] = {fd_serial, fd_master};
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
while (1) {
|
|
|
|
int index = fswait2(2,fds,200);
|
|
|
|
char buf[1024];
|
|
|
|
int r;
|
|
|
|
switch (index) {
|
|
|
|
case 0: /* fd_serial */
|
|
|
|
r = read(fd_serial, buf, 1);
|
|
|
|
write(fd_master, buf, 1);
|
|
|
|
break;
|
|
|
|
case 1: /* fd_master */
|
|
|
|
r = read(fd_master, buf, 1024);
|
|
|
|
write(fd_serial, buf, r);
|
|
|
|
break;
|
|
|
|
default: /* timeout */
|
|
|
|
{
|
|
|
|
int result = waitpid(child, NULL, WNOHANG);
|
|
|
|
if (result > 0) {
|
|
|
|
/* Child login shell has returned (session ended) */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-07-06 04:40:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|