Add debug output for used domain name server
[connman] / plugins / dhclient.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <sys/wait.h>
28 #include <glib/gstdio.h>
29
30 #define CONNMAN_API_SUBJECT_TO_CHANGE
31 #include <connman/plugin.h>
32 #include <connman/driver.h>
33 #include <connman/dbus.h>
34 #include <connman/log.h>
35
36 #include "inet.h"
37
38 #define DHCLIENT_INTF "org.isc.dhclient"
39 #define DHCLIENT_PATH "/org/isc/dhclient"
40
41 static const char *busname;
42
43 struct dhclient_task {
44         GPid pid;
45         int ifindex;
46         gchar *ifname;
47         struct connman_element *element;
48 };
49
50 static GSList *task_list = NULL;
51
52 static struct dhclient_task *find_task_by_pid(GPid pid)
53 {
54         GSList *list;
55
56         for (list = task_list; list; list = list->next) {
57                 struct dhclient_task *task = list->data;
58
59                 if (task->pid == pid)
60                         return task;
61         }
62
63         return NULL;
64 }
65
66 static struct dhclient_task *find_task_by_index(int index)
67 {
68         GSList *list;
69
70         for (list = task_list; list; list = list->next) {
71                 struct dhclient_task *task = list->data;
72
73                 if (task->ifindex == index)
74                         return task;
75         }
76
77         return NULL;
78 }
79
80 static void kill_task(struct dhclient_task *task)
81 {
82         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
83
84         if (task->pid > 0)
85                 kill(task->pid, SIGTERM);
86 }
87
88 static void unlink_task(struct dhclient_task *task)
89 {
90         gchar *pathname;
91
92         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
93
94         pathname = g_strdup_printf("%s/dhclient.%s.pid",
95                                                 STATEDIR, task->ifname);
96         g_unlink(pathname);
97         g_free(pathname);
98
99         pathname = g_strdup_printf("%s/dhclient.%s.leases",
100                                                 STATEDIR, task->ifname);
101         g_unlink(pathname);
102         g_free(pathname);
103 }
104
105 static void task_died(GPid pid, gint status, gpointer data)
106 {
107         struct dhclient_task *task = data;
108
109         if (WIFEXITED(status))
110                 DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
111         else
112                 DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
113
114         g_spawn_close_pid(pid);
115         task->pid = 0;
116
117         task_list = g_slist_remove(task_list, task);
118
119         unlink_task(task);
120
121         g_free(task->ifname);
122         g_free(task);
123 }
124
125 static void task_setup(gpointer data)
126 {
127         struct dhclient_task *task = data;
128
129         DBG("task %p name %s", task, task->ifname);
130 }
131
132 static int dhclient_probe(struct connman_element *element)
133 {
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];
137
138         DBG("element %p name %s", element, element->name);
139
140         if (access(DHCLIENT, X_OK) < 0)
141                 return -errno;
142
143         task = g_try_new0(struct dhclient_task, 1);
144         if (task == NULL)
145                 return -ENOMEM;
146
147         task->ifindex = element->index;
148         task->ifname = inet_index2name(element->index);
149         task->element = element;
150
151         if (task->ifname == NULL) {
152                 g_free(task);
153                 return -ENOMEM;
154         }
155
156         DBG("request %s", task->ifname);
157
158         snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
159         snprintf(pidfile, sizeof(pidfile) - 1,
160                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
161         snprintf(leases, sizeof(leases) - 1,
162                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
163         snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
164         snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
165
166         argv[0] = DHCLIENT;
167         argv[1] = "-d";
168         argv[2] = "-q";
169         argv[3] = "-n";
170         argv[4] = "-e";
171         argv[5] = address;
172         argv[6] = "-pf";
173         argv[7] = pidfile;
174         argv[8] = "-lf";
175         argv[9] = leases;
176         argv[10] = "-cf";
177         argv[11] = config;
178         argv[12] = "-sf";
179         argv[13] = script;
180         argv[14] = task->ifname;
181         argv[15] = NULL;
182
183         envp[0] = NULL;
184
185         if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
186                                 task_setup, task, &task->pid, NULL) == FALSE) {
187                 connman_error("Failed to spawn dhclient");
188                 return -1;
189         }
190
191         task_list = g_slist_append(task_list, task);
192
193         g_child_watch_add(task->pid, task_died, task);
194
195         DBG("executed %s with pid %d", DHCLIENT, task->pid);
196
197         return 0;
198 }
199
200 static void dhclient_remove(struct connman_element *element)
201 {
202         struct dhclient_task *task;
203
204         DBG("element %p name %s", element, element->name);
205
206         task = find_task_by_index(element->index);
207         if (task != NULL)
208                 task_list = g_slist_remove(task_list, task);
209
210         if (task == NULL)
211                 return;
212
213         DBG("release %s", task->ifname);
214
215         kill_task(task);
216 }
217
218 static void dhclient_change(struct connman_element *element)
219 {
220         DBG("element %p name %s", element, element->name);
221
222         if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
223                 connman_element_set_error(element->parent,
224                                         CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
225 }
226
227 static struct connman_driver dhclient_driver = {
228         .name           = "dhclient",
229         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
230         .probe          = dhclient_probe,
231         .remove         = dhclient_remove,
232         .change         = dhclient_change,
233 };
234
235 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
236                                                 DBusMessage *msg, void *data)
237 {
238         DBusMessageIter iter, dict;
239         dbus_uint32_t pid;
240         struct dhclient_task *task;
241         const char *text, *key, *value;
242
243         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
244                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
245
246         dbus_message_iter_init(msg, &iter);
247
248         dbus_message_iter_get_basic(&iter, &pid);
249         dbus_message_iter_next(&iter);
250
251         dbus_message_iter_get_basic(&iter, &text);
252         dbus_message_iter_next(&iter);
253
254         DBG("change %d to %s", pid, text);
255
256         task = find_task_by_pid(pid);
257
258         if (task == NULL)
259                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
260
261         dbus_message_iter_recurse(&iter, &dict);
262
263         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
264                 DBusMessageIter entry;
265
266                 dbus_message_iter_recurse(&dict, &entry);
267                 dbus_message_iter_get_basic(&entry, &key);
268                 dbus_message_iter_next(&entry);
269                 dbus_message_iter_get_basic(&entry, &value);
270
271                 DBG("%s = %s", key, value);
272
273                 if (g_ascii_strcasecmp(key, "new_ip_address") == 0) {
274                         g_free(task->element->ipv4.address);
275                         task->element->ipv4.address = g_strdup(value);
276                 }
277
278                 if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0) {
279                         g_free(task->element->ipv4.netmask);
280                         task->element->ipv4.netmask = g_strdup(value);
281                 }
282
283                 if (g_ascii_strcasecmp(key, "new_routers") == 0) {
284                         g_free(task->element->ipv4.gateway);
285                         task->element->ipv4.gateway = g_strdup(value);
286                 }
287
288                 if (g_ascii_strcasecmp(key, "new_network_number") == 0) {
289                         g_free(task->element->ipv4.network);
290                         task->element->ipv4.network = g_strdup(value);
291                 }
292
293                 if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0) {
294                         g_free(task->element->ipv4.broadcast);
295                         task->element->ipv4.broadcast = g_strdup(value);
296                 }
297
298                 if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0) {
299                         g_free(task->element->ipv4.nameserver);
300                         task->element->ipv4.nameserver = g_strdup(value);
301                 }
302
303                 dbus_message_iter_next(&dict);
304         }
305
306         if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
307         } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
308                                 g_ascii_strcasecmp(text, "REBOOT") == 0) {
309                 struct connman_element *element;
310                 element = connman_element_create(NULL);
311                 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
312                 element->index = task->ifindex;
313                 connman_element_update(task->element);
314                 if (connman_element_register(element, task->element) < 0)
315                         connman_element_unref(element);
316         } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
317                                 g_ascii_strcasecmp(text, "REBIND") == 0) {
318                 connman_element_update(task->element);
319         } else if (g_ascii_strcasecmp(text, "FAIL") == 0) {
320                 connman_element_set_error(task->element,
321                                                 CONNMAN_ELEMENT_ERROR_FAILED);
322         } else {
323         }
324
325         return DBUS_HANDLER_RESULT_HANDLED;
326 }
327
328 static DBusConnection *connection;
329
330 static const char *dhclient_rule = "path=" DHCLIENT_PATH
331                                                 ",interface=" DHCLIENT_INTF;
332
333 static int dhclient_init(void)
334 {
335         int err;
336
337         connection = connman_dbus_get_connection();
338
339         busname = dbus_bus_get_unique_name(connection);
340         busname = CONNMAN_SERVICE;
341
342         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
343
344         dbus_bus_add_match(connection, dhclient_rule, NULL);
345
346         err = connman_driver_register(&dhclient_driver);
347         if (err < 0) {
348                 dbus_connection_unref(connection);
349                 return err;
350         }
351
352         return 0;
353 }
354
355 static void dhclient_exit(void)
356 {
357         GSList *list;
358
359         for (list = task_list; list; list = list->next) {
360                 struct dhclient_task *task = list->data;
361
362                 DBG("killing process %d", task->pid);
363
364                 kill_task(task);
365                 unlink_task(task);
366         }
367
368         g_slist_free(task_list);
369
370         connman_driver_unregister(&dhclient_driver);
371
372         dbus_bus_remove_match(connection, dhclient_rule, NULL);
373
374         dbus_connection_remove_filter(connection, dhclient_filter, NULL);
375
376         dbus_connection_unref(connection);
377 }
378
379 CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
380                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)