Allow pre-processor pasting of plugin name
[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 #include "inet.h"
35
36 #define DHCLIENT_INTF "org.isc.dhclient"
37 #define DHCLIENT_PATH "/org/isc/dhclient"
38
39 static const char *busname;
40
41 struct dhclient_task {
42         GPid pid;
43         int ifindex;
44         gchar *ifname;
45         struct connman_element *element;
46 };
47
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         task_list = g_slist_remove(task_list, task);
116
117         unlink_task(task);
118
119         g_free(task->ifname);
120         g_free(task);
121 }
122
123 static void task_setup(gpointer data)
124 {
125         struct dhclient_task *task = data;
126
127         DBG("task %p name %s", task, task->ifname);
128 }
129
130 static int dhclient_probe(struct connman_element *element)
131 {
132         struct dhclient_task *task;
133         char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
134         char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
135
136         DBG("element %p name %s", element, element->name);
137
138         task = g_try_new0(struct dhclient_task, 1);
139         if (task == NULL)
140                 return -ENOMEM;
141
142         task->ifindex = element->index;
143         task->ifname = inet_index2name(element->index);
144         task->element = element;
145
146         if (task->ifname == NULL) {
147                 g_free(task);
148                 return -ENOMEM;
149         }
150
151         DBG("request %s", task->ifname);
152
153         snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
154         snprintf(pidfile, sizeof(pidfile) - 1,
155                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
156         snprintf(leases, sizeof(leases) - 1,
157                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
158         snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
159         snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
160
161         argv[0] = DHCLIENT;
162         argv[1] = "-d";
163         argv[2] = "-q";
164         argv[3] = "-n";
165         argv[4] = "-e";
166         argv[5] = address;
167         argv[6] = "-pf";
168         argv[7] = pidfile;
169         argv[8] = "-lf";
170         argv[9] = leases;
171         argv[10] = "-cf";
172         argv[11] = config;
173         argv[12] = "-sf";
174         argv[13] = script;
175         argv[14] = task->ifname;
176         argv[15] = NULL;
177
178         envp[0] = NULL;
179
180         if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
181                                 task_setup, task, &task->pid, NULL) == FALSE) {
182                 connman_error("Failed to spawn dhclient");
183                 return -1;
184         }
185
186         task_list = g_slist_append(task_list, task);
187
188         g_child_watch_add(task->pid, task_died, task);
189
190         DBG("executed %s with pid %d", DHCLIENT, task->pid);
191
192         return 0;
193 }
194
195 static void dhclient_remove(struct connman_element *element)
196 {
197         struct dhclient_task *task;
198
199         DBG("element %p name %s", element, element->name);
200
201         task = find_task_by_index(element->index);
202         if (task != NULL)
203                 task_list = g_slist_remove(task_list, task);
204
205         if (task == NULL)
206                 return;
207
208         DBG("release %s", task->ifname);
209
210         kill_task(task);
211 }
212
213 static struct connman_driver dhclient_driver = {
214         .name           = "dhclient",
215         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
216         .probe          = dhclient_probe,
217         .remove         = dhclient_remove,
218 };
219
220 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
221                                                 DBusMessage *msg, void *data)
222 {
223         DBusMessageIter iter, dict;
224         dbus_uint32_t pid;
225         struct dhclient_task *task;
226         const char *text, *key, *value;
227
228         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
229                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
230
231         dbus_message_iter_init(msg, &iter);
232
233         dbus_message_iter_get_basic(&iter, &pid);
234         dbus_message_iter_next(&iter);
235
236         dbus_message_iter_get_basic(&iter, &text);
237         dbus_message_iter_next(&iter);
238
239         DBG("change %d to %s", pid, text);
240
241         task = find_task_by_pid(pid);
242
243         if (task == NULL)
244                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
245
246         dbus_message_iter_recurse(&iter, &dict);
247
248         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
249                 DBusMessageIter entry;
250
251                 dbus_message_iter_recurse(&dict, &entry);
252                 dbus_message_iter_get_basic(&entry, &key);
253                 dbus_message_iter_next(&entry);
254                 dbus_message_iter_get_basic(&entry, &value);
255
256                 DBG("%s = %s", key, value);
257
258                 if (g_ascii_strcasecmp(key, "new_ip_address") == 0) {
259                         g_free(task->element->ipv4.address);
260                         task->element->ipv4.address = g_strdup(value);
261                 }
262
263                 if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0) {
264                         g_free(task->element->ipv4.netmask);
265                         task->element->ipv4.netmask = g_strdup(value);
266                 }
267
268                 if (g_ascii_strcasecmp(key, "new_routers") == 0) {
269                         g_free(task->element->ipv4.gateway);
270                         task->element->ipv4.gateway = g_strdup(value);
271                 }
272
273                 if (g_ascii_strcasecmp(key, "new_network_number") == 0) {
274                         g_free(task->element->ipv4.network);
275                         task->element->ipv4.network = g_strdup(value);
276                 }
277
278                 if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0) {
279                         g_free(task->element->ipv4.broadcast);
280                         task->element->ipv4.broadcast = g_strdup(value);
281                 }
282
283                 if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0) {
284                         g_free(task->element->ipv4.nameserver);
285                         task->element->ipv4.nameserver = g_strdup(value);
286                 }
287
288                 dbus_message_iter_next(&dict);
289         }
290
291         if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
292         } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
293                                 g_ascii_strcasecmp(text, "REBOOT") == 0) {
294                 struct connman_element *element;
295                 element = connman_element_create(NULL);
296                 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
297                 element->index = task->ifindex;
298                 connman_element_update(task->element);
299                 connman_element_register(element, task->element);
300         } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
301                                 g_ascii_strcasecmp(text, "REBIND") == 0) {
302                 connman_element_update(task->element);
303         } else {
304         }
305
306         return DBUS_HANDLER_RESULT_HANDLED;
307 }
308
309 static DBusConnection *connection;
310
311 static int dhclient_init(void)
312 {
313         gchar *filter;
314         int err;
315
316         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
317
318         busname = dbus_bus_get_unique_name(connection);
319         busname = CONNMAN_SERVICE;
320
321         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
322
323         filter = g_strdup_printf("interface=%s,path=%s",
324                                                 DHCLIENT_INTF, DHCLIENT_PATH);
325
326         dbus_bus_add_match(connection, filter, NULL);
327
328         g_free(filter);
329
330         err = connman_driver_register(&dhclient_driver);
331         if (err < 0) {
332                 dbus_connection_unref(connection);
333                 return err;
334         }
335
336         return 0;
337 }
338
339 static void dhclient_exit(void)
340 {
341         GSList *list;
342
343         for (list = task_list; list; list = list->next) {
344                 struct dhclient_task *task = list->data;
345
346                 DBG("killing process %d", task->pid);
347
348                 kill_task(task);
349                 unlink_task(task);
350         }
351
352         g_slist_free(task_list);
353
354         connman_driver_unregister(&dhclient_driver);
355
356         dbus_connection_unref(connection);
357 }
358
359 CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
360                                                 dhclient_init, dhclient_exit)