xatonum.h: add comment
[oweals/busybox.git] / networking / telnetd.c
index f06bff8b95f4827019ad7801b3a16a8762bc86eb..cccf03dfd54944bd88ff059eab52392117511db5 100644 (file)
@@ -141,7 +141,7 @@ remove_iacs(struct tsession *ts, int *pnum_totty)
                }
        }
 
-       num_totty = totty - ptr0;   
+       num_totty = totty - ptr0;
        *pnum_totty = num_totty;
        /* the difference between ptr and totty is number of iacs
           we removed from the stream. Adjust buf1 accordingly. */
@@ -228,10 +228,11 @@ make_new_session(
 #if ENABLE_FEATURE_TELNETD_STANDALONE
        ts->sockfd_read = sock;
        ndelay_on(sock);
-       if (!sock) /* We are called with fd 0 - we are in inetd mode */
-               sock++;
+       if (!sock) { /* We are called with fd 0 - we are in inetd mode */
+               sock++; /* so use fd 1 for output */
+               ndelay_on(sock);
+       }
        ts->sockfd_write = sock;
-       ndelay_on(sock);
        if (sock > maxfd)
                maxfd = sock;
 #else
@@ -262,7 +263,7 @@ make_new_session(
        if (pid < 0) {
                free(ts);
                close(fd);
-               /* sock_r and sock_w will be closed by caller */
+               /* sock will be closed by caller */
                bb_perror_msg("vfork");
                return NULL;
        }
@@ -313,6 +314,14 @@ make_new_session(
        _exit(1); /*bb_perror_msg_and_die("execv %s", loginpath);*/
 }
 
+/* Must match getopt32 string */
+enum {
+       OPT_WATCHCHILD = (1 << 2), /* -K */
+       OPT_INETD      = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
+       OPT_PORT       = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p */
+       OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
+};
+
 #if ENABLE_FEATURE_TELNETD_STANDALONE
 
 static void
@@ -320,6 +329,9 @@ free_session(struct tsession *ts)
 {
        struct tsession *t = sessions;
 
+       if (option_mask32 & OPT_INETD)
+               exit(0);
+
        /* Unlink this telnet session from the session list */
        if (t == ts)
                sessions = ts->next;
@@ -340,7 +352,7 @@ free_session(struct tsession *ts)
        close(ts->sockfd_read);
        /* We do not need to close(ts->sockfd_write), it's the same
         * as sockfd_read unless we are in inetd mode. But in inetd mode
-        * we do not free_session(), ever */
+        * we do not reach this */
        free(ts);
 
        /* Scan all sessions and find new maxfd */
@@ -362,8 +374,8 @@ free_session(struct tsession *ts)
 
 #else /* !FEATURE_TELNETD_STANDALONE */
 
-/* Never actually called */
-void free_session(struct tsession *ts);
+/* Used in main() only, thus exits. */
+#define free_session(ts) return 0
 
 #endif
 
@@ -406,13 +418,6 @@ int telnetd_main(int argc, char **argv)
                portnbr = 23,
        };
 #endif
-       enum {
-               OPT_WATCHCHILD = (1 << 2), /* -K */
-               OPT_INETD      = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
-               OPT_PORT       = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p */
-               OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
-       };
-
        /* 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" USE_FEATURE_TELNETD_STANDALONE("p:b:F"),
@@ -492,6 +497,7 @@ int telnetd_main(int argc, char **argv)
        while (ts) {
                struct tsession *next = ts->next; /* in case we free ts. */
                if (ts->shell_pid == -1) {
+                       /* Child died ad we detected that */
                        free_session(ts);
                } else {
                        if (ts->size1 > 0)       /* can write to pty */
@@ -552,8 +558,6 @@ int telnetd_main(int argc, char **argv)
                        if (count < 0) {
                                if (errno == EAGAIN)
                                        goto skip1;
-                               if (IS_INETD)
-                                       return 0;
                                goto kill_session;
                        }
                        ts->size1 -= count;
@@ -569,8 +573,6 @@ int telnetd_main(int argc, char **argv)
                        if (count < 0) {
                                if (errno == EAGAIN)
                                        goto skip2;
-                               if (IS_INETD)
-                                       return 0;
                                goto kill_session;
                        }
                        ts->size2 -= count;
@@ -601,8 +603,6 @@ int telnetd_main(int argc, char **argv)
                        if (count <= 0) {
                                if (count < 0 && errno == EAGAIN)
                                        goto skip3;
-                               if (IS_INETD)
-                                       return 0;
                                goto kill_session;
                        }
                        /* Ignore trailing NUL if it is there */
@@ -622,8 +622,6 @@ int telnetd_main(int argc, char **argv)
                        if (count <= 0) {
                                if (count < 0 && errno == EAGAIN)
                                        goto skip4;
-                               if (IS_INETD)
-                                       return 0;
                                goto kill_session;
                        }
                        ts->size2 += count;