Add skeleton for RTNL link detection plugin
[connman] / plugins / resolvfile.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 <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/stat.h>
31
32 #include <connman/plugin.h>
33 #include <connman/driver.h>
34 #include <connman/log.h>
35
36 static int resolvfile_probe(struct connman_element *element)
37 {
38         const char *nameserver = NULL;
39         struct connman_element *internet;
40         gchar *cmd;
41         int fd, len, err;
42
43         DBG("element %p name %s", element, element->name);
44
45         connman_element_get_value(element,
46                         CONNMAN_PROPERTY_TYPE_IPV4_NAMESERVER, &nameserver);
47
48         if (nameserver == NULL)
49                 return -EINVAL;
50
51         fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
52                                         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
53         if (fd < 0)
54                 return errno;
55
56         err = ftruncate(fd, 0);
57
58         cmd = g_strdup_printf("nameserver %s\n", nameserver);
59
60         len = write(fd, cmd, strlen(cmd));
61
62         g_free(cmd);
63
64         close(fd);
65
66         internet = connman_element_create();
67
68         internet->type = CONNMAN_ELEMENT_TYPE_INTERNET;
69
70         connman_element_set_data(element, internet);
71
72         connman_element_register(internet, element);
73
74         return 0;
75 }
76
77 static void resolvfile_remove(struct connman_element *element)
78 {
79         struct connman_element *internet = connman_element_get_data(element);
80
81         DBG("element %p name %s", element, element->name);
82
83         connman_element_set_data(element, NULL);
84
85         connman_element_unregister(internet);
86
87         connman_element_unref(internet);
88 }
89
90 static struct connman_driver resolvfile_driver = {
91         .name           = "resolvconf",
92         .type           = CONNMAN_ELEMENT_TYPE_RESOLVER,
93         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
94         .probe          = resolvfile_probe,
95         .remove         = resolvfile_remove,
96 };
97
98 static int resolvfile_init(void)
99 {
100         return connman_driver_register(&resolvfile_driver);
101 }
102
103 static void resolvfile_exit(void)
104 {
105         connman_driver_unregister(&resolvfile_driver);
106 }
107
108 CONNMAN_PLUGIN_DEFINE("resolvfile", "Name resolver plugin", VERSION,
109                                         resolvfile_init, resolvfile_exit)