Add signal handling: SIGUSR1 - dump cache; SIGHUP - restart server

This commit is contained in:
df 2020-07-27 15:18:17 +01:00
parent 0aa116dbce
commit d7c62ad55c
3 changed files with 69 additions and 19 deletions

1
mdns.h
View File

@ -86,6 +86,7 @@ void mdns_show_meminfo(void);
#else
#define DEBUG_PRINTF(...) ((void) 0)
#endif
#define MDNS_RR_DEBUG_PRINTF(...) printf(__VA_ARGS__)
struct rr_data_srv {

43
mdnsd.c
View File

@ -128,6 +128,7 @@ struct mdnsd {
int sockfd;
int notify_pipe[2];
int stop_flag;
int dump_cache;
int sendmsg_requested;
int domain;
@ -174,13 +175,13 @@ static void log_message(int loglevel, char *fmt_str, ...) {
fprintf(stderr, "%s\n", buf);
}
#ifdef MDNSD_RR_DEBUG
//#ifdef MDNSD_RR_DEBUG
static void print_rr_entry(struct rr_entry *rr_e)
{
char *str1, *str2;
if (!rr_e) {
DEBUG_PRINTF("ERROR: No RR Entry\n");
MDNS_RR_DEBUG_PRINTF("ERROR: No RR Entry\n");
return;
}
@ -198,12 +199,12 @@ static void print_rr_entry(struct rr_entry *rr_e)
str2 = NULL;
}
DEBUG_PRINTF("type:%s, ttl=%d, time=%d, ca_fl=%d, rr_class=%d, name=[%s]", rr_get_type_name(rr_e->type), rr_e->ttl, (unsigned int)(time(NULL) - rr_e->update_time), (int)rr_e->cache_flush, rr_e->rr_class, str1 ? str1 : "NULL");
MDNS_RR_DEBUG_PRINTF("type:%s, ttl=%d, time=%d, ca_fl=%d, rr_class=%d, name=[%s]", rr_get_type_name(rr_e->type), rr_e->ttl, (unsigned int)(time(NULL) - rr_e->update_time), (int)rr_e->cache_flush, rr_e->rr_class, str1 ? str1 : "NULL");
if (rr_e->type == RR_SRV || rr_e->type == RR_PTR) {
DEBUG_PRINTF(", target=[%s]\n", str2 ? str2 : "NULL");
MDNS_RR_DEBUG_PRINTF(", target=[%s]\n", str2 ? str2 : "NULL");
} else {
DEBUG_PRINTF("\n");
MDNS_RR_DEBUG_PRINTF("\n");
}
if (str1) {
@ -216,15 +217,17 @@ static void print_rr_entry(struct rr_entry *rr_e)
static void print_cache(struct mdnsd *svr)
{
DEBUG_PRINTF("\n");
DEBUG_PRINTF(" Multicast DNS Cache\n");
MDNS_RR_DEBUG_PRINTF("\n");
MDNS_RR_DEBUG_PRINTF(" Multicast DNS Cache\n");
for (struct rr_group *group = svr->cache; group; group = group->next) {
char *pname = group->name? nlabel_to_str(group->name): NULL;
DEBUG_PRINTF("==================================================\n");
DEBUG_PRINTF(" Group: %s\n", pname ? pname : "Unknown");
DEBUG_PRINTF("==================================================\n");
MDNS_RR_DEBUG_PRINTF(
"==================================================\n"
" Group: %s\n"
"==================================================\n",
pname ? pname : "Unknown");
if (pname) {
MDNS_FREE(pname);
}
@ -236,10 +239,9 @@ static void print_cache(struct mdnsd *svr)
}
}
}
DEBUG_PRINTF("==================================================\n");
DEBUG_PRINTF("\n");
MDNS_RR_DEBUG_PRINTF("==================================================\n\n");
}
#endif /* MDNSD_RR_DEBUG */
//#endif /* MDNSD_RR_DEBUG */
static int check_mdns_domain(const char *name)
{
@ -1252,7 +1254,7 @@ static void main_loop(struct mdnsd *svr) {
continue;
}
DEBUG_PRINTF("data from=%s:%d size=%ld\n", inet_ntoa(fromaddr.sin_addr), (long) recvsize);
DEBUG_PRINTF("data from=%s:%d size=%ld\n", inet_ntoa(fromaddr.sin_addr), ntohl(fromaddr.sin_port), (long) recvsize);
struct mdns_pkt *mdns = mdns_parse_pkt(pkt_buffer, recvsize);
if (mdns != NULL) {
struct sockaddr_in toaddr = fromaddr;
@ -1356,6 +1358,11 @@ static void main_loop(struct mdnsd *svr) {
break; /* exit main_loop */
sem_post(&svr->sendmsg_sem);
}
if (svr->dump_cache) {
svr->dump_cache = 0;
print_cache(svr);
}
}
if (svr->sendmsg_requested && svr->stop_flag) {
@ -1793,6 +1800,7 @@ static int init_mdns_context(int domain)
pthread_mutex_init(&g_svr->data_lock, NULL);
sem_init(&g_svr->sendmsg_sem, 0, 0);
g_svr->sendmsg_requested = 1;
g_svr->dump_cache= 0;
/* init thread */
if (pthread_attr_init(&attr) != 0) {
@ -2001,6 +2009,11 @@ static void mdns_cmd_mutex_unlock(void)
}
}
static void handler_dump_cache(int sig) {
(void)sig;
g_svr->dump_cache = 1;
}
/////////////////////////////////////////////////////
// Public Functions
/////////////////////////////////////////////////////
@ -2043,6 +2056,8 @@ int mdnsd_start(const char *desired_hostname, const char *netif_nameOrAddress)
goto out_with_mutex;
}
signal(SIGUSR1, handler_dump_cache);
/* success */
result = 0;

View File

@ -32,6 +32,7 @@
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#ifdef _WIN32
#include <winsock2.h>
@ -275,13 +276,30 @@ void dump_hosts(const struct hostent * hostents) {
}
}
static void handler_stop(int sig) {
(void)sig;
mdnsd_stop();
#ifdef WIN32
winsock_close();
#endif
exit(0);
}
volatile static int running;
static void handler_restart(int sig) {
/* a dummy handler so that sleep() will end early */
(void)sig;
if (running != 0)
running = 2;
}
int
main(int argc, char **argv)
{
char hostname[0x100], ifname[0x120], fullname[0x108];
struct in_addr saddr;
int notrunning = 1;
if (gethostname(hostname, sizeof(hostname) - 1) == -1)
{
@ -300,6 +318,21 @@ main(int argc, char **argv)
NULL
};
running = 0;
signal(SIGINT, handler_stop);
signal(SIGTERM, handler_stop);
#if defined(SIGPIPE)
signal(SIGPIPE, SIG_IGN);
#endif
#if defined(SIGQUIT)
signal(SIGQUIT, handler_stop);
#endif
#if defined(SIGHUP)
signal(SIGHUP, handler_restart);
#endif
for (saddr.s_addr = 0;;sleep(61)) {
struct hostent *hp = gethostbyname(hostname);
char * ip_addr;
@ -310,8 +343,8 @@ main(int argc, char **argv)
return 1;
}
if (saddr.s_addr != *(in_addr_t *)(hp->h_addr_list[0])) {
if ((notrunning == 0) && (0 > mdnsd_stop())) {
if (running != 1 || saddr.s_addr != *(in_addr_t *)(hp->h_addr_list[0])) {
if (running && (0 > mdnsd_stop())) {
fprintf(stderr, "mdnsd_stop() error\n");
return 1;
}
@ -326,7 +359,7 @@ main(int argc, char **argv)
fprintf(stderr, "mdnsd_start() error\n");
return 1;
}
notrunning = 0;
running = 1;
/* service must have FQDN */
if (0 <= mdnsd_register_service(
@ -340,6 +373,7 @@ main(int argc, char **argv)
if (0 <= mdnsd_discover_service("_http._tcp.local", 5000, &svcinfo, &numserv)) {
/* allow an extra entry for own host */
struct hostent * hosts = read_hosts(numserv+1);
if (!hosts) {
fprintf(stderr,"null hosts\n");
@ -362,7 +396,7 @@ main(int argc, char **argv)
}
}
if ((notrunning == 0) && (0 > mdnsd_stop())) {
if (running && (0 > mdnsd_stop())) {
fprintf(stderr,"mdnsd_stop() error\n");
}