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 GIOChannel *inotify_channel = NULL;
44 static int hostname_descriptor = -1;
46 static gboolean inotify_event(GIOChannel *channel,
47 GIOCondition condition, gpointer data)
49 unsigned char buf[129], *ptr = buf;
53 if (condition & (G_IO_HUP | G_IO_ERR))
56 memset(buf, 0, sizeof(buf));
58 err = g_io_channel_read(channel, (gchar *) buf, sizeof(buf) - 1, &len);
59 if (err != G_IO_ERROR_NONE) {
60 if (err == G_IO_ERROR_AGAIN)
62 connman_error("Reading from inotify channel failed");
66 while (len >= sizeof(struct inotify_event)) {
67 struct inotify_event *evt = (struct inotify_event *) ptr;
69 if (evt->wd == hostname_descriptor) {
70 if (evt->mask & (IN_CREATE | IN_MOVED_TO))
71 connman_info("create hostname file");
73 if (evt->mask & (IN_DELETE | IN_MOVED_FROM))
74 connman_info("delete hostname file");
76 if (evt->mask & (IN_MODIFY | IN_MOVE_SELF))
77 connman_info("modify hostname file");
80 len -= sizeof(struct inotify_event) + evt->len;
81 ptr += sizeof(struct inotify_event) + evt->len;
87 static int create_watch(void)
93 connman_error("Creation of inotify context failed");
97 inotify_channel = g_io_channel_unix_new(fd);
98 if (inotify_channel == NULL) {
99 connman_error("Creation of inotify channel failed");
104 hostname_descriptor = inotify_add_watch(fd, "/etc/hostname",
105 IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF);
106 if (hostname_descriptor < 0) {
107 connman_error("Creation of hostname watch failed");
108 g_io_channel_unref(inotify_channel);
109 inotify_channel = NULL;
114 g_io_add_watch(inotify_channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
115 inotify_event, NULL);
120 static void remove_watch(void)
124 if (inotify_channel == NULL)
127 fd = g_io_channel_unix_get_fd(inotify_channel);
129 if (hostname_descriptor >= 0)
130 inotify_rm_watch(fd, hostname_descriptor);
132 g_io_channel_unref(inotify_channel);
137 static int setup_hostname(void)
139 char name[HOST_NAME_MAX + 1];
141 memset(name, 0, sizeof(name));
143 if (gethostname(name, HOST_NAME_MAX) < 0) {
144 connman_error("Failed to get current hostname");
148 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
149 connman_info("System hostname is %s", name);
151 memset(name, 0, sizeof(name));
153 if (getdomainname(name, HOST_NAME_MAX) < 0) {
154 connman_error("Failed to get current domainname");
158 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
159 connman_info("System domainname is %s", name);
164 static int setup_loopback(void)
167 struct sockaddr_in *addr;
170 sk = socket(PF_INET, SOCK_DGRAM, 0);
174 memset(&ifr, 0, sizeof(ifr));
175 strcpy(ifr.ifr_name, "lo");
177 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
182 if (ifr.ifr_flags & IFF_UP) {
184 connman_info("The loopback interface is already up");
188 addr = (struct sockaddr_in *) &ifr.ifr_addr;
189 addr->sin_family = AF_INET;
190 addr->sin_addr.s_addr = inet_addr("127.0.0.1");
192 err = ioctl(sk, SIOCSIFADDR, &ifr);
195 connman_error("Setting address failed (%s)", strerror(-err));
199 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
200 addr->sin_family = AF_INET;
201 addr->sin_addr.s_addr = inet_addr("255.0.0.0");
203 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
206 connman_error("Setting netmask failed (%s)", strerror(-err));
210 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
215 ifr.ifr_flags |= IFF_UP;
217 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
219 connman_error("Activating loopback interface failed (%s)",
230 static int loopback_init(void)
241 static void loopback_exit(void)
246 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
247 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)