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 g_io_add_watch(inotify_channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
105 inotify_event, NULL);
107 hostname_descriptor = inotify_add_watch(fd, "/etc/hostname",
108 IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF);
109 if (hostname_descriptor < 0) {
110 connman_error("Creation of hostname watch failed");
111 g_io_channel_unref(inotify_channel);
112 inotify_channel = 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 connman_info("System hostname is %s", name);
150 memset(name, 0, sizeof(name));
152 if (getdomainname(name, HOST_NAME_MAX) < 0) {
153 connman_error("Failed to get current domainname");
157 if (strcmp(name, "(none)") == 0)
160 connman_info("System domainname is %s", name);
165 static int setup_loopback(void)
168 struct sockaddr_in *addr;
171 sk = socket(PF_INET, SOCK_DGRAM, 0);
175 memset(&ifr, 0, sizeof(ifr));
176 strcpy(ifr.ifr_name, "lo");
178 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
183 if (ifr.ifr_flags & IFF_UP) {
185 connman_info("The loopback interface is already up");
189 addr = (struct sockaddr_in *) &ifr.ifr_addr;
190 addr->sin_family = AF_INET;
191 addr->sin_addr.s_addr = inet_addr("127.0.0.1");
193 err = ioctl(sk, SIOCSIFADDR, &ifr);
196 connman_error("Setting address failed (%s)", strerror(-err));
200 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
201 addr->sin_family = AF_INET;
202 addr->sin_addr.s_addr = inet_addr("255.0.0.0");
204 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
207 connman_error("Setting netmask failed (%s)", strerror(-err));
211 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
216 ifr.ifr_flags |= IFF_UP;
218 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
220 connman_error("Activating loopback interface failed (%s)",
231 static int loopback_init(void)
242 static void loopback_exit(void)
247 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
248 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)