]> The Tcpdump Group git mirrors - libpcap/commitdiff
Do as much text output with rpcapd_log() as necessary.
authorGuy Harris <[email protected]>
Mon, 7 Jan 2019 21:00:28 +0000 (13:00 -0800)
committerGuy Harris <[email protected]>
Mon, 7 Jan 2019 21:00:28 +0000 (13:00 -0800)
Add a new LOGPRIO_DEBUG priority for logging, and use that instead of
SOCK_DEBUG_MESSAGE().

Use rpcapd_log() for command-line-argument errors as well.

This makes it easier to send errors somewhere other than the standard
output in daemons, and to control, at run time, whether to log debugging
messages.

rpcapd/daemon.c
rpcapd/fileconf.c
rpcapd/log-stderr.c
rpcapd/log.h
rpcapd/rpcapd.c
rpcapd/win32-svc.c

index 12b555ecceeb0189e3c03a55c15f701434dac311..dc92c0ac99301d781ec26251a4317d2ec70c0fc0 100644 (file)
@@ -683,7 +683,7 @@ daemon_serviceloop(SOCKET sockctrl_in, SOCKET sockctrl_out, SSL *ssl, int isacti
                                // This is used only in case of active mode.
                                //
                                client_told_us_to_close = 1;
-                               SOCK_DEBUG_MESSAGE("The other end system asked to close the connection.");
+                               rpcapd_log(LOGPRIO_DEBUG, "The other end system asked to close the connection.");
                                goto end;
                        }
 
@@ -881,8 +881,8 @@ end:
        }
 
        // Print message and return
-       SOCK_DEBUG_MESSAGE("I'm exiting from the child loop");
-       SOCK_DEBUG_MESSAGE(errbuf);
+       rpcapd_log(LOGPRIO_DEBUG, "I'm exiting from the child loop");
+       rpcapd_log(LOGPRIO_DEBUG, "%s", errbuf);
 
        return client_told_us_to_close;
 }
index d43fd6a4274470ea24f0cc866cb342b6c3d4487e..2f15c014204f18e6c9548221ac8c13c88333e66f 100644 (file)
@@ -43,7 +43,6 @@
 #include <signal.h>
 #include <pcap.h>              // for PCAP_ERRBUF_SIZE
 
-#include "sockutils.h"         // for SOCK_DEBUG_MESSAGE
 #include "portability.h"
 #include "rpcapd.h"
 #include "config_params.h"     // configuration file parameters
@@ -63,7 +62,6 @@ static char *skipws(char *ptr);
 void fileconf_read(void)
 {
        FILE *fp;
-       char msg[PCAP_ERRBUF_SIZE + 1];
        unsigned int num_active_clients;
 
        if ((fp = fopen(loadfile, "r")) != NULL)
@@ -478,8 +476,7 @@ done:
                        num_active_clients++;
                }
 
-               pcap_snprintf(msg, PCAP_ERRBUF_SIZE, "New passive host list: %s\n\n", hostlist);
-               SOCK_DEBUG_MESSAGE(msg);
+               rpcapd_log(LOGPRIO_DEBUG, "New passive host list: %s", hostlist);
                fclose(fp);
        }
 }
index 7adbd829e173324811e1dec9a6dabd12ba59f275..2956381c01d5d4a6d3eb869c7dc640a3c68bb153 100644 (file)
@@ -29,6 +29,10 @@ rpcapd_log(log_priority priority, const char *message, ...)
 
        switch (priority) {
 
+       case LOGPRIO_DEBUG:
+               tag = "";
+               break;
+
        case LOGPRIO_INFO:
                tag = "";
                break;
index b3806e1e5cb2ca0019225bb6596bf26736835941..6ed60469ed3d37e96bbb0733e0b4bff21aef926e 100644 (file)
@@ -3,6 +3,7 @@
 extern void rpcapd_log_init(void);
 
 typedef enum {
+       LOGPRIO_DEBUG,
        LOGPRIO_INFO,
        LOGPRIO_WARNING,
        LOGPRIO_ERROR
index f4c824e15cdddb031582c3fbb219eaac4d63533a..bd92b2484b53bff245a22e84ebc5ad3e5c8d9b8e 100644 (file)
@@ -191,12 +191,6 @@ int main(int argc, char *argv[])
        // Initialize errbuf
        memset(errbuf, 0, sizeof(errbuf));
 
-       if (sock_init(errbuf, PCAP_ERRBUF_SIZE) == -1)
-       {
-               SOCK_DEBUG_MESSAGE(errbuf);
-               exit(-1);
-       }
-
        strncpy(address, RPCAP_DEFAULT_NETADDR, MAX_LINE);
        strncpy(port, RPCAP_DEFAULT_NETPORT, MAX_LINE);
 
@@ -276,7 +270,7 @@ int main(int argc, char *argv[])
                                }
 
                                if (i > MAX_ACTIVE_LIST)
-                                       SOCK_DEBUG_MESSAGE("Only MAX_ACTIVE_LIST active connections are currently supported.");
+                                       rpcapd_log(LOGPRIO_ERROR, "Only MAX_ACTIVE_LIST active connections are currently supported.");
 
                                // I don't initialize the remaining part of the structure, since
                                // it is already zeroed (it is a global var)
@@ -315,13 +309,19 @@ int main(int argc, char *argv[])
 #ifndef _WIN32
        if (isdaemon && isrunbyinetd)
        {
-               fprintf(stderr, "rpcapd: -d and -i can't be used together\n");
+               rpcapd_log(LOGPRIO_ERROR, "rpcapd: -d and -i can't be used together");
                exit(1);
        }
 #endif
 
+       if (sock_init(errbuf, PCAP_ERRBUF_SIZE) == -1)
+       {
+               rpcapd_log(LOGPRIO_ERROR, "%s", errbuf);
+               exit(-1);
+       }
+
        if (savefile[0] && fileconf_save(savefile))
-               SOCK_DEBUG_MESSAGE("Error when saving the configuration to file");
+               rpcapd_log(LOGPRIO_DEBUG, "Error when saving the configuration to file");
 
        // If the file does not exist, it keeps the settings provided by the command line
        if (loadfile[0])
@@ -503,7 +503,7 @@ int main(int argc, char *argv[])
                // If this call succeeds, it is blocking on Win32
                //
                if (svc_start() != 1)
-                       SOCK_DEBUG_MESSAGE("Unable to start the service");
+                       rpcapd_log(LOGPRIO_DEBUG, "Unable to start the service");
 
                // When the previous call returns, the entire application has to be stopped.
                exit(0);
@@ -564,7 +564,7 @@ void main_startup(void)
                    (void *)&activelist[i], 0, NULL);
                if (threadId == 0)
                {
-                       SOCK_DEBUG_MESSAGE("Error creating the active child threads");
+                       rpcapd_log(LOGPRIO_DEBUG, "Error creating the active child threads");
                        continue;
                }
                CloseHandle(threadId);
