httpd: sendfile support
[oweals/busybox.git] / networking / inetd.c
index e22115a5cb3c6b24c9ed9f69bfd7f2b401eefafd..211a8dcbf9b3410b3f904897b9656a7cbbee6b12 100644 (file)
@@ -23,7 +23,7 @@
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
@@ -36,8 +36,7 @@
  * SUCH DAMAGE.
  */
 
-/*
- * Inetd - Internet super-server
+/* Inetd - Internet super-server
  *
  * This program invokes all internet services as needed.
  * connection-oriented services are invoked each time a
  * arrives; a process is created and passed a pending message
  * on file descriptor 0.  Datagram servers may either connect
  * to their peer, freeing up the original socket for inetd
- * to receive further messages on, or ``take over the socket'',
+ * to receive further messages on, or "take over the socket",
  * processing all arriving datagrams and, eventually, timing
- * out.  The first type of server is said to be ``multi-threaded'';
- * the second type of server ``single-threaded''.
+ * out.  The first type of server is said to be "multi-threaded";
+ * the second type of server "single-threaded".
  *
  * Inetd uses a configuration file which is read at startup
  * and, possibly, at some later time in response to a hangup signal.
- * The configuration file is ``free format'' with fields given in the
+ * The configuration file is "free format" with fields given in the
  * order shown below.  Continuation lines for an entry must begin with
  * a space or tab.  All fields must be present in each entry.
  *
  * Comment lines are indicated by a `#' in column 1.
  */
 
-/*
- * Here's the scoop concerning the user[.:]group feature:
+/* inetd rules for passing file descriptors to children
+ * (http://www.freebsd.org/cgi/man.cgi?query=inetd):
+ *
+ * The wait/nowait entry specifies whether the server that is invoked by
+ * inetd will take over the socket associated with the service access point,
+ * and thus whether inetd should wait for the server to exit before listen-
+ * ing for new service requests.  Datagram servers must use "wait", as
+ * they are always invoked with the original datagram socket bound to the
+ * specified service address.  These servers must read at least one datagram
+ * from the socket before exiting.  If a datagram server connects to its
+ * peer, freeing the socket so inetd can receive further messages on the
+ * socket, it is said to be a "multi-threaded" server; it should read one
+ * datagram from the socket and create a new socket connected to the peer.
+ * It should fork, and the parent should then exit to allow inetd to check
+ * for new service requests to spawn new servers.  Datagram servers which
+ * process all incoming datagrams on a socket and eventually time out are
+ * said to be "single-threaded".  The comsat(8), (biff(1)) and talkd(8)
+ * utilities are both examples of the latter type of datagram server.  The
+ * tftpd(8) utility is an example of a multi-threaded datagram server.
+ *
+ * Servers using stream sockets generally are multi-threaded and use the
+ * "nowait" entry. Connection requests for these services are accepted by
+ * inetd, and the server is given only the newly-accepted socket connected
+ * to a client of the service.  Most stream-based services operate in this
+ * manner.  Stream-based servers that use "wait" are started with the lis-
+ * tening service socket, and must accept at least one connection request
+ * before exiting.  Such a server would normally accept and process incoming
+ * connection requests until a timeout.
+ */
+
+/* Here's the scoop concerning the user[.:]group feature:
  *
  * 1) set-group-option off.
  *
  *      b) other:       setgid(specified group)
  *                      initgroups(name, specified group)
  *                      setuid()
- *
  */
 
-#include "busybox.h"
+#include "libbb.h"
 #include <syslog.h>
 #include <sys/un.h>
 
-//#define CONFIG_FEATURE_INETD_RPC
-//#define CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
-//#define CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
-//#define CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_TIME
-//#define CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
-//#define CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
-//#define CONFIG_FEATURE_IPV6
+//#define ENABLE_FEATURE_INETD_RPC 1
+//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO 1
+//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD 1
+//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME 1
+//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME 1
+//#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN 1
+//#define ENABLE_FEATURE_IPV6 1
 
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
 #include <rpc/rpc.h>
 #include <rpc/pmap_clnt.h>
 #endif
 
