forked from pantavisor/pantavisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.c
1360 lines (1159 loc) · 33.6 KB
/
state.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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020-2022 Pantacor Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include "state.h"
#include "drivers.h"
#include "paths.h"
#include "volumes.h"
#include "disk/disk.h"
#include "platforms.h"
#include "objects.h"
#include "jsons.h"
#include "addons.h"
#include "pantavisor.h"
#include "storage.h"
#include "metadata.h"
#include "utils/tsh.h"
#include "utils/math.h"
#include "utils/str.h"
#include "utils/json.h"
#include "utils/timer.h"
#define MODULE_NAME "state"
#define pv_log(level, msg, ...) vlog(MODULE_NAME, level, msg, ##__VA_ARGS__)
#include "log.h"
struct pv_state *pv_state_new(const char *rev, state_spec_t spec)
{
int len = strlen(rev) + 1;
struct pv_state *s;
s = calloc(1, sizeof(struct pv_state));
if (s) {
s->rev = calloc(len, sizeof(char));
SNPRINTF_WTRUNC(s->rev, len, "%s", rev);
s->status = PLAT_NONE;
s->spec = spec;
dl_list_init(&s->platforms);
dl_list_init(&s->volumes);
dl_list_init(&s->disks);
dl_list_init(&s->addons);
dl_list_init(&s->objects);
dl_list_init(&s->jsons);
dl_list_init(&s->groups);
dl_list_init(&s->bsp.drivers);
s->using_runlevels = false;
s->local = false;
s->done = false;
}
return s;
}
static void pv_state_empty_groups(struct pv_state *s)
{
int num_groups = 0;
struct pv_group *g, *tmp;
struct dl_list *groups = &s->groups;
// Iterate over all groups from state
dl_list_for_each_safe(g, tmp, groups, struct pv_group, list)
{
dl_list_del(&g->list);
pv_group_free(g);
num_groups++;
}
pv_log(INFO, "removed %d groups", num_groups);
}
void pv_state_free(struct pv_state *s)
{
if (!s)
return;
pv_log(INFO, "removing state with revision %s", s->rev);
if (s->rev)
free(s->rev);
if (s->bsp.config)
free(s->bsp.config);
if (s->bsp.img.ut.fit) {
free(s->bsp.img.ut.fit);
} else if (s->bsp.img.rpiab.bootimg) {
free(s->bsp.img.rpiab.bootimg);
} else {
if (s->bsp.img.std.kernel)
free(s->bsp.img.std.kernel);
if (s->bsp.img.std.fdt)
free(s->bsp.img.std.fdt);
if (s->bsp.img.std.initrd)
free(s->bsp.img.std.initrd);
}
if (s->bsp.firmware)
free(s->bsp.firmware);
if (s->bsp.modules)
free(s->bsp.modules);
pv_drivers_empty(s);
pv_platforms_empty(s);
pv_volumes_empty(s);
pv_disk_empty(&s->disks);
pv_addons_empty(s);
pv_objects_empty(s);
pv_jsons_empty(s);
pv_state_empty_groups(s);
free(s);
}
void pv_state_add_group(struct pv_state *s, struct pv_group *g)
{
pv_log(DEBUG,
"adding group '%s' with status goal '%s' and restart policy '%s' to state",
g->name, pv_platform_status_string(g->default_status_goal),
pv_platforms_restart_policy_str(g->default_restart_policy));
dl_list_init(&g->list);
dl_list_add_tail(&s->groups, &g->list);
}
struct pv_group *pv_state_fetch_group(struct pv_state *s, const char *name)
{
struct pv_group *g, *tmp;
if (!name)
return NULL;
// Iterate over all groups from state
dl_list_for_each_safe(g, tmp, &s->groups, struct pv_group, list)
{
if (pv_str_matches(g->name, strlen(g->name), name,
strlen(name)))
return g;
}
return NULL;
}
struct pv_platform *pv_state_fetch_platform(struct pv_state *s,
const char *name)
{
struct pv_platform *p, *tmp;
if (!name)
return NULL;
// Iterate over all platforms from state
dl_list_for_each_safe(p, tmp, &s->platforms, struct pv_platform, list)
{
if (pv_str_matches(p->name, strlen(p->name), name,
strlen(name)))
return p;
}
return NULL;
}
struct pv_object *pv_state_fetch_object(struct pv_state *s, const char *name)
{
struct pv_object *o, *tmp;
if (!name)
return NULL;
// Iterate over all objects from state
dl_list_for_each_safe(o, tmp, &s->objects, struct pv_object, list)
{
if (pv_str_matches(o->name, strlen(o->name), name,
strlen(name)))
return o;
}
return NULL;
}
struct pv_json *pv_state_fetch_json(struct pv_state *s, const char *name)
{
struct pv_json *j, *tmp;
if (!name)
return NULL;
// Iterate over all groups from state
dl_list_for_each_safe(j, tmp, &s->jsons, struct pv_json, list)
{
if (pv_str_matches(j->name, strlen(j->name), name,
strlen(name)))
return j;
}
return NULL;
}
static struct pv_disk *pv_state_fetch_disk(struct pv_state *s, const char *name)
{
struct pv_disk *d, *tmp;
if (!name)
return NULL;
// Iterate over all disks from state
dl_list_for_each_safe(d, tmp, &s->disks, struct pv_disk, list)
{
if (pv_str_matches(d->name, strlen(d->name), name,
strlen(name)))
return d;
}
return NULL;
}
void pv_state_print(struct pv_state *s)
{
if (!s)
return;
pv_log(DEBUG, "state %s:", s->rev);
pv_log(DEBUG, " kernel: '%s'", s->bsp.img.std.kernel);
pv_log(DEBUG, " initrd: '%s'", s->bsp.img.std.initrd);
struct pv_group *g, *tmp_g;
struct dl_list *groups = &s->groups;
dl_list_for_each_safe(g, tmp_g, groups, struct pv_group, list)
{
pv_log(DEBUG, " group: '%s'", g->name);
}
struct pv_platform *p, *tmp_p;
struct dl_list *platforms = &s->platforms;
dl_list_for_each_safe(p, tmp_p, platforms, struct pv_platform, list)
{
pv_log(DEBUG, " platform: '%s'", p->name);
pv_log(DEBUG, " type: '%s'", p->type);
if (p->group)
pv_log(DEBUG, " group: '%s'", p->group->name);
pv_log(DEBUG, " configs:");
char **config = p->configs;
while (config && *config) {
pv_log(DEBUG, " '%s'", *config);
config++;
}
}
struct pv_volume *v, *tmp_v;
struct dl_list *volumes = &s->volumes;
dl_list_for_each_safe(v, tmp_v, volumes, struct pv_volume, list)
{
pv_log(DEBUG, " volume: '%s'", v->name);
pv_log(DEBUG, " type: %d", v->type);
if (v->plat)
pv_log(DEBUG, " platform: '%s'", v->plat->name);
if (v->disk)
pv_log(DEBUG, " disk: '%s'", v->disk->name);
}
struct pv_disk *d, *tmp_d;
struct dl_list *disks = &s->disks;
dl_list_for_each_safe(d, tmp_d, disks, struct pv_disk, list)
{
pv_log(DEBUG, " disk: '%s'", d->name);
pv_log(DEBUG, " default: %d", d->def);
pv_log(DEBUG, " type: %d", d->type);
}
struct pv_addon *a, *tmp_a;
struct dl_list *addons = &s->addons;
dl_list_for_each_safe(a, tmp_a, addons, struct pv_addon, list)
{
pv_log(DEBUG, " addon: '%s'", a->name);
}
struct pv_object *curr;
pv_objects_iter_begin(s, curr)
{
pv_log(DEBUG, " object: '%s'", curr->name);
pv_log(DEBUG, " id: '%s'", curr->id);
if (curr->plat)
pv_log(DEBUG, " platform: '%s'", curr->plat->name);
}
pv_objects_iter_end;
struct pv_json *j, *tmp_j;
struct dl_list *jsons = &s->jsons;
dl_list_for_each_safe(j, tmp_j, jsons, struct pv_json, list)
{
pv_log(DEBUG, " json: '%s'", j->name);
if (j->plat)
pv_log(DEBUG, " platform: '%s'", j->plat->name);
}
pv_log(DEBUG, "done=%d", s->done);
}
static int pv_state_set_default_groups(struct pv_state *s)
{
struct pv_platform *p, *tmp;
struct dl_list *platforms = &s->platforms;
struct pv_group *d;
// default group is the last group added from groups.json
d = dl_list_last(&s->groups, struct pv_group, list);
dl_list_for_each_safe(p, tmp, platforms, struct pv_platform, list)
{
if (!p->group) {
pv_log(WARN,
"platform '%s' does not belong to any group. Setting default...",
p->name);
pv_group_add_platform(d, p);
}
}
return 0;
}
static int pv_state_set_default_runlevels(struct pv_state *s)
{
bool root_configured = false;
struct pv_platform *p, *tmp, *first_p = NULL;
struct dl_list *platforms = &s->platforms;
struct pv_group *r, *d;
r = pv_state_fetch_group(s, "root");
d = pv_state_fetch_group(s, "platform");
if (!r || !d) {
pv_log(ERROR, "could not find group root or platform");
return -1;
}
dl_list_for_each_safe(p, tmp, platforms, struct pv_platform, list)
{
// check if any platform has been configured in group root
if (p->group == r)
root_configured = true;
// get first unconfigured platform
if (!first_p && (p->group == NULL))
first_p = p;
}
// if not, set first platform in group root
if (!root_configured && first_p) {
pv_log(WARN,
"no platform was found in 'root' group, "
"so the first unconfigured one in alphabetical order will be set");
pv_group_add_platform(r, first_p);
}
// set rest of the non configured platforms in group platform
platforms = &s->platforms;
dl_list_for_each_safe(p, tmp, platforms, struct pv_platform, list)
{
if (p->group == NULL) {
pv_group_add_platform(d, p);
}
}
return 0;
}
static int pv_state_set_runlevel_groups(struct pv_state *s)
{
if (dl_list_empty(&s->platforms))
return 0;
if (s->using_runlevels)
return pv_state_set_default_runlevels(s);
return pv_state_set_default_groups(s);
}
static void pv_state_set_default_status_goals(struct pv_state *s)
{
struct pv_platform *p, *tmp;
struct dl_list *platforms = &s->platforms;
dl_list_for_each_safe(p, tmp, platforms, struct pv_platform, list)
{
if (p->status.goal != PLAT_NONE)
continue;
pv_log(INFO,
"platform '%s' in group '%s' has no explicit status goal. "
"It will be set by default to the group's '%s'",
p->name, p->group->name,
pv_platform_status_string(
p->group->default_status_goal));
pv_platform_set_status_goal(p, p->group->default_status_goal);
}
}
static void pv_state_set_default_restart_policies(struct pv_state *s)
{
struct pv_platform *p, *tmp;
struct dl_list *platforms = &s->platforms;
dl_list_for_each_safe(p, tmp, platforms, struct pv_platform, list)
{
if (p->restart_policy != RESTART_NONE)
continue;
pv_log(INFO,
"platform '%s' in group '%s' has no explicit restart_policy. "
"It will be set by default to the group's '%s'",
p->name, p->group->name,
pv_platforms_restart_policy_str(
p->group->default_restart_policy));
pv_platform_set_restart_policy(
p, p->group->default_restart_policy);
}
}
int pv_state_validate(struct pv_state *s)
{
// remove platforms that have no loaded data
pv_platforms_remove_not_installed(s);
// add loggers for all platforms
pv_platforms_add_all_loggers(s);
// set groups for all undefined platforms
if (pv_state_set_runlevel_groups(s))
return -1;
// set default status goal
pv_state_set_default_status_goals(s);
// set default restart policies
pv_state_set_default_restart_policies(s);
return 0;
}
static int pv_state_mount_bsp_volumes(struct pv_state *s)
{
if (pv_disk_mount_swap(&s->disks) != 0)
return -1;
struct pv_volume *v, *tmp;
dl_list_for_each_safe(v, tmp, &s->volumes, struct pv_volume, list)
{
if (!v->plat)
if (pv_volume_mount(v)) {
pv_log(ERROR, "bsp volume %s mount failed",
v->name);
return -1;
}
}
return pv_volumes_mount_firmware_modules();
}
int pv_state_start(struct pv_state *s)
{
return pv_state_mount_bsp_volumes(s);
}
static void pv_state_set_status(struct pv_state *s, plat_status_t status)
{
if (s->status == status)
return;
s->status = status;
pv_log(INFO, "state revision '%s' status is now %s", s->rev,
pv_platform_status_string(status));
pv_metadata_add_devmeta("pantavisor.status",
pv_platform_status_string(status));
}
void pv_state_eval_status(struct pv_state *s)
{
if (!s)
return;
plat_status_t state_status = PLAT_READY;
struct pv_group *g, *tmp;
dl_list_for_each_safe(g, tmp, &s->groups, struct pv_group, list)
{
if (g->status < state_status)
state_status = g->status;
}
pv_state_set_status(s, state_status);
}
plat_goal_state_t pv_state_check_goals(struct pv_state *s)
{
if (!s)
return PLAT_GOAL_NONE;
plat_goal_state_t s_goal_state = PLAT_GOAL_ACHIEVED;
plat_goal_state_t g_goal_state;
struct pv_group *g, *tmp;
dl_list_for_each_safe(g, tmp, &s->groups, struct pv_group, list)
{
// we are only interested in the highest goal state here. This will be
// ACHIEVED unless there is any group that is still UNACHIEVED, which
// will be also overwriten by any group that has TIMEDOUT
g_goal_state = pv_group_check_goals(g);
if (g_goal_state > s_goal_state)
s_goal_state = g_goal_state;
}
return s_goal_state;
}
static plat_goal_state_t pv_state_check_goal_prev_group(struct pv_state *s,
struct pv_group *group)
{
if (!s || !group)
return PLAT_GOAL_NONE;
plat_goal_state_t g_goal_state = PLAT_GOAL_ACHIEVED;
struct pv_group *g, *tmp;
dl_list_for_each_safe(g, tmp, &s->groups, struct pv_group, list)
{
// we iterate over all previous groups to the one we are checking
if (group == g)
break;
// each group that we iterate over will overwrite the return state,
// so we will always get the one inmediately previous to the one we
// are checking, unsless we find any group with UNACHIEVED state. In
// that case, we return UNACHIEVED, as further ACHIEVED groups means
// they have no platforms
g_goal_state = pv_group_check_goals(g);
if (g_goal_state == PLAT_GOAL_UNACHIEVED)
break;
}
return g_goal_state;
}
static int pv_state_start_platform(struct pv_state *s, struct pv_platform *p)
{
struct pv_volume *v, *tmp;
dl_list_for_each_safe(v, tmp, &s->volumes, struct pv_volume, list)
{
if (v->plat == p)
if (pv_volume_mount(v)) {
pv_log(ERROR, "volume %s could not be mounted",
v->name);
return -1;
}
}
pv_platform_set_mounted(p);
if (p->status.goal == PLAT_MOUNTED)
return 0;
if (pv_platform_load_drivers(p, NULL,
DRIVER_REQUIRED | DRIVER_OPTIONAL) < 0) {
pv_log(ERROR, "failed to load drivers");
return -1;
}
if (pv_platform_start(p)) {
pv_log(ERROR, "platform %s could not be started", p->name);
return -1;
}
return 0;
}
int pv_state_run(struct pv_state *s)
{
int ret = 0;
struct pv_platform *p, *tmp_p;
dl_list_for_each_safe(p, tmp_p, &s->platforms, struct pv_platform, list)
{
if (pv_platform_is_installed(p) || pv_platform_is_blocked(p)) {
plat_goal_state_t status_goal =
pv_state_check_goal_prev_group(s, p->group);
switch (status_goal) {
case PLAT_GOAL_UNACHIEVED:
pv_platform_set_blocked(p);
break;
case PLAT_GOAL_ACHIEVED:
case PLAT_GOAL_TIMEDOUT:
pv_log(DEBUG,
"platform '%s' from group '%s' can be started now",
p->name, p->group->name);
ret = pv_state_start_platform(s, p);
break;
default:
pv_log(WARN, "could not check groups goals");
}
} else if (pv_platform_is_starting(p)) {
if (!pv_platform_check_running(p))
pv_log(DEBUG,
"platform %s still not running ",
p->name);
} else if (pv_platform_is_started(p) ||
pv_platform_is_ready(p)) {
if (!pv_platform_check_running(p)) {
pv_log(ERROR, "platform %s suddenly stopped",
p->name);
ret = -1;
}
}
if (ret)
goto out;
}
out:
return ret;
}
static bool pv_state_check_all_stopped(struct pv_state *s)
{
bool try_again = false, ret = true;
struct pv_platform *p, *tmp_p;
for (int i = 0; i < 5; i++) {
ret = true;
dl_list_for_each_safe(p, tmp_p, &s->platforms,
struct pv_platform, list)
{
if (pv_platform_is_stopping(p)) {
if (pv_platform_check_running(p)) {
pv_log(DEBUG,
"platform %s still running",
p->name);
try_again = true;
ret = false;
}
}
}
if (!try_again)
break;
pv_log(DEBUG,
"some platforms are still running. Trying again in 1 second...");
sleep(1);
try_again = false;
}
return ret;
}
static void pv_state_lenient_stop(struct pv_state *s)
{
struct pv_platform *p, *tmp_p;
dl_list_for_each_safe(p, tmp_p, &s->platforms, struct pv_platform, list)
{
if (pv_platform_is_starting(p) || pv_platform_is_started(p) ||
pv_platform_is_ready(p))
pv_platform_stop(p);
}
}
static void pv_state_force_stop(struct pv_state *s)
{
struct pv_platform *p, *tmp_p;
dl_list_for_each_safe(p, tmp_p, &s->platforms, struct pv_platform, list)
{
if (pv_platform_is_stopping(p))
pv_platform_force_stop(p);
}
}
static int pv_state_unmount_platform_volumes(struct pv_state *s,
struct pv_platform *p)
{
int ret = 0;
struct pv_volume *v, *tmp_v;
dl_list_for_each_safe(v, tmp_v, &s->volumes, struct pv_volume, list)
{
if (v->plat == p) {
if (pv_volume_unmount(v))
ret = -1;
}
}
return ret;
}
static int pv_state_unmount_platforms_volumes(struct pv_state *s)
{
int ret = 0;
struct pv_platform *p, *tmp_p;
// unmount platform volumes
dl_list_for_each_safe(p, tmp_p, &s->platforms, struct pv_platform, list)
{
if (!(pv_platform_is_stopping(p) ||
pv_platform_is_starting(p) || pv_platform_is_started(p) ||
pv_platform_is_ready(p))) {
if (pv_state_unmount_platform_volumes(s, p))
ret = -1;
}
}
return ret;
}
void pv_state_stop_lenient(struct pv_state *s)
{
if (!s)
return;
pv_log(DEBUG, "leniently stopping state %s", s->rev);
pv_state_lenient_stop(s);
}
int pv_state_stop_force(struct pv_state *s)
{
int ret = 0;
if (!s)
return -1;
if (!pv_state_check_all_stopped(s)) {
pv_state_force_stop(s);
ret |= -2;
}
// unmount all platform related volumes
if (pv_state_unmount_platforms_volumes(s))
ret |= -4;
// unmount bsp volumes
if (pv_state_unmount_platform_volumes(s, NULL))
ret |= -8;
return ret;
}
static bool pv_state_platform_requires_reboot(struct pv_platform *p)
{
if (p->restart_policy == RESTART_SYSTEM) {
pv_log(DEBUG,
"it belongs to platform '%s', which has a 'system' restart policy. "
"Rebooting...",
p->name);
return true;
} else if (p->restart_policy == RESTART_CONTAINER) {
pv_log(DEBUG,
"it belongs to platform '%s', which has a 'container' restart policy. "
"Reseting container only...",
p->name);
} else {
pv_log(WARN,
"it belongs to platform '%s', which has a an unknown restart policy. "
"Rebooting...");
return true;
}
return false;
}
static bool pv_state_compare_objects(struct pv_state *current,
struct pv_state *pending)
{
struct pv_object *o, *tmp, *pend_o, *curr_o;
struct pv_platform *p;
if (!current || !pending)
return true;
// search for modified or deleted objects
dl_list_for_each_safe(o, tmp, ¤t->objects, struct pv_object, list)
{
pend_o = pv_state_fetch_object(pending, o->name);
if (!pend_o || strcmp(o->id, pend_o->id)) {
pv_log(DEBUG,
"object %s has been modified or deleted in the pending update",
o->name);
// changes in objects belonging to bsp require reboot
if (!o->plat) {
pv_log(DEBUG, "object belongs to bsp");
return true;
}
// changes in objects belonging to platforms in certain groups require reboot
if (pv_state_platform_requires_reboot(o->plat))
return true;
// lenient stop of platform and continue
if (pv_platform_is_starting(o->plat) ||
pv_platform_is_started(o->plat) ||
pv_platform_is_ready(o->plat))
pv_platform_stop(o->plat);
// set to updated so we can remove it later
pv_platform_set_updated(o->plat);
}
}
// search for new objects
dl_list_for_each_safe(o, tmp, &pending->objects, struct pv_object, list)
{
curr_o = pv_state_fetch_object(current, o->name);
if (!curr_o) {
pv_log(DEBUG,
"object %s has been added in the pending update",
o->name);
// new objects belonging to bsp require reboot
if (!o->plat) {
pv_log(DEBUG, "object belongs to bsp");
return true;
}
// new objects belonging to platforms in certain groups require reboot
if (pv_state_platform_requires_reboot(o->plat))
return true;
// lenient stop of platform and continue
p = pv_state_fetch_platform(current, o->plat->name);
if (!p)
continue;
if (pv_platform_is_starting(p) ||
pv_platform_is_started(p) ||
pv_platform_is_ready(p))
pv_platform_stop(p);
// set to updated so we can remove it later
pv_platform_set_updated(p);
}
}
return false;
}
static bool pv_state_compare_jsons(struct pv_state *current,
struct pv_state *pending)
{
struct pv_json *j, *tmp, *pend_j, *curr_j;
struct pv_platform *p;
if (!current || !pending)
return true;
// search for modified or deleted jsons
dl_list_for_each_safe(j, tmp, ¤t->jsons, struct pv_json, list)
{
pend_j = pv_state_fetch_json(pending, j->name);
if (!pend_j || strcmp(j->value, pend_j->value)) {
pv_log(DEBUG,
"json %s has been modified or deleted in the pending update",
j->name);
// changes in jsons belonging to bsp require reboot
if (!j->plat) {
pv_log(DEBUG, "json belongs to bsp");
return true;
}
// changes in jsons belonging to platforms in certain groups require reboot
if (pv_state_platform_requires_reboot(j->plat))
return true;
// lenient stop of platform and continue
if (pv_platform_is_starting(j->plat) ||
pv_platform_is_started(j->plat) ||
pv_platform_is_ready(j->plat))
pv_platform_stop(j->plat);
// set to updated so we can remove it later
pv_platform_set_updated(j->plat);
}
}
// search for new jsons
dl_list_for_each_safe(j, tmp, &pending->jsons, struct pv_json, list)
{
curr_j = pv_state_fetch_json(current, j->name);
if (!curr_j) {
pv_log(DEBUG,
"json %s has been added in the pending update",
j->name);
// new jsons belonging to bsp require reboot
if (!j->plat) {
pv_log(DEBUG, "json belongs to bsp");
return true;
}
// new jsons belonging to platforms in certain groups require reboot
if (pv_state_platform_requires_reboot(j->plat))
return true;
// lenient stop of platform and continue
p = pv_state_fetch_platform(current, j->plat->name);
if (!p)
continue;
if (pv_platform_is_starting(p) ||
pv_platform_is_started(p) ||
pv_platform_is_ready(p))
pv_platform_stop(p);
// set to updated so we can remove it later
pv_platform_set_updated(p);
}
}
return false;
}
// returns: 1 in case a reboot is required because something outside of plat changed
// returns: 0 if all good and no reboot is required
// returns: -1 if there was an error and reboot is mandatory
int pv_state_stop_platforms(struct pv_state *current, struct pv_state *pending)
{
if (pv_state_compare_jsons(current, pending) ||
pv_state_compare_objects(current, pending)) {
pv_log(INFO,
"could not just stop individual platforms for update");
return 1;
}
if (!pv_state_check_all_stopped(current))
pv_state_force_stop(current);
if (pv_state_unmount_platforms_volumes(current)) {
pv_log(ERROR, "could not unmount volumes");
return -1;
}
return 0;
}
static void pv_state_remove_updated_platforms(struct pv_state *s)
{
struct pv_json *j, *j_tmp;
struct pv_object *o, *o_tmp;
struct pv_volume *v, *v_tmp;
struct pv_platform *p, *p_tmp;
pv_log(DEBUG,
"removing artifacts that belong to stopped platforms from rev %s",
s->rev);
// remove jsons belonging to stopped platforms from state
dl_list_for_each_safe(j, j_tmp, &s->jsons, struct pv_json, list)
{
if (!j->plat || !pv_platform_is_updated(j->plat))
continue;
pv_log(DEBUG, "removing json %s that belongs to platform %s",
j->name, j->plat->name);
dl_list_del(&j->list);
pv_jsons_free(j);
}
// remove objects belonging to stopped platforms from state
dl_list_for_each_safe(o, o_tmp, &s->objects, struct pv_object, list)
{
if (!o->plat || !pv_platform_is_updated(o->plat))
continue;
pv_log(DEBUG, "removing object %s that belongs to platform %s",
o->name, o->plat->name);
dl_list_del(&o->list);
pv_object_free(o);
}
// remove volumes belonging to stopped platforms from state
dl_list_for_each_safe(v, v_tmp, &s->volumes, struct pv_volume, list)
{
if (!v->plat || !pv_platform_is_updated(v->plat))
continue;
pv_log(DEBUG, "removing volume %s that belongs to platform %s",
v->name, v->plat->name);
dl_list_del(&v->list);
pv_volume_free(v);
}
// remove platforms belonging to stopped platforms from state
dl_list_for_each_safe(p, p_tmp, &s->platforms, struct pv_platform, list)
{
if (!pv_platform_is_updated(p))
continue;
pv_log(DEBUG, "removing platform %s", p->name);
dl_list_del(&p->list);
pv_platform_free(p);
}
}
static void pv_state_transfer_platforms(struct pv_state *pending,
struct pv_state *current)
{
struct pv_json *j, *j_tmp;