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
29 #include <glib/gstdio.h>
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/dhcp.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 dhclient_task *pending;
48 struct connman_dhcp *dhcp;
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 connman_dhcp_unref(task->dhcp);
134 g_free(task->ifname);
138 static void task_setup(gpointer data)
140 struct dhclient_task *task = data;
142 DBG("task %p name %s", task, task->ifname);
144 task->killed = FALSE;
147 static int start_dhclient(struct dhclient_task *task)
149 char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
150 char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
152 snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
153 snprintf(pidfile, sizeof(pidfile) - 1,
154 "%s/dhclient.%s.pid", STATEDIR, task->ifname);
155 snprintf(leases, sizeof(leases) - 1,
156 "%s/dhclient.%s.leases", STATEDIR, task->ifname);
157 snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
158 snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
173 argv[13] = task->ifname;
179 if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
180 task_setup, task, &task->pid, NULL) == FALSE) {
181 connman_error("Failed to spawn dhclient");
185 task_list = g_slist_append(task_list, task);
187 g_child_watch_add(task->pid, task_died, task);
189 DBG("executed %s with pid %d", DHCLIENT, task->pid);
194 static int dhclient_request(struct connman_dhcp *dhcp)
196 struct dhclient_task *task, *previous;
198 DBG("dhcp %p", dhcp);
200 if (access(DHCLIENT, X_OK) < 0)
203 task = g_try_new0(struct dhclient_task, 1);
207 task->ifindex = connman_dhcp_get_index(dhcp);
208 task->ifname = connman_dhcp_get_interface(dhcp);
210 if (task->ifname == NULL) {
215 task->dhcp = connman_dhcp_ref(dhcp);
217 previous= find_task_by_index(task->ifindex);
218 if (previous != NULL) {
219 previous->pending = task;
224 return start_dhclient(task);
227 static int dhclient_release(struct connman_dhcp *dhcp)
229 struct dhclient_task *task;
232 DBG("dhcp %p", dhcp);
234 index = connman_dhcp_get_index(dhcp);
236 task = find_task_by_index(index);
240 DBG("release %s", task->ifname);
247 static struct connman_dhcp_driver dhclient_driver = {
249 .request = dhclient_request,
250 .release = dhclient_release,
253 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
254 DBusMessage *msg, void *data)
256 DBusMessageIter iter, dict;
258 struct dhclient_task *task;
259 const char *text, *key, *value;
261 if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
262 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
264 dbus_message_iter_init(msg, &iter);
266 dbus_message_iter_get_basic(&iter, &pid);
267 dbus_message_iter_next(&iter);
269 dbus_message_iter_get_basic(&iter, &text);
270 dbus_message_iter_next(&iter);
272 DBG("change %d to %s", pid, text);
274 task = find_task_by_pid(pid);
277 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
279 dbus_message_iter_recurse(&iter, &dict);
281 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
282 DBusMessageIter entry;
284 dbus_message_iter_recurse(&dict, &entry);
285 dbus_message_iter_get_basic(&entry, &key);
286 dbus_message_iter_next(&entry);
287 dbus_message_iter_get_basic(&entry, &value);
289 DBG("%s = %s", key, value);
291 if (g_strcmp0(key, "new_ip_address") == 0) {
292 connman_dhcp_set_value(task->dhcp, "Address", value);
293 } else if (g_strcmp0(key, "new_subnet_mask") == 0) {
294 connman_dhcp_set_value(task->dhcp, "Netmask", value);
295 } else if (g_strcmp0(key, "new_routers") == 0) {
296 connman_dhcp_set_value(task->dhcp, "Gateway", value);
297 } else if (g_strcmp0(key, "new_network_number") == 0) {
298 connman_dhcp_set_value(task->dhcp, "Network", value);
299 } else if (g_strcmp0(key, "new_broadcast_address") == 0) {
300 connman_dhcp_set_value(task->dhcp, "Broadcast", value);
301 } else if (g_strcmp0(key, "new_domain_name_servers") == 0) {
302 connman_dhcp_set_value(task->dhcp, "Nameserver", value);
303 } else if (g_ascii_strcasecmp(key, "new_domain_name") == 0) {
304 connman_dhcp_set_value(task->dhcp, "Domainname", value);
305 } else if (g_ascii_strcasecmp(key, "new_domain_search") == 0) {
306 } else if (g_ascii_strcasecmp(key, "new_host_name") == 0) {
307 connman_dhcp_set_value(task->dhcp, "Hostname", value);
308 } else if (g_ascii_strcasecmp(key, "new_ntp_servers") == 0) {
309 connman_dhcp_set_value(task->dhcp, "Timeserver", value);
310 } else if (g_ascii_strcasecmp(key, "new_interface_mtu") == 0) {
311 connman_dhcp_set_value(task->dhcp, "MTU", value);
314 dbus_message_iter_next(&dict);
317 if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
318 } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
319 g_ascii_strcasecmp(text, "REBOOT") == 0) {
320 connman_dhcp_bound(task->dhcp);
321 } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
322 g_ascii_strcasecmp(text, "REBIND") == 0) {
323 connman_dhcp_renew(task->dhcp);
324 } else if (g_ascii_strcasecmp(text, "FAIL") == 0) {
325 connman_dhcp_fail(task->dhcp);
329 return DBUS_HANDLER_RESULT_HANDLED;
332 static DBusConnection *connection;
334 static const char *dhclient_rule = "path=" DHCLIENT_PATH
335 ",interface=" DHCLIENT_INTF;
337 static int dhclient_init(void)
341 connection = connman_dbus_get_connection();
343 busname = dbus_bus_get_unique_name(connection);
344 busname = CONNMAN_SERVICE;
346 dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
348 dbus_bus_add_match(connection, dhclient_rule, NULL);
350 err = connman_dhcp_driver_register(&dhclient_driver);
352 dbus_connection_unref(connection);
359 static void dhclient_exit(void)
363 for (list = task_list; list; list = list->next) {
364 struct dhclient_task *task = list->data;
366 DBG("killing process %d", task->pid);
372 g_slist_free(task_list);
374 connman_dhcp_driver_unregister(&dhclient_driver);
376 dbus_bus_remove_match(connection, dhclient_rule, NULL);
378 dbus_connection_remove_filter(connection, dhclient_filter, NULL);
380 dbus_connection_unref(connection);
383 CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
384 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)