|
@@ -6,6 +6,7 @@
|
|
|
#include <unistd.h>
|
|
|
#include <fcntl.h>
|
|
|
#include <pty.h>
|
|
|
+#include <sys/wait.h>
|
|
|
#include <sys/fswait.h>
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
@@ -17,51 +18,55 @@ int main(int argc, char * argv[]) {
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
- if (!fork()) {
|
|
|
-
|
|
|
- if (argc > 1) {
|
|
|
- file = argv[1];
|
|
|
- }
|
|
|
+ if (argc > 1) {
|
|
|
+ file = argv[1];
|
|
|
+ }
|
|
|
|
|
|
- openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
|
|
|
- fd_serial = open(file, O_RDWR);
|
|
|
+ openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
|
|
|
+ fd_serial = open(file, O_RDWR);
|
|
|
|
|
|
- if (!fork()) {
|
|
|
- dup2(fd_slave, 0);
|
|
|
- dup2(fd_slave, 1);
|
|
|
- dup2(fd_slave, 2);
|
|
|
+ pid_t child = fork();
|
|
|
|
|
|
- system("ttysize -q");
|
|
|
+ if (!child) {
|
|
|
+ dup2(fd_slave, 0);
|
|
|
+ dup2(fd_slave, 1);
|
|
|
+ dup2(fd_slave, 2);
|
|
|
|
|
|
- char * tokens[] = {"/bin/login",NULL};
|
|
|
- execvp(tokens[0], tokens);
|
|
|
- exit(1);
|
|
|
- } else {
|
|
|
+ system("ttysize -q");
|
|
|
|
|
|
- int fds[2] = {fd_serial, fd_master};
|
|
|
+ char * tokens[] = {"/bin/login",NULL};
|
|
|
+ execvp(tokens[0], tokens);
|
|
|
+ exit(1);
|
|
|
+ } else {
|
|
|
|
|
|
- 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 */
|
|
|
- break;
|
|
|
- }
|
|
|
+ int fds[2] = {fd_serial, fd_master};
|
|
|
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- return 1;
|
|
|
}
|
|
|
|
|
|
return 0;
|