Skip to content

Commit

Permalink
Merge pull request #1309 from ejoerns/mark-only-bootslots
Browse files Browse the repository at this point in the history
Ensure Only Bootable Slots Can be Marked
  • Loading branch information
jluebbe committed Jan 2, 2024
2 parents 62100c1 + 4ff9566 commit 3a2b438
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/bootchooser.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ static gboolean grub_get_state(RaucSlot* slot, gboolean *good, GError **error)
gboolean found = FALSE;

g_return_val_if_fail(slot, FALSE);
g_return_val_if_fail(slot->bootname, FALSE);
g_return_val_if_fail(good, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

Expand Down Expand Up @@ -539,6 +540,7 @@ static gboolean grub_set_state(RaucSlot *slot, gboolean good, GError **error)
GError *ierror = NULL;

g_return_val_if_fail(slot, FALSE);
g_return_val_if_fail(slot->bootname, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

if (good) {
Expand Down
30 changes: 18 additions & 12 deletions src/mark.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "context.h"
#include "install.h"
#include "mark.h"
#include "slot.h"
#include "status_file.h"

static RaucSlot* get_slot_by_identifier(const gchar *identifier, GError **error)
Expand All @@ -19,7 +20,7 @@ static RaucSlot* get_slot_by_identifier(const gchar *identifier, GError **error)
booted = NULL;
}

if (!g_strcmp0(identifier, "booted")) {
if (g_strcmp0(identifier, "booted") == 0) {
if (booted)
slot = booted;
else
Expand All @@ -28,7 +29,7 @@ static RaucSlot* get_slot_by_identifier(const gchar *identifier, GError **error)
R_SLOT_ERROR,
R_SLOT_ERROR_NO_SLOT_WITH_STATE_BOOTED,
"Did not find booted slot");
} else if (!g_strcmp0(identifier, "other")) {
} else if (g_strcmp0(identifier, "other") == 0) {
if (booted) {
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer*) &slot)) {
Expand All @@ -52,19 +53,24 @@ static RaucSlot* get_slot_by_identifier(const gchar *identifier, GError **error)
g_auto(GStrv) groupsplit = g_strsplit(identifier, ".", -1);

if (g_strv_length(groupsplit) == 2) {
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer*) &slot)) {
if (!g_strcmp0(slot->sclass, groupsplit[0]) && !slot->parent && !g_strcmp0(slot->name, identifier))
break;
slot = NULL;
}
if (!slot)
slot = (RaucSlot*) g_hash_table_lookup(r_context()->config->slots, identifier);
if (!slot) {
g_set_error(error,
R_SLOT_ERROR,
R_SLOT_ERROR_FAILED,
"No slot with class %s and name %s found",
groupsplit[0],
identifier);
return NULL;
}
if (!slot->bootname) {
g_set_error(error,
R_SLOT_ERROR,
R_SLOT_ERROR_FAILED,
"Slot %s has no bootname set",
slot->name);
return NULL;
}
} else {
g_set_error(error,
R_SLOT_ERROR,
Expand Down Expand Up @@ -211,23 +217,23 @@ gboolean mark_run(const gchar *state,
goto out;
}

if (!g_strcmp0(state, "good")) {
if (g_strcmp0(state, "good") == 0) {
if (!r_mark_good(slot, &ierror)) {
res = FALSE;
*message = g_strdup(ierror->message);
} else {
res = TRUE;
*message = g_strdup_printf("marked slot %s as good", slot->name);
}
} else if (!g_strcmp0(state, "bad")) {
} else if (g_strcmp0(state, "bad") == 0) {
if (!r_mark_bad(slot, &ierror)) {
res = FALSE;
*message = g_strdup(ierror->message);
} else {
res = TRUE;
*message = g_strdup_printf("marked slot %s as bad", slot->name);
}
} else if (!g_strcmp0(state, "active")) {
} else if (g_strcmp0(state, "active") == 0) {
if (!r_mark_active(slot, &ierror)) {
res = FALSE;
*message = g_strdup(ierror->message);
Expand Down
16 changes: 16 additions & 0 deletions test/rauc.t
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,22 @@ test_expect_success !SERVICE,GRUB "rauc status mark-active: internally" "
rauc -c $SHARNESS_TEST_DIRECTORY/test.conf --override-boot-slot=system0 status mark-active
"

test_expect_success !SERVICE,GRUB "rauc status mark-good: booted" "
rauc -c $SHARNESS_TEST_DIRECTORY/test.conf --override-boot-slot=system0 status mark-good booted
"

test_expect_success !SERVICE,GRUB "rauc status mark-good: other" "
rauc -c $SHARNESS_TEST_DIRECTORY/test.conf --override-boot-slot=system0 status mark-good other
"

test_expect_success !SERVICE,GRUB "rauc status mark-good: any bootslot" "
rauc -c $SHARNESS_TEST_DIRECTORY/test.conf --override-boot-slot=system0 status mark-good rescue.0
"

test_expect_success !SERVICE,GRUB "rauc status mark-good: non-bootslot" "
test_must_fail rauc -c $SHARNESS_TEST_DIRECTORY/test.conf --override-boot-slot=system0 status mark-good bootloader.0
"

test_expect_success SERVICE,GRUB "rauc status mark-good: via D-Bus" "
start_rauc_dbus_service \
--conf=${SHARNESS_TEST_DIRECTORY}/test.conf \
Expand Down

0 comments on commit 3a2b438

Please sign in to comment.