chiark / gitweb /
Use CLOCK_MONOTONIC for all our timing needs, when possible
[secnet.git] / secnet.c
index 1762fd7734fad14f53f0fb01c711d2d5d6589f50..177c2ef893bb459c67a387d3410c9a736c82f9d4 100644 (file)
--- a/secnet.c
+++ b/secnet.c
@@ -4,7 +4,7 @@
  *
  * secnet is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version d of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  * 
  * secnet is distributed in the hope that it will be useful, but
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <getopt.h>
 #include <errno.h>
+#include <time.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
@@ -51,6 +52,32 @@ static char *pidfile=NULL;
 bool_t require_root_privileges=False;
 cstring_t require_root_privileges_explanation=NULL;
 
+const char *const closure_type_names[] = {
+ [ CL_PURE       ] = "PURE"       ,
+ [ CL_RESOLVER   ] = "RESOLVER"          ,
+ [ CL_RANDOMSRC  ] = "RANDOMSRC"  ,
+ [ CL_SIGPUBKEY  ] = "SIGPUBKEY"  ,
+ [ CL_SIGPRIVKEY ] = "SIGPRIVKEY" ,
+ [ CL_COMM       ] = "COMM"      ,
+ [ CL_IPIF       ] = "IPIF"      ,
+ [ CL_LOG        ] = "LOG"       ,
+ [ CL_SITE       ] = "SITE"      ,
+ [ CL_TRANSFORM  ] = "TRANSFORM"  ,
+ [ CL_DH         ] = "DH"        ,
+ [ CL_HASH       ] = "HASH"      ,
+ [ CL_BUFFER     ] = "BUFFER"    ,
+ [ CL_NETLINK    ] = "NETLINK"   ,
+ [ CL_PRIVCACHE  ] = "PRIVCACHE"  ,
+};
+
+const char *closure_type_name(uint32_t ty, char buf[]) {
+    if (ty < ARRAY_SIZE(closure_type_names))
+       return closure_type_names[ty];
+    sprintf(buf, "CL#%.6u", (unsigned)ty);
+    buf[9] = 0;
+    return buf;
+}
+
 static pid_t secnet_pid;
 
 /* Structures dealing with poll() call */
@@ -187,11 +214,9 @@ static void parse_options(int argc, char **argv)
 static void setup(dict_t *config)
 {
     list_t *l;
-    item_t *site;
     dict_t *system;
     struct passwd *pw;
     struct cloc loc;
-    int i;
 
     l=dict_lookup(config,"system");
 
@@ -226,6 +251,12 @@ static void setup(dict_t *config)
              "that secnet retain root privileges while running.",
              require_root_privileges_explanation);
     }
+}
+
+static void start_sites(dict_t *config) {
+    int i;
+    list_t *l;
+    item_t *site;
 
     /* Go along site list, starting sites */
     l=dict_lookup(config,sites_key);
@@ -243,7 +274,7 @@ static void setup(dict_t *config)
                cfgfatal(site->loc,"system","non-site closure in site list");
            }
            s=site->data.closure->interface;
-           s->control(s->st,True);
+           s->startup(s->st);
        }
     }
 }
@@ -325,12 +356,19 @@ static void run(void)
     struct pollfd *fds=0;
     int allocdfds=0, shortfall=0;
 
-    Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
-
     do {
+#if USE_MONOTONIC
+       struct timespec ts;
+       if (clock_gettime(CLOCK_MONOTONIC, &ts)!=0) {
+           fatal_perror("main loop: clock_gettime(CLOCK_MONOTONIC,)");
+       }
+       tv_now_global.tv_sec =  ts.tv_sec;
+       tv_now_global.tv_usec = ts.tv_nsec / 1000;
+#else /* !USE_MONOTONIC */
        if (gettimeofday(&tv_now_global, NULL)!=0) {
            fatal_perror("main loop: gettimeofday");
        }
+#endif /* !USE_MONOTONIC */
        now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
                   ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
        idx=0;
@@ -493,16 +531,19 @@ int main(int argc, char **argv)
 {
     dict_t *config;
 
+    log_early_init();
     phase_hooks_init();
 
     enter_phase(PHASE_GETOPTS);
     parse_options(argc,argv);
+    log_early_setlevel();
 
     enter_phase(PHASE_READCONFIG);
     config=read_conffile(configfile);
 
     enter_phase(PHASE_SETUP);
     setup(config);
+    start_sites(config);
 
     if (just_check_config) {
        Message(M_INFO,"configuration file check complete\n");
@@ -511,6 +552,7 @@ int main(int argc, char **argv)
 
     enter_phase(PHASE_DAEMONIZE);
     become_daemon();
+    Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
     
     enter_phase(PHASE_GETRESOURCES);
     /* Appropriate phase hooks will have been run */