-#define _PATH_INETDCONF "/etc/inetd.conf"
-#define _PATH_INETDPID  "/var/run/inetd.pid"
+extern char **environ;
 
 
-#define TOOMANY         0               /* don't start more than TOOMANY */
+#define _PATH_INETDPID  "/var/run/inetd.pid"
 
 #define CNT_INTVL       60              /* servers in CNT_INTVL sec. */
 #define RETRYTIME       (60*10)         /* retry after bind or server fail */
 #endif
 
 /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
-#define FD_MARGIN       (8)
+#define FD_MARGIN       8
 static rlim_t rlim_ofile_cur = OPEN_MAX;
 static struct rlimit rlim_ofile;
 
 
 /* Check unsupporting builtin */
-#if defined CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
-               defined CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \
-               defined CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_TIME || \
-               defined CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME || \
-               defined CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
+       ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \
+       ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME || \
+       ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME || \
+       ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
 # define INETD_FEATURE_ENABLED
 #endif
 
-#if defined CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
-               defined CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \
-               defined CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
+       ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \
+       ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
 # define INETD_SETPROCTITLE
 #endif
 
@@ -189,7 +215,7 @@ typedef struct servtab {
        int se_socktype;                      /* type of socket to use */
        int se_family;                        /* address family */
        char *se_proto;                       /* protocol used */
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
        int se_rpcprog;                       /* rpc program number */
        int se_rpcversl;                      /* rpc program lowest version */
        int se_rpcversh;                      /* rpc program highest version */
@@ -211,7 +237,7 @@ typedef struct servtab {
        union {
                struct sockaddr se_un_ctrladdr;
                struct sockaddr_in se_un_ctrladdr_in;
-#ifdef CONFIG_FEATURE_IPV6
+#if ENABLE_FEATURE_IPV6
                struct sockaddr_in6 se_un_ctrladdr_in6;
 #endif
                struct sockaddr_un se_un_ctrladdr_un;
@@ -239,53 +265,53 @@ struct builtin {
 };
 
                /* Echo received data */
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
 static void echo_stream(int, servtab_t *);
 static void echo_dg(int, servtab_t *);
 #endif
                /* Internet /dev/null */
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
 static void discard_stream(int, servtab_t *);
 static void discard_dg(int, servtab_t *);
 #endif
                /* Return 32 bit time since 1900 */
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_TIME
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
 static void machtime_stream(int, servtab_t *);
 static void machtime_dg(int, servtab_t *);
 #endif
                /* Return human-readable time */
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
 static void daytime_stream(int, servtab_t *);
 static void daytime_dg(int, servtab_t *);
 #endif
                /* Familiar character generator */
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
 static void chargen_stream(int, servtab_t *);
 static void chargen_dg(int, servtab_t *);
 #endif
 
 static const struct builtin builtins[] = {
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
        /* Echo received data */
        {"echo", SOCK_STREAM, 1, 0, echo_stream,},
        {"echo", SOCK_DGRAM, 0, 0, echo_dg,},
 #endif
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
        /* Internet /dev/null */
        {"discard", SOCK_STREAM, 1, 0, discard_stream,},
        {"discard", SOCK_DGRAM, 0, 0, discard_dg,},
 #endif
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_TIME
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
        /* Return 32 bit time since 1900 */
        {"time", SOCK_STREAM, 0, 0, machtime_stream,},
        {"time", SOCK_DGRAM, 0, 0, machtime_dg,},
 #endif
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
        /* Return human-readable time */
        {"daytime", SOCK_STREAM, 0, 0, daytime_stream,},
        {"daytime", SOCK_DGRAM, 0, 0, daytime_dg,},
 #endif
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
        /* Familiar character generator */
        {"chargen", SOCK_STREAM, 1, 0, chargen_stream,},
        {"chargen", SOCK_DGRAM, 0, 0, chargen_dg,},
@@ -297,15 +323,14 @@ static const struct builtin builtins[] = {
 static int global_queuelen = 128;
 static int nsock, maxsock;
 static fd_set allsock;
-static int toomany = TOOMANY;
+static int toomany;
 static int timingout;
 static struct servent *sp;
 static uid_t uid;
 
-static char *CONFIG = _PATH_INETDCONF;
+static const char *config_filename = "/etc/inetd.conf";
 
 static FILE *fconfig;
-static char line[1024];
 static char *defhost;
 
 /* xstrdup(NULL) returns NULL, but this one
@@ -325,7 +350,7 @@ static int setconfig(void)
                fseek(fconfig, 0L, SEEK_SET);
                return 1;
        }
-       fconfig = fopen(CONFIG, "r");
+       fconfig = fopen(config_filename, "r");
        return (fconfig != NULL);
 }
 
@@ -339,7 +364,7 @@ static void endconfig(void)
        defhost = 0;
 }
 
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
 static void register_rpc(servtab_t *sep)
 {
        int n;
@@ -376,7 +401,7 @@ static void unregister_rpc(servtab_t *sep)
                        bb_error_msg("pmap_unset(%u, %u)", sep->se_rpcprog, n);
        }
 }
-#endif /* CONFIG_FEATURE_INETD_RPC */
+#endif /* FEATURE_INETD_RPC */
 
 static void freeconfig(servtab_t *cp)
 {
@@ -392,13 +417,13 @@ static void freeconfig(servtab_t *cp)
                free(cp->se_argv[i]);
 }
 
-static int bump_nofile (void)
+static int bump_nofile(void)
 {
 #define FD_CHUNK        32
 
        struct rlimit rl;
 
-       if (getrlimit (RLIMIT_NOFILE, &rl) < 0) {
+       if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
                bb_perror_msg("getrlimit");
                return -1;
        }
@@ -421,7 +446,6 @@ static int bump_nofile (void)
 
 static void setup(servtab_t *sep)
 {
-       int on = 1;
        int r;
 
        sep->se_fd = socket(sep->se_family, sep->se_socktype, 0);
@@ -429,13 +453,10 @@ static void setup(servtab_t *sep)
                bb_perror_msg("%s/%s: socket", sep->se_service, sep->se_proto);
                return;
        }
-#define turnon(fd, opt) \
-setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof(on))
-       if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
+       if (setsockopt_reuseaddr(sep->se_fd) < 0)
                bb_perror_msg("setsockopt(SO_REUSEADDR)");
-#undef turnon
 
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
        if (isrpcservice(sep)) {
                struct passwd *pwd;
 
@@ -490,6 +511,8 @@ setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof(on))
 
 static char *nextline(void)
 {
+#define line bb_common_bufsiz1
+
        char *cp;
        FILE *fd = fconfig;
 
@@ -513,17 +536,19 @@ static char *skip(char **cpp) /* int report; */
                return NULL;
        }
 
-again:
+ again:
        while (*cp == ' ' || *cp == '\t')
                cp++;
        if (*cp == '\0') {
                int c;
 
                c = getc(fconfig);
-               (void) ungetc(c, fconfig);
-               if (c == ' ' || c == '\t')
-                       if ((cp = nextline()))
+               ungetc(c, fconfig);
+               if (c == ' ' || c == '\t') {
+                       cp = nextline();
+                       if (cp)
                                goto again;
+               }
                *cpp = NULL;
                /* goto erp; */
                return NULL;
@@ -556,7 +581,7 @@ static servtab_t *dupconfig(servtab_t *sep)
        newtab->se_socktype = sep->se_socktype;
        newtab->se_family = sep->se_family;
        newtab->se_proto = xstrdup(sep->se_proto);
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
        newtab->se_rpcprog = sep->se_rpcprog;
        newtab->se_rpcversl = sep->se_rpcversl;
        newtab->se_rpcversh = sep->se_rpcversh;
@@ -588,10 +613,10 @@ static servtab_t *getconfigent(void)
        sep = new_servtab();
 
        /* memset(sep, 0, sizeof *sep); */
-more:
+ more:
        /* freeconfig(sep); */
 
-       while ((cp = nextline()) && *cp == '#');
+       while ((cp = nextline()) && *cp == '#') /* skip comment line */;
        if (cp == NULL) {
                /* free(sep); */
                return NULL;
@@ -648,13 +673,13 @@ more:
        } else {
                sep->se_family = AF_INET;
                if (sep->se_proto[strlen(sep->se_proto) - 1] == '6')
-#ifdef CONFIG_FEATURE_IPV6
+#if ENABLE_FEATURE_IPV6
                        sep->se_family = AF_INET6;
 #else
                        bb_error_msg("%s: IPV6 not supported", sep->se_proto);
 #endif
                if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
                        char *p, *ccp;
                        long l;
 
@@ -666,7 +691,7 @@ more:
                        *p++ = '\0';
                        l = strtol(p, &ccp, 0);
                        if (ccp == p || l < 0 || l > INT_MAX) {
                      badafterall:
+ badafterall:
                                bb_error_msg("%s/%s: bad rpc version", sep->se_service, p);
                                goto more;
                        }
@@ -680,7 +705,7 @@ more:
                        } else if (*ccp != '\0')
                                goto badafterall;
 #else
-               bb_error_msg("%s: rpc services not supported", sep->se_service);
+                       bb_error_msg("%s: rpc services not supported", sep->se_service);
 #endif
                }
        }
@@ -692,7 +717,7 @@ more:
                char *s = strchr(arg, '.');
                if (s) {
                        *s++ = '\0';
-                       sep->se_max = atoi(s);
+                       sep->se_max = xatoi(s);
                } else
                        sep->se_max = toomany;
        }
@@ -765,7 +790,7 @@ more:
        while (nsep != NULL) {
                nsep->se_checked = 1;
                if (nsep->se_family == AF_INET) {
-                       if (!strcmp(nsep->se_hostaddr, "*"))
+                       if (LONE_CHAR(nsep->se_hostaddr, '*'))
                                nsep->se_ctrladdr_in.sin_addr.s_addr = INADDR_ANY;
                        else if (!inet_aton(nsep->se_hostaddr, &nsep->se_ctrladdr_in.sin_addr)) {
                                struct hostent *hp;
@@ -802,7 +827,7 @@ more:
                        }
                }
 /* XXX BUG?: is this skip: label supposed to remain? */
      skip:
+ skip:
                nsep = nsep->se_next;
        }
 
@@ -841,7 +866,7 @@ more:
        sigaddset(&m, SIGHUP); \
        sigaddset(&m, SIGALRM); \
        sigprocmask(SIG_BLOCK, &m, NULL); \
-} while(0)
+} while (0)
 
 static servtab_t *enter(servtab_t *cp)
 {
@@ -851,7 +876,7 @@ static servtab_t *enter(servtab_t *cp)
        sep = new_servtab();
        *sep = *cp;
        sep->se_fd = -1;
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
        sep->se_rpcprog = -1;
 #endif
        Block_Using_Signals(omask);
@@ -885,7 +910,7 @@ static int matchconf(servtab_t *old, servtab_t *new)
                                        sizeof(new->se_ctrladdr_in.sin_addr)) != 0)
                return 0;
 
