Make HAL requirement optional and check for PolicyKit
[connman] / 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                                                 nameserver, RESOLVCONF,
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_register(internet, element);
68
69         return 0;
70 }
71
72 static void resolvconf_remove(struct connman_element *element)
73 {
74         gchar *cmd;
75         int err;
76
77         DBG("element %p name %s", element, element->name);
78
79         cmd = g_strdup_printf("%s -d %s", RESOLVCONF, element->netdev.name);
80
81         DBG("%s", cmd);
82
83         err = system(cmd);
84
85         g_free(cmd);
86 }
87
88 static struct connman_driver resolvconf_driver = {
89         .name           = "resolvconf",
90         .type           = CONNMAN_ELEMENT_TYPE_RESOLVER,
91         .priority       = CONNMAN_DRIVER_PRIORITY_HIGH,
92         .probe          = resolvconf_probe,
93         .remove         = resolvconf_remove,
94 };
95
96 static int resolvconf_init(void)
97 {
98         return connman_driver_register(&resolvconf_driver);
99 }
100
101 static void resolvconf_exit(void)
102 {
103         connman_driver_unregister(&resolvconf_driver);
104 }
105
106 CONNMAN_PLUGIN_DEFINE("resolvconf", "Name resolver plugin", VERSION,
107                                         resolvconf_init, resolvconf_exit)