56e1575d3a754f0b054053779c1ef05531456026
[framework/connectivity/connman.git] / 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/inet.h>
34 #include <connman/dbus.h>
35 #include <connman/log.h>
36
37 #define DHCLIENT_INTF "org.isc.dhclient"
38 #define DHCLIENT_PATH "/org/isc/dhclient"
39
40 static const char *busname;
41
42 struct dhclient_task {
43         GPid pid;
44         gboolean killed;
45         int ifindex;
46         gchar *ifname;
47         struct connman_element *element;
48         struct dhclient_task *pending;
49 };
50
51 static GSList *task_list = NULL;
52
53 static struct dhclient_task *find_task_by_pid(GPid pid)
54 {
55         GSList *list;
56
57         for (list = task_list; list; list = list->next) {
58                 struct dhclient_task *task = list->data;
59
60                 if (task->pid == pid)
61                         return task;
62         }
63
64         return NULL;
65 }
66
67 static struct dhclient_task *find_task_by_index(int index)
68 {
69         GSList *list;
70
71         for (list = task_list; list; list = list->next) {
72                 struct dhclient_task *task = list->data;
73
74                 if (task->ifindex == index)
75                         return task;
76         }
77
78         return NULL;
79 }
80
81 static void kill_task(struct dhclient_task *task)
82 {
83         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
84
85         if (task->killed == TRUE)
86                 return;
87
88         if (task->pid > 0) {
89                 task->killed = TRUE;
90                 kill(task->pid, SIGTERM);
91         }
92 }
93
94 static void unlink_task(struct dhclient_task *task)
95 {
96         gchar *pathname;
97
98         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
99
100         pathname = g_strdup_printf("%s/dhclient.%s.pid",
101                                                 STATEDIR, task->ifname);
102         g_unlink(pathname);
103         g_free(pathname);
104
105         pathname = g_strdup_printf("%s/dhclient.%s.leases",
106                                                 STATEDIR, task->ifname);
107         g_unlink(pathname);
108         g_free(pathname);
109 }
110
111 static int start_dhclient(struct dhclient_task *task);
112
113 static void task_died(GPid pid, gint status, gpointer data)
114 {
115         struct dhclient_task *task = data;
116
117         if (WIFEXITED(status))
118                 DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
119         else
120                 DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
121
122         g_spawn_close_pid(pid);
123         task->pid = 0;
124
125         task_list = g_slist_remove(task_list, task);
126
127         unlink_task(task);
128
129         if (task->pending != NULL)
130                 start_dhclient(task->pending);
131
132         g_free(task->ifname);
133         g_free(task);
134 }
135
136 static void task_setup(gpointer data)
137 {
138         struct dhclient_task *task = data;
139
140         DBG("task %p name %s", task, task->ifname);
141
142         task->killed = FALSE;
143 }
144
145 static int start_dhclient(struct dhclient_task *task)
146 {
147         char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
148         char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
149
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);
157
158         argv[0] = DHCLIENT;
159         argv[1] = "-d";
160         argv[2] = "-q";
161         argv[3] = "-e";
162         argv[4] = address;
163         argv[5] = "-pf";
164         argv[6] = pidfile;
165         argv[7] = "-lf";
166         argv[8] = leases;
167         argv[9] = "-cf";
168         argv[10] = config;
169         argv[11] = "-sf";
170         argv[12] = script;
171         argv[13] = task->ifname;
172         argv[14] = "-n";
173         argv[15] = NULL;
174
175         envp[0] = NULL;
176
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");
180                 return -1;
181         }
182
183         task_list = g_slist_append(task_list, task);
184
185         g_child_watch_add(task->pid, task_died, task);
186
187         DBG("executed %s with pid %d", DHCLIENT, task->pid);
188
189         return 0;
190 }
191
192 static int dhclient_probe(struct connman_element *element)
193 {
194         struct dhclient_task *task, *previous;
195         DBG("element %p name %s", element, element->name);
196
197         if (access(DHCLIENT, X_OK) < 0)
198                 return -errno;
199
200         task = g_try_new0(struct dhclient_task, 1);
201         if (task == NULL)
202                 return -ENOMEM;
203
204         task->ifindex = element->index;
205         task->ifname  = connman_inet_ifname(element->index);
206         task->element = element;
207
208         if (task->ifname == NULL) {
209                 g_free(task);
210                 return -ENOMEM;
211         }
212
213         previous= find_task_by_index(element->index);
214         if (previous != NULL) {
215                 previous->pending = task;
216                 kill_task(previous);
217                 return 0;
218         }
219
220         return start_dhclient(task);
221 }
222
223 static void dhclient_remove(struct connman_element *element)
224 {
225         struct dhclient_task *task;
226
227         DBG("element %p name %s", element, element->name);
228
229         task = find_task_by_index(element->index);
230         if (task == NULL)
231                 return;
232
233         DBG("release %s", task->ifname);
234
235         kill_task(task);
236 }
237
238 static void dhclient_change(struct connman_element *element)
239 {
240         DBG("element %p name %s", element, element->name);
241
242         if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
243                 connman_element_set_error(element->parent,
244                                         CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
245 }
246
247 static struct connman_driver dhclient_driver = {
248         .name           = "dhclient",
249         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
250         .probe          = dhclient_probe,
251         .remove         = dhclient_remove,
252         .change         = dhclient_change,
253 };
254
255 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
256                                                 DBusMessage *msg, void *data)
257 {
258         DBusMessageIter iter, dict;
259         dbus_uint32_t pid;
260         struct dhclient_task *task;
261         const char *text, *key, *value;
262
263         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
264                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
265
266         dbus_message_iter_init(msg, &iter);
267
268         dbus_message_iter_get_basic(&iter, &pid);
269         dbus_message_iter_next(&iter);
270
271         dbus_message_iter_get_basic(&iter, &text);
272         dbus_message_iter_next(&iter);
273
274         DBG("change %d to %s", pid, text);
275
276         task = find_task_by_pid(pid);
277
278         if (task == NULL)
279                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
280
281         dbus_message_iter_recurse(&iter, &dict);
282
283         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
284                 DBusMessageIter entry;
285
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);
290
291                 DBG("%s = %s", key, value);
292
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);
296                 }
297
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);
301                 }
302
303                 if (g_ascii_strcasecmp(key, "new_routers") == 0) {
304                         g_free(task->element->ipv4.gateway);
305                         task->element->ipv4.gateway = g_strdup(value);
306                 }
307
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);
311                 }
312
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);
316                 }
317
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);
321                 }
322
323                 if (g_ascii_strcasecmp(key, "new_domain_name") == 0) {
324                 }
325
326                 if (g_ascii_strcasecmp(key, "new_domain_search") == 0) {
327                 }
328
329                 if (g_ascii_strcasecmp(key, "new_host_name") == 0) {
330                 }
331
332                 dbus_message_iter_next(&dict);
333         }
334
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);
351         } else {
352         }
353
354         return DBUS_HANDLER_RESULT_HANDLED;
355 }
356
357 static DBusConnection *connection;
358
359 static const char *dhclient_rule = "path=" DHCLIENT_PATH
360                                                 ",interface=" DHCLIENT_INTF;
361
362 static int dhclient_init(void)
363 {
364         int err;
365
366         connection = connman_dbus_get_connection();
367
368         busname = dbus_bus_get_unique_name(connection);
369         busname = CONNMAN_SERVICE;
370
371         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
372
373         dbus_bus_add_match(connection, dhclient_rule, NULL);
374
375         err = connman_driver_register(&dhclient_driver);
376         if (err < 0) {
377                 dbus_connection_unref(connection);
378                 return err;
379         }
380
381         return 0;
382 }
383
384 static void dhclient_exit(void)
385 {
386         GSList *list;
387
388         for (list = task_list; list; list = list->next) {
389                 struct dhclient_task *task = list->data;
390
391                 DBG("killing process %d", task->pid);
392
393                 kill_task(task);
394                 unlink_task(task);
395         }
396
397         g_slist_free(task_list);
398
399         connman_driver_unregister(&dhclient_driver);
400
401         dbus_bus_remove_match(connection, dhclient_rule, NULL);
402
403         dbus_connection_remove_filter(connection, dhclient_filter, NULL);
404
405         dbus_connection_unref(connection);
406 }
407
408 CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
409                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)