noip.c: Include the process-id in debugging output.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 2 May 2016 21:20:25 +0000 (22:20 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 2 May 2016 23:12:08 +0000 (00:12 +0100)
Reading dumps from complicated multi-process systems can be tricky
otherwise.

noip.c

diff --git a/noip.c b/noip.c
index f36c545..5cbb84e 100644 (file)
--- a/noip.c
+++ b/noip.c
@@ -161,8 +161,10 @@ static void import(void)
 /* Debugging */
 #ifdef DEBUG
 #  define D(body) { if (debug) { body } }
+#  define Dpid pid_t pid = debug ? getpid() : -1
 #else
 #  define D(body) ;
+#  define Dpid
 #endif
 
 /* Preservation of error status */
@@ -511,7 +513,7 @@ static void dump_aclnode(const aclnode *a)
   const char *p;
   int plen;
 
-  fprintf(stderr, "noip:   %c ", a->act ? '+' : '-');
+  fprintf(stderr, "noip(%d):   %c ", getpid(), a->act ? '+' : '-');
   plen = common_prefix_length(a->af, &a->minaddr, &a->maxaddr);
   p = inet_ntop(a->af, &a->minaddr, buf, sizeof(buf));
   fprintf(stderr, strchr(p, ':') ? "[%s]" : "%s", p);
@@ -536,7 +538,7 @@ static void dump_acl(const aclnode *a)
     dump_aclnode(a);
     act = a->act;
   }
-  fprintf(stderr, "noip:   [default policy: %s]\n",
+  fprintf(stderr, "noip(%d):   [default policy: %s]\n", getpid(),
          act == ALLOW ? "DENY" : "ALLOW");
 }
 
