- struct dhcpOfferedAddr *lease;
- int pid_fd;
- int max_sock;
- int sig;
- unsigned long num_ips;
-
- OPEN_LOG("udhcpd");
- LOG(LOG_INFO, "udhcp server (v%s) started", VERSION);
-
- memset(&server_config, 0, sizeof(struct server_config_t));
-
- if (argc < 2)
- read_config(DHCPD_CONF_FILE);
- else read_config(argv[1]);
-
- pid_fd = pidfile_acquire(server_config.pidfile);
- pidfile_write_release(pid_fd);
-
- if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
+ struct dhcpOfferedAddr *lease, static_lease;
+
+ opt = getopt32(argv, "fS");
+ argv += optind;
+
+ if (!(opt & 1)) { /* no -f */
+ bb_daemonize_or_rexec(0, argv);
+ logmode &= ~LOGMODE_STDIO;
+ }
+
+ if (opt & 2) { /* -S */
+ openlog(applet_name, LOG_PID, LOG_LOCAL0);
+ logmode |= LOGMODE_SYSLOG;
+ }
+
+ /* Would rather not do read_config before daemonization -
+ * otherwise NOMMU machines will parse config twice */
+ read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
+
+ /* Make sure fd 0,1,2 are open */
+ bb_sanitize_stdio();
+ /* Equivalent of doing a fflush after every \n */
+ setlinebuf(stdout);
+
+ /* Create pidfile */
+ write_pidfile(server_config.pidfile);
+ /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
+
+ bb_info_msg("%s (v%s) started", applet_name, BB_VER);
+
+ option = find_option(server_config.options, DHCP_LEASE_TIME);
+ server_config.lease = LEASE_TIME;
+ if (option) {