Execute resolvconf commands
[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 <errno.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29
30 #include <connman/plugin.h>
31 #include <connman/resolver.h>
32 #include <connman/log.h>
33
34 #include <glib.h>
35
36 #define RESOLVCONF "/sbin/resolvconf"
37
38 static int resolvconf_append(const char *interface, const char *domain,
39                                                         const char *server)
40 {
41         char *cmd;
42         int err;
43
44         DBG("interface %s server %s", interface, server);
45
46         if (access(RESOLVCONF, X_OK) < 0)
47                 return -errno;
48
49         cmd = g_strdup_printf("echo \"nameserver %s\" | %s -a %s",
50                                                 server, RESOLVCONF, interface);
51
52         DBG("%s", cmd);
53
54         err = system(cmd);
55
56         g_free(cmd);
57
58         return err;
59 }
60
61 static int resolvconf_remove(const char *interface, const char *domain,
62                                                         const char *server)
63 {
64         char *cmd;
65         int err;
66
67         DBG("interface %s server %s", interface, server);
68
69         cmd = g_strdup_printf("%s -d %s", RESOLVCONF, interface);
70
71         DBG("%s", cmd);
72
73         err = system(cmd);
74
75         g_free(cmd);
76
77         return err;
78 }
79
80 static struct connman_resolver resolvconf_resolver = {
81         .name           = "resolvconf",
82         .priority       = CONNMAN_RESOLVER_PRIORITY_DEFAULT,
83         .append         = resolvconf_append,
84         .remove         = resolvconf_remove,
85 };
86
87 static int resolvconf_init(void)
88 {
89         return connman_resolver_register(&resolvconf_resolver);
90 }
91
92 static void resolvconf_exit(void)
93 {
94         connman_resolver_unregister(&resolvconf_resolver);
95 }
96
97 CONNMAN_PLUGIN_DEFINE(resolvconf, "Name resolver plugin", VERSION,
98                                         resolvconf_init, resolvconf_exit)