5 * Copyright (C) 2007-2009 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
28 #include <glib/gstdio.h>
30 #define CONNMAN_API_SUBJECT_TO_CHANGE
31 #include <connman/plugin.h>
32 #include <connman/driver.h>
33 #include <connman/inet.h>
34 #include <connman/dbus.h>
35 #include <connman/log.h>
37 #define DHCLIENT_INTF "org.isc.dhclient"
38 #define DHCLIENT_PATH "/org/isc/dhclient"
40 static const char *busname;
42 struct dhclient_task {
47 struct connman_element *element;
48 struct dhclient_task *pending;
51 static GSList *task_list = NULL;
53 static struct dhclient_task *find_task_by_pid(GPid pid)
57 for (list = task_list; list; list = list->next) {
58 struct dhclient_task *task = list->data;
67 static struct dhclient_task *find_task_by_index(int index)
71 for (list = task_list; list; list = list->next) {
72 struct dhclient_task *task = list->data;
74 if (task->ifindex == index)
81 static void kill_task(struct dhclient_task *task)
83 DBG("task %p name %s pid %d", task, task->ifname, task->pid);
85 if (task->killed == TRUE)
90 kill(task->pid, SIGTERM);
94 static void unlink_task(struct dhclient_task *task)
98 DBG("task %p name %s pid %d", task, task->ifname, task->pid);
100 pathname = g_strdup_printf("%s/dhclient.%s.pid",
101 STATEDIR, task->ifname);
105 pathname = g_strdup_printf("%s/dhclient.%s.leases",
106 STATEDIR, task->ifname);
111 static int start_dhclient(struct dhclient_task *task);
113 static void task_died(GPid pid, gint status, gpointer data)
115 struct dhclient_task *task = data;
117 if (WIFEXITED(status))
118 DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
120 DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
122 g_spawn_close_pid(pid);
125 task_list = g_slist_remove(task_list, task);
129 if (task->pending != NULL)
130 start_dhclient(task->pending);
132 g_free(task->ifname);
136 static void task_setup(gpointer data)
138 struct dhclient_task *task = data;
140 DBG("task %p name %s", task, task->ifname);
142 task->killed = FALSE;
145 static int start_dhclient(struct dhclient_task *task)
147 char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
148 char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
150 snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
151 snprintf(pidfile, sizeof(pidfile) - 1,
152 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
153 snprintf(leases, sizeof(leases) - 1,
154 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
155 snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
156 snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
171 argv[13] = task->ifname;
177 if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
178 task_setup, task, &task->pid, NULL) == FALSE) {
179 connman_error("Failed to spawn dhclient");
183 task_list = g_slist_append(task_list, task);
185 g_child_watch_add(task->pid, task_died, task);
187 DBG("executed %s with pid %d", DHCLIENT, task->pid);
192 static int dhclient_probe(struct connman_element *element)
194 struct dhclient_task *task, *previous;
195 DBG("element %p name %s", element, element->name);
197 if (access(DHCLIENT, X_OK) < 0)
200 task = g_try_new0(struct dhclient_task, 1);
204 task->ifindex = element->index;
205 task->ifname = connman_inet_ifname(element->index);
206 task->element = element;
208 if (task->ifname == NULL) {
213 previous= find_task_by_index(element->index);
214 if (previous != NULL) {
215 previous->pending = task;
220 return start_dhclient(task);
223 static void dhclient_remove(struct connman_element *element)
225 struct dhclient_task *task;
227 DBG("element %p name %s", element, element->name);
229 task = find_task_by_index(element->index);
233 DBG("release %s", task->ifname);
238 static void dhclient_change(struct connman_element *element)
240 DBG("element %p name %s", element, element->name);
242 if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
243 connman_element_set_error(element->parent,
244 CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
247 static struct connman_driver dhclient_driver = {
249 .type = CONNMAN_ELEMENT_TYPE_DHCP,
250 .probe = dhclient_probe,
251 .remove = dhclient_remove,
252 .change = dhclient_change,
255 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
256 DBusMessage *msg, void *data)
258 DBusMessageIter iter, dict;
260 struct dhclient_task *task;
261 const char *text, *key, *value;
263 if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
264 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
266 dbus_message_iter_init(msg, &iter);
268 dbus_message_iter_get_basic(&iter, &pid);
269 dbus_message_iter_next(&iter);
271 dbus_message_iter_get_basic(&iter, &text);
272 dbus_message_iter_next(&iter);
274 DBG("change %d to %s", pid, text);
276 task = find_task_by_pid(pid);
279 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
281 dbus_message_iter_recurse(&iter, &dict);
283 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
284 DBusMessageIter entry;
286 dbus_message_iter_recurse(&dict, &entry);
287 dbus_message_iter_get_basic(&entry, &key);
288 dbus_message_iter_next(&entry);
289 dbus_message_iter_get_basic(&entry, &value);
291 DBG("%s = %s", key, value);
293 if (g_ascii_strcasecmp(key, "new_ip_address") == 0) {
294 g_free(task->element->ipv4.address);
295 task->element->ipv4.address = g_strdup(value);
298 if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0) {
299 g_free(task->element->ipv4.netmask);
300 task->element->ipv4.netmask = g_strdup(value);
303 if (g_ascii_strcasecmp(key, "new_routers") == 0) {
304 g_free(task->element->ipv4.gateway);
305 task->element->ipv4.gateway = g_strdup(value);
308 if (g_ascii_strcasecmp(key, "new_network_number") == 0) {
309 g_free(task->element->ipv4.network);
310 task->element->ipv4.network = g_strdup(value);
313 if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0) {
314 g_free(task->element->ipv4.broadcast);
315 task->element->ipv4.broadcast = g_strdup(value);
318 if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0) {
319 g_free(task->element->ipv4.nameserver);
320 task->element->ipv4.nameserver = g_strdup(value);
323 if (g_ascii_strcasecmp(key, "new_domain_name") == 0) {
326 if (g_ascii_strcasecmp(key, "new_domain_search") == 0) {
329 if (g_ascii_strcasecmp(key, "new_host_name") == 0) {
332 dbus_message_iter_next(&dict);
335 if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
336 } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
337 g_ascii_strcasecmp(text, "REBOOT") == 0) {
338 struct connman_element *element;
339 element = connman_element_create(NULL);
340 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
341 element->index = task->ifindex;
342 connman_element_update(task->element);
343 if (connman_element_register(element, task->element) < 0)
344 connman_element_unref(element);
345 } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
346 g_ascii_strcasecmp(text, "REBIND") == 0) {
347 connman_element_update(task->element);
348 } else if (g_ascii_strcasecmp(text, "FAIL") == 0) {
349 connman_element_set_error(task->element,
350 CONNMAN_ELEMENT_ERROR_FAILED);
354 return DBUS_HANDLER_RESULT_HANDLED;
357 static DBusConnection *connection;
359 static const char *dhclient_rule = "path=" DHCLIENT_PATH
360 ",interface=" DHCLIENT_INTF;
362 static int dhclient_init(void)
366 connection = connman_dbus_get_connection();
368 busname = dbus_bus_get_unique_name(connection);
369 busname = CONNMAN_SERVICE;
371 dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
373 dbus_bus_add_match(connection, dhclient_rule, NULL);
375 err = connman_driver_register(&dhclient_driver);
377 dbus_connection_unref(connection);
384 static void dhclient_exit(void)
388 for (list = task_list; list; list = list->next) {
389 struct dhclient_task *task = list->data;
391 DBG("killing process %d", task->pid);
397 g_slist_free(task_list);
399 connman_driver_unregister(&dhclient_driver);
401 dbus_bus_remove_match(connection, dhclient_rule, NULL);
403 dbus_connection_remove_filter(connection, dhclient_filter, NULL);
405 dbus_connection_unref(connection);
408 CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
409 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)