Use the host compiler for build tool c-ify.
[darkstat-debian] / darkstat.c
index 2213986..47a81ad 100644 (file)
@@ -1,5 +1,5 @@
 /* darkstat 3
- * copyright (c) 2001-2011 Emil Mikulic.
+ * copyright (c) 2001-2014 Emil Mikulic.
  *
  * darkstat.c: signals, cmdline parsing, program body.
  *
 #include "db.h"
 #include "dns.h"
 #include "err.h"
-#include "http.h"
 #include "hosts_db.h"
+#include "http.h"
 #include "localip.h"
 #include "ncache.h"
 #include "now.h"
 #include "pidfile.h"
+#include "str.h"
 
 #include <assert.h>
 #include <errno.h>
@@ -126,48 +127,23 @@ static void cb_local_only(const char *arg _unused_)
 const char *opt_chroot_dir = NULL;
 static void cb_chroot(const char *arg) { opt_chroot_dir = arg; }
 
+const char *opt_base = NULL;
+static void cb_base(const char *arg) { opt_base = arg; }
+
 const char *opt_privdrop_user = NULL;
 static void cb_user(const char *arg) { opt_privdrop_user = arg; }
 
-const char *daylog_fn = NULL;
-static void cb_daylog(const char *arg)
-{
-   if (opt_chroot_dir == NULL)
-      errx(1, "the daylog file is relative to the chroot.\n"
-      "You must specify a --chroot dir before you can use --daylog.");
-   else
-      daylog_fn = arg;
-}
+const char *opt_daylog_fn = NULL;
+static void cb_daylog(const char *arg) { opt_daylog_fn = arg; }
 
 const char *import_fn = NULL;
-static void cb_import(const char *arg)
-{
-   if (opt_chroot_dir == NULL)
-      errx(1, "the import file is relative to the chroot.\n"
-      "You must specify a --chroot dir before you can use --import.");
-   else
-      import_fn = arg;
-}
+static void cb_import(const char *arg) { import_fn = arg; }
 
 const char *export_fn = NULL;
-static void cb_export(const char *arg)
-{
-   if ((opt_chroot_dir == NULL) && (opt_capfile == NULL))
-      errx(1, "the export file is relative to the chroot.\n"
-      "You must specify a --chroot dir before you can use --export.");
-   else
-      export_fn = arg;
-}
+static void cb_export(const char *arg) { export_fn = arg; }
 
 static const char *pid_fn = NULL;
-static void cb_pidfile(const char *arg)
-{
-   if (opt_chroot_dir == NULL)
-      errx(1, "the pidfile is relative to the chroot.\n"
-      "You must specify a --chroot dir before you can use --pidfile.");
-   else
-      pid_fn = arg;
-}
+static void cb_pidfile(const char *arg) { pid_fn = arg; }
 
 unsigned int opt_hosts_max = 1000;
 static void cb_hosts_max(const char *arg)
@@ -177,7 +153,7 @@ unsigned int opt_hosts_keep = 500;
 static void cb_hosts_keep(const char *arg)
 { opt_hosts_keep = parsenum(arg, 0); }
 
-unsigned int opt_ports_max = 200;
+unsigned int opt_ports_max = 60;
 static void cb_ports_max(const char *arg)
 { opt_ports_max = parsenum(arg, 65536); }
 
@@ -218,6 +194,7 @@ static struct cmdline_arg cmdline_args[] = {
    {"-p",             "port",            cb_port,         0},
    {"-b",             "bindaddr",        cb_bindaddr,    -1},
    {"-l",             "network/netmask", cb_local,        0},
+   {"--base",         "path",            cb_base,         0},
    {"--local-only",   NULL,              cb_local_only,   0},
    {"--snaplen",      "bytes",           cb_snaplen,      0},
    {"--pppoe",        NULL,              cb_pppoe,        0},
@@ -329,9 +306,7 @@ static void parse_cmdline(const int argc, char * const *argv) {
    if (opt_want_syslog)
       openlog("darkstat", LOG_NDELAY | LOG_PID, LOG_DAEMON);
 
-   /* some default values */
-   if (opt_chroot_dir == NULL)
-      opt_chroot_dir = CHROOT_DIR;
+   /* default value */
    if (opt_privdrop_user == NULL)
       opt_privdrop_user = PRIVDROP_USER;
 
@@ -381,8 +356,8 @@ static void run_from_capfile(void) {
    hosts_db_free();
    graph_free();
    verbosef("Total packets: %llu, bytes: %llu",
-            (unsigned long long)acct_total_packets,
-            (unsigned long long)acct_total_bytes);
+            (llu)acct_total_packets,
+            (llu)acct_total_bytes);
 }
 
 /* --- Program body --- */
@@ -411,6 +386,7 @@ main(int argc, char **argv)
    /* do this first as it forks - minimize memory use */
    if (opt_want_dns) dns_init(opt_privdrop_user);
    cap_start(opt_want_promisc); /* needs root */
+   http_init_base(opt_base);
    http_listen(opt_bindport);
    ncache_init(); /* must do before chroot() */
 
@@ -418,7 +394,7 @@ main(int argc, char **argv)
 
    /* Don't need root privs for these: */
    now_init();
-   if (daylog_fn != NULL) daylog_init(daylog_fn);
+   if (opt_daylog_fn != NULL) daylog_init(opt_daylog_fn);
    graph_init();
    hosts_db_init();
    if (import_fn != NULL) db_import(import_fn);
@@ -436,7 +412,10 @@ main(int argc, char **argv)
    daemonize_finish();
 
    while (running) {
-      int select_ret, max_fd = -1, use_timeout = 0;
+      int select_ret;
+      int max_fd = -1;
+      int use_timeout = 0;
+      int cap_ret;
       struct timeval timeout;
       struct timespec t;
       fd_set rs, ws;
@@ -475,10 +454,14 @@ main(int argc, char **argv)
       }
 
       graph_rotate();
-      cap_poll(&rs);
+      cap_ret = cap_poll(&rs);
       dns_poll();
       http_poll(&rs, &ws);
       timer_stop(&t, 1000000000, "event processing took longer than a second");
+
+      if (!cap_ret) {
+         running = 0;
+      }
    }
 
    verbosef("shutting down");
@@ -490,7 +473,7 @@ main(int argc, char **argv)
    if (export_fn != NULL) db_export(export_fn);
    hosts_db_free();
    graph_free();
-   if (daylog_fn != NULL) daylog_free();
+   if (opt_daylog_fn != NULL) daylog_free();
    ncache_free();
    if (pid_fn) pidfile_unlink();
    verbosef("shut down");