Let the core handle the children unregister task
[framework/connectivity/connman.git] / plugins / dhclient.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 <sys/wait.h>
27 #include <glib/gstdio.h>
28
29 #include <connman/plugin.h>
30 #include <connman/driver.h>
31 #include <connman/dbus.h>
32 #include <connman/log.h>
33
34 #define DHCLIENT_INTF "org.isc.dhclient"
35 #define DHCLIENT_PATH "/org/isc/dhclient"
36
37 static const char *busname;
38
39 struct dhclient_task {
40         GPid pid;
41         int ifindex;
42         gchar *ifname;
43         struct connman_element *element;
44 };
45
46 static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
47 static GSList *task_list = NULL;
48
49 static struct dhclient_task *find_task_by_pid(GPid pid)
50 {
51         GSList *list;
52
53         for (list = task_list; list; list = list->next) {
54                 struct dhclient_task *task = list->data;
55
56                 if (task->pid == pid)
57                         return task;
58         }
59
60         return NULL;
61 }
62
63 static struct dhclient_task *find_task_by_index(int index)
64 {
65         GSList *list;
66
67         for (list = task_list; list; list = list->next) {
68                 struct dhclient_task *task = list->data;
69
70                 if (task->ifindex == index)
71                         return task;
72         }
73
74         return NULL;
75 }
76
77 static void kill_task(struct dhclient_task *task)
78 {
79         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
80
81         if (task->pid > 0)
82                 kill(task->pid, SIGTERM);
83 }
84
85 static void unlink_task(struct dhclient_task *task)
86 {
87         gchar *pathname;
88
89         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
90
91         pathname = g_strdup_printf("%s/dhclient.%s.pid",
92                                                 STATEDIR, task->ifname);
93         g_unlink(pathname);
94         g_free(pathname);
95
96         pathname = g_strdup_printf("%s/dhclient.%s.leases",
97                                                 STATEDIR, task->ifname);
98         g_unlink(pathname);
99         g_free(pathname);
100 }
101
102 static void task_died(GPid pid, gint status, gpointer data)
103 {
104         struct dhclient_task *task = data;
105
106         if (WIFEXITED(status))
107                 DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
108         else
109                 DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
110
111         g_spawn_close_pid(pid);
112         task->pid = 0;
113
114         g_static_mutex_lock(&task_mutex);
115         task_list = g_slist_remove(task_list, task);
116         g_static_mutex_unlock(&task_mutex);
117
118         unlink_task(task);
119
120         g_free(task->ifname);
121         g_free(task);
122 }
123
124 static void task_setup(gpointer data)
125 {
126         struct dhclient_task *task = data;
127
128         DBG("task %p name %s", task, task->ifname);
129 }
130
131 static int dhclient_probe(struct connman_element *element)
132 {
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];
136
137         DBG("element %p name %s", element, element->name);
138
139         task = g_try_new0(struct dhclient_task, 1);
140         if (task == NULL)
141                 return -ENOMEM;
142
143         task->ifindex = element->netdev.index;
144         task->ifname = g_strdup(element->netdev.name);
145         task->element = element;
146
147         if (task->ifname == NULL) {
148                 g_free(task);
149                 return -ENOMEM;
150         }
151
152         DBG("request %s", task->ifname);
153
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);
161
162         argv[0] = DHCLIENT;
163         argv[1] = "-d";
164         argv[2] = "-q";
165         argv[3] = "-n";
166         argv[4] = "-e";
167         argv[5] = address;
168         argv[6] = "-pf";
169         argv[7] = pidfile;
170         argv[8] = "-lf";
171         argv[9] = leases;
172         argv[10] = "-cf";
173         argv[11] = config;
174         argv[12] = "-sf";
175         argv[13] = script;
176         argv[14] = task->ifname;
177         argv[15] = NULL;
178
179         envp[0] = NULL;
180
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");
184                 return -1;
185         }
186
187         g_static_mutex_lock(&task_mutex);
188         task_list = g_slist_append(task_list, task);
189         g_static_mutex_unlock(&task_mutex);
190
191         g_child_watch_add(task->pid, task_died, task);
192
193         DBG("executed %s with pid %d", DHCLIENT, task->pid);
194
195         return 0;
196 }
197
198 static void dhclient_remove(struct connman_element *element)
199 {
200         struct dhclient_task *task;
201
202         DBG("element %p name %s", element, element->name);
203
204         g_static_mutex_lock(&task_mutex);
205
206         task = find_task_by_index(element->netdev.index);
207         if (task != NULL)
208                 task_list = g_slist_remove(task_list, task);
209
210         g_static_mutex_unlock(&task_mutex);
211
212         if (task == NULL)
213                 return;
214
215         DBG("release %s", task->ifname);
216
217         kill_task(task);
218 }
219
220 static struct connman_driver dhclient_driver = {
221         .name           = "dhclient",
222         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
223         .probe          = dhclient_probe,
224         .remove         = dhclient_remove,
225 };
226
227 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
228                                                 DBusMessage *msg, void *data)
229 {
230         DBusMessageIter iter, dict;
231         dbus_uint32_t pid;
232         struct dhclient_task *task;
233         const char *text, *key, *value;
234
235         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
236                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
237
238         dbus_message_iter_init(msg, &iter);
239
240         dbus_message_iter_get_basic(&iter, &pid);
241         dbus_message_iter_next(&iter);
242
243         dbus_message_iter_get_basic(&iter, &text);
244         dbus_message_iter_next(&iter);
245
246         DBG("change %d to %s", pid, text);
247
248         g_static_mutex_lock(&task_mutex);
249         task = find_task_by_pid(pid);
250         g_static_mutex_unlock(&task_mutex);
251
252         if (task == NULL)
253                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
254
255         dbus_message_iter_recurse(&iter, &dict);
256
257         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
258                 DBusMessageIter entry;
259
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);
264
265                 DBG("%s = %s", key, value);
266
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);
270                 }
271
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);
275                 }
276
277                 if (g_ascii_strcasecmp(key, "new_routers") == 0) {
278                         g_free(task->element->ipv4.gateway);
279                         task->element->ipv4.gateway = g_strdup(value);
280                 }
281
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);
285                 }
286
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);
290                 }
291
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);
295                 }
296
297                 dbus_message_iter_next(&dict);
298         }
299
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);
313         } else {
314         }
315
316         return DBUS_HANDLER_RESULT_HANDLED;
317 }
318
319 static DBusConnection *connection;
320
321 static int dhclient_init(void)
322 {
323         gchar *filter;
324         int err;
325
326         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
327
328         busname = dbus_bus_get_unique_name(connection);
329         busname = CONNMAN_SERVICE;
330
331         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
332
333         filter = g_strdup_printf("interface=%s,path=%s",
334                                                 DHCLIENT_INTF, DHCLIENT_PATH);
335
336         dbus_bus_add_match(connection, filter, NULL);
337
338         g_free(filter);
339
340         err = connman_driver_register(&dhclient_driver);
341         if (err < 0) {
342                 dbus_connection_unref(connection);
343                 return err;
344         }
345
346         return 0;
347 }
348
349 static void dhclient_exit(void)
350 {
351         GSList *list;
352
353         g_static_mutex_lock(&task_mutex);
354
355         for (list = task_list; list; list = list->next) {
356                 struct dhclient_task *task = list->data;
357
358                 DBG("killing process %d", task->pid);
359
360                 kill_task(task);
361                 unlink_task(task);
362         }
363
364         g_static_mutex_unlock(&task_mutex);
365
366         g_slist_free(task_list);
367
368         connman_driver_unregister(&dhclient_driver);
369
370         dbus_connection_unref(connection);
371 }
372
373 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
374                                                 dhclient_init, dhclient_exit)