5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <sys/ioctl.h>
31 #include <sys/inotify.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
38 #define CONNMAN_API_SUBJECT_TO_CHANGE
39 #include <connman/plugin.h>
40 #include <connman/log.h>
42 static in_addr_t loopback_address;
43 static in_addr_t loopback_netmask;
46 static GIOChannel *inotify_channel = NULL;
48 static int hostname_descriptor = -1;
50 static gboolean inotify_event(GIOChannel *channel,
51 GIOCondition condition, gpointer data)
53 unsigned char buf[129], *ptr = buf;
57 if (condition & (G_IO_HUP | G_IO_ERR))
60 memset(buf, 0, sizeof(buf));
62 err = g_io_channel_read(channel, (gchar *) buf, sizeof(buf) - 1, &len);
63 if (err != G_IO_ERROR_NONE) {
64 if (err == G_IO_ERROR_AGAIN)
66 connman_error("Reading from inotify channel failed");
70 while (len >= sizeof(struct inotify_event)) {
71 struct inotify_event *evt = (struct inotify_event *) ptr;
73 if (evt->wd == hostname_descriptor) {
74 if (evt->mask & (IN_CREATE | IN_MOVED_TO))
75 connman_info("create hostname file");
77 if (evt->mask & (IN_DELETE | IN_MOVED_FROM))
78 connman_info("delete hostname file");
80 if (evt->mask & (IN_MODIFY | IN_MOVE_SELF))
81 connman_info("modify hostname file");
84 len -= sizeof(struct inotify_event) + evt->len;
85 ptr += sizeof(struct inotify_event) + evt->len;
91 static int create_watch(void)
97 connman_error("Creation of inotify context failed");
101 inotify_channel = g_io_channel_unix_new(fd);
102 if (inotify_channel == NULL) {
103 connman_error("Creation of inotify channel failed");
108 hostname_descriptor = inotify_add_watch(fd, "/etc/hostname",
109 IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF);
110 if (hostname_descriptor < 0) {
111 connman_error("Creation of hostname watch failed");
112 g_io_channel_unref(inotify_channel);
113 inotify_channel = NULL;
118 g_io_add_watch(inotify_channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
119 inotify_event, NULL);
124 static void remove_watch(void)
128 if (inotify_channel == NULL)
131 fd = g_io_channel_unix_get_fd(inotify_channel);
133 if (hostname_descriptor >= 0)
134 inotify_rm_watch(fd, hostname_descriptor);
136 g_io_channel_unref(inotify_channel);
142 static void create_hostname(void)
144 const char *name = "localhost";
146 if (sethostname(name, strlen(name)) < 0)
147 connman_error("Failed to set hostname to %s", name);
150 static int setup_hostname(void)
152 char name[HOST_NAME_MAX + 1];
154 memset(name, 0, sizeof(name));
156 if (gethostname(name, HOST_NAME_MAX) < 0) {
157 connman_error("Failed to get current hostname");
161 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
162 connman_info("System hostname is %s", name);
166 memset(name, 0, sizeof(name));
168 if (getdomainname(name, HOST_NAME_MAX) < 0) {
169 connman_error("Failed to get current domainname");
173 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
174 connman_info("System domainname is %s", name);
179 static gboolean valid_loopback(int sk, struct ifreq *ifr)
181 struct sockaddr_in *addr;
184 err = ioctl(sk, SIOCGIFADDR, ifr);
187 connman_error("Getting address failed (%s)", strerror(-err));
191 addr = (struct sockaddr_in *) &ifr->ifr_addr;
192 if (addr->sin_addr.s_addr != loopback_address)
195 err = ioctl(sk, SIOCGIFNETMASK, ifr);
198 connman_error("Getting netmask failed (%s)", strerror(-err));
202 addr = (struct sockaddr_in *) &ifr->ifr_netmask;
203 if (addr->sin_addr.s_addr != loopback_netmask)
209 static int setup_loopback(void)
212 struct sockaddr_in addr;
215 sk = socket(PF_INET, SOCK_DGRAM, 0);
219 memset(&ifr, 0, sizeof(ifr));
220 strcpy(ifr.ifr_name, "lo");
222 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
227 if (ifr.ifr_flags & IFF_UP) {
228 connman_info("Checking loopback interface settings");
229 if (valid_loopback(sk, &ifr) == TRUE) {
234 connman_warn("Correcting wrong lookback settings");
237 memset(&addr, 0, sizeof(addr));
238 addr.sin_family = AF_INET;
239 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
240 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
242 err = ioctl(sk, SIOCSIFADDR, &ifr);
245 connman_error("Setting address failed (%s)", strerror(-err));
249 memset(&addr, 0, sizeof(addr));
250 addr.sin_family = AF_INET;
251 addr.sin_addr.s_addr = inet_addr("255.0.0.0");
252 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
254 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
257 connman_error("Setting netmask failed (%s)", strerror(-err));
261 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
266 ifr.ifr_flags |= IFF_UP;
268 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
270 connman_error("Activating loopback interface failed (%s)",
281 static int loopback_init(void)
283 loopback_address = inet_addr("127.0.0.1");
284 loopback_netmask = inet_addr("255.0.0.0");
295 static void loopback_exit(void)
300 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
301 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)