5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <glib/gstdio.h>
29 #include <connman/plugin.h>
30 #include <connman/driver.h>
31 #include <connman/dbus.h>
32 #include <connman/log.h>
34 #define DHCLIENT_INTF "org.isc.dhclient"
35 #define DHCLIENT_PATH "/org/isc/dhclient"
37 static const char *busname;
39 struct dhclient_task {
43 struct connman_element *element;
44 struct connman_element *child;
47 static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
48 static GSList *task_list = NULL;
50 static struct dhclient_task *find_task_by_pid(GPid pid)
54 for (list = task_list; list; list = list->next) {
55 struct dhclient_task *task = list->data;
64 static struct dhclient_task *find_task_by_index(int index)
68 for (list = task_list; list; list = list->next) {
69 struct dhclient_task *task = list->data;
71 if (task->ifindex == index)
78 static void kill_task(struct dhclient_task *task)
80 DBG("task %p name %s pid %d", task, task->ifname, task->pid);
83 kill(task->pid, SIGTERM);
86 static void unlink_task(struct dhclient_task *task)
90 DBG("task %p name %s pid %d", task, task->ifname, task->pid);
92 pathname = g_strdup_printf("%s/dhclient.%s.pid",
93 STATEDIR, task->ifname);
97 pathname = g_strdup_printf("%s/dhclient.%s.leases",
98 STATEDIR, task->ifname);
103 static void task_died(GPid pid, gint status, gpointer data)
105 struct dhclient_task *task = data;
107 if (WIFEXITED(status))
108 DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
110 DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
112 g_spawn_close_pid(pid);
115 g_static_mutex_lock(&task_mutex);
116 task_list = g_slist_remove(task_list, task);
117 g_static_mutex_unlock(&task_mutex);
121 g_free(task->ifname);
125 static void task_setup(gpointer data)
127 struct dhclient_task *task = data;
129 DBG("task %p name %s", task, task->ifname);
132 static int dhclient_probe(struct connman_element *element)
134 struct dhclient_task *task;
135 char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
136 char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
138 DBG("element %p name %s", element, element->name);
140 task = g_try_new0(struct dhclient_task, 1);
144 task->ifindex = element->netdev.index;
145 task->ifname = g_strdup(element->netdev.name);
146 task->element = element;
149 if (task->ifname == NULL) {
154 DBG("request %s", task->ifname);
156 snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
157 snprintf(pidfile, sizeof(pidfile) - 1,
158 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
159 snprintf(leases, sizeof(leases) - 1,
160 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
161 snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
162 snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
178 argv[14] = task->ifname;
183 if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
184 task_setup, task, &task->pid, NULL) == FALSE) {
185 connman_error("Failed to spawn dhclient");
189 g_static_mutex_lock(&task_mutex);
190 task_list = g_slist_append(task_list, task);
191 g_static_mutex_unlock(&task_mutex);
193 g_child_watch_add(task->pid, task_died, task);
195 DBG("executed %s with pid %d", DHCLIENT, task->pid);
200 static void dhclient_remove(struct connman_element *element)
202 struct dhclient_task *task;
204 DBG("element %p name %s", element, element->name);
206 g_static_mutex_lock(&task_mutex);
207 task = find_task_by_index(element->netdev.index);
208 g_static_mutex_unlock(&task_mutex);
213 DBG("release %s", task->ifname);
215 g_static_mutex_lock(&task_mutex);
216 task_list = g_slist_remove(task_list, task);
217 g_static_mutex_unlock(&task_mutex);
219 if (task->child != NULL) {
220 connman_element_unregister(task->child);
221 connman_element_unref(task->child);
228 static struct connman_driver dhclient_driver = {
230 .type = CONNMAN_ELEMENT_TYPE_DHCP,
231 .probe = dhclient_probe,
232 .remove = dhclient_remove,
235 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
236 DBusMessage *msg, void *data)
238 DBusMessageIter iter, dict;
240 struct dhclient_task *task;
241 const char *text, *key, *value;
243 if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
244 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
246 dbus_message_iter_init(msg, &iter);
248 dbus_message_iter_get_basic(&iter, &pid);
249 dbus_message_iter_next(&iter);
251 dbus_message_iter_get_basic(&iter, &text);
252 dbus_message_iter_next(&iter);
254 DBG("change %d to %s", pid, text);
256 g_static_mutex_lock(&task_mutex);
257 task = find_task_by_pid(pid);
258 g_static_mutex_unlock(&task_mutex);
261 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
263 dbus_message_iter_recurse(&iter, &dict);
265 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
266 DBusMessageIter entry;
268 dbus_message_iter_recurse(&dict, &entry);
269 dbus_message_iter_get_basic(&entry, &key);
270 dbus_message_iter_next(&entry);
271 dbus_message_iter_get_basic(&entry, &value);
273 DBG("%s = %s", key, value);
275 if (g_ascii_strcasecmp(key, "new_ip_address") == 0) {
276 g_free(task->element->ipv4.address);
277 task->element->ipv4.address = g_strdup(value);
280 if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0) {
281 g_free(task->element->ipv4.netmask);
282 task->element->ipv4.netmask = g_strdup(value);
285 if (g_ascii_strcasecmp(key, "new_routers") == 0) {
286 g_free(task->element->ipv4.gateway);
287 task->element->ipv4.gateway = g_strdup(value);
290 if (g_ascii_strcasecmp(key, "new_network_number") == 0) {
291 g_free(task->element->ipv4.network);
292 task->element->ipv4.network = g_strdup(value);
295 if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0) {
296 g_free(task->element->ipv4.broadcast);
297 task->element->ipv4.broadcast = g_strdup(value);
300 if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0) {
301 g_free(task->element->ipv4.nameserver);
302 task->element->ipv4.nameserver = g_strdup(value);
305 dbus_message_iter_next(&dict);
308 if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
309 } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
310 g_ascii_strcasecmp(text, "REBOOT") == 0) {
311 task->child = connman_element_create();
312 task->child->type = CONNMAN_ELEMENT_TYPE_IPV4;
313 task->child->netdev.index = task->ifindex;
314 connman_element_update(task->element);
315 connman_element_register(task->child, task->element);
316 } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
317 g_ascii_strcasecmp(text, "REBIND") == 0) {
318 connman_element_update(task->element);
319 connman_element_update(task->child);
323 return DBUS_HANDLER_RESULT_HANDLED;
326 static DBusConnection *connection;
328 static int dhclient_init(void)
333 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
335 busname = dbus_bus_get_unique_name(connection);
336 busname = CONNMAN_SERVICE;
338 dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
340 filter = g_strdup_printf("interface=%s,path=%s",
341 DHCLIENT_INTF, DHCLIENT_PATH);
343 dbus_bus_add_match(connection, filter, NULL);
347 err = connman_driver_register(&dhclient_driver);
349 dbus_connection_unref(connection);
356 static void dhclient_exit(void)
360 g_static_mutex_lock(&task_mutex);
362 for (list = task_list; list; list = list->next) {
363 struct dhclient_task *task = list->data;
366 connman_element_unregister(task->child);
367 connman_element_unref(task->child);
371 DBG("killing process %d", task->pid);
377 g_static_mutex_unlock(&task_mutex);
379 g_slist_free(task_list);
381 connman_driver_unregister(&dhclient_driver);
383 dbus_connection_unref(connection);
386 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
387 dhclient_init, dhclient_exit)