From: Roy Marples Date: Tue, 13 Nov 2012 09:39:20 +0000 (+0000) Subject: Fix some memory issues X-Git-Tag: v5.99.3~16 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b7fdc6ed451272c7406c782403a5cbbef5beaf2b;p=thirdparty%2Fdhcpcd.git Fix some memory issues --- diff --git a/configure.c b/configure.c index eb8155b0..334e375c 100644 --- a/configure.c +++ b/configure.c @@ -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, diff --git a/control.c b/control.c index 30e6e506..e9af1b95 100644 --- a/control.c +++ b/control.c @@ -1,6 +1,6 @@ /* * dhcpcd - DHCP client daemon - * Copyright (c) 2006-2009 Roy Marples + * Copyright (c) 2006-2012 Roy Marples * 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); } diff --git a/control.h b/control.h index f0faa407..d0e1b60c 100644 --- a/control.h +++ b/control.h @@ -1,6 +1,6 @@ /* * dhcpcd - DHCP client daemon - * Copyright (c) 2006-2008 Roy Marples + * Copyright (c) 2006-2012 Roy Marples * 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);