commit eb1810c924e5f99683ae3c8391ca58cbc5a96a88 from: Omar Polo date: Tue Jul 05 17:15:06 2022 UTC bind_socket early so that if it fails the user is promptly notified. This requires us to make sure that fds 0-2 are opened, so steal the same trick slowcgi have. commit - c56ccc84283eb9c7c5b4acd3ecdf245669145031 commit + eb1810c924e5f99683ae3c8391ca58cbc5a96a88 blob - 81359413f8f1662fb683a115f0f596b0b9c9adfe blob + 582f359c1f1a29c6957ed7fefa8a7e8656e744c8 --- lstun.c +++ lstun.c @@ -15,12 +15,14 @@ */ #include +#include #include #include #include #include #include +#include #include #include #include @@ -420,9 +422,26 @@ usage(void) int main(int argc, char **argv) { - int ch, i; + int ch, i, fd; const char *errstr; + struct stat sb; + /* + * Ensure we have fds 0-2 open so that we have no issue with + * calling bind_socket before daemon(3). + */ + for (i = 0; i < 3; ++i) { + if (fstat(i, &sb) == -1) { + if ((fd = open("/dev/null", O_RDWR)) != -1) { + if (dup2(fd, i) == -1) + exit(1); + if (fd > i) + close(fd); + } else + exit(1); + } + } + log_init(1, LOG_DAEMON); log_setverbose(1); @@ -458,14 +477,14 @@ main(int argc, char **argv) ssh_dest = argv[0]; + bind_socket(); + log_init(debug, LOG_DAEMON); log_setverbose(verbose); if (!debug) daemon(1, 0); - bind_socket(); - signal(SIGPIPE, SIG_IGN); event_init();