-#ifdef CONFIG_FEATURE_IPV6
+#if ENABLE_FEATURE_IPV6
        if (old->se_family == AF_INET6 && new->se_family == AF_INET6 &&
                        memcmp(&old->se_ctrladdr_in6.sin6_addr,
                                        &new->se_ctrladdr_in6.sin6_addr,
@@ -903,7 +928,7 @@ static void config(int sig ATTRIBUTE_UNUSED)
        char protoname[10];
 
        if (!setconfig()) {
-               bb_perror_msg("%s", CONFIG);
+               bb_perror_msg("%s", config_filename);
                return;
        }
        for (sep = servtab; sep; sep = sep->se_next)
@@ -928,7 +953,7 @@ static void config(int sig ATTRIBUTE_UNUSED)
                         */
                        if (
 #ifdef INETD_FEATURE_ENABLED
-                                        cp->se_bi == 0 &&
+                               cp->se_bi == 0 &&
 #endif
                                (sep->se_wait == 1 || cp->se_wait == 0))
                                sep->se_wait = cp->se_wait;
@@ -940,7 +965,7 @@ static void config(int sig ATTRIBUTE_UNUSED)
                                SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
 #undef SWAP
 
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
                        if (isrpcservice(sep))
                                unregister_rpc(sep);
                        sep->se_rpcversl = cp->se_rpcversl;
@@ -971,10 +996,10 @@ static void config(int sig ATTRIBUTE_UNUSED)
                        /* se_ctrladdr_in was set in getconfigent */
                        sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
 
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
                        if (isrpcservice(sep)) {
                                struct rpcent *rp;
-
+                               // FIXME: atoi_or_else(str, 0) would be handy here
                                sep->se_rpcprog = atoi(sep->se_service);
                                if (sep->se_rpcprog == 0) {
                                        rp = getrpcbyname(sep->se_service);
@@ -990,9 +1015,9 @@ static void config(int sig ATTRIBUTE_UNUSED)
                                        register_rpc(sep);
                        } else
 #endif
-                                {
-                               u_short port = htons(atoi(sep->se_service));
-
+                       {
+                               uint16_t port = htons(atoi(sep->se_service));
+                               // FIXME: atoi_or_else(str, 0) would be handy here
                                if (!port) {
                                         /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname));
                                        if (isdigit(protoname[strlen(protoname) - 1]))
@@ -1018,13 +1043,13 @@ static void config(int sig ATTRIBUTE_UNUSED)
                                        setup(sep);
                        }
                        break;
-#ifdef CONFIG_FEATURE_IPV6
+#if ENABLE_FEATURE_IPV6
                case AF_INET6:
                        sep->se_ctrladdr_in6.sin6_family = AF_INET6;
                        /* se_ctrladdr_in was set in getconfigent */
                        sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in6;
 
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
                        if (isrpcservice(sep)) {
                                struct rpcent *rp;
 
@@ -1043,8 +1068,8 @@ static void config(int sig ATTRIBUTE_UNUSED)
                                        register_rpc(sep);
                        } else
 #endif
-                               {
-                               u_short port = htons(atoi(sep->se_service));
+                       {
+                               uint16_t port = htons(atoi(sep->se_service));
 
                                if (!port) {
                                         /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname));
@@ -1071,9 +1096,9 @@ static void config(int sig ATTRIBUTE_UNUSED)
                                        setup(sep);
                        }
                        break;
-#endif /* CONFIG_FEATURE_IPV6 */
+#endif /* FEATURE_IPV6 */
                }
      serv_unknown:
+ serv_unknown:
                if (cp->se_next != NULL) {
                        servtab_t *tmp = cp;
 
@@ -1101,7 +1126,7 @@ static void config(int sig ATTRIBUTE_UNUSED)
                        nsock--;
                        (void) close(sep->se_fd);
                }
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
                if (isrpcservice(sep))
                        unregister_rpc(sep);
 #endif
@@ -1150,11 +1175,11 @@ static void retry(int sig ATTRIBUTE_UNUSED)
                        switch (sep->se_family) {
                        case AF_UNIX:
                        case AF_INET:
-#ifdef CONFIG_FEATURE_IPV6
+#if ENABLE_FEATURE_IPV6
                        case AF_INET6:
 #endif
                                setup(sep);
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
                                if (sep->se_fd != -1 && isrpcservice(sep))
                                        register_rpc(sep);
 #endif
@@ -1178,10 +1203,10 @@ static void goaway(int sig ATTRIBUTE_UNUSED)
                        (void) unlink(sep->se_service);
                        break;
                case AF_INET:
-#ifdef CONFIG_FEATURE_IPV6
+#if ENABLE_FEATURE_IPV6
                case AF_INET6:
 #endif
-#ifdef CONFIG_FEATURE_INETD_RPC
+#if ENABLE_FEATURE_INETD_RPC
                        if (sep->se_wait == 1 && isrpcservice(sep))
                                unregister_rpc(sep);   /* XXX signal race */
 #endif
@@ -1189,7 +1214,7 @@ static void goaway(int sig ATTRIBUTE_UNUSED)
                }
                (void) close(sep->se_fd);
        }
-       (void) unlink(_PATH_INETDPID);
+       remove_pidfile(_PATH_INETDPID);
        exit(0);
 }
 
