summaryrefslogtreecommitdiff
path: root/debian/patches/Force-online-state-with-unmanaged-devices.patch
blob: 27f668b129d20c9811521a71f4340ef7f7987a68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
From: Michael Biebl <biebl@debian.org>
Date: Wed, 2 Apr 2014 03:15:53 +0200
Subject: Force online state with unmanaged devices

If we have unmanaged devices in /e/n/i, monitor the ifupdown state file
and in case we find active interfaces besides lo, forcefully set the
online state to CONNECTED.

Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=512286
---
 src/nm-manager.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 122 insertions(+)

diff --git a/src/nm-manager.c b/src/nm-manager.c
index 0fa88d1..0a39dfb 100644
--- a/src/nm-manager.c
+++ b/src/nm-manager.c
@@ -64,6 +64,8 @@
 #define NM_AUTOIP_DBUS_SERVICE "org.freedesktop.nm_avahi_autoipd"
 #define NM_AUTOIP_DBUS_IFACE   "org.freedesktop.nm_avahi_autoipd"
 
+#define IFUPDOWN_STATE_FILE "/run/network/ifstate"
+
 static gboolean impl_manager_get_devices (NMManager *manager,
                                           GPtrArray **devices,
                                           GError **err);
@@ -198,6 +200,11 @@ typedef struct {
 	GFileMonitor *fw_monitor;
 	guint fw_changed_id;
 
+	/* ifupdown state file monitor */
+	GFileMonitor *ifstate_monitor;
+	guint ifstate_monitor_id;
+	gboolean ifstate_force_online;
+
 	guint timestamp_update_id;
 
 	gboolean startup;
@@ -621,6 +628,29 @@ find_best_device_state (NMManager *manager)
 	return best_state;
 }
 
+static NMState
+find_unmanaged_state (NMManager *manager, NMState current_state)
+{
+	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
+	NMState new_state = current_state;
+	GSList *iter;
+
+	for (iter = priv->devices; iter; iter = iter->next) {
+		NMDevice *dev = NM_DEVICE (iter->data);
+		NMDeviceState state = nm_device_get_state (dev);
+
+
+		if (state == NM_DEVICE_STATE_UNMANAGED) {
+			const char *iface = nm_device_get_ip_iface (dev);
+			if (priv->ifstate_force_online) {
+				new_state = NM_STATE_CONNECTED_GLOBAL;
+				nm_log_dbg (LOGD_CORE, "Unmanaged device found: %s; state CONNECTED forced.", iface);
+			}
+		}
+	}
+	return new_state;
+}
+
 static void
 nm_manager_update_state (NMManager *manager)
 {
@@ -638,6 +668,9 @@ nm_manager_update_state (NMManager *manager)
 
 	nm_connectivity_set_online (priv->connectivity, new_state >= NM_STATE_CONNECTED_LOCAL);
 
+	if (new_state != NM_STATE_CONNECTED_GLOBAL)
+		new_state = find_unmanaged_state (manager, new_state);
+
 	if (new_state == NM_STATE_CONNECTED_SITE) {
 		nm_connectivity_check_async (priv->connectivity,
 		                             checked_connectivity,
@@ -4172,6 +4205,65 @@ impl_manager_check_connectivity (NMManager *manager,
 	nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL, TRUE);
 }
 
+static void
+check_ifstate_file (gpointer user_data)
+{
+	NMManager *self = NM_MANAGER (user_data);
+	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
+	GIOChannel *channel;
+	gchar *line;
+	gboolean online = FALSE;
+
+	channel = g_io_channel_new_file (IFUPDOWN_STATE_FILE, "r", NULL);
+	if (!channel) {
+		nm_log_warn (LOGD_CORE, "Error: failed to open %s", IFUPDOWN_STATE_FILE);
+		return;
+	}
+	
+	while (g_io_channel_read_line (channel, &line, NULL, NULL, NULL)
+	       != G_IO_STATUS_EOF && !online) {
+		g_strstrip (line);
+		if (strlen (line) > 0 && g_strcmp0 (line, "lo=lo") != 0) {
+			online = TRUE;
+		}
+		g_free (line);
+	}
+	
+	g_io_channel_shutdown (channel, FALSE, NULL);
+	g_io_channel_unref (channel);
+
+	if (priv->ifstate_force_online != online) {
+		priv->ifstate_force_online = online;
+		nm_manager_update_state (self);
+	}
+}
+
+static void
+ifstate_file_changed (GFileMonitor *monitor,
+                      GFile *file,
+                      GFile *other_file,
+                      GFileMonitorEvent event_type,
+                      gpointer user_data)
+{
+	NMManager *self = NM_MANAGER (user_data);
+	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
+
+	switch (event_type) {
+//	case G_FILE_MONITOR_EVENT_CREATED:
+//#if GLIB_CHECK_VERSION(2,23,4)
+//	case G_FILE_MONITOR_EVENT_MOVED:
+//#endif
+//	case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
+	case G_FILE_MONITOR_EVENT_CHANGED:
+	case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
+		nm_log_dbg (LOGD_CORE, "ifupdown state file %s was changed", IFUPDOWN_STATE_FILE);
+		check_ifstate_file (user_data);
+		break;
+	default:
+		break;
+	}
+}
+
 void
 nm_manager_start (NMManager *self)
 {
@@ -4220,6 +4312,9 @@ nm_manager_start (NMManager *self)
 	system_create_virtual_devices (self);
 
 	check_if_startup_complete (self);
+
+	/* Trigger ifupdown state file check */
+	check_ifstate_file (self);
 }
 
 void
@@ -4916,6 +5011,22 @@ nm_manager_init (NMManager *manager)
 		             KERNEL_FIRMWARE_DIR);
 	}
 
+	/* Monitor the ifupdown state file */
+	file = g_file_new_for_path (IFUPDOWN_STATE_FILE);
+	priv->ifstate_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
+	g_object_unref (file);
+
+	if (priv->ifstate_monitor) {
+		priv->ifstate_monitor_id = g_signal_connect (priv->ifstate_monitor, "changed",
+		                                             G_CALLBACK (ifstate_file_changed),
+		                                             manager);
+		nm_log_info (LOGD_CORE, "monitoring ifupdown state file '%s'.",
+		             IFUPDOWN_STATE_FILE);
+	} else {
+		nm_log_warn (LOGD_CORE, "failed to monitor ifupdown state file '%s'.",
+		             IFUPDOWN_STATE_FILE);
+	}
+
 	/* Update timestamps in active connections */
 	priv->timestamp_update_id = g_timeout_add_seconds (300, (GSourceFunc) periodic_update_active_connection_timestamps, manager);
 }
@@ -5113,6 +5224,17 @@ dispose (GObject *object)
 		g_clear_object (&priv->fw_monitor);
 	}
 
+	if (priv->ifstate_monitor) {
+		if (priv->ifstate_monitor_id)
+			g_signal_handler_disconnect (priv->ifstate_monitor, priv->ifstate_monitor_id);
+
+		if (priv->ifstate_force_online)
+			g_source_remove (priv->ifstate_force_online);
+
+		g_file_monitor_cancel (priv->ifstate_monitor);
+		g_object_unref (priv->ifstate_monitor);
+	}
+
 	for (iter = priv->factories; iter; iter = iter->next) {
 		NMDeviceFactory *factory = iter->data;