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;
46 static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
47 static GSList *task_list = NULL;
49 static struct dhclient_task *find_task_by_pid(GPid pid)
53 for (list = task_list; list; list = list->next) {
54 struct dhclient_task *task = list->data;
63 static struct dhclient_task *find_task_by_index(int index)
67 for (list = task_list; list; list = list->next) {
68 struct dhclient_task *task = list->data;
70 if (task->ifindex == index)
77 static void kill_task(struct dhclient_task *task)
79 DBG("task %p name %s pid %d", task, task->ifname, task->pid);
82 kill(task->pid, SIGTERM);
85 static void unlink_task(struct dhclient_task *task)
89 DBG("task %p name %s pid %d", task, task->ifname, task->pid);
91 pathname = g_strdup_printf("%s/dhclient.%s.pid",
92 STATEDIR, task->ifname);
96 pathname = g_strdup_printf("%s/dhclient.%s.leases",
97 STATEDIR, task->ifname);
102 static void task_died(GPid pid, gint status, gpointer data)
104 struct dhclient_task *task = data;
106 if (WIFEXITED(status))
107 DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
109 DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
111 g_spawn_close_pid(pid);
114 g_static_mutex_lock(&task_mutex);
115 task_list = g_slist_remove(task_list, task);
116 g_static_mutex_unlock(&task_mutex);
120 g_free(task->ifname);
124 static void task_setup(gpointer data)
126 struct dhclient_task *task = data;
128 DBG("task %p name %s", task, task->ifname);
131 static int dhclient_probe(struct connman_element *element)
133 struct dhclient_task *task;
134 char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
135 char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
137 DBG("element %p name %s", element, element->name);
139 task = g_try_new0(struct dhclient_task, 1);
143 task->ifindex = element->netdev.index;
144 task->ifname = g_strdup(element->netdev.name);
145 task->element = element;
147 if (task->ifname == NULL) {
152 DBG("request %s", task->ifname);
154 snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
155 snprintf(pidfile, sizeof(pidfile) - 1,
156 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
157 snprintf(leases, sizeof(leases) - 1,
158 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
159 snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
160 snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
176 argv[14] = task->ifname;
181 if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
182 task_setup, task, &task->pid, NULL) == FALSE) {
183 connman_error("Failed to spawn dhclient");
187 g_static_mutex_lock(&task_mutex);
188 task_list = g_slist_append(task_list, task);
189 g_static_mutex_unlock(&task_mutex);
191 g_child_watch_add(task->pid, task_died, task);
193 DBG("executed %s with pid %d", DHCLIENT, task->pid);
198 static void dhclient_remove(struct connman_element *element)
200 struct dhclient_task *task;
202 DBG("element %p name %s", element, element->name);
204 g_static_mutex_lock(&task_mutex);
206 task = find_task_by_index(element->netdev.index);
208 task_list = g_slist_remove(task_list, task);
210 g_static_mutex_unlock(&task_mutex);
215 DBG("release %s", task->ifname);
220 static struct connman_driver dhclient_driver = {
222 .type = CONNMAN_ELEMENT_TYPE_DHCP,
223 .probe = dhclient_probe,
224 .remove = dhclient_remove,
227 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
228 DBusMessage *msg, void *data)
230 DBusMessageIter iter, dict;
232 struct dhclient_task *task;
233 const char *text, *key, *value;
235 if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
236 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
238 dbus_message_iter_init(msg, &iter);
240 dbus_message_iter_get_basic(&iter, &pid);
241 dbus_message_iter_next(&iter);
243 dbus_message_iter_get_basic(&iter, &text);
244 dbus_message_iter_next(&iter);
246 DBG("change %d to %s", pid, text);
248 g_static_mutex_lock(&task_mutex);
249 task = find_task_by_pid(pid);
250 g_static_mutex_unlock(&task_mutex);
253 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
255 dbus_message_iter_recurse(&iter, &dict);
257 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
258 DBusMessageIter entry;
260 dbus_message_iter_recurse(&dict, &entry);
261 dbus_message_iter_get_basic(&entry, &key);
262 dbus_message_iter_next(&entry);
263 dbus_message_iter_get_basic(&entry, &value);
265 DBG("%s = %s", key, value);
267 if (g_ascii_strcasecmp(key, "new_ip_address") == 0) {
268 g_free(task->element->ipv4.address);
269 task->element->ipv4.address = g_strdup(value);
272 if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0) {
273 g_free(task->element->ipv4.netmask);
274 task->element->ipv4.netmask = g_strdup(value);
277 if (g_ascii_strcasecmp(key, "new_routers") == 0) {
278 g_free(task->element->ipv4.gateway);
279 task->element->ipv4.gateway = g_strdup(value);
282 if (g_ascii_strcasecmp(key, "new_network_number") == 0) {
283 g_free(task->element->ipv4.network);
284 task->element->ipv4.network = g_strdup(value);
287 if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0) {
288 g_free(task->element->ipv4.broadcast);
289 task->element->ipv4.broadcast = g_strdup(value);
292 if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0) {
293 g_free(task->element->ipv4.nameserver);
294 task->element->ipv4.nameserver = g_strdup(value);
297 dbus_message_iter_next(&dict);
300 if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
301 } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
302 g_ascii_strcasecmp(text, "REBOOT") == 0) {
303 struct connman_element *element;
304 element = connman_element_create();
305 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
306 element->netdev.index = task->ifindex;
307 element->netdev.name = g_strdup(task->ifname);
308 connman_element_update(task->element);
309 connman_element_register(element, task->element);
310 } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
311 g_ascii_strcasecmp(text, "REBIND") == 0) {
312 connman_element_update(task->element);
316 return DBUS_HANDLER_RESULT_HANDLED;
319 static DBusConnection *connection;
321 static int dhclient_init(void)
326 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
328 busname = dbus_bus_get_unique_name(connection);
329 busname = CONNMAN_SERVICE;
331 dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
333 filter = g_strdup_printf("interface=%s,path=%s",
334 DHCLIENT_INTF, DHCLIENT_PATH);
336 dbus_bus_add_match(connection, filter, NULL);
340 err = connman_driver_register(&dhclient_driver);
342 dbus_connection_unref(connection);
349 static void dhclient_exit(void)
353 g_static_mutex_lock(&task_mutex);
355 for (list = task_list; list; list = list->next) {
356 struct dhclient_task *task = list->data;
358 DBG("killing process %d", task->pid);
364 g_static_mutex_unlock(&task_mutex);
366 g_slist_free(task_list);
368 connman_driver_unregister(&dhclient_driver);
370 dbus_connection_unref(connection);
373 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
374 dhclient_init, dhclient_exit)