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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
From 283dba290ad1dd98526080c2d1ce565c2ff64c41 Mon Sep 17 00:00:00 2001
From: Mu Qiao <qiaomuf@gmail.com>
Date: Wed, 18 Aug 2010 17:38:38 +0800
Subject: [PATCH] reading & writing: reserve functions defined in /etc/conf.d/net
Signed-off-by: Mu Qiao <qiaomuf@gmail.com>
---
system-settings/plugins/ifnet/net_parser.c | 173 +++++++++++++++++++-----
system-settings/plugins/ifnet/tests/net | 80 +++++++++++
system-settings/plugins/ifnet/tests/test_all.c | 1 +
3 files changed, 222 insertions(+), 32 deletions(-)
diff --git a/system-settings/plugins/ifnet/net_parser.c b/system-settings/plugins/ifnet/net_parser.c
index d4bc461..b4a381d 100644
--- a/system-settings/plugins/ifnet/net_parser.c
+++ b/system-settings/plugins/ifnet/net_parser.c
@@ -31,6 +31,9 @@ static GHashTable *conn_table;
/* Save global settings which are used for writing*/
static GHashTable *global_settings_table;
+/* Save functions */
+static GList *functions_list;
+
/* Used to decide whether to write changes to file*/
static gboolean net_parser_data_changed = FALSE;
@@ -213,6 +216,71 @@ ifnet_get_global_setting (gchar * group, gchar * key)
return result;
}
+static void
+strip_function (GIOChannel * channel, gchar * line)
+{
+
+ int counter = 0;
+ gchar *p, *tmp;
+ gboolean begin = FALSE;
+ GString *function_str = g_string_new (line);
+
+ g_string_append (function_str, "\n");
+ while (1) {
+ p = line;
+ while (*p != '\0') {
+ if (*p == '{') {
+ counter++;
+ begin = TRUE;
+ } else if (*p == '}')
+ counter--;
+ p++;
+ }
+ if (begin && counter == 0) {
+ g_free (line);
+ goto done;
+ }
+ while (1) {
+ g_free (line);
+ if (g_io_channel_read_line
+ (channel, &line, NULL, NULL,
+ NULL) == G_IO_STATUS_EOF)
+ goto done;
+ g_string_append (function_str, line);
+ tmp = g_strdup (line);
+ g_strstrip (tmp);
+ if (tmp[0] != '#' && tmp[0] != '\0') {
+ g_free (tmp);
+ break;
+ } else
+ g_free (tmp);
+ }
+ }
+ done:
+ functions_list =
+ g_list_append (functions_list, g_strdup (function_str->str));
+ g_string_free (function_str, TRUE);
+}
+
+static gboolean
+is_function (gchar * line)
+{
+ static gchar *func_names[] =
+ { "preup", "predown", "postup", "postdown", "failup", "faildown",
+ NULL,
+ };
+ int i;
+
+ for (i = 0; func_names[i]; i++) {
+ if (g_str_has_prefix (line, func_names[i])) {
+ PLUGIN_PRINT (IFNET_PLUGIN_NAME,
+ "Ignoring function: %s", func_names[i]);
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
gboolean
ifnet_init (gchar * config_file)
{
@@ -229,12 +297,13 @@ ifnet_init (gchar * config_file)
conn_table = g_hash_table_new (g_str_hash, g_str_equal);
global_settings_table = g_hash_table_new (g_str_hash, g_str_equal);
+ functions_list = NULL;
if (g_file_test (config_file, G_FILE_TEST_IS_REGULAR))
channel = g_io_channel_new_file (config_file, "r", NULL);
if (channel == NULL) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME, "Error: Can't open %s\n",
- config_file);
+ PLUGIN_WARN (IFNET_PLUGIN_NAME,
+ "Error: Can't open %s\n", config_file);
return FALSE;
}
@@ -244,6 +313,10 @@ ifnet_init (gchar * config_file)
g_strstrip (line);
/* convert multiple lines to a complete line and
* pass it to init_block_by_line() */
+ if (is_function (line)) {
+ strip_function (channel, line);
+ continue;
+ }
if (line[0] != '#' && line[0] != '\0') {
gchar *pos = NULL;
@@ -255,15 +328,16 @@ ifnet_init (gchar * config_file)
*pos = '\0';
g_strstrip (line);
if (line[0] != '\0') {
- g_string_append_printf (buf, " %s",
- line);
+ g_string_append_printf (buf,
+ " %s", line);
}
g_free (line);
if (!complete)
continue;
} else {
- complete = (g_strrstr (line, "(") != NULL
- && g_strrstr (line, ")") != NULL)
+ complete =
+ (g_strrstr (line, "(") != NULL
+ && g_strrstr (line, ")") != NULL)
|| g_strrstr (line, "(") == NULL;
if ((pos = strchr (line, '#')) != NULL)
*pos = '\0';
@@ -305,8 +379,8 @@ ifnet_set_data (gchar * conn_name, gchar * key, gchar * value)
GHashTable *conn = g_hash_table_lookup (conn_table, conn_name);
if (!conn) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME, "%s does not exsit!",
- conn_name);
+ PLUGIN_WARN (IFNET_PLUGIN_NAME,
+ "%s does not exsit!", conn_name);
return;
}
/* Remove existing key value pair */
@@ -371,8 +445,8 @@ format_ips (gchar * value, gchar ** out_line, gchar * key, gchar * name)
// Multiple lines
g_string_append_printf (formated_string, "%s_%s=(\n", key, name);
for (i = 0; i < length; i++)
- g_string_append_printf (formated_string, "\t\"%s\"\n",
- ipset[i]);
+ g_string_append_printf (formated_string,
+ "\t\"%s\"\n", ipset[i]);
g_string_append (formated_string, ")\n");
*out_line = g_strdup (formated_string->str);
done:
@@ -387,6 +461,7 @@ ifnet_flush_to_file (gchar * config_file)
GError **error = NULL;
gpointer key, value, name, network;
GHashTableIter iter, iter_network;
+ GList *list_iter;
gchar *out_line;
gsize bytes_written;
gboolean result = FALSE;
@@ -412,22 +487,22 @@ ifnet_flush_to_file (gchar * config_file)
while (g_hash_table_iter_next (&iter, &key, &value)) {
out_line =
g_strdup_printf ("%s=%s\n", (gchar *) key, (gchar *) value);
- g_io_channel_write_chars (channel, out_line, -1, &bytes_written,
- error);
+ g_io_channel_write_chars (channel, out_line, -1,
+ &bytes_written, error);
if (bytes_written == 0 || (error && *error))
break;
g_free (out_line);
}
if (error && *error) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME, "Found error: %s",
- (*error)->message);
+ PLUGIN_WARN (IFNET_PLUGIN_NAME,
+ "Found error: %s", (*error)->message);
goto done;
}
+
+ /* Writing connection data */
g_io_channel_write_chars (channel,
"\n###### Connection Configuration ######\n",
-1, &bytes_written, error);
-
- /* Writing connection data */
g_hash_table_iter_init (&iter, conn_table);
while (g_hash_table_iter_next (&iter, &name, &network)) {
g_hash_table_iter_init (&iter_network, (GHashTable *) network);
@@ -439,22 +514,26 @@ ifnet_flush_to_file (gchar * config_file)
if (!g_str_has_prefix ((gchar *) key, "name")
&& !g_str_has_prefix ((gchar *) key, "type")) {
/* These keys contain brackets */
- if (strcmp ((gchar *) key, "config") == 0
- || strcmp ((gchar *) key, "routes") == 0
- || strcmp ((gchar *) key, "pppd") == 0
+ if (strcmp
+ ((gchar *) key,
+ "config") == 0
+ || strcmp ((gchar *) key,
+ "routes") == 0
+ || strcmp ((gchar *) key,
+ "pppd") == 0
|| strcmp ((gchar *) key, "chat") == 0)
- format_ips (value, &out_line,
- (gchar *) key,
- (gchar *) name);
+ format_ips (value, &out_line, (gchar *)
+ key, (gchar *)
+ name);
else
out_line =
g_strdup_printf
("%s_%s=\"%s\"\n",
- (gchar *) key, (gchar *) name,
- (gchar *) value);
- g_io_channel_write_chars (channel, out_line, -1,
- &bytes_written,
- error);
+ (gchar *) key,
+ (gchar *) name, (gchar *) value);
+ g_io_channel_write_chars
+ (channel, out_line, -1,
+ &bytes_written, error);
if (bytes_written == 0 || (error && *error))
break;
g_free (out_line);
@@ -462,14 +541,38 @@ ifnet_flush_to_file (gchar * config_file)
}
}
if (error && *error) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME, "Found error: %s",
- (*error)->message);
+ PLUGIN_WARN (IFNET_PLUGIN_NAME,
+ "Found error: %s", (*error)->message);
goto done;
}
+
+ /* Writing reserved functions */
+ if (functions_list) {
+ g_io_channel_write_chars (channel,
+ "\n###### Reserved Functions ######\n",
+ -1, &bytes_written, error);
+ /* Writing functions */
+ for (list_iter = functions_list; list_iter;
+ list_iter = g_list_next (list_iter)) {
+ out_line =
+ g_strdup_printf ("%s\n", (gchar *) list_iter->data);
+ g_io_channel_write_chars (channel, out_line, -1,
+ &bytes_written, error);
+ if (bytes_written == 0 || (error && *error))
+ break;
+ g_free (out_line);
+ }
+ if (error && *error) {
+ PLUGIN_WARN (IFNET_PLUGIN_NAME,
+ "Found error: %s", (*error)->message);
+ goto done;
+ }
+ }
+
g_io_channel_flush (channel, error);
if (error && *error) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME, "Found error: %s",
- (*error)->message);
+ PLUGIN_WARN (IFNET_PLUGIN_NAME,
+ "Found error: %s", (*error)->message);
goto done;
}
result = TRUE;
@@ -502,12 +605,14 @@ ifnet_destroy (void)
GHashTableIter iter;
gpointer key;
gpointer value;
+ GList *list_iter;
/* Destroy connection setting */
if (conn_table) {
g_hash_table_iter_init (&iter, conn_table);
while (g_hash_table_iter_next (&iter, &key, &value)) {
- destroy_connection_config ((GHashTable *) value);
+ destroy_connection_config ((GHashTable *)
+ value);
}
g_hash_table_destroy (conn_table);
conn_table = NULL;
@@ -523,4 +628,8 @@ ifnet_destroy (void)
g_hash_table_destroy (global_settings_table);
global_settings_table = NULL;
}
+ for (list_iter = functions_list; list_iter;
+ list_iter = g_list_next (list_iter))
+ g_free (list_iter->data);
+ g_list_free (functions_list);
}
diff --git a/system-settings/plugins/ifnet/tests/net b/system-settings/plugins/ifnet/tests/net
index 1d8042c..e755000 100644
--- a/system-settings/plugins/ifnet/tests/net
+++ b/system-settings/plugins/ifnet/tests/net
@@ -65,3 +65,83 @@ bridge_br0="eth0 kvm0 kvm1"
config_br0=( "192.168.1.10/24" )
brctl_br0=( "setfd 0")
dhcp_eth1="nosendhost nontp -I"
+
+predown() {
+ # The default in the script is to test for NFS root and disallow
+ # downing interfaces in that case. Note that if you specify a
+ # predown() function you will override that logic. Here it is, in
+ # case you still want it...
+ if is_net_fs /; then
+ eerror "root filesystem is network mounted -- can't stop ${IFACE}"
+ return 1
+ fi
+
+ # Remember to return 0 on success
+ return 0
+}
+
+postup() {
+ # This function could be used, for example, to register with a
+ # dynamic DNS service. Another possibility would be to
+ # send/receive mail once the interface is brought up.
+
+ # Here is an example that allows the use of iproute rules
+ # which have been configured using the rules_eth0 variable.
+ #rules_eth0=" \
+ # 'from 24.80.102.112/32 to 192.168.1.0/24 table localnet priority 100' \
+ # 'from 216.113.223.51/32 to 192.168.1.0/24 table localnet priority 100' \
+ #"
+ eval set -- \$rules_${IFVAR}
+ if [ $# != 0 ]; then
+ einfo "Adding IP policy routing rules"
+ eindent
+ # Ensure that the kernel supports policy routing
+ if ! ip rule list | grep -q "^"; then
+ eerror "You need to enable IP Policy Routing (CONFIG_IP_MULTIPLE_TABLES)"
+ eerror "in your kernel to use ip rules"
+ else
+ for x; do
+ ebegin "${x}"
+ ip rule add ${x}
+ eend $?
+ done
+ fi
+ eoutdent
+ # Flush the cache
+ ip route flush cache dev "${IFACE}"
+ fi
+
+}
+
+postdown() {
+ # Enable Wake-On-LAN for every interface except for lo
+ # Probably a good idea to set ifdown="no" in /etc/conf.d/net
+ # as well ;)
+ [ "${IFACE}" != "lo" ] && ethtool -s "${IFACE}" wol g
+
+ Automatically erase any ip rules created in the example postup above
+ if interface_exists "${IFACE}"; then
+ # Remove any rules for this interface
+ local rule
+ ip rule list | grep " iif ${IFACE}[ ]*" | {
+ while read rule; do
+ rule="${rule#*:}"
+ ip rule del ${rule}
+ done
+ }
+ # Flush the route cache
+ ip route flush cache dev "${IFACE}"
+ fi
+
+ # Return 0 always
+ return 0
+}
+
+failup() {
+ # This function is mostly here for completeness... I haven't
+ # thought of anything nifty to do with it yet ;-)
+}
+
+faildown()
+{}
+
diff --git a/system-settings/plugins/ifnet/tests/test_all.c b/system-settings/plugins/ifnet/tests/test_all.c
index a0218bd..ba98397 100644
--- a/system-settings/plugins/ifnet/tests/test_all.c
+++ b/system-settings/plugins/ifnet/tests/test_all.c
@@ -369,6 +369,7 @@ main (void)
wpa_parser_destroy ();
ifnet_init ("net");
wpa_parser_init ("wpa_supplicant.conf");
+ printf("Initialization complete\n");
run_all (TRUE);
--
1.6.1
|