/
ifup.c
917 lines (764 loc) · 23.9 KB
/
ifup.c
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/*
* wicked client ifup action and utilities
*
* Copyright (C) 2010-2014 SUSE LINUX Products GmbH, Nuernberg, Germany.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/> or write
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Authors:
* Olaf Kirch <okir@suse.de>
* Marius Tomaschewski <mt@suse.de>
* Pawel Wieczorkiewicz <pwieczorkiewicz@suse.de>
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <wicked/netinfo.h>
#include <wicked/logging.h>
#include <wicked/fsm.h>
#include "client/ifconfig.h"
#include "wicked-client.h"
#include "appconfig.h"
#include "ifup.h"
#include "ifstatus.h"
struct ni_nanny_fsm_monitor {
const ni_timer_t * timer;
unsigned long timeout;
ni_ifworker_array_t * marked;
};
static xml_node_t *
__ni_ifup_generate_match_dev(xml_node_t *node, ni_ifworker_t *w)
{
ni_iftype_t iftype;
const char *type;
if (!node || !w || ni_string_empty(w->name))
return NULL;
/* Conditional <link-type> generation */
iftype = ni_ifworker_iftype_from_xml(w->config.node);
type = ni_linktype_type_to_name(iftype);
if (iftype != NI_IFTYPE_UNKNOWN && !ni_string_empty(type)) {
if (!xml_node_new_element(NI_NANNY_IFPOLICY_MATCH_LINK_TYPE, node, type))
return NULL; /* Error */
}
return xml_node_new_element(NI_NANNY_IFPOLICY_MATCH_DEV, node, w->name);
}
static xml_node_t *
__ni_ifup_generate_match(const char *name, ni_ifworker_t *w)
{
xml_node_t *match;
if (!(match = xml_node_new(name, NULL)))
goto error;
if (!__ni_ifup_generate_match_dev(match, w))
goto error;
if (w->children.count) {
xml_node_t *or;
unsigned int i;
if (!(or = xml_node_new(NI_NANNY_IFPOLICY_MATCH_COND_OR, match)))
goto error;
for (i = 0; i < w->children.count; i++) {
ni_ifworker_t *child = w->children.data[i];
xml_node_t *cnode;
cnode = __ni_ifup_generate_match(NI_NANNY_IFPOLICY_MATCH_COND_CHILD, child);
xml_node_add_child(or ,cnode);
}
}
return match;
error:
xml_node_free(match);
return NULL;
}
static ni_bool_t
ni_ifup_start_policy(ni_ifworker_t *w)
{
xml_node_t *ifcfg = NULL, *policy = NULL;
ni_bool_t rv = FALSE;
char *pname;
if (!w || !w->config.node)
return rv;
ni_debug_application("%s: hiring nanny", w->name);
/* Create a config duplicate for a policy */
ifcfg = xml_node_clone(w->config.node, NULL);
if (!ifcfg)
goto error;
pname = ni_ifpolicy_name_from_ifname(w->name);
ni_debug_application("%s: converting config into policy '%s'",
w->name, pname);
policy = ni_convert_cfg_into_policy_node(ifcfg,
__ni_ifup_generate_match(NI_NANNY_IFPOLICY_MATCH, w),
pname, w->config.meta.origin);
ni_string_free(&pname);
if (!policy) {
policy = ifcfg; /* Free cloned config*/
goto error;
}
#if 0
/* Do we need this? */
/* Add link type to match node*/
if (dev) {
ni_debug_application("%s: adding link type (%s) to match",
w->name, ni_linktype_type_to_name(dev->link.type));
ni_ifpolicy_match_add_link_type(policy, dev->link.type);
}
ni_debug_application("%s: adding minimum device state (%s) to match",
w->name, ni_ifworker_state_name(w->fsm.state));
/* Add minimum device state to match node */
if (!ni_ifpolicy_match_add_min_state(policy, w->fsm.state))
goto error;
#endif
ni_debug_application("%s: adding policy %s to nanny", w->name,
xml_node_get_attr(policy, NI_NANNY_IFPOLICY_NAME));
if (ni_nanny_addpolicy_node(policy, w->config.meta.origin) <= 0)
goto error;
ni_debug_application("%s: nanny hired!", w->name);
ni_ifworker_success(w);
rv = TRUE;
error:
if (!rv)
ni_ifworker_fail(w, "%s: unable to apply configuration to nanny", w->name);
xml_node_free(policy);
return rv;
}
ni_bool_t
ni_ifup_hire_nanny(ni_ifworker_array_t *array, ni_bool_t set_persistent)
{
unsigned int i;
ni_bool_t rv = TRUE;
/* Send policies to nanny */
for (i = 0; i < array->count; i++) {
ni_ifworker_t *w = array->data[array->count-1-i];
if (!w || xml_node_is_empty(w->config.node))
continue;
if (set_persistent)
ni_client_state_set_persistent(w->config.node);
if (!ni_ifup_start_policy(w)) {
ni_error("%s: unable to apply configuration to nanny", w->name);
rv = FALSE;
}
else
ni_info("%s: configuration applied to nanny", w->name);
}
/* Enable devices with policies */
for (i = 0; i < array->count; i++) {
ni_ifworker_t *w = array->data[array->count-1-i];
ni_netdev_t *dev = w ? w->device : NULL;
/* Ignore non-existing device */
if (!dev || !ni_netdev_device_is_ready(dev) ||
xml_node_is_empty(w->config.node)) {
continue;
}
if (w->failed) {
ni_debug_application("%s: disabling failed device for nanny", w->name);
ni_nanny_call_device_disable(w->name);
}
else if (w->done) {
ni_debug_application("%s: enabling device for nanny", w->name);
if (!ni_nanny_call_device_enable(w->name)) {
ni_error("%s: unable to enable device", w->name);
rv = FALSE;
}
}
}
if (0 == array->count)
ni_note("ifup: no matching interfaces");
return rv;
}
/*
* Wickedd is sending us a signal indicating internal device state change.
* We want to wait for this signal and when it is >= device-up return TRUE.
* After timeout we fail...
*/
static void
ni_nanny_fsm_monitor_handler(ni_dbus_connection_t *conn, ni_dbus_message_t *msg, void *user_data)
{
const char *signal_name = dbus_message_get_member(msg);
const char *object_path = dbus_message_get_path(msg);
ni_dbus_variant_t argv = NI_DBUS_VARIANT_INIT;
ni_nanny_fsm_monitor_t *monitor = user_data;
ni_fsm_state_t cur_state, target_state;
const char *ifname;
unsigned int i;
if (ni_string_empty(object_path) || !monitor || !monitor->marked)
return;
/* Deserialize dbus message */
if (ni_dbus_message_get_args_variants(msg, &argv, 1) < 0 ||
!ni_dbus_variant_is_dict(&argv)) {
ni_error("Unable to retrieve dict from signal %s, object_path=%s",
signal_name, object_path);
return;
}
if (!ni_dbus_dict_get_uint32(&argv, "current-state", &cur_state) ||
!ni_dbus_dict_get_uint32(&argv, "target-state", &target_state) ||
!ni_dbus_dict_get_string(&argv, "ifname", &ifname)) {
ni_error("Unable to retrieve dict's values from signal %s, object_path=%s",
signal_name, object_path);
return;
}
ni_debug_application("received signal %s; object_path=%s; target_state=%s, state_name=%s",
signal_name, object_path, ni_ifworker_state_name(target_state),
ni_ifworker_state_name(cur_state));
for (i = 0; i < monitor->marked->count; ++i) {
ni_ifworker_t *w = monitor->marked->data[i];
if (cur_state != NI_FSM_STATE_NONE && cur_state != target_state)
continue;
if (!ni_string_eq(w->name, ifname))
continue;
ni_ifworker_array_remove_with_children(monitor->marked, w);
break;
}
ni_dbus_variant_destroy(&argv);
}
void
ni_nanny_fsm_monitor_timeout(void *user_data, const ni_timer_t *timer)
{
ni_nanny_fsm_monitor_t *monitor = user_data;
if (monitor && timer == monitor->timer) {
monitor->timer = NULL;
monitor->timeout = 0;
ni_info("Interface wait time reached");
}
}
ni_nanny_fsm_monitor_t *
ni_nanny_fsm_monitor_new(ni_fsm_t *fsm)
{
ni_nanny_fsm_monitor_t *monitor;
ni_dbus_client_t *client;
if (!fsm)
return NULL;
if (!(fsm->client_root_object = ni_call_create_client()))
return NULL;
if (!(client = ni_dbus_object_get_client(fsm->client_root_object)))
return NULL;
monitor = calloc(1, sizeof(*monitor));
if (monitor) {
ni_dbus_client_add_signal_handler(client, NULL, NULL,
NI_OBJECTMODEL_MANAGED_NETIF_INTERFACE,
ni_nanny_fsm_monitor_handler, monitor);
}
return monitor;
}
ni_bool_t
ni_nanny_fsm_monitor_arm(ni_nanny_fsm_monitor_t *monitor, unsigned long timeout)
{
if (monitor) {
monitor->timeout = timeout;
if (monitor->timer)
monitor->timer = ni_timer_rearm(monitor->timer, timeout);
else
monitor->timer = ni_timer_register(timeout,
ni_nanny_fsm_monitor_timeout, monitor);
return monitor->timer != NULL;
}
return FALSE;
}
void
ni_nanny_fsm_monitor_run(ni_nanny_fsm_monitor_t *monitor, ni_ifworker_array_t *marked, int status)
{
if (!monitor || monitor->marked || !marked)
return;
monitor->marked = ni_ifworker_array_clone(marked);
while (!ni_caught_terminal_signal()) {
long timeout;
if (!monitor->marked || !monitor->marked->count)
break;
timeout = ni_timer_next_timeout();
if (monitor->timeout == 0 ||
(monitor->timeout > 0 && timeout < 0))
break;
if (ni_socket_wait(timeout) != 0)
break;
}
if (monitor->timer) {
ni_timer_cancel(monitor->timer);
monitor->timer = NULL;
}
}
void
ni_nanny_fsm_monitor_reset(ni_nanny_fsm_monitor_t *monitor)
{
if (monitor) {
monitor->timeout = 0;
if (monitor->timer) {
ni_timer_cancel(monitor->timer);
monitor->timer = NULL;
}
ni_ifworker_array_free(monitor->marked);
monitor->marked = NULL;
}
}
void
ni_nanny_fsm_monitor_free(ni_nanny_fsm_monitor_t *monitor)
{
ni_nanny_fsm_monitor_reset(monitor);
free(monitor);
}
static int
ni_do_ifup_nanny(int argc, char **argv)
{
enum { OPT_HELP, OPT_IFCONFIG, OPT_CONTROL_MODE, OPT_STAGE, OPT_TIMEOUT,
OPT_SKIP_ACTIVE, OPT_SKIP_ORIGIN, OPT_PERSISTENT, OPT_TRANSIENT,
#ifdef NI_TEST_HACKS
OPT_IGNORE_PRIO, OPT_IGNORE_STARTMODE,
#endif
};
static struct option ifup_options[] = {
{ "help", no_argument, NULL, OPT_HELP },
{ "ifconfig", required_argument, NULL, OPT_IFCONFIG },
{ "mode", required_argument, NULL, OPT_CONTROL_MODE },
{ "boot-stage", required_argument, NULL, OPT_STAGE },
{ "skip-active",required_argument, NULL, OPT_SKIP_ACTIVE },
{ "skip-origin",required_argument, NULL, OPT_SKIP_ORIGIN },
{ "timeout", required_argument, NULL, OPT_TIMEOUT },
{ "transient", no_argument, NULL, OPT_TRANSIENT },
#ifdef NI_TEST_HACKS
{ "ignore-prio",no_argument, NULL, OPT_IGNORE_PRIO },
{ "ignore-startmode",no_argument, NULL, OPT_IGNORE_STARTMODE },
#endif
{ "persistent", no_argument, NULL, OPT_PERSISTENT },
{ NULL }
};
ni_ifmatcher_t ifmatch;
ni_ifworker_array_t ifmarked;
ni_nanny_fsm_monitor_t *monitor = NULL;
ni_string_array_t opt_ifconfig = NI_STRING_ARRAY_INIT;
ni_string_array_t ifnames = NI_STRING_ARRAY_INIT;
ni_bool_t check_prio = TRUE, set_persistent = FALSE;
ni_bool_t opt_transient = FALSE;
int c, status = NI_WICKED_RC_USAGE;
unsigned int timeout = 0;
ni_fsm_t *fsm;
fsm = ni_fsm_new();
ni_assert(fsm);
ni_fsm_require_register_type("reachable", ni_ifworker_reachability_check_new);
memset(&ifmatch, 0, sizeof(ifmatch));
memset(&ifmarked, 0, sizeof(ifmarked));
/* Allow ifup on all interfaces we have config for */
ifmatch.require_configured = FALSE;
ifmatch.allow_persistent = TRUE;
ifmatch.require_config = TRUE;
optind = 1;
while ((c = getopt_long(argc, argv, "", ifup_options, NULL)) != EOF) {
switch (c) {
case OPT_IFCONFIG:
ni_string_array_append(&opt_ifconfig, optarg);
break;
case OPT_CONTROL_MODE:
ifmatch.mode = optarg;
break;
case OPT_STAGE:
ifmatch.boot_stage= optarg;
break;
case OPT_TIMEOUT:
if (!strcmp(optarg, "infinite")) {
timeout = NI_IFWORKER_INFINITE_TIMEOUT;
} else {
unsigned int sec;
if (ni_parse_uint(optarg, &sec, 10) < 0) {
ni_error("ifup: cannot parse timeout option \"%s\"", optarg);
goto usage;
}
timeout = sec * 1000; /* sec -> msec */
}
break;
case OPT_SKIP_ORIGIN:
ifmatch.skip_origin = optarg;
break;
case OPT_SKIP_ACTIVE:
ifmatch.skip_active = TRUE;
break;
#ifdef NI_TEST_HACKS
case OPT_IGNORE_PRIO:
check_prio = FALSE;
break;
case OPT_IGNORE_STARTMODE:
ifmatch.ignore_startmode = TRUE;
break;
#endif
case OPT_PERSISTENT:
set_persistent = TRUE;
break;
case OPT_TRANSIENT:
opt_transient = TRUE;
break;
default:
case OPT_HELP:
usage:
fprintf(stderr,
"wicked [options] ifup [ifup-options] <ifname ...>|all\n"
"\nSupported ifup-options:\n"
" --help\n"
" Show this help text.\n"
" --transient\n"
" Enable transient interface return codes\n"
" --ifconfig <pathname>\n"
" Read interface configuration(s) from file/directory rather than using system config\n"
" --mode <label>\n"
" Only touch interfaces with matching control <mode>\n"
" --boot-stage <label>\n"
" Only touch interfaces with matching <boot-stage>\n"
" --skip-active\n"
" Do not touch running interfaces\n"
" --skip-origin <name>\n"
" Skip interfaces that have a configuration origin of <name>\n"
" Usually, you would use this with the name \"firmware\" to avoid\n"
" touching interfaces that have been set up via firmware (like iBFT) previously\n"
" --timeout <sec>\n"
" Timeout after <sec> seconds\n"
#ifdef NI_TEST_HACKS
" --ignore-prio\n"
" Ignore checking the config origin priorities\n"
" --ignore-startmode\n"
" Ignore checking the STARTMODE=off and STARTMODE=manual configs\n"
#endif
" --persistent\n"
" Set interface into persistent mode (no regular ifdown allowed)\n"
);
goto cleanup;
}
}
if (optind >= argc) {
fprintf(stderr, "Missing interface argument\n");
goto usage;
}
if (!(monitor = ni_nanny_fsm_monitor_new(fsm)) || !ni_fsm_refresh_state(fsm)) {
/* Severe error we always explicitly return */
status = NI_WICKED_RC_ERROR;
goto cleanup;
}
if (opt_ifconfig.count == 0) {
const ni_string_array_t *sources = ni_config_sources("ifconfig");
if (sources && sources->count)
ni_string_array_copy(&opt_ifconfig, sources);
if (opt_ifconfig.count == 0) {
ni_error("ifup: unable to load interface config source list");
status = NI_WICKED_RC_NOT_CONFIGURED;
goto cleanup;
}
}
if (!ni_ifconfig_load(fsm, opt_global_rootdir, &opt_ifconfig, check_prio, TRUE)) {
status = NI_WICKED_RC_NOT_CONFIGURED;
goto cleanup;
}
/* Set timeout how long the action is allowed to wait */
if (timeout) {
fsm->worker_timeout = timeout; /* One set by user */
} else
if (ni_wait_for_interfaces) {
fsm->worker_timeout = ni_fsm_find_max_timeout(fsm,
ni_wait_for_interfaces*1000);
} else {
fsm->worker_timeout = ni_fsm_find_max_timeout(fsm,
NI_IFWORKER_DEFAULT_TIMEOUT);
}
if (fsm->worker_timeout == NI_IFWORKER_INFINITE_TIMEOUT)
ni_debug_application("wait for interfaces infinitely");
else
ni_debug_application("wait %u seconds for interfaces",
fsm->worker_timeout/1000);
ni_nanny_fsm_monitor_arm(monitor, fsm->worker_timeout);
if (ni_fsm_build_hierarchy(fsm, TRUE) < 0) {
ni_error("ifup: unable to build device hierarchy");
/* Severe error we always explicitly return */
status = NI_WICKED_RC_ERROR;
goto cleanup;
}
status = NI_WICKED_RC_SUCCESS;
/* Get workers that match given criteria */
while (optind < argc) {
ifmatch.name = argv[optind++];
if (!strcmp(ifmatch.name, "boot")) {
ifmatch.name = "all";
ifmatch.mode = "boot";
}
ni_fsm_get_matching_workers(fsm, &ifmatch, &ifmarked);
if (ni_string_eq(ifmatch.name, "all") ||
ni_string_empty(ifmatch.name)) {
ni_string_array_destroy(&ifnames);
break;
}
if (ni_string_array_index(&ifnames, ifmatch.name) < 0)
ni_string_array_append(&ifnames, ifmatch.name);
}
ni_fsm_pull_in_children(&ifmarked);
ni_ifworkers_flatten(&ifmarked);
if (!ni_ifup_hire_nanny(&ifmarked, set_persistent))
status = NI_WICKED_RC_NOT_CONFIGURED;
/* Wait for device up-transition progress events */
ni_nanny_fsm_monitor_run(monitor, &ifmarked, status);
ni_fsm_wait_tentative_addrs(fsm);
status = ni_ifstatus_display_result(fsm, &ifnames, &ifmarked,
opt_transient);
/*
* Do not report any errors to systemd -- returning an error
* here, will cause sytemd to stop the network completely.
*/
if (opt_systemd)
status = NI_LSB_RC_SUCCESS;
cleanup:
ni_string_array_destroy(&ifnames);
ni_nanny_fsm_monitor_free(monitor);
ni_ifworker_array_destroy(&ifmarked);
ni_string_array_destroy(&opt_ifconfig);
return status;
}
static int
ni_do_ifup_direct(int argc, char **argv)
{
enum { OPT_HELP, OPT_IFCONFIG, OPT_CONTROL_MODE, OPT_STAGE, OPT_TIMEOUT,
OPT_SKIP_ACTIVE, OPT_SKIP_ORIGIN, OPT_PERSISTENT, OPT_TRANSIENT,
#ifdef NI_TEST_HACKS
OPT_IGNORE_PRIO, OPT_IGNORE_STARTMODE,
#endif
};
static struct option ifup_options[] = {
{ "help", no_argument, NULL, OPT_HELP },
{ "ifconfig", required_argument, NULL, OPT_IFCONFIG },
{ "mode", required_argument, NULL, OPT_CONTROL_MODE },
{ "boot-stage", required_argument, NULL, OPT_STAGE },
{ "skip-active",required_argument, NULL, OPT_SKIP_ACTIVE },
{ "skip-origin",required_argument, NULL, OPT_SKIP_ORIGIN },
{ "timeout", required_argument, NULL, OPT_TIMEOUT },
{ "transient", no_argument, NULL, OPT_TRANSIENT },
#ifdef NI_TEST_HACKS
{ "ignore-prio",no_argument, NULL, OPT_IGNORE_PRIO },
{ "ignore-startmode",no_argument, NULL, OPT_IGNORE_STARTMODE },
#endif
{ "persistent", no_argument, NULL, OPT_PERSISTENT },
{ NULL }
};
ni_ifmatcher_t ifmatch;
ni_ifmarker_t ifmarker;
ni_ifworker_array_t ifmarked;
ni_string_array_t opt_ifconfig = NI_STRING_ARRAY_INIT;
ni_string_array_t ifnames = NI_STRING_ARRAY_INIT;
ni_bool_t check_prio = TRUE;
ni_bool_t opt_transient = FALSE;
unsigned int nmarked;
ni_fsm_t *fsm;
int c, status = NI_WICKED_RC_USAGE;
unsigned int timeout = 0;
const char *ptr;
fsm = ni_fsm_new();
ni_assert(fsm);
ni_fsm_require_register_type("reachable", ni_ifworker_reachability_check_new);
memset(&ifmatch, 0, sizeof(ifmatch));
memset(&ifmarker, 0, sizeof(ifmarker));
memset(&ifmarked, 0, sizeof(ifmarked));
/* Allow ifup on all interfaces we have config for */
ifmatch.require_configured = FALSE;
ifmatch.allow_persistent = TRUE;
ifmatch.require_config = TRUE;
ifmarker.target_range.min = __NI_FSM_STATE_MAX - 1;
ifmarker.target_range.max = __NI_FSM_STATE_MAX;
/*
* Workaround to consider WAIT_FOR_INTERFACES variable
* in network/config (bnc#863371, bnc#862530 timeouts).
* Correct would be to get it from compat layer, but
* the network/config is sourced in systemd service...
*/
if ((ptr = getenv("WAIT_FOR_INTERFACES"))) {
unsigned int sec;
if (ni_parse_uint(ptr, &sec, 10) == 0 &&
(sec * 1000 > fsm->worker_timeout)) {
ni_debug_application("wait %u sec for interfaces", sec);
timeout = sec * 1000;
}
}
optind = 1;
while ((c = getopt_long(argc, argv, "", ifup_options, NULL)) != EOF) {
switch (c) {
case OPT_IFCONFIG:
ni_string_array_append(&opt_ifconfig, optarg);
break;
case OPT_CONTROL_MODE:
ifmatch.mode = optarg;
break;
case OPT_STAGE:
ifmatch.boot_stage= optarg;
break;
case OPT_TIMEOUT:
if (!strcmp(optarg, "infinite")) {
timeout = NI_IFWORKER_INFINITE_TIMEOUT;
} else if (ni_parse_uint(optarg, &timeout, 10) >= 0) {
timeout *= 1000; /* sec -> msec */
} else {
ni_error("ifup: cannot parse timeout option \"%s\"", optarg);
goto usage;
}
break;
case OPT_SKIP_ORIGIN:
ifmatch.skip_origin = optarg;
break;
case OPT_SKIP_ACTIVE:
ifmatch.skip_active = TRUE;
break;
#ifdef NI_TEST_HACKS
case OPT_IGNORE_PRIO:
check_prio = FALSE;
break;
case OPT_IGNORE_STARTMODE:
ifmatch.ignore_startmode = TRUE;
break;
#endif
case OPT_PERSISTENT:
ifmarker.persistent = TRUE;
break;
case OPT_TRANSIENT:
opt_transient = TRUE;
break;
default:
case OPT_HELP:
usage:
fprintf(stderr,
"wicked [options] ifup [ifup-options] <ifname ...>|all\n"
"\nSupported ifup-options:\n"
" --help\n"
" Show this help text.\n"
" --transient\n"
" Enable transient interface return codes\n"
" --ifconfig <pathname>\n"
" Read interface configuration(s) from file/directory rather than using system config\n"
" --mode <label>\n"
" Only touch interfaces with matching control <mode>\n"
" --boot-stage <label>\n"
" Only touch interfaces with matching <boot-stage>\n"
" --skip-active\n"
" Do not touch running interfaces\n"
" --skip-origin <name>\n"
" Skip interfaces that have a configuration origin of <name>\n"
" Usually, you would use this with the name \"firmware\" to avoid\n"
" touching interfaces that have been set up via firmware (like iBFT) previously\n"
" --timeout <sec>\n"
" Timeout after <sec> seconds\n"
#ifdef NI_TEST_HACKS
" --ignore-prio\n"
" Ignore checking the config origin priorities\n"
" --ignore-startmode\n"
" Ignore checking the STARTMODE=off and STARTMODE=manual configs\n"
#endif
" --persistent\n"
" Set interface into persistent mode (no regular ifdown allowed)\n"
);
goto cleanup;
}
}
if (optind >= argc) {
fprintf(stderr, "Missing interface argument\n");
goto usage;
}
if (!ni_fsm_create_client(fsm)) {
/* Severe error we always explicitly return */
status = NI_WICKED_RC_ERROR;
goto cleanup;
}
if (!ni_fsm_refresh_state(fsm)) {
/* Severe error we always explicitly return */
status = NI_WICKED_RC_ERROR;
goto cleanup;
}
if (opt_ifconfig.count == 0) {
const ni_string_array_t *sources = ni_config_sources("ifconfig");
if (sources && sources->count)
ni_string_array_copy(&opt_ifconfig, sources);
if (opt_ifconfig.count == 0) {
ni_error("ifup: unable to load interface config source list");
status = NI_WICKED_RC_NOT_CONFIGURED;
goto cleanup;
}
}
if (!ni_ifconfig_load(fsm, opt_global_rootdir, &opt_ifconfig, check_prio, TRUE)) {
status = NI_WICKED_RC_NOT_CONFIGURED;
goto cleanup;
}
/* Set timeout how long the action is allowed to wait */
if (timeout) {
fsm->worker_timeout = timeout; /* One set by user */
} else
if (ni_wait_for_interfaces) {
fsm->worker_timeout = ni_fsm_find_max_timeout(fsm,
ni_wait_for_interfaces*1000);
} else {
fsm->worker_timeout = ni_fsm_find_max_timeout(fsm,
NI_IFWORKER_DEFAULT_TIMEOUT);
}
if (fsm->worker_timeout == NI_IFWORKER_INFINITE_TIMEOUT)
ni_debug_application("wait for interfaces infinitely");
else
ni_debug_application("wait %u seconds for interfaces",
fsm->worker_timeout/1000);
if (ni_fsm_build_hierarchy(fsm, TRUE) < 0) {
ni_error("ifup: unable to build device hierarchy");
/* Severe error we always explicitly return */
status = NI_WICKED_RC_ERROR;
goto cleanup;
}
/* Get workers that match given criteria */
nmarked = 0;
while (optind < argc) {
ifmatch.name = argv[optind++];
if (!strcmp(ifmatch.name, "boot")) {
ifmatch.name = "all";
ifmatch.mode = "boot";
}
ni_fsm_get_matching_workers(fsm, &ifmatch, &ifmarked);
if (ni_string_eq(ifmatch.name, "all") ||
ni_string_empty(ifmatch.name)) {
ni_string_array_destroy(&ifnames);
break;
}
if (ni_string_array_index(&ifnames, ifmatch.name) < 0)
ni_string_array_append(&ifnames, ifmatch.name);
}
ni_fsm_pull_in_children(&ifmarked);
/* Mark and start selected workers */
if (ifmarked.count)
nmarked = ni_fsm_mark_matching_workers(fsm, &ifmarked, &ifmarker);
if (nmarked == 0) {
ni_note("ifup: no matching interfaces");
status = NI_WICKED_RC_SUCCESS;
} else {
if (ni_fsm_schedule(fsm) != 0)
ni_fsm_mainloop(fsm);
ni_fsm_wait_tentative_addrs(fsm);
status = ni_ifstatus_display_result(fsm, &ifnames, &ifmarked,
opt_transient);
/*
* Do not report any errors to systemd -- returning an error
* here, will cause sytemd to stop the network completely.
*/
if (opt_systemd)
status = NI_LSB_RC_SUCCESS;
}
cleanup:
ni_ifworker_array_destroy(&ifmarked);
ni_string_array_destroy(&ifnames);
ni_string_array_destroy(&opt_ifconfig);
return status;
}
int
ni_do_ifup(int argc, char **argv)
{
if (ni_config_use_nanny())
return ni_do_ifup_nanny(argc, argv);
else
return ni_do_ifup_direct(argc, argv);
}