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