Add some ipconfig helpers
[framework/connectivity/connman.git] / plugins / resolvconf.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 #define CONNMAN_API_SUBJECT_TO_CHANGE
31 #include <connman/plugin.h>
32 #include <connman/resolver.h>
33 #include <connman/log.h>
34
35 #include <glib.h>
36
37 static int resolvconf_append(const char *interface, const char *domain,
38                                                         const char *server)
39 {
40         char *cmd;
41         int err;
42
43         DBG("interface %s server %s", interface, server);
44
45         if (access(RESOLVCONF, X_OK) < 0)
46                 return -errno;
47
48         if (interface == NULL)
49                 return 0;
50
51         cmd = g_strdup_printf("echo \"nameserver %s\" | %s -a %s",
52                                                 server, RESOLVCONF, interface);
53
54         DBG("%s", cmd);
55
56         err = system(cmd);
57
58         g_free(cmd);
59
60         return err;
61 }
62
63 static int resolvconf_remove(const char *interface, const char *domain,
64                                                         const char *server)
65 {
66         char *cmd;
67         int err;
68
69         DBG("interface %s server %s", interface, server);
70
71         if (interface == NULL)
72                 return 0;
73
74         cmd = g_strdup_printf("%s -d %s", RESOLVCONF, interface);
75
76         DBG("%s", cmd);
77
78         err = system(cmd);
79
80         g_free(cmd);
81
82         return err;
83 }
84
85 static struct connman_resolver resolvconf_resolver = {
86         .name           = "resolvconf",
87         .priority       = CONNMAN_RESOLVER_PRIORITY_DEFAULT,
88         .append         = resolvconf_append,
89         .remove         = resolvconf_remove,
90 };
91
92 static int resolvconf_init(void)
93 {
94         return connman_resolver_register(&resolvconf_resolver);
95 }
96
97 static void resolvconf_exit(void)
98 {
99         connman_resolver_unregister(&resolvconf_resolver);
100 }
101
102 CONNMAN_PLUGIN_DEFINE(resolvconf, "Name resolver plugin", VERSION,
103                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, resolvconf_init, resolvconf_exit)