@@ -1225,8 +1250,8 @@ inetd_setproctitle(char *a, int s)
 #endif
 
 
-int
-inetd_main(int argc, char *argv[])
+int inetd_main(int argc, char **argv);
+int inetd_main(int argc, char **argv)
 {
        servtab_t *sep;
        struct passwd *pwd;
@@ -1240,7 +1265,6 @@ inetd_main(int argc, char *argv[])
        sigset_t omask, wait_mask;
 
 #ifdef INETD_SETPROCTITLE
-       extern char **environ;
        char **envp = environ;
 
        Argv = argv;
@@ -1251,56 +1275,34 @@ inetd_main(int argc, char *argv[])
        LastArg = envp[-1] + strlen(envp[-1]);
 #endif
 
-       openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
+       uid = getuid();
+       if (uid != 0)
+               config_filename = NULL;
 
        opt = getopt32(argc, argv, "R:f", &stoomany);
-       if(opt & 1) {
-               char *e;
-
-               toomany = strtoul(stoomany, &e, 0);
-               if (!(toomany >= 0 && *e == '\0')) {
-                               toomany = TOOMANY;
-                               bb_perror_msg("-R %s: bad value for service invocation rate", stoomany);
-               }
-       }
-       argc -= optind;
+       if (opt & 1)
+               toomany = xatoi_u(stoomany);
        argv += optind;
-
-       uid = getuid();
-       if (uid != 0)
-               CONFIG = NULL;
-       if (argc > 0)
-               CONFIG = argv[0];
-       if (CONFIG == NULL)
+       argc -= optind;
+       if (argc)
+               config_filename = argv[0];
+       if (config_filename == NULL)
                bb_error_msg_and_die("non-root must specify a config file");
 
-       if (!(opt & 2)) {
-#ifdef BB_NOMMU
-               /* reexec for vfork() do continue parent */
-               vfork_daemon_rexec(0, 0, argc, argv, "-f");
-#else
-               xdaemon(0, 0);
-#endif
-       } else {
-               setsid();
-       }
+       if (!(opt & 2))
+               bb_daemonize_or_rexec(0, argv - optind);
+       else
+               bb_sanitize_stdio();
+       openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
        logmode = LOGMODE_SYSLOG;
 
        if (uid == 0) {
-               gid_t gid = getgid();
-
                /* If run by hand, ensure groups vector gets trashed */
+               gid_t gid = getgid();
                setgroups(1, &gid);
        }
 
-       {
-               FILE *fp = fopen(_PATH_INETDPID, "w");
-
-               if (fp != NULL) {
-                       fprintf(fp, "%u\n", getpid());
-                       (void) fclose(fp);
-               }
-       }
+       write_pidfile(_PATH_INETDPID);
 
        if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
                bb_perror_msg("getrlimit");
@@ -1317,7 +1319,6 @@ inetd_main(int argc, char *argv[])
        sigaddset(&sa.sa_mask, SIGHUP);
        sa.sa_handler = retry;
        sigaction(SIGALRM, &sa, NULL);
-       /* doconfig(); */
        config(SIGHUP);
        sa.sa_handler = config;
        sigaction(SIGHUP, &sa, NULL);
@@ -1380,7 +1381,7 @@ inetd_main(int argc, char *argv[])
                                        socklen_t plen = sizeof(peer);
 
                                        if (getpeername(ctrl, (struct sockaddr *) &peer, &plen) < 0) {
-                                               bb_error_msg("could not getpeername");
+                                               bb_error_msg("cannot getpeername");
                                                close(ctrl);
                                                continue;
                                        }
@@ -1460,7 +1461,7 @@ inetd_main(int argc, char *argv[])
                                        (*sep->se_bi->bi_fn)(ctrl, sep);
                                } else
 #endif
