diff --git a/src/main.c b/src/main.c index 0466757..a8149ba 100644 --- a/src/main.c +++ b/src/main.c @@ -29,6 +29,7 @@ #include #include "log/log.h" +#include "utils.h" #include "rtty.h" enum { @@ -235,6 +236,26 @@ int main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); + if (!rtty.devid) { + log_err("you must specify an id for your device\n"); + return -1; + } + + if (!valid_id(rtty.devid)) { + log_err("invalid device id\n"); + return -1; + } + + if (find_login(rtty.login_path, sizeof(rtty.login_path) - 1) < 0) { + log_err("the program 'login' is not found\n"); + return -1; + } + + if (getuid() > 0) { + log_err("Operation not permitted, must be run as root\n"); + return -1; + } + if (background && daemon(0, 0)) log_err("Can't run in the background: %s\n", strerror(errno)); @@ -243,11 +264,6 @@ int main(int argc, char **argv) log_info("rtty version %s\n", RTTY_VERSION_STRING); - if (getuid() > 0) { - log_err("Operation not permitted, must be run as root\n"); - return -1; - } - ev_signal_init(&signal_watcher, signal_cb, SIGINT); ev_signal_start(loop, &signal_watcher); diff --git a/src/rtty.c b/src/rtty.c index 21e4766..4ae3be9 100644 --- a/src/rtty.c +++ b/src/rtty.c @@ -34,12 +34,9 @@ #include "file.h" #include "rtty.h" #include "list.h" -#include "utils.h" #include "command.h" #include "log/log.h" -static char login_path[128]; /* /bin/login */ - static void del_tty(struct tty *tty) { struct rtty *rtty = tty->rtty; @@ -194,9 +191,9 @@ static void tty_login(struct rtty *rtty, const char *sid) if (pid == 0) { if (rtty->username) - execl(login_path, "login", "-f", rtty->username, NULL); + execl(rtty->login_path, "login", "-f", rtty->username, NULL); else - execl(login_path, "login", NULL); + execl(rtty->login_path, "login", NULL); exit(1); } @@ -762,21 +759,6 @@ static void rtty_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents) int rtty_start(struct rtty *rtty) { - if (!rtty->devid) { - log_err("you must specify an id for your device\n"); - return -1; - } - - if (!valid_id(rtty->devid)) { - log_err("invalid device id\n"); - return -1; - } - - if (find_login(login_path, sizeof(login_path) - 1) < 0) { - log_err("the program 'login' is not found\n"); - return -1; - } - rtty_run_state(RTTY_STATE_DISCONNECTED); ev_init(&rtty->tmr, rtty_timer_cb); diff --git a/src/rtty.h b/src/rtty.h index fb911bb..b317ed5 100644 --- a/src/rtty.h +++ b/src/rtty.h @@ -92,6 +92,7 @@ struct tty { }; struct rtty { + char login_path[128]; /* /bin/login */ const char *host; int port; int sock;