+ enum {
+ IS_INETD = 1,
+ master_fd = -1,
+ };
+#endif
+ INIT_G();
+
+ /* -w NUM, and implies -F. -w and -i don't mix */
+ IF_FEATURE_TELNETD_INETD_WAIT(opt_complementary = "wF:w+:i--w:w--i";)
+ /* Even if !STANDALONE, we accept (and ignore) -i, thus people
+ * don't need to guess whether it's ok to pass -i to us */
+ opt = getopt32(argv, "f:l:Ki"
+ IF_FEATURE_TELNETD_STANDALONE("p:b:F")
+ IF_FEATURE_TELNETD_INETD_WAIT("Sw:"),
+ &G.issuefile, &G.loginpath
+ IF_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr)
+ IF_FEATURE_TELNETD_INETD_WAIT(, &sec_linger)
+ );
+ if (!IS_INETD /*&& !re_execed*/) {
+ /* inform that we start in standalone mode?
+ * May be useful when people forget to give -i */
+ /*bb_error_msg("listening for connections");*/
+ if (!(opt & OPT_FOREGROUND)) {
+ /* DAEMON_CHDIR_ROOT was giving inconsistent
+ * behavior with/without -F, -i */
+ bb_daemonize_or_rexec(0 /*was DAEMON_CHDIR_ROOT*/, argv);
+ }
+ }
+ /* Redirect log to syslog early, if needed */
+ if (IS_INETD || (opt & OPT_SYSLOG) || !(opt & OPT_FOREGROUND)) {
+ openlog(applet_name, LOG_PID, LOG_DAEMON);
+ logmode = LOGMODE_SYSLOG;
+ }
+#if ENABLE_FEATURE_TELNETD_STANDALONE
+ if (IS_INETD) {
+ G.sessions = make_new_session(0);
+ if (!G.sessions) /* pty opening or vfork problem, exit */
+ return 1; /* make_new_session printed error message */
+ } else {
+ master_fd = 0;
+ if (!(opt & OPT_WAIT)) {
+ unsigned portnbr = 23;
+ if (opt & OPT_PORT)
+ portnbr = xatou16(opt_portnbr);
+ master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
+ xlisten(master_fd, 1);
+ }
+ close_on_exec_on(master_fd);
+ }
+#else
+ G.sessions = make_new_session();
+ if (!G.sessions) /* pty opening or vfork problem, exit */
+ return 1; /* make_new_session printed error message */