Check if resolvconf is actually installed
[framework/connectivity/connman.git] / plugins / resolvconf.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 <unistd.h>
27 #include <stdlib.h>
28
29 #include <connman/plugin.h>
30 #include <connman/driver.h>
31 #include <connman/log.h>
32
33 #define RESOLVCONF "/sbin/resolvconf"
34
35 static int resolvconf_probe(struct connman_element *element)
36 {
37         const char *nameserver = NULL;
38         struct connman_element *internet;
39         gchar *cmd;
40         int err;
41
42         DBG("element %p name %s", element, element->name);
43
44         if (access(RESOLVCONF, X_OK) < 0)
45                 return -errno;
46
47         connman_element_get_value(element,
48                         CONNMAN_PROPERTY_TYPE_IPV4_NAMESERVER, &nameserver);
49
50         if (nameserver == NULL)
51                 return -EINVAL;
52
53         cmd = g_strdup_printf("echo \"nameserver %s\" | %s -a %s",
54                                                 RESOLVCONF, nameserver,
55                                                         element->netdev.name);
56
57         DBG("%s", cmd);
58
59         err = system(cmd);
60
61         g_free(cmd);
62
63         internet = connman_element_create();
64
65         internet->type = CONNMAN_ELEMENT_TYPE_INTERNET;
66
67         connman_element_set_data(element, internet);
68
69         connman_element_register(internet, element);
70
71         return 0;
72 }
73
74 static void resolvconf_remove(struct connman_element *element)
75 {
76         struct connman_element *internet = connman_element_get_data(element);
77         gchar *cmd;
78         int err;
79
80         DBG("element %p name %s", element, element->name);
81
82         connman_element_set_data(element, NULL);
83
84         connman_element_unregister(internet);
85
86         connman_element_unref(internet);
87
88         cmd = g_strdup_printf("%s -d %s", RESOLVCONF, element->netdev.name);
89
90         DBG("%s", cmd);
91
92         err = system(cmd);
93
94         g_free(cmd);
95 }
96
97 static struct connman_driver resolvconf_driver = {
98         .name           = "resolvconf",
99         .type           = CONNMAN_ELEMENT_TYPE_RESOLVER,
100         .priority       = CONNMAN_DRIVER_PRIORITY_HIGH,
101         .probe          = resolvconf_probe,
102         .remove         = resolvconf_remove,
103 };
104
105 static int resolvconf_init(void)
106 {
107         return connman_driver_register(&resolvconf_driver);
108 }
109
110 static void resolvconf_exit(void)
111 {
112         connman_driver_unregister(&resolvconf_driver);
113 }
114
115 CONNMAN_PLUGIN_DEFINE("resolvconf", "Name resolver plugin", VERSION,
116                                         resolvconf_init, resolvconf_exit)