Don't unregister elements that are not valid
[connman] / 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         struct connman_element *child;
45 };
46
47 static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
48 static GSList *task_list = NULL;
49
50 static struct dhclient_task *find_task_by_pid(GPid pid)
51 {
52         GSList *list;
53
54         for (list = task_list; list; list = list->next) {
55                 struct dhclient_task *task = list->data;
56
57                 if (task->pid == pid)
58                         return task;
59         }
60
61         return NULL;
62 }
63
64 static struct dhclient_task *find_task_by_index(int index)
65 {
66         GSList *list;
67
68         for (list = task_list; list; list = list->next) {
69                 struct dhclient_task *task = list->data;
70
71                 if (task->ifindex == index)
72                         return task;
73         }
74
75         return NULL;
76 }
77
78 static void kill_task(struct dhclient_task *task)
79 {
80         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
81
82         if (task->pid > 0)
83                 kill(task->pid, SIGTERM);
84 }
85
86 static void unlink_task(struct dhclient_task *task)
87 {
88         gchar *pathname;
89
90         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
91
92         pathname = g_strdup_printf("%s/dhclient.%s.pid",
93                                                 STATEDIR, task->ifname);
94         g_unlink(pathname);
95         g_free(pathname);
96
97         pathname = g_strdup_printf("%s/dhclient.%s.leases",
98                                                 STATEDIR, task->ifname);
99         g_unlink(pathname);
100         g_free(pathname);
101 }
102
103 static void task_died(GPid pid, gint status, gpointer data)
104 {
105         struct dhclient_task *task = data;
106
107         if (WIFEXITED(status))
108                 DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
109         else
110                 DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
111
112         g_spawn_close_pid(pid);
113         task->pid = 0;
114
115         g_static_mutex_lock(&task_mutex);
116         task_list = g_slist_remove(task_list, task);
117         g_static_mutex_unlock(&task_mutex);
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         task = g_try_new0(struct dhclient_task, 1);
141         if (task == NULL)
142                 return -ENOMEM;
143
144         task->ifindex = element->netdev.index;
145         task->ifname = g_strdup(element->netdev.name);
146         task->element = element;
147         task->child = NULL;
148
149         if (task->ifname == NULL) {
150                 g_free(task);
151                 return -ENOMEM;
152         }
153
154         DBG("request %s", task->ifname);
155
156         snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
157         snprintf(pidfile, sizeof(pidfile) - 1,
158                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
159         snprintf(leases, sizeof(leases) - 1,
160                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
161         snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
162         snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
163
164         argv[0] = DHCLIENT;
165         argv[1] = "-d";
166         argv[2] = "-q";
167         argv[3] = "-n";
168         argv[4] = "-e";
169         argv[5] = address;
170         argv[6] = "-pf";
171         argv[7] = pidfile;
172         argv[8] = "-lf";
173         argv[9] = leases;
174         argv[10] = "-cf";
175         argv[11] = config;
176         argv[12] = "-sf";
177         argv[13] = script;
178         argv[14] = task->ifname;
179         argv[15] = NULL;
180
181         envp[0] = NULL;
182
183         if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
184                                 task_setup, task, &task->pid, NULL) == FALSE) {
185                 connman_error("Failed to spawn dhclient");
186                 return -1;
187         }
188
189         g_static_mutex_lock(&task_mutex);
190         task_list = g_slist_append(task_list, task);
191         g_static_mutex_unlock(&task_mutex);
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         g_static_mutex_lock(&task_mutex);
207         task = find_task_by_index(element->netdev.index);
208         g_static_mutex_unlock(&task_mutex);
209
210         if (task == NULL)
211                 return;
212
213         DBG("release %s", task->ifname);
214
215         g_static_mutex_lock(&task_mutex);
216         task_list = g_slist_remove(task_list, task);
217         g_static_mutex_unlock(&task_mutex);
218
219         if (task->child != NULL) {
220                 connman_element_unregister(task->child);
221                 connman_element_unref(task->child);
222                 task->child = NULL;
223         }
224
225         kill_task(task);
226 }
227
228 static struct connman_driver dhclient_driver = {
229         .name           = "dhclient",
230         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
231         .probe          = dhclient_probe,
232         .remove         = dhclient_remove,
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         g_static_mutex_lock(&task_mutex);
257         task = find_task_by_pid(pid);
258         g_static_mutex_unlock(&task_mutex);
259
260         if (task == NULL)
261                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
262
263         dbus_message_iter_recurse(&iter, &dict);
264
265         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
266                 DBusMessageIter entry;
267
268                 dbus_message_iter_recurse(&dict, &entry);
269                 dbus_message_iter_get_basic(&entry, &key);
270                 dbus_message_iter_next(&entry);
271                 dbus_message_iter_get_basic(&entry, &value);
272
273                 DBG("%s = %s", key, value);
274
275                 if (g_ascii_strcasecmp(key, "new_ip_address") == 0) {
276                         g_free(task->element->ipv4.address);
277                         task->element->ipv4.address = g_strdup(value);
278                 }
279
280                 if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0) {
281                         g_free(task->element->ipv4.netmask);
282                         task->element->ipv4.netmask = g_strdup(value);
283                 }
284
285                 if (g_ascii_strcasecmp(key, "new_routers") == 0) {
286                         g_free(task->element->ipv4.gateway);
287                         task->element->ipv4.gateway = g_strdup(value);
288                 }
289
290                 if (g_ascii_strcasecmp(key, "new_network_number") == 0) {
291                         g_free(task->element->ipv4.network);
292                         task->element->ipv4.network = g_strdup(value);
293                 }
294
295                 if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0) {
296                         g_free(task->element->ipv4.broadcast);
297                         task->element->ipv4.broadcast = g_strdup(value);
298                 }
299
300                 if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0) {
301                         g_free(task->element->ipv4.nameserver);
302                         task->element->ipv4.nameserver = g_strdup(value);
303                 }
304
305                 dbus_message_iter_next(&dict);
306         }
307
308         if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
309         } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
310                                 g_ascii_strcasecmp(text, "REBOOT") == 0) {
311                 task->child = connman_element_create();
312                 task->child->type = CONNMAN_ELEMENT_TYPE_IPV4;
313                 task->child->netdev.index = task->ifindex;
314                 connman_element_update(task->element);
315                 connman_element_register(task->child, task->element);
316         } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
317                                 g_ascii_strcasecmp(text, "REBIND") == 0) {
318                 connman_element_update(task->element);
319                 connman_element_update(task->child);
320         } else {
321         }
322
323         return DBUS_HANDLER_RESULT_HANDLED;
324 }
325
326 static DBusConnection *connection;
327
328 static int dhclient_init(void)
329 {
330         gchar *filter;
331         int err;
332
333         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
334
335         busname = dbus_bus_get_unique_name(connection);
336         busname = CONNMAN_SERVICE;
337
338         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
339
340         filter = g_strdup_printf("interface=%s,path=%s",
341                                                 DHCLIENT_INTF, DHCLIENT_PATH);
342
343         dbus_bus_add_match(connection, filter, NULL);
344
345         g_free(filter);
346
347         err = connman_driver_register(&dhclient_driver);
348         if (err < 0) {
349                 dbus_connection_unref(connection);
350                 return err;
351         }
352
353         return 0;
354 }
355
356 static void dhclient_exit(void)
357 {
358         GSList *list;
359
360         g_static_mutex_lock(&task_mutex);
361
362         for (list = task_list; list; list = list->next) {
363                 struct dhclient_task *task = list->data;
364
365                 if (task->child) {
366                         connman_element_unregister(task->child);
367                         connman_element_unref(task->child);
368                         task->child = NULL;
369                 }
370
371                 DBG("killing process %d", task->pid);
372
373                 kill_task(task);
374                 unlink_task(task);
375         }
376
377         g_static_mutex_unlock(&task_mutex);
378
379         g_slist_free(task_list);
380
381         connman_driver_unregister(&dhclient_driver);
382
383         dbus_connection_unref(connection);
384 }
385
386 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
387                                                 dhclient_init, dhclient_exit)