From baf41b9cfdb124c9369c7da522ec024625f877b0 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 9 Jan 2008 16:43:50 +0100 Subject: [PATCH] Add support for terminating dhclient --- plugins/dhclient.c | 64 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/plugins/dhclient.c b/plugins/dhclient.c index 8506c63..139eb8f 100644 --- a/plugins/dhclient.c +++ b/plugins/dhclient.c @@ -51,7 +51,7 @@ struct dhclient_task { static GSList *tasks = NULL; -static struct dhclient_task *find_task(GPid pid) +static struct dhclient_task *find_task_by_pid(GPid pid) { GSList *list; @@ -65,6 +65,39 @@ static struct dhclient_task *find_task(GPid pid) return NULL; } +static struct dhclient_task *find_task_by_index(int index) +{ + GSList *list; + + for (list = tasks; list; list = list->next) { + struct dhclient_task *task = list->data; + + if (task->ifindex == index) + return task; + } + + return NULL; +} + +static void kill_task(struct dhclient_task *task) +{ + char pathname[PATH_MAX]; + + kill(task->pid, SIGTERM); + + snprintf(pathname, sizeof(pathname) - 1, + "%s/dhclient.%s.pid", STATEDIR, task->ifname); + unlink(pathname); + + snprintf(pathname, sizeof(pathname) - 1, + "%s/dhclient.%s.leases", STATEDIR, task->ifname); + unlink(pathname); + + free(task->ifname); + + g_free(task); +} + static int dhclient_request(struct connman_iface *iface) { struct ifreq ifr; @@ -142,7 +175,17 @@ static int dhclient_request(struct connman_iface *iface) static int dhclient_release(struct connman_iface *iface) { - printf("[DHCP] release\n"); + struct dhclient_task *task; + + task = find_task_by_index(iface->index); + if (task == NULL) + return NULL; + + printf("[DHCP] release %s\n", task->ifname); + + tasks = g_slist_remove(tasks, task); + + kill_task(task); return 0; } @@ -174,7 +217,7 @@ static DBusMessage *notify_method(DBusConnection *conn, printf("[DHCP] change %d to %s\n", pid, text); - task = find_task(pid); + task = find_task_by_pid(pid); if (task == NULL) return NULL; @@ -257,23 +300,10 @@ static void plugin_exit(void) for (list = tasks; list; list = list->next) { struct dhclient_task *task = list->data; - char pathname[PATH_MAX]; printf("[DHCP] killing process %d\n", task->pid); - kill(task->pid, SIGTERM); - - snprintf(pathname, sizeof(pathname) - 1, - "%s/dhclient.%s.pid", STATEDIR, task->ifname); - unlink(pathname); - - snprintf(pathname, sizeof(pathname) - 1, - "%s/dhclient.%s.leases", STATEDIR, task->ifname); - unlink(pathname); - - free(task->ifname); - - g_free(task); + kill_task(task); } g_slist_free(tasks); -- 1.7.9.5