Index: rtty-7.1.4/src/main.c =================================================================== --- rtty-7.1.4.orig/src/main.c +++ rtty-7.1.4/src/main.c @@ -37,6 +37,9 @@ enum { LONG_OPT_HELP = 1 }; +int force_exit = 0; +int connected = 0; + static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents) { if (w->signum == SIGINT) { @@ -53,6 +56,7 @@ static struct option long_options[] = { {"token", required_argument, NULL, 't'}, {"verbose", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'V'}, + {"exit", required_argument, NULL, 'e'}, {"help", no_argument, NULL, LONG_OPT_HELP}, {0, 0, 0, 0} }; @@ -79,10 +83,17 @@ static void usage(const char *prog) exit(1); } +static void timeout_cb(EV_P_ ev_timer *w, int revents) +{ + if (!connected) + exit(1); +} + int main(int argc, char **argv) { struct ev_loop *loop = EV_DEFAULT; struct ev_signal signal_watcher; + struct ev_timer timeout_watcher; bool background = false; bool verbose = false; struct rtty rtty = { @@ -95,7 +106,7 @@ int main(int argc, char **argv) int c; while (true) { - c = getopt_long(argc, argv, "I:h:p:d:asDt:f:RS:vV", long_options, &option_index); + c = getopt_long(argc, argv, "I:h:p:d:asDt:f:RS:vVe:", long_options, &option_index); if (c == -1) break; @@ -146,6 +157,9 @@ int main(int argc, char **argv) case LONG_OPT_HELP: usage(argv[0]); break; + case 'e': + force_exit = atoi(optarg); + break; default: /* '?' */ usage(argv[0]); break; @@ -167,6 +181,10 @@ int main(int argc, char **argv) ev_signal_init(&signal_watcher, signal_cb, SIGINT); ev_signal_start(loop, &signal_watcher); + if (force_exit) { + ev_timer_init(&timeout_watcher, timeout_cb, force_exit, 0.); + ev_timer_start(loop, &timeout_watcher); + } if (rtty_start(&rtty) < 0) return -1; Index: rtty-7.1.4/src/rtty.c =================================================================== --- rtty-7.1.4.orig/src/rtty.c +++ rtty-7.1.4/src/rtty.c @@ -36,6 +36,9 @@ #include "utils.h" #include "command.h" +extern int force_exit; +extern int connected; + static char login_path[128]; /* /bin/login */ static void del_tty(struct tty *tty) @@ -57,6 +60,8 @@ static void del_tty(struct tty *tty) log_info("delete tty: %d\n", tty->sid); free(tty); + if (force_exit) + exit(0); } static inline struct tty *find_tty(struct rtty *rtty, int sid) @@ -69,6 +74,7 @@ static inline struct tty *find_tty(struc static inline void tty_logout(struct rtty *rtty, int sid) { struct tty *tty = find_tty(rtty, sid); + if (tty) del_tty(tty); } @@ -136,6 +142,8 @@ static void pty_on_exit(struct ev_loop * ev_io_start(loop, &rtty->iow); del_tty(tty); + if (force_exit) + exit(0); } static void tty_login(struct rtty *rtty) @@ -188,6 +196,7 @@ static void tty_login(struct rtty *rtty) buffer_put_u8(&rtty->wb, sid); ev_io_start(rtty->loop, &rtty->iow); + connected = 1; log_info("new tty: %d\n", sid); }