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>
43 static GIOChannel *inotify_channel = NULL;
45 static int hostname_descriptor = -1;
47 static gboolean inotify_event(GIOChannel *channel,
48 GIOCondition condition, gpointer data)
50 unsigned char buf[129], *ptr = buf;
54 if (condition & (G_IO_HUP | G_IO_ERR))
57 memset(buf, 0, sizeof(buf));
59 err = g_io_channel_read(channel, (gchar *) buf, sizeof(buf) - 1, &len);
60 if (err != G_IO_ERROR_NONE) {
61 if (err == G_IO_ERROR_AGAIN)
63 connman_error("Reading from inotify channel failed");
67 while (len >= sizeof(struct inotify_event)) {
68 struct inotify_event *evt = (struct inotify_event *) ptr;
70 if (evt->wd == hostname_descriptor) {
71 if (evt->mask & (IN_CREATE | IN_MOVED_TO))
72 connman_info("create hostname file");
74 if (evt->mask & (IN_DELETE | IN_MOVED_FROM))
75 connman_info("delete hostname file");
77 if (evt->mask & (IN_MODIFY | IN_MOVE_SELF))
78 connman_info("modify hostname file");
81 len -= sizeof(struct inotify_event) + evt->len;
82 ptr += sizeof(struct inotify_event) + evt->len;
88 static int create_watch(void)
94 connman_error("Creation of inotify context failed");
98 inotify_channel = g_io_channel_unix_new(fd);
99 if (inotify_channel == NULL) {
100 connman_error("Creation of inotify channel failed");
105 hostname_descriptor = inotify_add_watch(fd, "/etc/hostname",
106 IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF);
107 if (hostname_descriptor < 0) {
108 connman_error("Creation of hostname watch failed");
109 g_io_channel_unref(inotify_channel);
110 inotify_channel = NULL;
115 g_io_add_watch(inotify_channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
116 inotify_event, NULL);
121 static void remove_watch(void)
125 if (inotify_channel == NULL)
128 fd = g_io_channel_unix_get_fd(inotify_channel);
130 if (hostname_descriptor >= 0)
131 inotify_rm_watch(fd, hostname_descriptor);
133 g_io_channel_unref(inotify_channel);
139 static void create_hostname(void)
141 const char *name = "localhost";
143 if (sethostname(name, strlen(name)) < 0)
144 connman_error("Failed to set hostname to %s", name);
147 static int setup_hostname(void)
149 char name[HOST_NAME_MAX + 1];
151 memset(name, 0, sizeof(name));
153 if (gethostname(name, HOST_NAME_MAX) < 0) {
154 connman_error("Failed to get current hostname");
158 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
159 connman_info("System hostname is %s", name);
163 memset(name, 0, sizeof(name));
165 if (getdomainname(name, HOST_NAME_MAX) < 0) {
166 connman_error("Failed to get current domainname");
170 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
171 connman_info("System domainname is %s", name);
176 static int setup_loopback(void)
179 struct sockaddr_in *addr;
182 sk = socket(PF_INET, SOCK_DGRAM, 0);
186 memset(&ifr, 0, sizeof(ifr));
187 strcpy(ifr.ifr_name, "lo");
189 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
194 if (ifr.ifr_flags & IFF_UP) {
196 connman_info("The loopback interface is already up");
200 addr = (struct sockaddr_in *) &ifr.ifr_addr;
201 addr->sin_family = AF_INET;
202 addr->sin_addr.s_addr = inet_addr("127.0.0.1");
204 err = ioctl(sk, SIOCSIFADDR, &ifr);
207 connman_error("Setting address failed (%s)", strerror(-err));
211 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
212 addr->sin_family = AF_INET;
213 addr->sin_addr.s_addr = inet_addr("255.0.0.0");
215 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
218 connman_error("Setting netmask failed (%s)", strerror(-err));
222 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
227 ifr.ifr_flags |= IFF_UP;
229 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
231 connman_error("Activating loopback interface failed (%s)",
242 static int loopback_init(void)
253 static void loopback_exit(void)
258 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
259 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)