Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh352 committed May 12, 2024
1 parent 6648d01 commit 5a79015
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions src/lib/ares_event_configchg.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
/* Cleanup happens automatically */
}


static void ares_event_configchg_free(void *data)
{
ares_event_configchg_t *configchg = data;
if (configchg == NULL)
if (configchg == NULL) {
return;
}

if (configchg->inotify_fd >= 0) {
close(configchg->inotify_fd);
Expand All @@ -73,7 +73,8 @@ static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,
* decrease performance. Hence, the buffer used for reading from
* the inotify file descriptor should have the same alignment as
* struct inotify_event. */
unsigned char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
unsigned char buf[4096]
__attribute__((aligned(__alignof__(struct inotify_event))));
const struct inotify_event *event;
ssize_t len;
ares_bool_t triggered = ARES_FALSE;
Expand All @@ -91,13 +92,13 @@ static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,

/* Loop over all events in the buffer. Says kernel will check the buffer
* size provided, so I assume it won't ever return partial events. */
for (ptr = buf; ptr < buf + len;
for (ptr = buf; ptr < buf + len;
ptr += sizeof(struct inotify_event) + event->len) {

event = (const struct inotify_event *)ptr;

if (event->name == NULL)
if (event->name == NULL) {
continue;
}

if (strcasecmp(event->name, "resolv.conf") == 0 ||
strcasecmp(event->name, "nsswitch.conf") == 0) {
Expand All @@ -113,7 +114,6 @@ static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,
}
}


ares_status_t ares_event_configchg_init(ares_event_thread_t *e)
{
ares_status_t status = ARES_SUCCESS;
Expand All @@ -124,21 +124,23 @@ ares_status_t ares_event_configchg_init(ares_event_thread_t *e)
return ARES_ENOMEM;
}

configchg->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
configchg->inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (configchg->inotify_fd == -1) {
status = ARES_ESERVFAIL;
goto done;
}

/* We need to monitor /etc/resolv.conf, /etc/nsswitch.conf */
if (inotify_add_watch(configchg->inotify_fd, "/etc", IN_CREATE|IN_MODIFY|IN_MOVED_TO|IN_ONLYDIR) == -1) {
if (inotify_add_watch(configchg->inotify_fd, "/etc",
IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_ONLYDIR) ==
-1) {
status = ARES_ESERVFAIL;
goto done;
}

status = ares_event_update(NULL, e, ARES_EVENT_FLAG_READ,
ares_event_configchg_cb, configchg->inotify_fd,
configchg,ares_event_configchg_free, NULL);
configchg, ares_event_configchg_free, NULL);

done:
if (status != ARES_SUCCESS) {
Expand Down Expand Up @@ -206,8 +208,9 @@ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)

static void *ares_event_configchg_thread(void *arg)
{
ares_event_configchg_t *configchg = arg;
HANDLE handles[3] = { configchg->terminate_event, configchg->ifchg_ol.hEvent, configchg->route_ol.hEvent };
ares_event_configchg_t *configchg = arg;
HANDLE handles[3] = { configchg->terminate_event, configchg->ifchg_ol.hEvent,
configchg->route_ol.hEvent };

while (1) {
ares_bool_t triggered = ARES_FALSE;
Expand Down Expand Up @@ -239,7 +242,8 @@ static void *ares_event_configchg_thread(void *arg)
return NULL;
}

ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg, ares_event_thread_t *e)
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
ares_status_t status = ARES_SUCCESS;

Expand All @@ -256,17 +260,20 @@ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg, ares
goto done;
}

if (NotifyRouteChange(&(*configchg)->route_hnd, &(*configchg)->route_ol) != NO_ERROR) {
if (NotifyRouteChange(&(*configchg)->route_hnd, &(*configchg)->route_ol) !=
NO_ERROR) {
status = ARES_ESERVFAIL;
goto done;
}

if (NotifyIpInterfaceChange(&(*configchg)->ifchg_hnd, &(*configchg)->ifchg_ol) != NO_ERROR) {
if (NotifyIpInterfaceChange(&(*configchg)->ifchg_hnd,
&(*configchg)->ifchg_ol) != NO_ERROR) {
status = ARES_ESERVFAIL;
goto done;
}

status = ares__thread_create(&(*configchg)->thread, ares_event_configchg_thread, *configchg);
status = ares__thread_create(&(*configchg)->thread,
ares_event_configchg_thread, *configchg);
if (status != ARES_SUCCESS) {
goto done;
}
Expand All @@ -287,7 +294,6 @@ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg, ares
# include <notify.h>
# include <dlfcn.h>


struct ares_event_configchg {
int fd;
int token;
Expand All @@ -300,12 +306,12 @@ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
/* Cleanup happens automatically */
}


static void ares_event_configchg_free(void *data)
{
ares_event_configchg_t *configchg = data;
if (configchg == NULL)
if (configchg == NULL) {
return;
}

if (configchg->fd >= 0) {
notify_cancel(configchg->token);
Expand All @@ -331,8 +337,9 @@ static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,

len = read(configchg->fd, &t, sizeof(t));

if (len < (ssize_t)sizeof(t))
if (len < (ssize_t)sizeof(t)) {
break;
}

if (t != configchg->token) {
continue;
Expand All @@ -348,12 +355,13 @@ static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,
}
}

ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg, ares_event_thread_t *e)
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
ares_status_t status = ARES_SUCCESS;
void *handle = NULL;
const char *(*pdns_configuration_notify_key)(void) = NULL;
const char *notify_key = NULL;
ares_status_t status = ARES_SUCCESS;
void *handle = NULL;
const char *(*pdns_configuration_notify_key)(void) = NULL;
const char *notify_key = NULL;

*configchg = ares_malloc_zero(sizeof(**configchg));
if (*configchg == NULL) {
Expand All @@ -367,8 +375,7 @@ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg, ares
goto done;
}

pdns_configuration_notify_key =
dlsym(handle, "dns_configuration_notify_key");
pdns_configuration_notify_key = dlsym(handle, "dns_configuration_notify_key");

if (pdns_configuration_notify_key == NULL) {
status = ARES_ESERVFAIL;
Expand All @@ -381,8 +388,9 @@ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg, ares
goto done;
}

if (notify_register_file_descriptor(notify_key,
&(*configchg)->fd, 0, &(*configchg)->token) != NOTIFY_STATUS_OK) {
if (notify_register_file_descriptor(notify_key, &(*configchg)->fd, 0,
&(*configchg)->token) !=
NOTIFY_STATUS_OK) {
status = ARES_ESERVFAIL;
goto done;
}
Expand All @@ -406,7 +414,8 @@ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg, ares

#else

ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg, ares_event_thread_t *e)
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
/* Not implemented yet, need to spawn thread */
return ARES_SUCCESS;
Expand Down

0 comments on commit 5a79015

Please sign in to comment.