d39b2e1d04e4e47f6371f07136c7c50ade7fd1eb
[connman] / plugins / resolvfile.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 <stdlib.h>
27
28 #include <connman/plugin.h>
29 #include <connman/driver.h>
30 #include <connman/log.h>
31
32 static int resolvfile_probe(struct connman_element *element)
33 {
34         const char *nameserver = NULL;
35         struct connman_element *internet;
36         gchar *cmd;
37         int err;
38
39         DBG("element %p name %s", element, element->name);
40
41         connman_element_get_value(element,
42                         CONNMAN_PROPERTY_TYPE_IPV4_NAMESERVER, &nameserver);
43
44         if (nameserver == NULL)
45                 return -EINVAL;
46
47         cmd = g_strdup_printf("echo \"nameserver %s\" > /etc/resolv.conf",
48                                                                 nameserver);
49
50         DBG("%s", cmd);
51
52         err = system(cmd);
53
54         g_free(cmd);
55
56         internet = connman_element_create();
57
58         internet->type = CONNMAN_ELEMENT_TYPE_INTERNET;
59
60         connman_element_set_data(element, internet);
61
62         connman_element_register(internet, element);
63
64         return 0;
65 }
66
67 static void resolvfile_remove(struct connman_element *element)
68 {
69         struct connman_element *internet = connman_element_get_data(element);
70
71         DBG("element %p name %s", element, element->name);
72
73         connman_element_set_data(element, NULL);
74
75         connman_element_unregister(internet);
76
77         connman_element_unref(internet);
78 }
79
80 static struct connman_driver resolvfile_driver = {
81         .name           = "resolvconf",
82         .type           = CONNMAN_ELEMENT_TYPE_RESOLVER,
83         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
84         .probe          = resolvfile_probe,
85         .remove         = resolvfile_remove,
86 };
87
88 static int resolvfile_init(void)
89 {
90         return connman_driver_register(&resolvfile_driver);
91 }
92
93 static void resolvfile_exit(void)
94 {
95         connman_driver_unregister(&resolvfile_driver);
96 }
97
98 CONNMAN_PLUGIN_DEFINE("resolvfile", "Name resolver plugin", VERSION,
99                                         resolvfile_init, resolvfile_exit)