Fix some memory issues

This commit is contained in:
Roy Marples 2012-11-13 09:39:20 +00:00
parent 94582a4d23
commit b7fdc6ed45
3 changed files with 44 additions and 20 deletions

View File

@ -447,7 +447,7 @@ run_script_reason(const struct interface *iface, const char *reason)
/* Send to our listeners */
bigenv = NULL;
for (fd = fds; fd != NULL; fd = fd->next) {
for (fd = control_fds; fd != NULL; fd = fd->next) {
if (fd->listener) {
if (bigenv == NULL) {
elen = arraytostr((const char *const *)env,

View File

@ -1,6 +1,6 @@
/*
* dhcpcd - DHCP client daemon
* Copyright (c) 2006-2009 Roy Marples <roy@marples.name>
* Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
* All rights reserved
* Redistribution and use in source and binary forms, with or without
@ -45,26 +45,44 @@ static int fd = -1;
static char buffer[1024];
static char *argvp[255];
struct sockaddr_un sun;
struct fd_list *fds = NULL;
static struct sockaddr_un sun;
struct fd_list *control_fds = NULL;
#ifdef DEBUG_MEMORY
static void
cleanup(void)
{
struct fd_list *f;
f = control_fds;
while (f) {
control_fds = f->next;
free(f);
f = control_fds;
}
}
#endif
static void
remove_control_data(void *arg)
{
struct fd_list *l, *last = NULL;
struct fd_list *l, *n, *last = NULL;
for (l = fds; l != NULL; l = l->next) {
l = control_fds;
while (l) {
n = l->next;
if (l == arg) {
close(l->fd);
delete_event(l->fd);
if (last == NULL)
fds = l->next;
control_fds = l->next;
else
last->next = l->next;
free(l);
break;
}
last = l;
l = n;
}
}
@ -107,12 +125,15 @@ handle_control(_unused void *arg)
len = sizeof(run);
if ((f = accept(fd, (struct sockaddr *)&run, &len)) == -1)
return;
l = xmalloc(sizeof(*l));
l->fd = f;
l->listener = 0;
l->next = fds;
fds = l;
add_event(l->fd, handle_control_data, l);
set_cloexec(f);
l = malloc(sizeof(*l));
if (l) {
l->fd = f;
l->listener = 0;
l->next = control_fds;
control_fds = l;
add_event(l->fd, handle_control_data, l);
}
}
static int
@ -139,7 +160,7 @@ start_control(void)
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) == -1 ||
set_cloexec(fd) == -1 ||
set_nonblock(fd) == -1 ||
listen(fd, sizeof(fds)) == -1)
listen(fd, sizeof(control_fds)) == -1)
{
close(fd);
return -1;
@ -152,7 +173,7 @@ int
stop_control(void)
{
int retval = 0;
struct fd_list *l, *ll;
struct fd_list *l;
delete_event(fd);
if (shutdown(fd, SHUT_RDWR) == -1)
@ -161,13 +182,13 @@ stop_control(void)
if (unlink(CONTROLSOCKET) == -1)
retval = -1;
l = fds;
l = control_fds;
while (l != NULL) {
ll = l->next;
control_fds = l->next;
delete_event(l->fd);
shutdown(l->fd, SHUT_RDWR);
free(l);
l = ll;
l = control_fds;
}
return retval;
@ -180,6 +201,9 @@ open_control(void)
if ((len = make_sock()) == -1)
return -1;
#ifdef DEBUG_MEMORY
atexit(cleanup);
#endif
return connect(fd, (struct sockaddr *)&sun, len);
}

View File

@ -1,6 +1,6 @@
/*
* dhcpcd - DHCP client daemon
* Copyright (c) 2006-2008 Roy Marples <roy@marples.name>
* Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
* All rights reserved
* Redistribution and use in source and binary forms, with or without
@ -35,7 +35,7 @@ struct fd_list {
int listener;
struct fd_list *next;
};
extern struct fd_list *fds;
extern struct fd_list *control_fds;
int start_control(void);
int stop_control(void);