@@ -547,20 +549,23 @@ static int acl_allows_p(const aclnode *a, const struct sockaddr *sa)
 {
   unsigned short port = port_from_sockaddr(sa);
   int act = ALLOW;
+  Dpid;
 
   D({ char buf[ADDRBUFSZ];
-      fprintf(stderr, "noip: check %s\n",
+      fprintf(stderr, "noip(%d): check %s\n", pid,
              present_sockaddr(sa, 0, buf, sizeof(buf))); })
   for (; a; a = a->next) {
     D( dump_aclnode(a); )
     if (sockaddr_in_range_p(sa, &a->minaddr, &a->maxaddr) &&
        a->minport <= port && port <= a->maxport) {
-      D( fprintf(stderr, "noip: aha!  %s\n", a->act ? "ALLOW" : "DENY"); )
+      D( fprintf(stderr, "noip(%d): aha!  %s\n", pid,
+                a->act ? "ALLOW" : "DENY"); )
       return (a->act);
     }
     act = a->act;
   }
-  D( fprintf(stderr, "noip: nothing found: %s\n", act ? "DENY" : "ALLOW"); )
+  D( fprintf(stderr, "noip(%d): nothing found: %s\n", pid,
+            act ? "DENY" : "ALLOW"); )
   return (!act);
 }
 
@@ -634,7 +639,7 @@ static int encode_inet_addr(struct sockaddr_un *sun,
   char buf[ADDRBUFSZ];
   int rc;
 
-  D( fprintf(stderr, "noip: encode %s (%s)",
+  D( fprintf(stderr, "noip(%d): encode %s (%s)", getpid(),
             present_sockaddr(sa, 0, buf, sizeof(buf)),
             want == WANT_EXISTING ? "EXISTING" : "FRESH"); )
   sun->sun_family = AF_UNIX;
@@ -698,7 +703,7 @@ static int decode_inet_addr(struct sockaddr *sa, int af_hint,
   if (len > sizeof(*sun)) return (-1);
   ((char *)sun)[len] = 0;
   nn = strlen(sun->sun_path);
-  D( fprintf(stderr, "noip: decode `%s'", sun->sun_path); )
+  D( fprintf(stderr, "noip(%d): decode `%s'", getpid(), sun->sun_path); )
   if (af_hint && !sun->sun_path[0]) {
     wildcard_address(af_hint, sa);
     D( fprintf(stderr, " -- unbound socket\n"); )
@@ -1053,7 +1058,7 @@ static void parse_acl_line(char **pp, aclnode ***tail)
   return;
 
 bad:
-  D( fprintf(stderr, "noip: bad acl spec (ignored)\n"); )
+  D( fprintf(stderr, "noip(%d): bad acl spec (ignored)\n", getpid()); )
   return;
 }
 
@@ -1074,7 +1079,7 @@ static void parse_autoports(char **pp)
   return;
 
 bad:
-  D( fprintf(stderr, "bad port range (ignored)\n"); )
+  D( fprintf(stderr, "noip(%d): bad port range (ignored)\n", getpid()); )
   return;
 }
 
@@ -1098,6 +1103,7 @@ static void readconfig(void)
   char buf[1024];
   size_t n;
   char *p, *q, *cmd;
+  Dpid;
 
   parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail);
   parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail);
@@ -1108,11 +1114,11 @@ static void readconfig(void)
   }
   if ((p = getenv("NOIP_CONFIG")) == 0)
     snprintf(p = buf, sizeof(buf), "%s/.noip", home());
-  D( fprintf(stderr, "noip: config file: %s\n", p); )
+  D( fprintf(stderr, "noip(%d): config file: %s\n", pid, p); )
 
   if ((fp = fopen(p, "r")) == 0) {
-    D( fprintf(stderr, "noip: couldn't read config: %s\n",
-              strerror(errno)); )
+    D( fprintf(stderr, "noip(%d): couldn't read config: %s\n",
+              pid, strerror(errno)); )
     goto done;
   }
   while (fgets(buf, sizeof(buf), fp)) {
@@ -1153,12 +1159,12 @@ done:
     snprintf(buf, sizeof(buf), "%s/noip-%s", tmpdir(), user());
     sockdir = xstrdup(buf);
   }
-  D( fprintf(stderr, "noip: socketdir: %s\n", sockdir);
-     fprintf(stderr, "noip: autoports: %u-%u\n",
-            minautoport, maxautoport);
-     fprintf(stderr, "noip: realbind acl:\n");
+  D( fprintf(stderr, "noip(%d): socketdir: %s\n", pid, sockdir);
+     fprintf(stderr, "noip(%d): autoports: %u-%u\n",
+            pid, minautoport, maxautoport);
+     fprintf(stderr, "noip(%d): realbind acl:\n", pid);
      dump_acl(bind_real);
-     fprintf(stderr, "noip: realconnect acl:\n");
+     fprintf(stderr, "noip(%d): realconnect acl:\n", pid);
      dump_acl(connect_real); )
 }
 
@@ -1410,6 +1416,7 @@ static void cleanup_sockdir(void)
   address addr;
   struct sockaddr_un sun;
   struct stat st;
+  Dpid;
 
   if ((dir = opendir(sockdir)) == 0) return;
   sun.sun_family = AF_UNIX;
@@ -1420,13 +1427,13 @@ static void cleanup_sockdir(void)
     if (decode_inet_addr(&addr.sa, 0, &sun, SUN_LEN(&sun)) ||
        stat(sun.sun_path, &st) ||
        !S_ISSOCK(st.st_mode)) {
-      D( fprintf(stderr, "noip: ignoring unknown socketdir entry `%s'\n",
-                sun.sun_path); )
+      D( fprintf(stderr, "noip(%d): ignoring unknown socketdir entry `%s'\n",
+                pid, sun.sun_path); )
       continue;
     }
     if (unix_socket_status(&sun, 0) == STALE) {
-      D( fprintf(stderr, "noip: clearing away stale socket %s\n",
-                d->d_name); )
+      D( fprintf(stderr, "noip(%d): clearing away stale socket %s\n",
+                pid, d->d_name); )
       unlink(sun.sun_path);
     }
   }
@@ -1441,7 +1448,9 @@ static void get_local_ipaddrs(void)
   struct ifaddrs *ifa_head, *ifa;
   ipaddr a;
   int i;
+  Dpid;
 
+  D( fprintf(stderr, "noip(%d): fetching local addresses...\n", pid); )
   if (getifaddrs(&ifa_head)) { perror("getifaddrs"); return; }
   for (n_local_ipaddrs = 0, ifa = ifa_head;
        n_local_ipaddrs < MAX_LOCAL_IPADDRS && ifa;
@@ -1450,7 +1459,8 @@ static void get_local_ipaddrs(void)
       continue;
     ipaddr_from_sockaddr(&a, ifa->ifa_addr);
     D({ char buf[ADDRBUFSZ];
-       fprintf(stderr, "noip: local addr %s = %s", ifa->ifa_name,
+       fprintf(stderr, "noip(%d):   local addr %s = %s", pid,
+               ifa->ifa_name,
                inet_ntop(ifa->ifa_addr->sa_family, &a,
                          buf, sizeof(buf))); })
     for (i = 0; i < n_local_ipaddrs; i++) {