-                                       {
+                               {
                                        pwd = getpwnam(sep->se_user);
                                        if (pwd == NULL) {
                                                bb_error_msg("getpwnam: %s: no such user", sep->se_user);
@@ -1499,7 +1500,7 @@ inetd_main(int argc, char *argv[])
                                        sigaction(SIGPIPE, &sapipe, NULL);
                                        execv(sep->se_server, sep->se_argv);
                                        bb_perror_msg("execv %s", sep->se_server);
-do_exit1:
+ do_exit1:
                                        if (sep->se_socktype != SOCK_STREAM)
                                                recv(0, buf, sizeof(buf), 0);
                                        _exit(1);
@@ -1508,7 +1509,7 @@ do_exit1:
                        if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
                                close(ctrl);
                } /* for (sep = servtab...) */
-       } /* for(;;) */
+       } /* for (;;) */
 }
 
 /*
@@ -1516,9 +1517,9 @@ do_exit1:
  */
 #define BUFSIZE 4096
 
-#if defined(CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO) || \
-               defined(CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN) || \
-               defined(CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME)
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \
+       ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN || \
+       ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
 static int dg_badinput(struct sockaddr_in *dg_sin)
 {
        if (ntohs(dg_sin->sin_port) < IPPORT_RESERVED)
@@ -1530,7 +1531,7 @@ static int dg_badinput(struct sockaddr_in *dg_sin)
 }
 #endif
 
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
 /* Echo service -- echo data back */
 /* ARGSUSED */
 static void
@@ -1568,9 +1569,9 @@ echo_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
                return;
        (void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
 }
-#endif  /* CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO */
+#endif  /* FEATURE_INETD_SUPPORT_BUILTIN_ECHO */
 
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
 /* Discard service -- ignore data */
 /* ARGSUSED */
 static void
@@ -1595,10 +1596,10 @@ discard_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
 
        (void) read(s, buffer, sizeof(buffer));
 }
-#endif /* CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD */
+#endif /* FEATURE_INETD_SUPPORT_BUILTIN_DISCARD */
 
 
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
 #define LINESIZ 72
 static char ring[128];
 static char *endring;
@@ -1685,10 +1686,10 @@ chargen_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
        text[LINESIZ + 1] = '\n';
        (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
 }
-#endif /* CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN */
+#endif /* FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN */
 
 
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_TIME
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
 /*
  * Return a machine readable date and time, in the form of the
  * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
@@ -1697,7 +1698,7 @@ chargen_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
  * some seventy years Bell Labs was asleep.
  */
 
-static u_int machtime(void)
+static unsigned machtime(void)
 {
        struct timeval tv;
 
@@ -1705,14 +1706,14 @@ static u_int machtime(void)
                fprintf(stderr, "Unable to get time of day\n");
                return 0L;
        }
-       return htonl((u_int) tv.tv_sec + 2208988800UL);
+       return htonl((unsigned) tv.tv_sec + 2208988800UL);
 }
 
 /* ARGSUSED */
 static void
 machtime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED)
 {
-       u_int result;
+       unsigned result;
 
        result = machtime();
        (void) write(s, (char *) &result, sizeof(result));
@@ -1722,7 +1723,7 @@ machtime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED)
 static void
 machtime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
 {
-       u_int result;
+       unsigned result;
        /* struct sockaddr_storage ss; */
        struct sockaddr sa;
        struct sockaddr_in *dg_sin;
@@ -1739,19 +1740,20 @@ machtime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
        result = machtime();
        (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
 }
-#endif /* CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_TIME */
+#endif /* FEATURE_INETD_SUPPORT_BUILTIN_TIME */
 
 
-#ifdef CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
+#if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
 /* Return human-readable time of day */
 /* ARGSUSED */
 static void daytime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED)
 {
-       char buffer[256];
+       char buffer[32];
        time_t t;
 
        t = time(NULL);
 
+// fdprintf instead?
        (void) sprintf(buffer, "%.24s\r\n", ctime(&t));
        (void) write(s, buffer, strlen(buffer));
 }
@@ -1777,4 +1779,4 @@ daytime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED)
        (void) sprintf(buffer, "%.24s\r\n", ctime(&t));
        (void) sendto(s, buffer, strlen(buffer), 0, &sa, sizeof(sa));
 }
-#endif /* CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */
+#endif /* FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */