forked from pantavisor/pantavisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.c
2045 lines (1748 loc) · 50.2 KB
/
updater.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-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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/statfs.h>
#include <fcntl.h>
#include <errno.h>
#include <mtd/mtd-user.h>
#include <inttypes.h>
#include <thttp.h>
#include <mbedtls/sha256.h>
#include <jsmn/jsmnutil.h>
#include "trestclient.h"
#include "updater.h"
#include "paths.h"
#include "utils/str.h"
#include "utils/fs.h"
#include "objects.h"
#include "parser/parser.h"
#include "bootloader.h"
#include "pantahub.h"
#include "storage.h"
#include "wdt.h"
#include "init.h"
#include "bootloader.h"
#include "parser/parser_bundle.h"
#include "state.h"
#include "json.h"
#include "signature.h"
#include "logserver/logserver.h"
#define MODULE_NAME "updater"
#define pv_log(level, msg, ...) vlog(MODULE_NAME, level, msg, ##__VA_ARGS__)
#include "log.h"
#define VOLATILE_TMP_OBJ_PATH "/tmp/object-XXXXXX"
#define MMC_TMP_OBJ_FMT "%s.tmp"
typedef int (*token_iter_f)(void *d1, void *d2, char *buf, jsmntok_t *tok,
int c);
void pv_update_free(struct pv_update *update)
{
if (!update)
return;
pv_log(DEBUG, "removing update");
if (update->endpoint)
free(update->endpoint);
if (update->rev)
free(update->rev);
if (update->pending) {
pv_state_free(update->pending);
update->pending = NULL;
}
free(update);
}
static void pv_update_remove(struct pantavisor *pv)
{
if (!pv->update)
return;
pv_update_free(pv->update);
pv->update = NULL;
}
// takes an allocated buffer
static char *unescape_utf8_to_apvii(char *buf, char *code, char c)
{
char *p = 0;
char *new_str = 0;
char *old;
int pos = 0, replaced = 0;
char *tmp;
size_t len = strlen(buf) + strlen(code) + 1;
tmp = malloc(strlen(buf) + strlen(code) + 1);
snprintf(tmp, len, "%s%s", buf, code);
old = tmp;
p = strstr(tmp, code);
while (p) {
*p = '\0';
new_str = realloc(new_str, pos + strlen(tmp) + 2);
snprintf(new_str + pos, strlen(tmp) + 2, "%s", tmp);
pos = pos + strlen(tmp);
new_str[pos] = c;
pos += 1;
new_str[pos] = '\0';
replaced += 1;
tmp = p + strlen(code);
p = strstr(tmp, code);
}
if (new_str && new_str[strlen(new_str) - 1] == c)
new_str[strlen(new_str) - 1] = '\0';
if (old)
free(old);
if (buf)
free(buf);
return new_str;
}
static int trail_remote_init(struct pantavisor *pv)
{
struct trail_remote *remote = NULL;
trest_auth_status_enum status = TREST_AUTH_STATUS_NOTAUTH;
trest_ptr client = 0;
char *endpoint_trail = NULL;
int size = -1;
const char *id = pv_config_get_str(PH_CREDS_ID);
if (pv->remote || !id)
return 0;
client = pv_get_trest_client(pv, NULL);
if (!client) {
pv_log(INFO, "unable to create device client");
goto err;
}
status = trest_update_auth(client);
if (status != TREST_AUTH_STATUS_OK) {
pv_log(INFO, "unable to auth device client");
goto err;
}
remote = calloc(1, sizeof(struct trail_remote));
remote->client = client;
size = sizeof(DEVICE_TRAIL_ENDPOINT_FMT) + strlen(id);
endpoint_trail = malloc(size * sizeof(char));
if (!endpoint_trail)
goto err;
SNPRINTF_WTRUNC(endpoint_trail, size, DEVICE_TRAIL_ENDPOINT_FMT, id);
size = strlen(endpoint_trail) + sizeof(DEVICE_TRAIL_ENDPOINT_PENDING);
remote->endpoint_trail_pending = calloc(size, sizeof(char));
if (!remote->endpoint_trail_pending)
goto err;
SNPRINTF_WTRUNC(remote->endpoint_trail_pending, size, "%s%s",
endpoint_trail, DEVICE_TRAIL_ENDPOINT_PENDING);
size = strlen(endpoint_trail) + sizeof(DEVICE_TRAIL_ENDPOINT_NEW);
remote->endpoint_trail_new = calloc(size, sizeof(char));
if (!remote->endpoint_trail_new)
goto err;
SNPRINTF_WTRUNC(remote->endpoint_trail_new, size, "%s%s",
endpoint_trail, DEVICE_TRAIL_ENDPOINT_NEW);
pv->remote = remote;
free(endpoint_trail);
return 0;
err:
if (client)
free(client);
if (remote)
free(remote);
if (endpoint_trail)
free(endpoint_trail);
return -1;
}
static int pv_update_send_progress(struct pv_update *update, char *json)
{
struct pantavisor *pv = pv_get_instance();
if ((update->pending && update->pending->local) ||
!pv_get_instance()->remote_mode || !pv->online ||
trail_remote_init(pv))
return 0;
trest_request_ptr req = NULL;
trest_response_ptr res = NULL;
int ret = -1;
req = trest_make_request(THTTP_METHOD_PUT, update->endpoint, json);
res = trest_do_json_request(pv->remote->client, req);
if (!res) {
pv_log(WARN, "HTTP request PUT %s could not be initialized",
update->endpoint);
} else if (!res->code && res->status != TREST_AUTH_STATUS_OK) {
pv_log(WARN, "HTTP request PUT %s could not auth (status=%d)",
update->endpoint, res->status);
} else if (res->code != THTTP_STATUS_OK) {
pv_log(WARN,
"HTTP request PUT %s returned error (code=%d; body='%s')",
update->endpoint, res->code, res->body);
} else {
pv_log(DEBUG, "remote state updated to %s", res->body);
ret = 0;
}
if (req)
trest_request_free(req);
if (res)
trest_response_free(res);
return ret;
}
struct pv_update_progress {
char status[UPDATE_PROGRESS_STATUS_SIZE];
char msg[UPDATE_PROGRESS_STATUS_MSG_SIZE];
char data[UPDATE_PROGRESS_DATA_SIZE];
char *logs;
struct download_info *total;
unsigned int progress;
};
static void pv_update_free_progress(struct pv_update_progress *progress)
{
if (progress->logs)
free(progress->logs);
}
static void pv_update_fill_progress(struct pv_update_progress *progress,
struct pv_update *update)
{
struct pv_update_progress *p = progress;
struct pv_update *u = update;
SNPRINTF_WTRUNC(p->data, sizeof(p->data), "%d", u->retries);
int revision_retries = pv_config_get_int(PV_REVISION_RETRIES);
switch (update->status) {
case UPDATE_QUEUED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "QUEUED");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Retried %d of %d",
u->retries, revision_retries);
p->progress = 0;
break;
case UPDATE_DOWNLOADED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "INPROGRESS");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"Update objects downloaded");
p->progress = 25;
break;
case UPDATE_APPLIED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "INPROGRESS");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Update applied");
p->progress = 50;
break;
case UPDATE_INSTALLED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "INPROGRESS");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Update installed");
p->progress = 50;
break;
case UPDATE_TRY:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "INPROGRESS");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"Starting updated version");
p->progress = 75;
break;
case UPDATE_TRANSITION:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "INPROGRESS");
SNPRINTF_WTRUNC(
p->msg, sizeof(p->msg),
"Transitioning to new revision without rebooting");
p->progress = 50;
break;
case UPDATE_REBOOT:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "INPROGRESS");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Rebooting");
p->progress = 50;
break;
case UPDATE_UPDATED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "UPDATED");
SNPRINTF_WTRUNC(
p->msg, sizeof(p->msg),
"Update finished, revision not set as rollback point");
p->progress = 100;
break;
case UPDATE_DONE:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "DONE");
SNPRINTF_WTRUNC(
p->msg, sizeof(p->msg),
"Update finished, revision set as rollback point");
p->progress = 100;
break;
case UPDATE_ABORTED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "WONTGO");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Update aborted");
p->progress = 100;
break;
case UPDATE_NO_DOWNLOAD:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "WONTGO");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"Max download retries reached");
p->progress = 100;
break;
case UPDATE_NO_SPACE:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "WONTGO");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "%s", u->msg);
p->progress = 100;
break;
case UPDATE_BAD_SIGNATURE:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "WONTGO");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "%s", u->msg);
p->progress = 100;
break;
case UPDATE_NO_PARSE:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "WONTGO");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"State JSON has bad format");
p->progress = 100;
break;
case UPDATE_SIGNATURE_FAILED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "ERROR");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "%s", u->msg);
p->progress = 100;
break;
case UPDATE_BAD_CHECKSUM:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "ERROR");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"Object validation went wrong");
p->progress = 100;
break;
case UPDATE_HUB_NOT_REACHABLE:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "ERROR");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Hub not reachable");
p->progress = 100;
break;
case UPDATE_HUB_NOT_STABLE:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "ERROR");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"Hub communication not stable");
p->progress = 100;
break;
case UPDATE_STALE_REVISION:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "ERROR");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Stale revision");
p->progress = 100;
break;
case UPDATE_STATUS_GOAL_FAILED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "ERROR");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"Status goal not reached");
p->progress = 100;
break;
case UPDATE_CONTAINER_FAILED:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "ERROR");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"A container could not be started");
p->progress = 100;
break;
case UPDATE_RETRY_DOWNLOAD:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "QUEUED");
SNPRINTF_WTRUNC(
p->msg, sizeof(p->msg),
"Network unavailable while downloading, retry %d of %d",
update->retries, revision_retries);
p->progress = 0;
break;
case UPDATE_TESTING_REBOOT:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "TESTING");
SNPRINTF_WTRUNC(
p->msg, sizeof(p->msg),
"Awaiting to set rollback point if update is stable");
p->progress = 75;
break;
case UPDATE_TESTING_NONREBOOT:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "TESTING");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg),
"Awaiting to see if update is stable");
p->progress = 75;
break;
case UPDATE_DOWNLOAD_PROGRESS:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "DOWNLOADING");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Retry %d of %d",
update->retries, revision_retries);
p->progress = 0;
p->total = &update->total;
break;
default:
SNPRINTF_WTRUNC(p->status, sizeof(p->status), "ERROR");
SNPRINTF_WTRUNC(p->msg, sizeof(p->msg), "Internal error");
p->progress = 100;
break;
}
char path[PATH_MAX];
pv_paths_storage_trail_pv_file(path, PATH_MAX, u->rev, LOGS_FNAME);
p->logs = pv_fs_file_load(path, UPDATE_PROGRESS_LOGS_SIZE);
}
static char *pv_update_get_progress_json(struct pv_update_progress *progress)
{
struct pv_json_ser js;
pv_json_ser_init(&js, UPDATE_PROGRESS_JSON_SIZE);
pv_json_ser_object(&js);
{
pv_json_ser_key(&js, "status");
pv_json_ser_string(&js, progress->status);
pv_json_ser_key(&js, "status-msg");
pv_json_ser_string(&js, progress->msg);
pv_json_ser_key(&js, "progress");
pv_json_ser_number(&js, progress->progress);
if (strlen(progress->data) > 0) {
pv_json_ser_key(&js, "data");
pv_json_ser_string(&js, progress->data);
}
if (progress->total) {
pv_json_ser_key(&js, "downloads");
pv_json_ser_object(&js);
pv_json_ser_key(&js, "total");
pv_json_ser_object(&js);
{
pv_json_ser_key(&js, "object_name");
pv_json_ser_string(&js, "total");
pv_json_ser_key(&js, "object_id");
pv_json_ser_string(&js, "none");
pv_json_ser_key(&js, "total_size");
pv_json_ser_number(&js,
progress->total->total_size);
pv_json_ser_key(&js, "start_time");
pv_json_ser_number(&js,
progress->total->start_time);
pv_json_ser_key(&js, "current_time");
pv_json_ser_number(
&js, progress->total->current_time);
pv_json_ser_key(&js, "total_downloaded");
pv_json_ser_number(
&js, progress->total->total_downloaded);
pv_json_ser_object_pop(&js);
}
pv_json_ser_key(&js, "objects");
pv_json_ser_array(&js);
{
pv_json_ser_array_pop(&js);
}
pv_json_ser_object_pop(&js);
}
if (progress->logs) {
pv_json_ser_key(&js, "logs");
pv_json_ser_string(&js, progress->logs);
}
pv_json_ser_object_pop(&js);
}
return pv_json_ser_str(&js);
}
static int pv_update_report_progress(struct pv_update *update)
{
// prepare update progress struct
struct pv_update_progress progress;
memset(&progress, 0, sizeof(progress));
pv_update_fill_progress(&progress, update);
// serialize update progress json
char *json = NULL;
json = pv_update_get_progress_json(&progress);
pv_update_free_progress(&progress);
// store progress in trails
pv_storage_set_rev_progress(update->rev, json);
// send progress to hub
int ret = 0;
ret = pv_update_send_progress(update, json);
if (json)
free(json);
return ret;
}
static int trail_get_steps_response(struct pantavisor *pv, char *endpoint,
trest_response_ptr *response)
{
trest_request_ptr req = NULL;
trest_response_ptr res = NULL;
struct trail_remote *remote = NULL;
int ret = -1, size = 0;
if (!endpoint || !pv)
goto out;
remote = pv->remote;
req = trest_make_request(THTTP_METHOD_GET, endpoint, 0);
res = trest_do_json_request(remote->client, req);
if (!res) {
pv_log(WARN, "HTTP request GET %s could not be initialized",
endpoint);
} else if (!res->code && res->status != TREST_AUTH_STATUS_OK) {
pv_log(WARN, "HTTP request GET %s could not auth (status=%d)",
endpoint, res->status);
} else if (res->code != THTTP_STATUS_OK) {
pv_log(WARN,
"HTTP request GET %s returned error (code=%d; body='%s')",
endpoint, res->code, res->body);
} else {
size = jsmnutil_array_count(res->body, res->json_tokv);
if (!size) {
ret = 0;
goto out;
}
pv_log(DEBUG, "%d steps found", size);
trest_request_free(req);
*response = res;
return 1;
}
out:
if (req)
trest_request_free(req);
if (res)
trest_response_free(res);
return ret;
}
/*
* the "data" field is structured as,
* "data" : "value"
* }
*/
struct jka_update_ctx {
int *retries;
};
static int do_progress_action(struct json_key_action *jka, char *value)
{
char *retry_count = NULL;
struct jka_update_ctx *ctx = (struct jka_update_ctx *)jka->opaque;
struct json_key_action jka_arr[] = {
ADD_JKA_ENTRY("data", JSMN_STRING, &retry_count, NULL, true),
ADD_JKA_NULL_ENTRY()
};
int ret = __start_json_parsing_with_action(
jka->buf, jka_arr, JSMN_OBJECT, jka->tokv, jka->tokc);
if (!ret) {
if (retry_count) {
pv_log(DEBUG, "retry_count = %s", retry_count);
sscanf(retry_count, "%d", ctx->retries);
free(retry_count);
}
}
return ret;
}
static struct pv_update *pv_update_new(const char *id, const char *rev,
bool local)
{
struct pv_update *u;
int size;
if (!rev)
return NULL;
u = calloc(1, sizeof(struct pv_update));
if (u) {
u->progress_size = PATH_MAX;
u->status = UPDATE_INIT;
u->rev = strdup(rev);
u->retries = 0;
u->local = local;
if (!id) {
u->local = true;
goto out;
}
size = sizeof(DEVICE_STEP_ENDPOINT_FMT) + strlen(id) +
strlen(rev);
u->endpoint = malloc(sizeof(char) * size);
SNPRINTF_WTRUNC(u->endpoint, size, DEVICE_STEP_ENDPOINT_FMT, id,
rev);
}
out:
return u;
}
static int pv_update_signature_verify(struct pv_update *update,
const char *state)
{
int ret = -1;
sign_state_res_t sres;
sres = pv_signature_verify(state);
if (sres != SIGN_STATE_OK) {
pv_log(WARN, "invalid state signature with result %d", sres);
pv_update_set_status_msg(update, UPDATE_BAD_SIGNATURE,
pv_signature_sign_state_str(sres));
goto out;
}
ret = 0;
out:
return ret;
}
void pv_update_set_status_msg(struct pv_update *update,
enum update_status status, const char *msg)
{
if (!update) {
pv_log(WARN, "uninitialized update");
return;
}
// in case we do not have new information, we get out
if ((update->status == status) && !msg)
return;
update->status = status;
SNPRINTF_WTRUNC(update->msg, sizeof(update->msg), "%s", msg);
// in this case, we don't want to overwrite the ERROR progress information
if (status == UPDATE_ROLLEDBACK)
return;
pv_update_report_progress(update);
}
void pv_update_set_status(struct pv_update *update, enum update_status status)
{
pv_update_set_status_msg(update, status, NULL);
}
void pv_update_set_factory_status()
{
struct pantavisor *pv = pv_get_instance();
if (strncmp(pv->state->rev, "0", sizeof("0")))
return;
struct pv_update_progress p;
memset(&p, 0, sizeof(p));
SNPRINTF_WTRUNC(p.status, sizeof(p.status), "DONE");
SNPRINTF_WTRUNC(p.msg, sizeof(p.msg), "Factory revision");
p.progress = 100;
// serialize update progress json
char *json = NULL;
json = pv_update_get_progress_json(&p);
// store progress in trails
pv_storage_set_rev_progress("0", json);
if (json)
free(json);
pv_storage_set_rev_done("0");
}
static int pv_update_refresh_progress(struct pv_update *update)
{
int ret = -1;
if (!update)
return ret;
char *json;
json = pv_storage_get_rev_progress(update->rev);
if (!json)
return ret;
pv_log(DEBUG,
"rev '%s' already existed in this device with progress '%s'",
update->rev, json);
ret = pv_update_send_progress(update, json);
free(json);
return ret;
}
static int trail_get_new_steps(struct pantavisor *pv)
{
bool wrong_revision = false;
int ret = 0;
char *state = 0, *rev = 0;
struct trail_remote *remote = pv->remote;
trest_response_ptr res = NULL;
jsmntok_t *tokv = 0;
int retries = 0;
struct jka_update_ctx update_ctx = { .retries = &retries };
struct json_key_action jka[] = {
ADD_JKA_ENTRY("progress", JSMN_OBJECT, &update_ctx,
do_progress_action, false),
ADD_JKA_NULL_ENTRY()
};
struct pv_update *update;
if (!remote)
return 0;
// if update is going on, just check for NEW updates so test json can be parsed
if (pv->update && pv->update->status != UPDATE_APPLIED) {
// check for NEW updates
ret = trail_get_steps_response(pv, remote->endpoint_trail_new,
&res);
} else {
// check for NEW, DOWNLOAD INPROGRESS or QUEUED updates
ret = trail_get_steps_response(
pv, remote->endpoint_trail_pending, &res);
}
if (ret > 0)
pv_log(DEBUG,
"found pending revision") else if (ret < 0) goto out;
ret = 0;
// if we have no response, we go out normally
if (!res)
goto out;
// parse revision id
rev = pv_json_get_value(res->body, "rev", res->json_tokv,
res->json_tokc);
// this could mean either the server is returning a malformed response or json parser is not working properly
if (!rev) {
pv_log(ERROR,
"rev not found in endpoint response, ignoring...");
goto out;
}
pv_logserver_start_update(rev);
pv_log(DEBUG, "parse rev %s...", rev);
// create temp update to be able to report the revision state
update = pv_update_new(pv_config_get_str(PH_CREDS_ID), rev, false);
if (!update)
goto out;
if (atoi(rev) <= atoi(pv->state->rev)) {
pv_log(WARN, "stale rev %s found on remote", rev);
pv_update_set_status(update, UPDATE_STALE_REVISION);
wrong_revision = true;
goto send_feedback;
}
// get raw revision state and parse retry
state = pv_json_get_value(res->body, "state", res->json_tokv,
res->json_tokc);
if (start_json_parsing_with_action(res->body, jka, JSMN_ARRAY) ||
!state) {
pv_log(WARN, "failed to parse the rest of the response");
pv_update_set_status(update, UPDATE_NO_PARSE);
pv_update_free(update);
goto out;
}
send_feedback:
if (pv_update_refresh_progress(update))
pv_log(DEBUG, "could not refresh progress from rev %s", rev);
if (wrong_revision) {
pv_log(WARN, "stale revision found. Aborting update...");
pv_update_free(update);
goto out;
}
// report revision retry max reached
if (retries > pv_config_get_int(PV_REVISION_RETRIES)) {
pv_log(WARN, "max retries reached in rev %s", rev);
pv_update_set_status(update, UPDATE_NO_DOWNLOAD);
pv_update_free(update);
goto out;
}
// retry number recovered from endpoint response
update->retries = retries;
// if everything went well until this point, put revision to queue
pv_update_set_status(update, UPDATE_QUEUED);
// parse state
if (pv_update_signature_verify(update, state)) {
pv_update_free(update);
goto out;
}
update->pending = pv_parser_get_state(state, rev);
if (!update->pending) {
pv_log(WARN, "invalid state from rev %s", rev);
pv_update_set_status(update, UPDATE_NO_PARSE);
pv_update_free(update);
goto out;
}
// make sure target directories exist
char path[PATH_MAX];
pv_paths_storage_trail_pvr_file(path, PATH_MAX, update->rev, "");
pv_fs_mkdir_p(path, 0755);
// install state.json for new rev
pv_paths_storage_trail_pvr_file(path, PATH_MAX, rev, JSON_FNAME);
if (pv_fs_file_save(path, state, 0644) < 0)
pv_log(ERROR, "could not save %s: %s", path, strerror(errno));
// if an applied update is staged, reset it
if (pv->update && pv->update->status == UPDATE_APPLIED)
pv_update_finish(pv);
// set newly processed update if no update is going on
if (!pv->update) {
pv->update = update;
ret = 1;
} else
pv_update_free(update);
out:
if (rev)
free(rev);
if (state)
free(state);
if (tokv)
free(tokv);
if (res)
trest_response_free(res);
return ret;
}
static int trail_is_available(struct trail_remote *r)
{
trest_request_ptr req;
trest_response_ptr res;
int size = -1;
if (!r)
return size;
req = trest_make_request(THTTP_METHOD_GET, "/trails/", 0);
res = trest_do_json_request(r->client, req);
if (!res) {
pv_log(WARN, "GET /trails/ could not be initialized");
} else if (!res->code && res->status != TREST_AUTH_STATUS_OK) {
pv_log(WARN, "GET /trails/ could not auth (status=%d)",
res->status);
} else if (res->code != THTTP_STATUS_OK) {
pv_log(WARN, "GET /trails/ returned error (code=%d; body='%s')",
res->code, res->body);
} else {
size = jsmnutil_array_count(res->body, res->json_tokv);
if (size)
pv_log(DEBUG, "trail found, using remote");
}
if (req)
trest_request_free(req);
if (res)
trest_response_free(res);
return size;
}
static void __trail_log_resp_err(char *buf, jsmntok_t *tokv, int tokc)
{
char *error = NULL, *msg = NULL, *__code = NULL;
int code = 0;
/*
* Error response looks like
* {
* "error": <String>
* "msg": <String> <Maybe empty>
* "code": <int> <Maybe empty>
* }
*/
error = pv_json_get_value(buf, "error", tokv, tokc);
msg = pv_json_get_value(buf, "msg", tokv, tokc);
__code = pv_json_get_value(buf, "code", tokv, tokc);
if (__code) {
sscanf(__code, "%d", &code);
free(__code);
__code = NULL;
}
if (error && msg) {
pv_log(WARN, "Error %s: Message %s, code = %d", error, msg,
code);
} else {
pv_log(WARN,
"Malformed Error JSON from API,"
" error:%s,msg:%s, code=%d",
(error ? error : "nil"), (msg ? msg : "nil"), code);
}
if (error)
free(error);
if (msg)
free(msg);
}
static void trail_log_thttp_err(thttp_response_t *thttp_res)
{
int tokc;
jsmntok_t *tokv = NULL;
char *buf = NULL;
if (!thttp_res || thttp_res->code == THTTP_STATUS_OK)
return;
buf = thttp_res->body;
if (!buf)
return;
if (jsmnutil_parse_json(buf, &tokv, &tokc) >= 0) {
__trail_log_resp_err(buf, tokv, tokc);
}
if (tokv)
free(tokv);
}
static void trail_log_trest_err(trest_response_ptr tres)
{
if (!tres || tres->code == THTTP_STATUS_OK)
return;
if (!tres->json_tokv)
return;
__trail_log_resp_err(tres->body, tres->json_tokv, tres->json_tokc);
}
#define SHA256_STR_SIZE ((256 / 4) + 1)
static int trail_put_object(struct pantavisor *pv, struct pv_object *o,
const char **crtfiles)
{
int ret = -1;
int fd, bytes;
int size, pos, i, str_size;
char *signed_puturl = NULL;
char sha_str[SHA256_STR_SIZE];
char body[512];
unsigned char buf[4096];
unsigned char local_sha[32];
struct stat st;
trest_request_ptr treq = 0;
trest_response_ptr tres = 0;
thttp_request_t *req = 0;
thttp_request_tls_t *tls_req = 0;
thttp_response_t *res = 0;
if (o->uploaded) {
pv_log(INFO, "object '%s' already uploaded, skipping", o->id);
return 0;
}
fd = open(o->objpath, O_RDONLY);
if (fd < 0)
return -1;
stat(o->objpath, &st);
size = st.st_size;
mbedtls_sha256_context sha256_ctx;
mbedtls_sha256_init(&sha256_ctx);
mbedtls_sha256_starts(&sha256_ctx, 0);
while ((bytes = read(fd, buf, 4096)) > 0) {
mbedtls_sha256_update(&sha256_ctx, buf, bytes);
}