httpd: sendfile support
[oweals/busybox.git] / networking / inetd.c
index 83123463f9db84a9dc1caffacc9840ea07325d14..211a8dcbf9b3410b3f904897b9656a7cbbee6b12 100644 (file)
  *                      setuid()
  */
 
-#include "busybox.h"
+#include "libbb.h"
 #include <syslog.h>
 #include <sys/un.h>
 
 #include <rpc/pmap_clnt.h>
 #endif
 
-#define _PATH_INETDPID  "/var/run/inetd.pid"
+extern char **environ;
+
 
+#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 */
@@ -1212,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);
 }
 
@@ -1248,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);
+int inetd_main(int argc, char **argv)
 {
        servtab_t *sep;
        struct passwd *pwd;
@@ -1263,7 +1265,6 @@ int inetd_main(int argc, char *argv[])
        sigset_t omask, wait_mask;
 
 #ifdef INETD_SETPROCTITLE
-       extern char **environ;
        char **envp = environ;
 
        Argv = argv;
@@ -1274,30 +1275,24 @@ int inetd_main(int argc, char *argv[])
        LastArg = envp[-1] + strlen(envp[-1]);
 #endif
 
-       opt = getopt32(argc, argv, "R:f", &stoomany);
-       if(opt & 1) {
-               toomany = xatoi_u(stoomany);
-       }
-       argc -= optind;
-       argv += optind;
-
        uid = getuid();
        if (uid != 0)
                config_filename = NULL;
-       if (argc > 0)
+
+       opt = getopt32(argc, argv, "R:f", &stoomany);
+       if (opt & 1)
+               toomany = xatoi_u(stoomany);
+       argv += optind;
+       argc -= optind;
+       if (argc)
                config_filename = argv[0];
        if (config_filename == NULL)
                bb_error_msg_and_die("non-root must specify a config file");
 
-#ifdef BB_NOMMU
-       if (!(opt & 2)) {
-               if (!re_execed)
-                       vfork_daemon_rexec(0, 0, argv);
-       }
-       bb_sanitize_stdio();
-#else
-       bb_sanitize_stdio_maybe_daemonize(!(opt & 2));
-#endif
+       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;
 
@@ -1307,13 +1302,7 @@ int inetd_main(int argc, char *argv[])
                setgroups(1, &gid);
        }
 
-       {
-               FILE *fp = fopen(_PATH_INETDPID, "w");
-               if (fp != NULL) {
-                       fprintf(fp, "%u\n", getpid());
-                       fclose(fp);
-               }
-       }
+       write_pidfile(_PATH_INETDPID);
 
        if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
                bb_perror_msg("getrlimit");