@@ -599,7 +599,7 @@ void main_startup(void)
                //
                if (sock_initaddress((address[0]) ? address : NULL, port, &mainhints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
                {
-                       SOCK_DEBUG_MESSAGE(errbuf);
+                       rpcapd_log(LOGPRIO_DEBUG, "%s", errbuf);
                        return;
                }
 
@@ -678,7 +678,7 @@ void main_startup(void)
        //
        // We're done; exit.
        //
-       SOCK_DEBUG_MESSAGE(PROGRAM_NAME " is closing.\n");
+       rpcapd_log(LOGPRIO_DEBUG, PROGRAM_NAME " is closing.\n");
 
 #ifndef _WIN32
        //
@@ -822,7 +822,7 @@ static void main_reap_children(int sign _U_)
        // For reference, Stevens, pg 128
 
        while ((pid = waitpid(-1, &exitstat, WNOHANG)) > 0)
-               SOCK_DEBUG_MESSAGE("Child terminated");
+               rpcapd_log(LOGPRIO_DEBUG, "Child terminated");
 
        return;
 }
@@ -1348,10 +1348,9 @@ main_active(void *ptr)
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_family = activepars->ai_family;
 
-       pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Connecting to host %s, port %s, using protocol %s",
-                       activepars->address, activepars->port, (hints.ai_family == AF_INET) ? "IPv4":
-                       (hints.ai_family == AF_INET6) ? "IPv6" : "Unspecified");
-       SOCK_DEBUG_MESSAGE(errbuf);
+       rpcapd_log(LOGPRIO_DEBUG, "Connecting to host %s, port %s, using protocol %s",
+           activepars->address, activepars->port, (hints.ai_family == AF_INET) ? "IPv4":
+           (hints.ai_family == AF_INET6) ? "IPv6" : "Unspecified");
 
        // Initialize errbuf
        memset(errbuf, 0, sizeof(errbuf));
@@ -1359,7 +1358,7 @@ main_active(void *ptr)
        // Do the work
        if (sock_initaddress(activepars->address, activepars->port, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
        {
-               SOCK_DEBUG_MESSAGE(errbuf);
+               rpcapd_log(LOGPRIO_DEBUG, "%s", errbuf);
                return 0;
        }
 
@@ -1369,13 +1368,13 @@ main_active(void *ptr)
 
                if ((sockctrl = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
                {
-                       SOCK_DEBUG_MESSAGE(errbuf);
+                       rpcapd_log(LOGPRIO_DEBUG, "%s", errbuf);
 
                        pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error connecting to host %s, port %s, using protocol %s",
                                        activepars->address, activepars->port, (hints.ai_family == AF_INET) ? "IPv4":
                                        (hints.ai_family == AF_INET6) ? "IPv6" : "Unspecified");
 
-                       SOCK_DEBUG_MESSAGE(errbuf);
+                       rpcapd_log(LOGPRIO_DEBUG, "%s", errbuf);
 
                        sleep_secs(RPCAP_ACTIVE_WAIT);
 
index 8cc7dc92193ee8b6fe6119d3491fddde5906f19a..78d778ecb86a47a2eccc4d869cc1722cfef82acb 100644 (file)
@@ -33,7 +33,6 @@
 
 #include "rpcapd.h"
 #include <pcap.h>              // for PCAP_ERRBUF_SIZE
-#include "sockutils.h"         // for SOCK_DEBUG_MESSAGE
 #include "portability.h"
 #include "fileconf.h"
 
@@ -73,9 +72,7 @@ void svc_geterr(char *str)
                                  NULL, val, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                  (LPSTR) string, PCAP_ERRBUF_SIZE, NULL);
 
-       pcap_snprintf(message, PCAP_ERRBUF_SIZE, "%s failed with error %d: %s", str, val, string);
-
-       SOCK_DEBUG_MESSAGE(message);
+       rpcapd_log(LOGPRIO_ERROR, "%s failed with error %d: %s", str, val, string);
 }
 
 void WINAPI svc_control_handler(DWORD Opcode)