]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Fix some memory issues
authorRoy Marples <roy@marples.name>
Tue, 13 Nov 2012 09:39:20 +0000 (09:39 +0000)
committerRoy Marples <roy@marples.name>
Tue, 13 Nov 2012 09:39:20 +0000 (09:39 +0000)
configure.c
control.c
control.h

index eb8155b0d6471172ce9ae0633b49457a45c21315..334e375c667b96f49ca2a88cbba301ee103eabbd 100644 (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,
index 30e6e5066653a77f9416b7b264f236746f9b5e75..e9af1b9552f2e1111feae36e0cb80ff3b3a3a809 100644 (file)
--- a/control.c
+++ b/control.c
@@ -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);
 }
 
index f0faa4079cc126a2f306a2a547cf898be6b45e64..d0e1b60c5c6efa97fff3909b9ab181d79eb9dd03 100644 (file)
--- a/control.h
+++ b/control.h
@@ -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);