Update dhclient plugin with support for element infrastructure
[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 *parent;
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->parent = 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         connman_element_unregister(task->child);
220         connman_element_unref(task->child);
221         task->child = NULL;
222
223         kill_task(task);
224 }
225
226 static struct connman_driver dhclient_driver = {
227         .name           = "dhclient",
228         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
229         .probe          = dhclient_probe,
230         .remove         = dhclient_remove,
231 };
232
233 static void copy_ipv4(struct connman_element *src, struct connman_element *dst)
234 {
235         g_free(dst->ipv4.address);
236         g_free(dst->ipv4.netmask);
237         g_free(dst->ipv4.gateway);
238         g_free(dst->ipv4.network);
239         g_free(dst->ipv4.broadcast);
240         g_free(dst->ipv4.nameserver);
241
242         dst->ipv4.address = g_strdup(src->ipv4.address);
243         dst->ipv4.netmask = g_strdup(src->ipv4.netmask);
244         dst->ipv4.gateway = g_strdup(src->ipv4.gateway);
245         dst->ipv4.network = g_strdup(src->ipv4.network);
246         dst->ipv4.broadcast = g_strdup(src->ipv4.broadcast);
247         dst->ipv4.nameserver = g_strdup(src->ipv4.nameserver);
248 }
249
250 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
251                                                 DBusMessage *msg, void *data)
252 {
253         DBusMessageIter iter, dict;
254         dbus_uint32_t pid;
255         struct dhclient_task *task;
256         const char *text, *key, *value;
257
258         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
259                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
260
261         dbus_message_iter_init(msg, &iter);
262
263         dbus_message_iter_get_basic(&iter, &pid);
264         dbus_message_iter_next(&iter);
265
266         dbus_message_iter_get_basic(&iter, &text);
267         dbus_message_iter_next(&iter);
268
269         DBG("change %d to %s", pid, text);
270
271         g_static_mutex_lock(&task_mutex);
272         task = find_task_by_pid(pid);
273         g_static_mutex_unlock(&task_mutex);
274
275         if (task == NULL)
276                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
277
278         dbus_message_iter_recurse(&iter, &dict);
279
280         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
281                 DBusMessageIter entry;
282
283                 dbus_message_iter_recurse(&dict, &entry);
284                 dbus_message_iter_get_basic(&entry, &key);
285                 dbus_message_iter_next(&entry);
286                 dbus_message_iter_get_basic(&entry, &value);
287
288                 DBG("%s = %s", key, value);
289
290                 if (g_ascii_strcasecmp(key, "new_ip_address") == 0)
291                         task->parent->ipv4.address = g_strdup(value);
292
293                 if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0)
294                         task->parent->ipv4.netmask = g_strdup(value);
295
296                 if (g_ascii_strcasecmp(key, "new_routers") == 0)
297                         task->parent->ipv4.gateway = g_strdup(value);
298
299                 if (g_ascii_strcasecmp(key, "new_network_number") == 0)
300                         task->parent->ipv4.network = g_strdup(value);
301
302                 if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0)
303                         task->parent->ipv4.broadcast = g_strdup(value);
304
305                 if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0)
306                         task->parent->ipv4.nameserver = g_strdup(value);
307
308                 dbus_message_iter_next(&dict);
309         }
310
311         if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
312         } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
313                                 g_ascii_strcasecmp(text, "REBOOT") == 0) {
314                 task->child = connman_element_create();
315                 task->child->type = CONNMAN_ELEMENT_TYPE_IPV4;
316                 task->child->netdev.index = task->ifindex;
317                 copy_ipv4(task->parent, task->child);
318                 connman_element_register(task->child, task->parent);
319         } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
320                                 g_ascii_strcasecmp(text, "REBIND") == 0) {
321                 copy_ipv4(task->parent, task->child);
322                 connman_element_update(task->child);
323         } else {
324         }
325
326         return DBUS_HANDLER_RESULT_HANDLED;
327 }
328
329 static DBusConnection *connection;
330
331 static int dhclient_init(void)
332 {
333         gchar *filter;
334         int err;
335
336         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
337
338         busname = dbus_bus_get_unique_name(connection);
339         busname = CONNMAN_SERVICE;
340
341         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
342
343         filter = g_strdup_printf("interface=%s,path=%s",
344                                                 DHCLIENT_INTF, DHCLIENT_PATH);
345
346         dbus_bus_add_match(connection, filter, NULL);
347
348         g_free(filter);
349
350         err = connman_driver_register(&dhclient_driver);
351         if (err < 0) {
352                 dbus_connection_unref(connection);
353                 return err;
354         }
355
356         return 0;
357 }
358
359 static void dhclient_exit(void)
360 {
361         GSList *list;
362
363         g_static_mutex_lock(&task_mutex);
364
365         for (list = task_list; list; list = list->next) {
366                 struct dhclient_task *task = list->data;
367
368                 if (task->child) {
369                         connman_element_unregister(task->child);
370                         connman_element_unref(task->child);
371                         task->child = NULL;
372                 }
373
374                 DBG("killing process %d", task->pid);
375
376                 kill_task(task);
377                 unlink_task(task);
378         }
379
380         g_static_mutex_unlock(&task_mutex);
381
382         g_slist_free(task_list);
383
384         connman_driver_unregister(&dhclient_driver);
385
386         dbus_connection_unref(connection);
387 }
388
389 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
390                                                 dhclient_init, dhclient_exit)