5 * Copyright (C) 2007-2010 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/utsname.h>
41 #include <connman/log.h>
43 static in_addr_t loopback_address;
44 static in_addr_t loopback_netmask;
46 static char system_hostname[HOST_NAME_MAX + 1];
49 static GIOChannel *inotify_channel = NULL;
51 static int hostname_descriptor = -1;
53 static gboolean inotify_event(GIOChannel *channel,
54 GIOCondition condition, gpointer data)
56 unsigned char buf[129], *ptr = buf;
60 if (condition & (G_IO_HUP | G_IO_ERR))
63 memset(buf, 0, sizeof(buf));
65 err = g_io_channel_read(channel, (gchar *) buf, sizeof(buf) - 1, &len);
66 if (err != G_IO_ERROR_NONE) {
67 if (err == G_IO_ERROR_AGAIN)
69 connman_error("Reading from inotify channel failed");
73 while (len >= sizeof(struct inotify_event)) {
74 struct inotify_event *evt = (struct inotify_event *) ptr;
76 if (evt->wd == hostname_descriptor) {
77 if (evt->mask & (IN_CREATE | IN_MOVED_TO))
78 connman_info("create hostname file");
80 if (evt->mask & (IN_DELETE | IN_MOVED_FROM))
81 connman_info("delete hostname file");
83 if (evt->mask & (IN_MODIFY | IN_MOVE_SELF))
84 connman_info("modify hostname file");
87 len -= sizeof(struct inotify_event) + evt->len;
88 ptr += sizeof(struct inotify_event) + evt->len;
94 static int create_watch(void)
100 connman_error("Creation of inotify context failed");
104 inotify_channel = g_io_channel_unix_new(fd);
105 if (inotify_channel == NULL) {
106 connman_error("Creation of inotify channel failed");
111 hostname_descriptor = inotify_add_watch(fd, "/etc/hostname",
112 IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF);
113 if (hostname_descriptor < 0) {
114 connman_error("Creation of hostname watch failed");
115 g_io_channel_unref(inotify_channel);
116 inotify_channel = NULL;
121 g_io_add_watch(inotify_channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
122 inotify_event, NULL);
127 static void remove_watch(void)
131 if (inotify_channel == NULL)
134 fd = g_io_channel_unix_get_fd(inotify_channel);
136 if (hostname_descriptor >= 0)
137 inotify_rm_watch(fd, hostname_descriptor);
139 g_io_channel_unref(inotify_channel);
145 static void create_hostname(void)
147 const char *name = "localhost";
149 if (sethostname(name, strlen(name)) < 0)
150 connman_error("Failed to set hostname to %s", name);
152 strncpy(system_hostname, name, HOST_NAME_MAX);
155 static int setup_hostname(void)
157 char name[HOST_NAME_MAX + 1];
159 memset(system_hostname, 0, sizeof(system_hostname));
161 if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
162 connman_error("Failed to get current hostname");
166 if (strlen(system_hostname) > 0 &&
167 strcmp(system_hostname, "(none)") != 0)
168 connman_info("System hostname is %s", system_hostname);
172 memset(name, 0, sizeof(name));
174 if (getdomainname(name, HOST_NAME_MAX) < 0) {
175 connman_error("Failed to get current domainname");
179 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
180 connman_info("System domainname is %s", name);
185 static gboolean valid_loopback(int sk, struct ifreq *ifr)
187 struct sockaddr_in *addr;
189 char buf[INET_ADDRSTRLEN];
191 /* It is possible to end up in situations in which the
192 * loopback interface is up but has no valid address. In that
193 * case, we expect EADDRNOTAVAIL and should return FALSE.
196 err = ioctl(sk, SIOCGIFADDR, ifr);
199 connman_error("Getting address failed (%s)", strerror(-err));
200 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
203 addr = (struct sockaddr_in *) &ifr->ifr_addr;
204 if (addr->sin_addr.s_addr != loopback_address) {
205 connman_warn("Invalid loopback address %s",
206 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
210 err = ioctl(sk, SIOCGIFNETMASK, ifr);
213 connman_error("Getting netmask failed (%s)", strerror(-err));
217 addr = (struct sockaddr_in *) &ifr->ifr_netmask;
218 if (addr->sin_addr.s_addr != loopback_netmask) {
219 connman_warn("Invalid loopback netmask %s",
220 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
227 static int setup_loopback(void)
230 struct sockaddr_in addr;
233 sk = socket(PF_INET, SOCK_DGRAM, 0);
237 memset(&ifr, 0, sizeof(ifr));
238 strcpy(ifr.ifr_name, "lo");
240 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
245 if (ifr.ifr_flags & IFF_UP) {
246 connman_info("Checking loopback interface settings");
247 if (valid_loopback(sk, &ifr) == TRUE) {
252 connman_warn("Correcting wrong lookback settings");
255 memset(&addr, 0, sizeof(addr));
256 addr.sin_family = AF_INET;
257 addr.sin_addr.s_addr = loopback_address;
258 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
260 err = ioctl(sk, SIOCSIFADDR, &ifr);
263 connman_error("Setting address failed (%s)", strerror(-err));
267 memset(&addr, 0, sizeof(addr));
268 addr.sin_family = AF_INET;
269 addr.sin_addr.s_addr = loopback_netmask;
270 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
272 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
275 connman_error("Setting netmask failed (%s)", strerror(-err));
279 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
284 ifr.ifr_flags |= IFF_UP;
286 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
288 connman_error("Activating loopback interface failed (%s)",
299 static const char *loopback_get_hostname(void)
301 return system_hostname;
304 static int loopback_set_hostname(const char *hostname)
308 if (g_strcmp0(hostname, "<hostname>") == 0)
311 if (sethostname(hostname, strlen(hostname)) < 0) {
313 connman_error("Failed to set hostname to %s", hostname);
317 connman_info("Setting hostname to %s", hostname);
322 static int loopback_set_domainname(const char *domainname)
326 if (setdomainname(domainname, strlen(domainname)) < 0) {
328 connman_error("Failed to set domainname to %s", domainname);
332 connman_info("Setting domainname to %s", domainname);
337 static struct connman_utsname_driver loopback_driver = {
339 .get_hostname = loopback_get_hostname,
340 .set_hostname = loopback_set_hostname,
341 .set_domainname = loopback_set_domainname,
344 static int loopback_init(void)
346 loopback_address = inet_addr("127.0.0.1");
347 loopback_netmask = inet_addr("255.0.0.0");
355 connman_utsname_driver_register(&loopback_driver);
360 static void loopback_exit(void)
362 connman_utsname_driver_unregister(&loopback_driver);
367 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
368 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)