From: Guy Harris Date: Mon, 7 Jan 2019 21:00:28 +0000 (-0800) Subject: Do as much text output with rpcapd_log() as necessary. X-Git-Tag: libpcap-1.10-bp~660 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/117fb4c9a3b539d86e1aea3a8cca6b2ebb4c2917 Do as much text output with rpcapd_log() as necessary. 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. --- diff --git a/rpcapd/daemon.c b/rpcapd/daemon.c index 12b555ec..dc92c0ac 100644 --- a/rpcapd/daemon.c +++ b/rpcapd/daemon.c @@ -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; } diff --git a/rpcapd/fileconf.c b/rpcapd/fileconf.c index d43fd6a4..2f15c014 100644 --- a/rpcapd/fileconf.c +++ b/rpcapd/fileconf.c @@ -43,7 +43,6 @@ #include #include // 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); } } diff --git a/rpcapd/log-stderr.c b/rpcapd/log-stderr.c index 7adbd829..2956381c 100644 --- a/rpcapd/log-stderr.c +++ b/rpcapd/log-stderr.c @@ -29,6 +29,10 @@ rpcapd_log(log_priority priority, const char *message, ...) switch (priority) { + case LOGPRIO_DEBUG: + tag = ""; + break; + case LOGPRIO_INFO: tag = ""; break; diff --git a/rpcapd/log.h b/rpcapd/log.h index b3806e1e..6ed60469 100644 --- a/rpcapd/log.h +++ b/rpcapd/log.h @@ -3,6 +3,7 @@ extern void rpcapd_log_init(void); typedef enum { + LOGPRIO_DEBUG, LOGPRIO_INFO, LOGPRIO_WARNING, LOGPRIO_ERROR diff --git a/rpcapd/rpcapd.c b/rpcapd/rpcapd.c index f4c824e1..bd92b248 100644 --- a/rpcapd/rpcapd.c +++ b/rpcapd/rpcapd.c @@ -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); diff --git a/rpcapd/win32-svc.c b/rpcapd/win32-svc.c index 8cc7dc92..78d778ec 100644 --- a/rpcapd/win32-svc.c +++ b/rpcapd/win32-svc.c @@ -33,7 +33,6 @@ #include "rpcapd.h" #include // 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)