forked from pantavisor/pantavisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pantavisor.c
1047 lines (889 loc) · 26.1 KB
/
pantavisor.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) 2017-2023 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <netdb.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/reboot.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <linux/limits.h>
#include <linux/reboot.h>
#include "pantavisor.h"
#include "loop.h"
#include "platforms.h"
#include "volumes.h"
#include "disk/disk.h"
#include "pantahub.h"
#include "bootloader.h"
#include "ctrl.h"
#include "version.h"
#include "wdt.h"
#include "network.h"
#include "blkid.h"
#include "init.h"
#include "state.h"
#include "updater.h"
#include "storage.h"
#include "metadata.h"
#include "signature.h"
#include "paths.h"
#include "ph_logger.h"
#include "logserver/logserver.h"
#include "mount.h"
#include "debug.h"
#include "cgroup.h"
#include "parser/parser.h"
#include "utils/timer.h"
#include "utils/fs.h"
#include "utils/str.h"
#include "utils/tsh.h"
#define MODULE_NAME "controller"
#define pv_log(level, msg, ...) vlog(MODULE_NAME, level, msg, ##__VA_ARGS__)
#include "log.h"
#define CMDLINE_OFFSET 7
char pv_user_agent[4096];
static struct pantavisor *global_pv;
struct pantavisor *pv_get_instance()
{
return global_pv;
}
static struct timer timer_rollback_remote;
static struct timer timer_wait_delay;
static struct timer timer_usrmeta_interval;
static struct timer timer_devmeta_interval;
static struct timer timer_updater_interval;
static struct timer timer_commit;
static const int PV_WAIT_PERIOD = 1;
typedef enum {
PV_STATE_INIT,
PV_STATE_RUN,
PV_STATE_WAIT,
PV_STATE_COMMAND,
PV_STATE_UPDATE,
PV_STATE_UPDATE_APPLY,
PV_STATE_ROLLBACK,
PV_STATE_REBOOT,
PV_STATE_POWEROFF,
PV_STATE_ERROR,
PV_STATE_EXIT,
MAX_STATES
} pv_state_t;
static const char *pv_state_string(pv_state_t st)
{
switch (st) {
case PV_STATE_INIT:
return "STATE_INIT";
case PV_STATE_RUN:
return "STATE_RUN";
case PV_STATE_WAIT:
return "STATE_WAIT";
case PV_STATE_COMMAND:
return "STATE_COMMAND";
case PV_STATE_UPDATE:
return "STATE_UPDATE";
case PV_STATE_UPDATE_APPLY:
return "STATE_UPDATE_APPLY";
case PV_STATE_ROLLBACK:
return "STATE_ROLLBACK";
case PV_STATE_REBOOT:
return "STATE_REBOOT";
case PV_STATE_POWEROFF:
return "STATE_POWEROFF";
case PV_STATE_ERROR:
return "STATE_ERROR";
case PV_STATE_EXIT:
return "STATE_EXIT";
default:
return "STATE_UNKNOWN";
}
return "STATE_UNKNOWN";
}
typedef pv_state_t pv_state_func_t(struct pantavisor *pv);
typedef enum {
PH_STATE_INIT,
PH_STATE_REGISTER,
PH_STATE_CLAIM,
PH_STATE_SYNC,
PH_STATE_IDLE,
PH_STATE_UPDATE,
PH_STATE_UPDATE_APPLY,
} ph_state_t;
static const char *ph_state_string(ph_state_t st)
{
switch (st) {
case PH_STATE_INIT:
return "init";
case PH_STATE_REGISTER:
return "register";
case PH_STATE_CLAIM:
return "claim";
case PH_STATE_SYNC:
return "sync";
case PH_STATE_IDLE:
return "idle";
case PH_STATE_UPDATE:
return "update";
case PH_STATE_UPDATE_APPLY:
return "updateapply";
default:
return "STATE_UNKNOWN";
}
return "STATE_UNKNOWN";
}
static bool pv_wait_delay_timedout()
{
struct timer_state tstate = timer_current_state(&timer_wait_delay);
// first, we check if timed out
if (!tstate.fin)
return false;
// then, we set 1 sec for next wait cycle
timer_start(&timer_wait_delay, PV_WAIT_PERIOD, 0, RELATIV_TIMER);
return true;
}
static pv_state_t _pv_init(struct pantavisor *pv)
{
pv_log(DEBUG, "%s():%d", __func__, __LINE__);
if (pv_do_execute_init())
return PV_STATE_EXIT;
return PV_STATE_RUN;
}
static pv_state_t _pv_run(struct pantavisor *pv)
{
pv_log(DEBUG, "%s():%d", __func__, __LINE__);
pv_state_t next_state = PV_STATE_ROLLBACK;
char *json = NULL;
// resume update if we have booted to test a new revision
if (pv_update_resume(pv)) {
pv_log(ERROR, "update could not be resumed");
goto out;
}
if (pv_update_is_transitioning(pv->update)) {
// for non-reboot updates...
pv_log(INFO, "transitioning...");
pv_logserver_transition(pv->update->pending->rev);
ph_logger_stop_lenient();
ph_logger_stop_force();
pv_state_transition(pv->update->pending, pv->state);
} else {
// after a reboot...
json = pv_storage_get_state_json(pv_bootloader_get_rev());
sign_state_res_t sres;
sres = pv_signature_verify(json);
if (sres != SIGN_STATE_OK) {
pv_log(ERROR,
"state signature verification went wrong");
pv_update_set_status_msg(
pv->update, UPDATE_SIGNATURE_FAILED,
pv_signature_sign_state_str(sres));
goto out;
}
pv->state = pv_parser_get_state(json, pv_bootloader_get_rev());
if (!pv->state) {
pv_log(ERROR, "state could not be loaded");
goto out;
}
// if an update is going on, we are going to need the state to report progress, so no need to parse it
if (pv->update) {
pv->update->pending = pv_state_new(
pv_bootloader_get_try(), SPEC_UNKNOWN);
}
}
if (!pv->state) {
pv_log(ERROR, "current state not loaded");
goto out;
}
// set current log and trail links
pv_storage_set_active(pv);
if (!pv_state_validate_checksum(pv->state)) {
pv_log(ERROR, "state objects validation went wrong");
pv_update_set_status(pv->update, UPDATE_BAD_CHECKSUM);
goto out;
}
pv_update_set_factory_status();
// this might be a enum in the future, for now, we only need to know if DONE
pv->state->done = pv_storage_is_rev_done(pv->state->rev);
// once we have verified the state, we can load update time configuration
pv_config_load_update(pv->state->rev, pv->state->bsp.config);
// reload remote bool after non reboot updates, when we don't load config again
pv->remote_mode = pv_config_get_bool(PV_CONTROL_REMOTE);
pv->loading_objects = false;
pv->state->local = !pv_config_get_bool(PV_CONTROL_REMOTE);
;
// we know if we are in local if the running revision has the local format
if (pv_storage_is_revision_local(pv->state->rev)) {
pv_log(DEBUG, "running local revision %s", pv->state->rev);
pv->state->local = true;
pv->remote_mode = false;
if (pv_config_get_bool(PV_CONTROL_REMOTE_ALWAYS)) {
pv_log(DEBUG,
"remote mode forced on local revision by configuration");
pv->remote_mode = true;
}
}
if (!pv->remote_mode)
pv_log(INFO,
"running in local mode. Will not consume new updates from Pantahub");
// only start local ph logger, start cloud services if connected
pv_logserver_toggle(pv, pv->state->rev);
ph_logger_toggle(pv->state->rev);
if (!pv_update_is_transitioning(pv->update)) {
if (pv_state_start(pv->state)) {
pv_log(ERROR, "error starting state");
goto out;
}
if (pv_metadata_init()) {
pv_log(ERROR, "metadata mount failed");
goto out;
}
}
// trail .pvr/ initialization
pv_storage_init_trail_pvr();
// meta data initialization, also to be uploaded as soon as possible when connected
pv_metadata_init_devmeta(pv);
timer_start(&timer_commit, pv_config_get_int(PV_UPDATER_COMMIT_DELAY),
0, RELATIV_TIMER);
timer_start(&timer_rollback_remote,
pv_config_get_int(PH_UPDATER_NETWORK_TIMEOUT), 0,
RELATIV_TIMER);
timer_start(&timer_wait_delay, PV_WAIT_PERIOD, 0, RELATIV_TIMER);
timer_start(&timer_usrmeta_interval,
pv_config_get_int(PH_METADATA_USRMETA_INTERVAL), 0,
RELATIV_TIMER);
timer_start(&timer_devmeta_interval,
pv_config_get_int(PH_METADATA_DEVMETA_INTERVAL), 0,
RELATIV_TIMER);
timer_start(&timer_updater_interval,
pv_config_get_int(PH_UPDATER_INTERVAL), 0, RELATIV_TIMER);
if (pv_config_get_wdt_mode() <= WDT_STARTUP)
pv_wdt_stop();
next_state = PV_STATE_WAIT;
out:
if (json)
free(json);
return next_state;
}
static pv_state_t pv_wait_unclaimed(struct pantavisor *pv)
{
int need_register = 1;
char *c = NULL;
char path[PATH_MAX];
struct timer_state tstate =
timer_current_state(&timer_updater_interval);
if (!tstate.fin)
return PV_STATE_WAIT;
if (!pv->state->done) {
WARN_ONCE(
"will not allow claiming if not running a DONE revision");
return PV_STATE_WAIT;
}
timer_start(&timer_updater_interval,
pv_config_get_int(PH_UPDATER_INTERVAL), 0, RELATIV_TIMER);
pv_config_load_unclaimed_creds();
const char *id = pv_config_get_str(PH_CREDS_ID);
if (id && strcmp(id, "") && pv_ph_device_exists(pv))
need_register = 0;
if (need_register) {
pv_metadata_add_devmeta(DEVMETA_KEY_PH_STATE,
ph_state_string(PH_STATE_REGISTER));
if (!pv_ph_register_self(pv)) {
pv_ph_release_client(pv);
return PV_STATE_WAIT;
}
pv_config_save_creds();
pv_ph_release_client(pv);
}
if (!pv_ph_device_is_owned(pv, &c)) {
pv_metadata_add_devmeta(DEVMETA_KEY_PH_STATE,
ph_state_string(PH_STATE_CLAIM));
pv_log(INFO, "device challenge: '%s'", c);
pv_ph_update_hint_file(pv, c);
} else {
pv_metadata_add_devmeta(DEVMETA_KEY_PH_STATE,
ph_state_string(PH_STATE_SYNC));
pv_log(INFO, "device has been claimed, proceeding normally");
pv->unclaimed = false;
pv_config_save_creds();
pv_ph_release_client(pv);
pv_paths_pv_file(path, PATH_MAX, CHALLENGE_FNAME);
if (pv_fs_file_save(path, "", 0444) < 0)
pv_log(WARN, "could not save file %s: %s", path,
strerror(errno));
pv_metadata_add_devmeta("pantahub.claimed", "1");
}
pv_ph_update_hint_file(pv, NULL);
if (c)
free(c);
return PV_STATE_WAIT;
}
static int pv_meta_update_to_ph(struct pantavisor *pv)
{
struct timer_state tstate;
if (!pv)
return -1;
tstate = timer_current_state(&timer_usrmeta_interval);
if (tstate.fin) {
if (pv_ph_device_get_meta(pv))
return -1;
timer_start(&timer_usrmeta_interval,
pv_config_get_int(PH_METADATA_USRMETA_INTERVAL), 0,
RELATIV_TIMER);
}
tstate = timer_current_state(&timer_devmeta_interval);
if (tstate.fin) {
if (pv_metadata_upload_devmeta(pv))
return -1;
timer_start(&timer_devmeta_interval,
pv_config_get_int(PH_METADATA_DEVMETA_INTERVAL), 0,
RELATIV_TIMER);
}
return 0;
}
static pv_state_t pv_wait_update()
{
struct pantavisor *pv = pv_get_instance();
plat_goal_state_t goal_state;
// if an update is going on at this point, it means we still have to finish it
if (pv->update && pv->update->status != UPDATE_APPLIED) {
if (pv_update_is_trying(pv->update)) {
goal_state = pv_state_check_goals(pv->state);
switch (goal_state) {
case PLAT_GOAL_UNACHIEVED:
return PV_STATE_WAIT;
case PLAT_GOAL_ACHIEVED:
timer_start(&timer_commit,
pv_config_get_int(
PV_UPDATER_COMMIT_DELAY),
0, RELATIV_TIMER);
// progress update state to testing
pv_update_test(pv);
break;
case PLAT_GOAL_TIMEDOUT:
pv_log(ERROR,
"timed out before all goals were met. Rolling back...");
pv_update_set_status(pv->update,
UPDATE_STATUS_GOAL_FAILED);
return PV_STATE_ROLLBACK;
default:
pv_log(ERROR,
"could not check groups goals. Rolling back...");
return PV_STATE_ROLLBACK;
}
}
// if the update is being tested, we might have to wait
if (pv_update_is_testing(pv->update)) {
// progress if possible the state of testing update
struct timer_state tstate =
timer_current_state(&timer_commit);
if (!tstate.fin) {
pv_log(INFO,
"committing new update in %lld seconds",
(long long)tstate.sec);
return PV_STATE_WAIT;
}
}
if (pv_update_finish(pv) < 0) {
pv_log(ERROR,
"update could not be finished. Rolling back...");
return PV_STATE_ROLLBACK;
}
}
return PV_STATE_WAIT;
}
static pv_state_t pv_wait_network(struct pantavisor *pv)
{
struct timer_state tstate;
// check if we are online and authenticated
if (!pv_ph_is_auth(pv) || !pv_trail_is_auth(pv)) {
// this could mean the trying update cannot connect to ph
if (pv_update_is_trying(pv->update)) {
tstate = timer_current_state(&timer_rollback_remote);
if (tstate.fin) {
pv_log(ERROR,
"timed out before getting any response from cloud. Rolling back...");
pv_update_set_status(pv->update,
UPDATE_HUB_NOT_REACHABLE);
return PV_STATE_ROLLBACK;
}
pv_log(WARN,
"no connection. Will rollback in %d seconds",
tstate.sec);
// or we directly rollback is connection is not stable during testing
} else if (pv_update_is_testing(pv->update)) {
pv_log(ERROR,
"connection with cloud not stable during testing, Rolling back...");
pv_update_set_status(pv->update, UPDATE_HUB_NOT_STABLE);
return PV_STATE_ROLLBACK;
}
// if there is no connection and no rollback yet, we avoid the rest of network operations
return PV_STATE_WAIT;
}
// start or stop ph logger depending on network and configuration
ph_logger_toggle(pv->state->rev);
if (pv_meta_update_to_ph(pv))
goto out;
// check for new remote update
tstate = timer_current_state(&timer_updater_interval);
if (tstate.fin) {
if (pv_updater_check_for_updates(pv) > 0) {
pv_metadata_add_devmeta(
DEVMETA_KEY_PH_STATE,
ph_state_string(PH_STATE_UPDATE));
return PV_STATE_UPDATE;
}
timer_start(&timer_updater_interval,
pv_config_get_int(PH_UPDATER_INTERVAL), 0,
RELATIV_TIMER);
}
if (pv->synced)
pv_metadata_add_devmeta(DEVMETA_KEY_PH_STATE,
ph_state_string(PH_STATE_IDLE));
out:
// process ongoing updates, if any
return pv_wait_update();
}
static pv_state_t _pv_wait(struct pantavisor *pv)
{
struct timer t;
struct timer_state tstate;
pv_state_t next_state = PV_STATE_WAIT;
// check if any platform has exited and we need to tear down
if (pv_state_run(pv->state)) {
pv_log(ERROR,
"a platform did not work as expected. Tearing down...");
if (pv_update_is_trying(pv->update) ||
pv_update_is_testing(pv->update)) {
pv_update_set_status(pv->update,
UPDATE_CONTAINER_FAILED);
next_state = PV_STATE_ROLLBACK;
} else
next_state = PV_STATE_REBOOT;
goto out;
}
// we only get into network operations if remote mode is set to 1 in config (can be unset if revision is "locals/...")
// also, in case device is unclaimed, the current update must finish first (this is specially done for rev 0 that comes from command make-factory)
if (pv->remote_mode &&
(!pv->unclaimed ||
(pv->unclaimed &&
!(pv->update && pv->update->status != UPDATE_APPLIED)))) {
timer_start(&t, 5, 0, RELATIV_TIMER);
// with this wait, we make sure we have not consecutively executed network stuff
// twice in less than the configured interval
if (pv_wait_delay_timedout()) {
// check if device is unclaimed
if (pv->unclaimed) {
// unclaimed wait operations
next_state = pv_wait_unclaimed(pv);
} else {
// rest of network wait stuff: connectivity check. update management,
// meta data uppload, ph logger push start...
next_state = pv_wait_network(pv);
}
}
tstate = timer_current_state(&t);
if (tstate.fin)
pv_log(DEBUG,
"network operations are taking %d seconds!",
5 + tstate.sec);
} else {
// process ongoing updates, if any
next_state = pv_wait_update();
}
if (next_state != PV_STATE_WAIT)
goto out;
// update network info in devmeta
pv_network_update_meta(pv);
// check if we need to run garbage collector
pv_storage_gc_run_threshold();
// check state of debug tools
pv_debug_check_ssh_running();
// receive new command. Set 2 secs as the select max blocking time, so we can do the
// rest of WAIT operations
pv->cmd = pv_ctrl_socket_wait(pv->ctrl_fd, 2);
if (pv->cmd)
next_state = PV_STATE_COMMAND;
out:
return next_state;
}
static pv_state_t _pv_command(struct pantavisor *pv)
{
struct pv_cmd *cmd = pv->cmd;
pv_state_t next_state = PV_STATE_WAIT;
char *rev;
if (!cmd)
return PV_STATE_WAIT;
switch (cmd->op) {
case CMD_UPDATE_METADATA:
if (pv->remote_mode) {
pv_log(DEBUG,
"metadata command with payload '%s' received. Parsing metadata...",
cmd->payload);
pv_metadata_parse_devmeta(cmd->payload);
}
break;
case CMD_REBOOT_DEVICE:
if (pv->update && pv->update->status != UPDATE_APPLIED) {
pv_log(WARN,
"ignoring reboot command because an update is in progress");
goto out;
} else if (pv->update && pv->update->status == UPDATE_APPLIED) {
pv_log(INFO,
"aborting current applied update to allow new reboot request to proceed");
pv_update_finish(pv);
}
pv_log(DEBUG,
"reboot command with message '%s' received. Rebooting...",
cmd->payload);
next_state = PV_STATE_REBOOT;
break;
case CMD_POWEROFF_DEVICE:
if (pv->update) {
pv_log(WARN,
"ignoring poweroff command because an update is in progress");
goto out;
}
pv_log(DEBUG,
"poweroff command with message '%s' received. Powering off...",
cmd->payload);
next_state = PV_STATE_POWEROFF;
break;
case CMD_LOCAL_RUN:
if (pv->update && pv->update->status != UPDATE_APPLIED) {
pv_log(WARN,
"ignoring install local command because an update is in progress");
goto out;
} else if (pv->update && pv->update->status == UPDATE_APPLIED) {
pv_log(INFO,
"aborting current applied update to allow new update request to proceed");
pv_update_finish(pv);
}
pv_log(DEBUG, "install local received. Processing %s json...",
cmd->payload);
pv->update = pv_update_get_step_local(cmd->payload);
if (pv->update)
next_state = PV_STATE_UPDATE;
break;
case CMD_LOCAL_APPLY:
if (pv->update && pv->update->status != UPDATE_APPLIED) {
pv_log(WARN,
"ignoring applying local command because an update is in progress");
goto out;
} else if (pv->update && pv->update->status == UPDATE_APPLIED) {
pv_log(INFO,
"aborting current applied update to allow new apply request to proceed");
pv_update_finish(pv);
}
pv_log(DEBUG, "apply local received. Processing %s json...",
cmd->payload);
pv->update = pv_update_get_step_local(cmd->payload);
if (pv->update)
next_state = PV_STATE_UPDATE_APPLY;
break;
case CMD_MAKE_FACTORY:
if (!pv->unclaimed) {
pv_log(WARN,
"ignoring make factory command because device is already claimed");
goto out;
}
if (pv->update && pv->update->status != UPDATE_APPLIED) {
pv_log(WARN,
"ignoring make factory command because an update is in progress");
goto out;
} else if (pv->update && pv->update->status == UPDATE_APPLIED) {
pv_log(INFO,
"aborting current applied update to allow new make factory request to proceed");
pv_update_finish(pv);
}
if (strlen(cmd->payload) > 0)
rev = cmd->payload;
else
rev = pv->state->rev;
pv_log(DEBUG,
"make factory received. Transferring revision %s to remote revision 0",
rev);
if (pv_storage_update_factory(rev) < 0) {
pv_log(ERROR, "cannot update factory revision");
goto out;
}
pv_log(INFO, "revision 0 updated. Progressing to revision 0");
pv->update = pv_update_get_step_local("0");
if (pv->update)
next_state = PV_STATE_UPDATE;
break;
case CMD_RUN_GC:
pv_log(DEBUG, "run garbage collector received. Running...");
pv_storage_gc_run();
break;
case CMD_ENABLE_SSH:
pv_log(DEBUG, "enable SSH command received");
pv_config_override_value("debug.ssh", "1");
break;
case CMD_DISABLE_SSH:
pv_log(DEBUG, "disable SSH command received");
pv_config_override_value("debug.ssh", "0");
break;
case CMD_GO_REMOTE:
pv_log(DEBUG, "go remote command received");
if (!pv_config_get_bool(PV_CONTROL_REMOTE)) {
pv_log(WARN, "remote mode is disabled by config");
goto out;
}
pv->remote_mode = true;
break;
default:
pv_log(WARN, "unknown command received. Ignoring...");
}
out:
pv_ctrl_free_cmd(pv->cmd);
pv->cmd = NULL;
return next_state;
}
static pv_state_t _pv_update_apply(struct pantavisor *pv)
{
pv_metadata_add_devmeta(DEVMETA_KEY_PH_STATE,
ph_state_string(PH_STATE_UPDATE_APPLY));
// download and install pending step
if (pv_update_download(pv) || pv_update_install(pv)) {
pv_log(ERROR, "update has failed, continue...");
pv_update_finish(pv);
return PV_STATE_WAIT;
}
pv_update_set_status(pv->update, UPDATE_APPLIED);
return PV_STATE_WAIT;
}
static pv_state_t _pv_update(struct pantavisor *pv)
{
pv_metadata_add_devmeta(DEVMETA_KEY_PH_STATE,
ph_state_string(PH_STATE_UPDATE));
// download and install pending step
if (pv_update_download(pv) || pv_update_install(pv)) {
pv_log(ERROR, "update has failed, continue...");
pv_update_finish(pv);
return PV_STATE_WAIT;
}
// after installing, try to only stop the platforms that we need for the new update
if (pv_state_stop_platforms(pv->state, pv->update->pending)) {
pv_log(INFO, "update requires reboot");
pv_update_set_status(pv->update, UPDATE_REBOOT);
return PV_STATE_REBOOT;
}
pv_log(INFO, "update does not require reboot");
pv_update_set_status(pv->update, UPDATE_TRANSITION);
return PV_STATE_RUN;
}
static pv_state_t _pv_rollback(struct pantavisor *pv)
{
pv_log(DEBUG, "%s():%d", __func__, __LINE__);
// We shouldnt get a rollback event on rev 0
if (pv->state && !strncmp(pv->state->rev, "0", sizeof("0"))) {
pv_log(ERROR, "bad factory revision");
return PV_STATE_ERROR;
}
// rollback means current update needs to be reported to PH as FAILED
if (pv->update)
pv_update_finish(pv);
return PV_STATE_REBOOT;
}
typedef enum { POWEROFF, REBOOT } shutdown_type_t;
static char *shutdown_type_string(shutdown_type_t t)
{
switch (t) {
case POWEROFF:
return "power off";
case REBOOT:
return "reboot";
default:
return "invalid shutdown type";
}
}
static int shutdown_type_reboot_cmd(shutdown_type_t t)
{
switch (t) {
case POWEROFF:
return LINUX_REBOOT_CMD_POWER_OFF;
case REBOOT:
return LINUX_REBOOT_CMD_RESTART;
default:
return LINUX_REBOOT_CMD_RESTART;
}
}
static void pv_remove(struct pantavisor *pv)
{
pv_log(DEBUG, "removing pantavisor");
if (!pv)
return;
if (pv->cmdline)
free(pv->cmdline);
if (pv->conn)
free(pv->conn);
pv_update_free(pv->update);
pv->update = NULL;
pv_state_free(pv->state);
pv->state = NULL;
pv_ctrl_free_cmd(pv->cmd);
pv_trail_remote_remove(pv);
pv_config_free();
pv_metadata_remove();
free(pv);
global_pv = NULL;
}
static pv_state_t pv_shutdown(struct pantavisor *pv, shutdown_type_t t)
{
if (!pv)
return PV_STATE_EXIT;
init_mode_t init_mode = pv_config_get_system_init_mode();
pv_log(INFO, "preparing '%s'...", shutdown_type_string(t));
if (init_mode == IM_APPENGINE)
pv_log(INFO,
"will not actually perform '%s' as we are in appengine mode",
shutdown_type_string(t));
pv_debug_wait_shell();
if ((REBOOT == t) && (pv_config_get_wdt_mode() >= WDT_SHUTDOWN))
pv_wdt_start();
// give it a final sync here...
sync();
// stop childs leniently
pv_state_stop_lenient(pv->state);
ph_logger_stop_lenient();
// force stop childs
pv_state_stop_force(pv->state);
ph_logger_stop_force();
// close pvctrl
pv_ctrl_socket_close(pv->ctrl_fd);
pv_debug_stop_ssh();
pv_logserver_stop();
pv_volumes_umount_firmware_modules();
pv_log_umount();
pv_mount_umount();
pv_metadata_umount();
pv_init_umount();
// unmount disks
pv_disk_umount_all(&pv->state->disks);
pv_storage_umount();
// free up memory
pv_bootloader_remove();
// at this point, we can shutdown if not in appengine
if (init_mode != IM_APPENGINE) {
pv_log(INFO,
"shutdown complete, performing '%s' in 2 second...",
shutdown_type_string(t));
sleep(2);
pv_remove(pv);
sync();
reboot(shutdown_type_reboot_cmd(t));
} else {
pv_log(INFO, "shutdown complete...");
pv_remove(pv);
sync();
if (t == POWEROFF)
exit(3);
}
return PV_STATE_EXIT;
}
static pv_state_t _pv_reboot(struct pantavisor *pv)
{
return pv_shutdown(pv, REBOOT);
}
static pv_state_t _pv_poweroff(struct pantavisor *pv)
{
return pv_shutdown(pv, POWEROFF);
}
static pv_state_t _pv_error(struct pantavisor *pv)
{
return PV_STATE_REBOOT;
}
pv_state_func_t *const state_table[MAX_STATES] = {
_pv_init, _pv_run, _pv_wait, _pv_command,
_pv_update, _pv_update_apply, _pv_rollback, _pv_reboot,
_pv_poweroff, _pv_error, NULL,
};
static pv_state_t _pv_run_state(pv_state_t state, struct pantavisor *pv)
{
pv_wdt_kick();
return state_table[state](pv);
}
int pv_start()
{
struct pantavisor *pv = pv_get_instance();
if (!pv)
return 1;
SNPRINTF_WTRUNC(pv_user_agent, sizeof(pv_user_agent), PV_USER_AGENT_FMT,
pv_build_arch, pv_build_version, pv_build_date);
prctl(PR_SET_NAME, "pantavisor");
struct rlimit core_limit;
core_limit.rlim_cur = RLIM_INFINITY;
core_limit.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limit);
pv_state_t state = PV_STATE_INIT;
while (1) {
pv_log(DEBUG, "going to state = %s", pv_state_string(state));
state = _pv_run_state(state, pv);
if (state == PV_STATE_EXIT)
return 1;
if (pv->hard_poweroff)
state = PV_STATE_POWEROFF;
}