forked from rauc/rauc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.c
1172 lines (958 loc) · 34.3 KB
/
install.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
#include <errno.h>
#include <fcntl.h>
#include <gio/gfiledescriptorbased.h>
#include <gio/gio.h>
#include <gio/gunixmounts.h>
#include <gio/gunixoutputstream.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "bootchooser.h"
#include "bundle.h"
#include "context.h"
#include "install.h"
#include "manifest.h"
#include "mark.h"
#include "mount.h"
#include "service.h"
#include "signature.h"
#include "update_handler.h"
#include "utils.h"
/* All exit codes of hook script above this mean 'rejected' */
#define INSTALL_HOOK_REJECT_CODE 10
#define R_INSTALL_ERROR r_install_error_quark()
GQuark r_install_error_quark(void)
{
return g_quark_from_static_string("r_install_error_quark");
}
static void install_args_update(RaucInstallArgs *args, const gchar *msg)
{
g_mutex_lock(&args->status_mutex);
g_queue_push_tail(&args->status_messages, g_strdup(msg));
g_mutex_unlock(&args->status_mutex);
g_main_context_invoke(NULL, args->notify, args);
}
static gchar *resolve_loop_device(const gchar *devicepath)
{
g_autofree gchar *devicename = NULL;
g_autofree gchar *syspath = NULL;
gchar *content = NULL;
GError *ierror = NULL;
if (!g_str_has_prefix(devicepath, "/dev/loop"))
return g_strdup(devicepath);
devicename = g_path_get_basename(devicepath);
syspath = g_build_filename("/sys/block", devicename, "loop/backing_file", NULL);
content = read_file_str(syspath, &ierror);
if (!content) {
g_message("%s", ierror->message);
g_clear_error(&ierror);
return NULL;
}
/* g_strchomp modifies the string and returns it */
return g_strchomp(content);
}
gboolean determine_slot_states(GError **error)
{
GList *slotlist = NULL;
GList *mountlist = NULL;
RaucSlot *booted = NULL;
GHashTableIter iter;
RaucSlot *slot;
gboolean res = FALSE;
g_assert_nonnull(r_context()->config);
if (r_context()->config->slots == NULL) {
g_set_error_literal(
error,
R_SLOT_ERROR,
R_SLOT_ERROR_NO_CONFIG,
"No slot configuration found");
goto out;
}
g_assert_nonnull(r_context()->config->slots);
/* Clear all previously detected external mount points as we will
* re-deterrmine them. */
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer*) &slot)) {
g_clear_pointer(&slot->ext_mount_point, g_free);
}
/* Determine active slot mount points */
mountlist = g_unix_mounts_get(NULL);
for (GList *l = mountlist; l != NULL; l = l->next) {
GUnixMountEntry *m = (GUnixMountEntry*)l->data;
g_autofree gchar *devicepath = resolve_loop_device(g_unix_mount_get_device_path(m));
RaucSlot *s = find_config_slot_by_device(r_context()->config,
devicepath);
if (s) {
/* We might have multiple mount entries matching the same device and thus the same slot.
* To avoid leaking the string returned by g_unix_mount_get_mount_path() here, we skip all further matches
*/
if (s->ext_mount_point) {
break;
}
s->ext_mount_point = g_strdup(g_unix_mount_get_mount_path(m));
g_debug("Found external mountpoint for slot %s at %s", s->name, s->ext_mount_point);
}
}
g_list_free_full(mountlist, (GDestroyNotify)g_unix_mount_free);
if (r_context()->bootslot == NULL) {
g_set_error_literal(
error,
R_SLOT_ERROR,
R_SLOT_ERROR_NO_BOOTSLOT,
"Bootname or device of booted slot not found");
goto out;
}
slotlist = g_hash_table_get_keys(r_context()->config->slots);
for (GList *l = slotlist; l != NULL; l = l->next) {
g_autofree gchar *realdev = NULL;
RaucSlot *s = g_hash_table_lookup(r_context()->config->slots, l->data);
g_assert_nonnull(s);
if (g_strcmp0(s->bootname, r_context()->bootslot) == 0) {
booted = s;
break;
}
if (g_strcmp0(s->name, r_context()->bootslot) == 0) {
booted = s;
break;
}
realdev = r_realpath(s->device);
if (realdev == NULL) {
g_message("Failed to resolve realpath for '%s'", s->device);
realdev = g_strdup(s->device);
}
if (g_strcmp0(realdev, r_context()->bootslot) == 0) {
booted = s;
break;
}
}
/* Determine active group members */
for (GList *l = slotlist; l != NULL; l = l->next) {
RaucSlot *s = g_hash_table_lookup(r_context()->config->slots, l->data);
g_assert_nonnull(s);
if (s == booted) {
s->state = ST_BOOTED;
g_debug("Found booted slot: %s on %s", s->name, s->device);
} else if (s->parent && s->parent == booted) {
s->state = ST_ACTIVE;
} else {
s->state = ST_INACTIVE;
}
}
if (!booted) {
if (g_strcmp0(r_context()->bootslot, "/dev/nfs") == 0) {
g_message("Detected nfs boot, ignoring missing active slot");
res = TRUE;
goto out;
}
if (g_strcmp0(r_context()->bootslot, "_external_") == 0) {
g_message("Detected explicit external boot, ignoring missing active slot");
res = TRUE;
goto out;
}
g_set_error_literal(
error,
R_SLOT_ERROR,
R_SLOT_ERROR_NO_SLOT_WITH_STATE_BOOTED,
"Did not find booted slot");
goto out;
}
res = TRUE;
out:
g_clear_pointer(&slotlist, g_list_free);
return res;
}
gboolean determine_boot_states(GError **error)
{
GHashTableIter iter;
RaucSlot *slot;
gchar *name;
GError *ierror = NULL;
/* get boot state */
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, (gpointer*) &name, (gpointer*) &slot)) {
if (slot->bootname && !r_boot_get_state(slot, &slot->boot_good, &ierror)) {
g_propagate_error(error, ierror);
return FALSE;
}
}
return TRUE;
}
/* Returns NULL-teminated intern string array of all classes listed in
* given manifest.
* Free with g_free */
static gchar** get_all_manifest_slot_classes(const RaucManifest *manifest)
{
GPtrArray *slotclasses = NULL;
g_return_val_if_fail(manifest, NULL);
slotclasses = g_ptr_array_new();
for (GList *l = manifest->images; l != NULL; l = l->next) {
const gchar *key = NULL;
RaucImage *iterimage = l->data;
g_assert_nonnull(iterimage->slotclass);
key = g_intern_string(iterimage->slotclass);
g_ptr_array_remove_fast(slotclasses, (gpointer)key); /* avoid duplicates */
g_ptr_array_add(slotclasses, (gpointer)key);
}
g_ptr_array_add(slotclasses, NULL);
return (gchar**) g_ptr_array_free(slotclasses, FALSE);
}
/* Selects a single appropriate inactive slot of root slot class
*
* Note: This function may be extended to be more sophisticated or follow a
* certain policy for selecting an appropriate slot!
*
* @param rootclass name of root slot class
*
* @return pointer to appropriate slot in system slot list
*/
static RaucSlot *select_inactive_slot_class_member(const gchar *rootclass)
{
RaucSlot *iterslot;
GHashTableIter iter;
g_return_val_if_fail(rootclass, NULL);
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer*) &iterslot)) {
if (iterslot->state != ST_INACTIVE)
continue;
if (g_strcmp0(iterslot->sclass, rootclass) == 0) {
return iterslot;
}
}
return NULL;
}
/* Map each slot class available to a potential target slot.
*
* Algorithm:
*
* - Get all root classes (classes of slots that do not have a parent)
* - For each root class:
* - select 1 (inactive) member (function)
* -> set of selected root slots
* -> collect target install group
*
* @return Newly allocated HashTable of
* slotclass (gchar*) -> target slot (RaucSlot *)
*/
GHashTable* determine_target_install_group(void)
{
g_autofree gchar **rootclasses = NULL;
GHashTable *targetgroup = NULL;
GHashTableIter iter;
RaucSlot *iterslot = NULL;
GList *selected_root_slots = NULL;
r_context_begin_step("determine_target_install_group", "Determining target install group", 0);
/* collect all root classes available in system.conf */
rootclasses = r_slot_get_root_classes(r_context()->config->slots);
for (gchar **rootslot = rootclasses; *rootslot != NULL; rootslot++) {
RaucSlot *selected = NULL;
selected = select_inactive_slot_class_member(*rootslot);
if (selected == NULL)
continue;
selected_root_slots = g_list_append(selected_root_slots, selected);
}
targetgroup = g_hash_table_new(g_str_hash, g_str_equal);
/* Now, iterate over all slots available and add those who's parent are
* in the selected root slots */
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer*) &iterslot)) {
RaucSlot *parent = r_slot_get_parent_root(iterslot);
g_debug("Checking slot: %s", iterslot->name);
if (r_slot_list_contains(selected_root_slots, parent)) {
g_debug("\tAdding mapping: %s -> %s", iterslot->sclass, iterslot->name);
g_hash_table_insert(targetgroup, (gpointer) iterslot->sclass, iterslot);
} else {
g_debug("\tNo mapping found");
}
}
r_context_end_step("determine_target_install_group", TRUE);
return targetgroup;
}
GList* get_install_images(const RaucManifest *manifest, GHashTable *target_group, GError **error)
{
GList *install_images = NULL;
g_autofree gchar **slotclasses = NULL;
g_return_val_if_fail(manifest != NULL, NULL);
g_return_val_if_fail(target_group != NULL, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
slotclasses = get_all_manifest_slot_classes(manifest);
/* Find exactly 1 image for each class listed in manifest */
for (gchar **cls = slotclasses; *cls != NULL; cls++) {
RaucImage *matching_img = NULL;
for (GList *l = manifest->images; l != NULL; l = l->next) {
RaucImage *lookup_image = l->data;
RaucSlot *target_slot = NULL;
/* Not interested in slots of other classes */
if (g_strcmp0(lookup_image->slotclass, *cls) != 0)
continue;
/* Check if target_group contains an appropriate slot for this image */
target_slot = g_hash_table_lookup(target_group, lookup_image->slotclass);
if (!target_slot) {
g_set_error(error,
R_INSTALL_ERROR,
R_INSTALL_ERROR_FAILED,
"No target slot for class %s of image %s found", lookup_image->slotclass, lookup_image->filename);
g_clear_pointer(&install_images, g_list_free);
goto out;
}
if (target_slot->readonly) {
g_set_error(error,
R_INSTALL_ERROR,
R_INSTALL_ERROR_FAILED,
"Target slot for class %s of image %s is readonly", lookup_image->slotclass, lookup_image->filename);
g_clear_pointer(&install_images, g_list_free);
goto out;
}
/* If this is a default variant and we have no better
* match yet, use it and continue scanning.
* Otherwise test if it is our variant and directly use
* it if so */
if (lookup_image->variant == NULL) {
if (!matching_img)
matching_img = lookup_image;
} else if (g_strcmp0(lookup_image->variant, r_context()->config->system_variant) == 0) {
g_debug("Using variant %s image %s for %s", lookup_image->variant, lookup_image->filename, lookup_image->slotclass);
matching_img = lookup_image;
break;
}
}
/* If we have an image for a class in the manifest but none
* that matches our variant, we assume this to be a failure */
if (!matching_img) {
g_set_error(error,
R_INSTALL_ERROR,
R_INSTALL_ERROR_FAILED,
"Failed to find matching variant of image for %s", *cls);
g_clear_pointer(&install_images, g_list_free);
goto out;
}
g_debug("Found image mapping: %s -> %s", matching_img->filename, matching_img->slotclass);
install_images = g_list_append(install_images, matching_img);
}
if (!install_images)
g_set_error_literal(error,
R_INSTALL_ERROR,
R_INSTALL_ERROR_FAILED,
"No install image found");
out:
return install_images;
}
static gchar* parse_handler_output(gchar* line)
{
gchar *message = NULL;
g_auto(GStrv) split = NULL;
g_assert_nonnull(line);
if (!g_str_has_prefix(line, "<< ")) {
g_print("# %s\n", line);
return NULL;
}
split = g_strsplit(line, " ", 5);
if (!split[1])
return NULL;
if (g_strcmp0(split[1], "handler") == 0) {
message = g_strdup_printf("Handler status: %s", split[2]);
} else if (g_strcmp0(split[1], "image") == 0) {
message = g_strdup_printf("Image '%s' status: %s", split[2], split[3]);
} else if (g_strcmp0(split[1], "bootloader") == 0) {
message = g_strdup_printf("Bootloader status: %s", split[2]);
} else if (g_strcmp0(split[1], "error") == 0) {
g_autofree gchar *joined = g_strjoinv(" ", &split[2]);
message = g_strdup_printf("Error: %s", joined);
} else if (g_strcmp0(split[1], "debug") == 0) {
g_autofree gchar *joined = g_strjoinv(" ", &split[2]);
message = g_strdup_printf("Debug: %s", joined);
} else {
message = g_strdup_printf("Unknown handler output: %s", line);
}
return message;
}
static gboolean verify_compatible(RaucInstallArgs *args, RaucManifest *manifest)
{
if (args->ignore_compatible) {
return TRUE;
} else if (g_strcmp0(r_context()->config->system_compatible,
manifest->update_compatible) == 0) {
return TRUE;
} else {
g_warning("incompatible manifest for this system (%s): %s",
r_context()->config->system_compatible,
manifest->update_compatible);
return FALSE;
}
}
static void prepare_environment(GSubprocessLauncher *launcher, gchar *update_source, RaucManifest *manifest, GHashTable *target_group)
{
GHashTableIter iter;
RaucSlot *slot;
gint slotcnt = 0;
g_autoptr(GString) slots = g_string_sized_new(128);
g_autoptr(GString) target_slots = g_string_sized_new(128);
g_subprocess_launcher_setenv(launcher, "RAUC_SYSTEM_CONFIG", r_context()->configpath, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_CURRENT_BOOTNAME", r_context()->bootslot, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_MOUNT_PREFIX", r_context()->config->mount_prefix, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_BUNDLE_MOUNT_POINT", update_source, TRUE);
/* Deprecated, included for backwards compatibility: */
g_subprocess_launcher_setenv(launcher, "RAUC_UPDATE_SOURCE", update_source, TRUE);
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer*) &slot)) {
gchar *varname;
GHashTableIter iiter;
gpointer member;
slotcnt++;
if (slots->len)
g_string_append_c(slots, ' ');
g_string_append_printf(slots, "%i", slotcnt);
g_hash_table_iter_init(&iiter, target_group);
while (g_hash_table_iter_next(&iiter, NULL, &member)) {
if (slot != member) {
continue;
}
/* for target slots, get image name and add number to list */
for (GList *l = manifest->images; l != NULL; l = l->next) {
RaucImage *img = l->data;
if (g_str_equal(slot->sclass, img->slotclass)) {
varname = g_strdup_printf("RAUC_IMAGE_NAME_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, img->filename, TRUE);
g_clear_pointer(&varname, g_free);
varname = g_strdup_printf("RAUC_IMAGE_DIGEST_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, img->checksum.digest, TRUE);
g_clear_pointer(&varname, g_free);
varname = g_strdup_printf("RAUC_IMAGE_CLASS_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, img->slotclass, TRUE);
g_clear_pointer(&varname, g_free);
break;
}
}
if (target_slots->len)
g_string_append_c(target_slots, ' ');
g_string_append_printf(target_slots, "%i", slotcnt);
}
varname = g_strdup_printf("RAUC_SLOT_NAME_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, slot->name, TRUE);
g_clear_pointer(&varname, g_free);
varname = g_strdup_printf("RAUC_SLOT_CLASS_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, slot->sclass, TRUE);
g_clear_pointer(&varname, g_free);
varname = g_strdup_printf("RAUC_SLOT_TYPE_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, slot->type, TRUE);
g_clear_pointer(&varname, g_free);
varname = g_strdup_printf("RAUC_SLOT_DEVICE_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, slot->device, TRUE);
g_clear_pointer(&varname, g_free);
varname = g_strdup_printf("RAUC_SLOT_BOOTNAME_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, slot->bootname ? slot->bootname : "", TRUE);
g_clear_pointer(&varname, g_free);
varname = g_strdup_printf("RAUC_SLOT_PARENT_%i", slotcnt);
g_subprocess_launcher_setenv(launcher, varname, slot->parent ? slot->parent->name : "", TRUE);
g_clear_pointer(&varname, g_free);
}
g_subprocess_launcher_setenv(launcher, "RAUC_SLOTS", slots->str, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_TARGET_SLOTS", target_slots->str, TRUE);
}
static gboolean launch_and_wait_handler(RaucInstallArgs *args, gchar *update_source, gchar *handler_name, RaucManifest *manifest, GHashTable *target_group, GError **error)
{
g_autoptr(GSubprocessLauncher) handlelaunch = NULL;
g_autoptr(GSubprocess) handleproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
GInputStream *instream = NULL;
g_autoptr(GDataInputStream) datainstream = NULL;
gchar *outline;
g_autoptr(GString) handler_args = NULL;
handlelaunch = g_subprocess_launcher_new(G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_MERGE);
prepare_environment(handlelaunch, update_source, manifest, target_group);
handler_args = g_string_new(manifest->handler_args);
if (r_context()->handlerextra) {
if (handler_args->len)
g_string_append_c(handler_args, ' ');
g_string_append(handler_args, r_context()->handlerextra);
}
handleproc = g_subprocess_launcher_spawn(
handlelaunch, &ierror,
handler_name,
handler_args->str,
NULL);
if (handleproc == NULL) {
g_propagate_error(error, ierror);
goto out;
}
instream = g_subprocess_get_stdout_pipe(handleproc);
datainstream = g_data_input_stream_new(instream);
do {
g_autofree gchar *handler_message = NULL;
outline = g_data_input_stream_read_line(datainstream, NULL, NULL, NULL);
if (!outline)
continue;
handler_message = parse_handler_output(outline);
if (handler_message != NULL)
install_args_update(args, handler_message);
g_free(outline);
} while (outline);
res = g_subprocess_wait_check(handleproc, NULL, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
res = TRUE;
out:
return res;
}
static gboolean run_bundle_hook(RaucManifest *manifest, gchar* bundledir, const gchar *hook_cmd, GError **error)
{
g_autofree gchar *hook_name = NULL;
g_autoptr(GSubprocessLauncher) launcher = NULL;
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
GInputStream *instream = NULL;
g_autoptr(GDataInputStream) datainstream = NULL;
gboolean res = FALSE;
gchar *outline, *hookreturnmsg = NULL;
g_assert_nonnull(manifest->hook_name);
hook_name = g_build_filename(bundledir, manifest->hook_name, NULL);
g_message("Running bundle hook %s", hook_cmd);
launcher = g_subprocess_launcher_new(G_SUBPROCESS_FLAGS_STDERR_PIPE);
g_subprocess_launcher_setenv(launcher, "RAUC_SYSTEM_COMPATIBLE", r_context()->config->system_compatible, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_SYSTEM_VARIANT", r_context()->config->system_variant ?: "", TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_MF_COMPATIBLE", manifest->update_compatible, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_MF_VERSION", manifest->update_version ?: "", TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_MOUNT_PREFIX", r_context()->config->mount_prefix, TRUE);
sproc = g_subprocess_launcher_spawn(
launcher, &ierror,
hook_name,
hook_cmd,
NULL);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start bundle hook: ");
goto out;
}
/* Read scripts stderr output */
instream = g_subprocess_get_stderr_pipe(sproc);
datainstream = g_data_input_stream_new(instream);
do {
outline = g_data_input_stream_read_line(datainstream, NULL, NULL, NULL);
if (outline) {
g_clear_pointer(&hookreturnmsg, g_free);
hookreturnmsg = outline;
}
} while (outline);
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
/* Subprocess exited with code 1 */
if ((ierror->domain == G_SPAWN_EXIT_ERROR) && (ierror->code >= INSTALL_HOOK_REJECT_CODE)) {
if (hookreturnmsg) {
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_REJECTED,
"Hook returned: %s", hookreturnmsg);
} else {
g_propagate_prefixed_error(
error,
ierror,
"Hook returned with exit code %d: ", ierror->code);
}
} else {
g_propagate_prefixed_error(
error,
ierror,
"failed to run bundle hook: ");
}
goto out;
}
out:
return res;
}
static gboolean launch_and_wait_custom_handler(RaucInstallArgs *args, gchar* bundledir, RaucManifest *manifest, GHashTable *target_group, GError **error)
{
g_autofree gchar* handler_name = NULL;
gboolean res = FALSE;
r_context_begin_step("launch_and_wait_custom_handler", "Launching update handler", 0);
/* Allow overriding compatible check by hook */
if (manifest->hooks.install_check) {
GError *ierror = NULL;
run_bundle_hook(manifest, bundledir, "install-check", &ierror);
if (ierror) {
g_propagate_error(error, ierror);
res = FALSE;
goto out;
}
} else if (!verify_compatible(args, manifest)) {
g_set_error_literal(error, R_INSTALL_ERROR, R_INSTALL_ERROR_COMPAT_MISMATCH,
"Compatible mismatch");
res = FALSE;
goto out;
}
handler_name = g_build_filename(bundledir, manifest->handler_name, NULL);
res = launch_and_wait_handler(args, bundledir, handler_name, manifest, target_group, error);
out:
r_context_end_step("launch_and_wait_custom_handler", res);
return res;
}
static gboolean pre_install_check_slot_mount_status(RaucSlot *slot, RaucImage *mfimage, GError **error)
{
/* OK if the slot is not mounted at all. */
if (!(slot->mount_point || slot->ext_mount_point))
return TRUE;
/* OK if the configuration says it may already be mounted and the
* bundle has a custom install hook. */
if (slot->allow_mounted && mfimage->hooks.install)
return TRUE;
if (slot->allow_mounted)
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_MOUNTED,
"Mounted device '%s' may only be updated by a custom install hook", slot->device);
else
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_MOUNTED,
"Destination device '%s' already mounted", slot->device);
return FALSE;
}
static gboolean pre_install_checks(gchar* bundledir, GList *install_images, GHashTable *target_group, GError **error)
{
for (GList *l = install_images; l != NULL; l = l->next) {
RaucImage *mfimage = l->data;
RaucSlot *dest_slot = g_hash_table_lookup(target_group, mfimage->slotclass);
/* if image filename is relative, make it absolute */
if (!g_path_is_absolute(mfimage->filename)) {
gchar *filename = g_build_filename(bundledir, mfimage->filename, NULL);
g_free(mfimage->filename);
mfimage->filename = filename;
}
if (!g_file_test(mfimage->filename, G_FILE_TEST_EXISTS)) {
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_NOSRC,
"Source image '%s' not found", mfimage->filename);
return FALSE;
}
if (!g_file_test(dest_slot->device, G_FILE_TEST_EXISTS)) {
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_NODST,
"Destination device '%s' not found", dest_slot->device);
return FALSE;
}
if (!pre_install_check_slot_mount_status(dest_slot, mfimage, error)) {
/* error is already set */
return FALSE;
}
}
return TRUE;
}
static gboolean launch_and_wait_default_handler(RaucInstallArgs *args, gchar* bundledir, RaucManifest *manifest, GHashTable *target_group, GError **error)
{
gchar *hook_name = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
GList *install_images = NULL;
install_images = get_install_images(manifest, target_group, &ierror);
if (install_images == NULL) {
g_propagate_error(error, ierror);
goto early_out;
}
/* Allow overriding compatible check by hook */
if (manifest->hooks.install_check) {
run_bundle_hook(manifest, bundledir, "install-check", &ierror);
if (ierror) {
if (g_error_matches(ierror, R_INSTALL_ERROR, R_INSTALL_ERROR_REJECTED)) {
g_propagate_prefixed_error(
error,
ierror,
"Bundle rejected: ");
} else {
g_propagate_prefixed_error(
error,
ierror,
"Install-check hook failed: ");
}
res = FALSE;
goto early_out;
}
} else if (!verify_compatible(args, manifest)) {
res = FALSE;
g_set_error_literal(error, R_INSTALL_ERROR, R_INSTALL_ERROR_COMPAT_MISMATCH,
"Compatible mismatch");
goto early_out;
}
res = pre_install_checks(bundledir, install_images, target_group, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto early_out;
}
/* Mark all parent destination slots non-bootable */
for (GList *l = install_images; l != NULL; l = l->next) {
RaucSlot *dest_slot = g_hash_table_lookup(target_group, ((RaucImage*)l->data)->slotclass);
g_assert_nonnull(dest_slot);
if (dest_slot->parent || !dest_slot->bootname) {
continue;
}
g_message("Marking target slot %s as non-bootable...", dest_slot->name);
res = r_boot_set_state(dest_slot, FALSE, &ierror);
if (!res) {
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_MARK_NONBOOTABLE,
"Failed marking slot %s non-bootable: %s", dest_slot->name, ierror->message);
g_clear_error(&ierror);
goto early_out;
}
}
if (manifest->hook_name)
hook_name = g_build_filename(bundledir, manifest->hook_name, NULL);
r_context_begin_step("update_slots", "Updating slots", g_list_length(install_images) * 2);
install_args_update(args, "Updating slots...");
for (GList *l = install_images; l != NULL; l = l->next) {
RaucImage *mfimage;
RaucSlot *dest_slot;
img_to_slot_handler update_handler = NULL;
RaucSlotStatus *slot_state = NULL;
GDateTime *now;
mfimage = l->data;
dest_slot = g_hash_table_lookup(target_group, mfimage->slotclass);
/* determine whether update image type is compatible with destination slot type */
update_handler = get_update_handler(mfimage, dest_slot, &ierror);
if (update_handler == NULL) {
res = FALSE;
g_propagate_error(error, ierror);
goto out;
}
install_args_update(args, g_strdup_printf("Checking slot %s", dest_slot->name));
r_context_begin_step_formatted("check_slot", 0, "Checking slot %s", dest_slot->name);
load_slot_status(dest_slot);
slot_state = dest_slot->status;
/* In case we failed unmounting while reading status
* file, abort here */
if (dest_slot->mount_point) {
res = FALSE;
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_MOUNTED,
"Slot '%s' still mounted", dest_slot->device);
r_context_end_step("check_slot", FALSE);
goto out;
}
/* if explicitly enabled, skip update of up-to-date slots */
if (!dest_slot->install_same && g_strcmp0(mfimage->checksum.digest, slot_state->checksum.digest) == 0) {
install_args_update(args, g_strdup_printf("Skipping update for correct image %s", mfimage->filename));
g_message("Skipping update for correct image %s", mfimage->filename);
r_context_end_step("check_slot", TRUE);
/* Dummy step to indicate slot was skipped */
r_context_begin_step("skip_image", "Copying image skipped", 0);
r_context_end_step("skip_image", TRUE);
goto image_out;
}
g_free(slot_state->status);
slot_state->status = g_strdup("update");
g_message("Slot needs to be updated with %s", mfimage->filename);
r_context_end_step("check_slot", TRUE);
install_args_update(args, g_strdup_printf("Updating slot %s", dest_slot->name));
/* update slot */
if (mfimage->variant)
g_message("Updating %s with %s (variant: %s)", dest_slot->device, mfimage->filename, mfimage->variant);
else
g_message("Updating %s with %s", dest_slot->device, mfimage->filename);
r_context_begin_step_formatted("copy_image", 0, "Copying image to %s", dest_slot->name);
res = update_handler(
mfimage,
dest_slot,
hook_name,
&ierror);
if (!res) {
g_propagate_prefixed_error(error, ierror,
"Failed updating slot %s: ", dest_slot->name);
r_context_end_step("copy_image", FALSE);
goto out;
}
g_free(slot_state->bundle_compatible);
g_free(slot_state->bundle_version);
g_free(slot_state->bundle_description);
g_free(slot_state->bundle_build);
g_free(slot_state->status);
g_free(slot_state->checksum.digest);
g_free(slot_state->installed_timestamp);
now = g_date_time_new_now_utc();
slot_state->bundle_compatible = g_strdup(manifest->update_compatible);
slot_state->bundle_version = g_strdup(manifest->update_version);
slot_state->bundle_description = g_strdup(manifest->update_description);
slot_state->bundle_build = g_strdup(manifest->update_build);
slot_state->status = g_strdup("ok");
slot_state->checksum.type = mfimage->checksum.type;
slot_state->checksum.digest = g_strdup(mfimage->checksum.digest);
slot_state->checksum.size = mfimage->checksum.size;
slot_state->installed_timestamp = g_date_time_format(now, "%Y-%m-%dT%H:%M:%SZ");
slot_state->installed_count++;
g_date_time_unref(now);
r_context_end_step("copy_image", TRUE);
install_args_update(args, g_strdup_printf("Updating slot %s status", dest_slot->name));
res = save_slot_status(dest_slot, &ierror);
if (!res) {
g_propagate_prefixed_error(error, ierror, "Error while writing status file: ");
goto out;
}
image_out:
install_args_update(args, g_strdup_printf("Updating slot %s done", dest_slot->name));
}
if (r_context()->config->activate_installed) {
/* Mark all parent destination slots bootable */
for (GList *l = install_images; l != NULL; l = l->next) {
RaucSlot *dest_slot = g_hash_table_lookup(target_group, ((RaucImage*)l->data)->slotclass);
if (dest_slot->parent || !dest_slot->bootname)
continue;
g_message("Marking target slot %s as bootable...", dest_slot->name);
mark_active(dest_slot, &ierror);
if (g_error_matches(ierror, R_INSTALL_ERROR, R_INSTALL_ERROR_MARK_BOOTABLE)) {
g_propagate_prefixed_error(error, ierror,
"Failed marking slot %s bootable: ", dest_slot->name);
res = FALSE;
goto out;
} else if (g_error_matches(ierror, R_INSTALL_ERROR, R_INSTALL_ERROR_FAILED)) {
g_propagate_prefixed_error(error, ierror,
"Marked slot %s bootable, but failed to write status file: ",
dest_slot->name);
res = FALSE;
goto out;
} else if (ierror) {
g_propagate_prefixed_error(error, ierror,
"Unexpected error while trying to mark slot %s bootable: ",
dest_slot->name);
res = FALSE;
goto out;
}
}
} else {
g_message("Leaving target slot non-bootable as requested by activate_installed == false.");
}
install_args_update(args, "All slots updated");
res = TRUE;
out:
//g_free(hook_name);
r_context_end_step("update_slots", res);
early_out: