last_patch95 from vodz:
[oweals/busybox.git] / networking / inetd.c
index 047358c905dee884d10e7ae98f78f2a8d1087706..af262c39c8177535fdb8fe562feef5189a50d765 100644 (file)
 #include <signal.h>
 #include <netdb.h>
 #include <syslog.h>
-#include <pwd.h>
-#include <grp.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <getopt.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <time.h>
 
 #define _PATH_INETDCONF "/etc/inetd.conf"
 #define _PATH_INETDPID  "/var/run/inetd.pid"
@@ -156,6 +155,24 @@ static int     rlim_ofile_cur = OPEN_MAX;
 static struct rlimit   rlim_ofile;
 #endif
 
+#define INETD_UNSUPPORT_BILTIN 1
+
+/* Check unsupporting builtin */
+#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
+#undef INETD_UNSUPPORT_BILTIN
+#endif
+#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
+#undef INETD_UNSUPPORT_BILTIN
+#endif
+#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
+#undef INETD_UNSUPPORT_BILTIN
+#endif
+#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
+#undef INETD_UNSUPPORT_BILTIN
+#endif
+#ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
+#undef INETD_UNSUPPORT_BILTIN
+#endif
 
 static struct  servtab {
        char    *se_service;            /* name of service */
@@ -197,35 +214,28 @@ static int      timingout;
 static sigset_t blockmask, emptymask;
 
 
-#define INETD_UNSUPPORT_BILTIN 1
-
        /* Echo received data */
 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
-#undef INETD_UNSUPPORT_BILTIN
 static void echo_stream(int, struct servtab *);
 static void echo_dg(int, struct servtab *);
 #endif
        /* Internet /dev/null */
 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
-#undef INETD_UNSUPPORT_BILTIN
 static void discard_stream(int, struct servtab *);
 static void discard_dg(int, struct servtab *);
 #endif
        /* Return 32 bit time since 1900 */
 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
-#undef INETD_UNSUPPORT_BILTIN
 static void machtime_stream(int, struct servtab *);
 static void machtime_dg(int, struct servtab *);
 #endif
        /* Return human-readable time */
 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
-#undef INETD_UNSUPPORT_BILTIN
 static void daytime_stream(int, struct servtab *);
 static void daytime_dg(int, struct servtab *);
 #endif
        /* Familiar character generator */
 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
-#undef INETD_UNSUPPORT_BILTIN
 static void chargen_stream(int, struct servtab *);
 static void chargen_dg(int, struct servtab *);
 #endif
@@ -509,7 +519,7 @@ enter(struct servtab *cp)
 
        sep = (struct servtab *)malloc(sizeof (*sep));
        if (sep == NULL) {
-               syslog_err_and_discard_dg(SOCK_STREAM, memory_exhausted);
+               syslog_err_and_discard_dg(SOCK_STREAM, bb_msg_memory_exhausted);
        }
        *sep = *cp;
        sep->se_fd = -1;
@@ -536,7 +546,11 @@ bump_nofile(void)
        rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
        if (rl.rlim_cur <= rlim_ofile_cur) {
                syslog(LOG_ERR,
-                       "bump_nofile: cannot extend file limit, max = %d",
+#if _FILE_OFFSET_BITS == 64
+                       "bump_nofile: cannot extend file limit, max = %lld",
+#else
+                       "bump_nofile: cannot extend file limit, max = %ld",
+#endif
                        rl.rlim_cur);
                return -1;
        }
@@ -792,7 +806,9 @@ inetd_main(int argc, char *argv[])
        struct passwd *pwd;
        struct group *grp = NULL;
        struct sigaction sa;
-       int ch, pid;
+       int pid;
+       unsigned long opt;
+       char *sq;
        gid_t gid;
 
 #ifdef INETD_UNSUPPORT_BILTIN
@@ -814,14 +830,21 @@ inetd_main(int argc, char *argv[])
        LastArg = environ[-1] + strlen(environ[-1]);
 #endif
 
-       while ((ch = getopt(argc, argv, "q:")) != EOF)
-               switch(ch) {
-               case 'q':
+#if defined(__uClinux__)
+       opt = bb_getopt_ulflags(argc, argv, "q:f", &sq);
+       if (!(opt & 4)) {
+           daemon(0, 0);
+           /* reexec for vfork() do continue parent */
+           vfork_daemon_rexec(argc, argv, "-f");
+       }
+#else
+       opt = bb_getopt_ulflags(ac, av, "q:", &sq);
+       daemon(0, 0);
+#endif /* uClinux */
+
+       if(opt & 1) {
                        global_queuelen = atoi(optarg);
                        if (global_queuelen < 8) global_queuelen=8;
-                       break;
-               default:
-                       show_usage(); // "[-q len] [conf]"
                }
        argc -= optind;
        argv += optind;
@@ -829,8 +852,7 @@ inetd_main(int argc, char *argv[])
        if (argc > 0)
                CONFIG = argv[0];
 
-       daemon(0, 0);
-       openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
+       openlog(bb_applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
        {
                FILE *fp;