Add signal handling: SIGUSR1 - dump cache; SIGHUP - restart server
This commit is contained in:
1
mdns.h
1
mdns.h
@@ -86,6 +86,7 @@ void mdns_show_meminfo(void);
|
|||||||
#else
|
#else
|
||||||
#define DEBUG_PRINTF(...) ((void) 0)
|
#define DEBUG_PRINTF(...) ((void) 0)
|
||||||
#endif
|
#endif
|
||||||
|
#define MDNS_RR_DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
||||||
|
|
||||||
|
|
||||||
struct rr_data_srv {
|
struct rr_data_srv {
|
||||||
|
|||||||
43
mdnsd.c
43
mdnsd.c
@@ -128,6 +128,7 @@ struct mdnsd {
|
|||||||
int sockfd;
|
int sockfd;
|
||||||
int notify_pipe[2];
|
int notify_pipe[2];
|
||||||
int stop_flag;
|
int stop_flag;
|
||||||
|
int dump_cache;
|
||||||
int sendmsg_requested;
|
int sendmsg_requested;
|
||||||
int domain;
|
int domain;
|
||||||
|
|
||||||
@@ -174,13 +175,13 @@ static void log_message(int loglevel, char *fmt_str, ...) {
|
|||||||
fprintf(stderr, "%s\n", buf);
|
fprintf(stderr, "%s\n", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MDNSD_RR_DEBUG
|
//#ifdef MDNSD_RR_DEBUG
|
||||||
static void print_rr_entry(struct rr_entry *rr_e)
|
static void print_rr_entry(struct rr_entry *rr_e)
|
||||||
{
|
{
|
||||||
char *str1, *str2;
|
char *str1, *str2;
|
||||||
|
|
||||||
if (!rr_e) {
|
if (!rr_e) {
|
||||||
DEBUG_PRINTF("ERROR: No RR Entry\n");
|
MDNS_RR_DEBUG_PRINTF("ERROR: No RR Entry\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,12 +199,12 @@ static void print_rr_entry(struct rr_entry *rr_e)
|
|||||||
str2 = NULL;
|
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) {
|
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 {
|
} else {
|
||||||
DEBUG_PRINTF("\n");
|
MDNS_RR_DEBUG_PRINTF("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str1) {
|
if (str1) {
|
||||||
@@ -216,15 +217,17 @@ static void print_rr_entry(struct rr_entry *rr_e)
|
|||||||
|
|
||||||
static void print_cache(struct mdnsd *svr)
|
static void print_cache(struct mdnsd *svr)
|
||||||
{
|
{
|
||||||
DEBUG_PRINTF("\n");
|
MDNS_RR_DEBUG_PRINTF("\n");
|
||||||
DEBUG_PRINTF(" Multicast DNS Cache\n");
|
MDNS_RR_DEBUG_PRINTF(" Multicast DNS Cache\n");
|
||||||
|
|
||||||
for (struct rr_group *group = svr->cache; group; group = group->next) {
|
for (struct rr_group *group = svr->cache; group; group = group->next) {
|
||||||
char *pname = group->name? nlabel_to_str(group->name): NULL;
|
char *pname = group->name? nlabel_to_str(group->name): NULL;
|
||||||
|
|
||||||
DEBUG_PRINTF("==================================================\n");
|
MDNS_RR_DEBUG_PRINTF(
|
||||||
DEBUG_PRINTF(" Group: %s\n", pname ? pname : "Unknown");
|
"==================================================\n"
|
||||||
DEBUG_PRINTF("==================================================\n");
|
" Group: %s\n"
|
||||||
|
"==================================================\n",
|
||||||
|
pname ? pname : "Unknown");
|
||||||
if (pname) {
|
if (pname) {
|
||||||
MDNS_FREE(pname);
|
MDNS_FREE(pname);
|
||||||
}
|
}
|
||||||
@@ -236,10 +239,9 @@ static void print_cache(struct mdnsd *svr)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DEBUG_PRINTF("==================================================\n");
|
MDNS_RR_DEBUG_PRINTF("==================================================\n\n");
|
||||||
DEBUG_PRINTF("\n");
|
|
||||||
}
|
}
|
||||||
#endif /* MDNSD_RR_DEBUG */
|
//#endif /* MDNSD_RR_DEBUG */
|
||||||
|
|
||||||
static int check_mdns_domain(const char *name)
|
static int check_mdns_domain(const char *name)
|
||||||
{
|
{
|
||||||
@@ -1252,7 +1254,7 @@ static void main_loop(struct mdnsd *svr) {
|
|||||||
continue;
|
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);
|
struct mdns_pkt *mdns = mdns_parse_pkt(pkt_buffer, recvsize);
|
||||||
if (mdns != NULL) {
|
if (mdns != NULL) {
|
||||||
struct sockaddr_in toaddr = fromaddr;
|
struct sockaddr_in toaddr = fromaddr;
|
||||||
@@ -1356,6 +1358,11 @@ static void main_loop(struct mdnsd *svr) {
|
|||||||
break; /* exit main_loop */
|
break; /* exit main_loop */
|
||||||
sem_post(&svr->sendmsg_sem);
|
sem_post(&svr->sendmsg_sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (svr->dump_cache) {
|
||||||
|
svr->dump_cache = 0;
|
||||||
|
print_cache(svr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (svr->sendmsg_requested && svr->stop_flag) {
|
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);
|
pthread_mutex_init(&g_svr->data_lock, NULL);
|
||||||
sem_init(&g_svr->sendmsg_sem, 0, 0);
|
sem_init(&g_svr->sendmsg_sem, 0, 0);
|
||||||
g_svr->sendmsg_requested = 1;
|
g_svr->sendmsg_requested = 1;
|
||||||
|
g_svr->dump_cache= 0;
|
||||||
|
|
||||||
/* init thread */
|
/* init thread */
|
||||||
if (pthread_attr_init(&attr) != 0) {
|
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
|
// Public Functions
|
||||||
/////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////
|
||||||
@@ -2043,6 +2056,8 @@ int mdnsd_start(const char *desired_hostname, const char *netif_nameOrAddress)
|
|||||||
goto out_with_mutex;
|
goto out_with_mutex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
signal(SIGUSR1, handler_dump_cache);
|
||||||
|
|
||||||
/* success */
|
/* success */
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
||||||
|
|||||||
44
testmdnsd.c
44
testmdnsd.c
@@ -32,6 +32,7 @@
|
|||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#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
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char hostname[0x100], ifname[0x120], fullname[0x108];
|
char hostname[0x100], ifname[0x120], fullname[0x108];
|
||||||
|
|
||||||
struct in_addr saddr;
|
struct in_addr saddr;
|
||||||
int notrunning = 1;
|
|
||||||
|
|
||||||
if (gethostname(hostname, sizeof(hostname) - 1) == -1)
|
if (gethostname(hostname, sizeof(hostname) - 1) == -1)
|
||||||
{
|
{
|
||||||
@@ -300,6 +318,21 @@ main(int argc, char **argv)
|
|||||||
NULL
|
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)) {
|
for (saddr.s_addr = 0;;sleep(61)) {
|
||||||
struct hostent *hp = gethostbyname(hostname);
|
struct hostent *hp = gethostbyname(hostname);
|
||||||
char * ip_addr;
|
char * ip_addr;
|
||||||
@@ -310,8 +343,8 @@ main(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (saddr.s_addr != *(in_addr_t *)(hp->h_addr_list[0])) {
|
if (running != 1 || saddr.s_addr != *(in_addr_t *)(hp->h_addr_list[0])) {
|
||||||
if ((notrunning == 0) && (0 > mdnsd_stop())) {
|
if (running && (0 > mdnsd_stop())) {
|
||||||
fprintf(stderr, "mdnsd_stop() error\n");
|
fprintf(stderr, "mdnsd_stop() error\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -326,7 +359,7 @@ main(int argc, char **argv)
|
|||||||
fprintf(stderr, "mdnsd_start() error\n");
|
fprintf(stderr, "mdnsd_start() error\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
notrunning = 0;
|
running = 1;
|
||||||
|
|
||||||
/* service must have FQDN */
|
/* service must have FQDN */
|
||||||
if (0 <= mdnsd_register_service(
|
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)) {
|
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);
|
struct hostent * hosts = read_hosts(numserv+1);
|
||||||
if (!hosts) {
|
if (!hosts) {
|
||||||
fprintf(stderr,"null hosts\n");
|
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");
|
fprintf(stderr,"mdnsd_stop() error\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user