forked from rauc/rauc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmark.c
167 lines (148 loc) · 4.55 KB
/
mark.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
#include "bootchooser.h"
#include "context.h"
#include "install.h"
#include "mark.h"
static RaucSlot* get_slot_by_identifier(const gchar *identifier, GError **error)
{
GHashTableIter iter;
RaucSlot *slot = NULL, *booted = NULL;
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer*) &booted)) {
if (booted->state == ST_BOOTED)
break;
booted = NULL;
}
if (!g_strcmp0(identifier, "booted")) {
if (booted)
slot = booted;
else
g_set_error(
error,
R_SLOT_ERROR,
R_SLOT_ERROR_NO_SLOT_WITH_STATE_BOOTED,
"Did not find booted slot");
} else if (!g_strcmp0(identifier, "other")) {
if (booted) {
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer*) &slot)) {
if (slot->sclass == booted->sclass && !slot->parent && slot->bootname && slot != booted)
break;
slot = NULL;
}
if (!slot)
g_set_error(error,
R_SLOT_ERROR,
R_SLOT_ERROR_FAILED,
"No other bootable slot of the same class found");
} else {
g_set_error(
error,
R_SLOT_ERROR,
R_SLOT_ERROR_NO_SLOT_WITH_STATE_BOOTED,
"Did not find booted slot needed to find another bootable slot of the same class");
}
} else {
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)
g_set_error(error,
R_SLOT_ERROR,
R_SLOT_ERROR_FAILED,
"No slot with class %s and name %s found",
groupsplit[0],
identifier);
} else {
g_set_error(error,
R_SLOT_ERROR,
R_SLOT_ERROR_FAILED,
"Invalid slot name format: '%s'", identifier);
}
}
return slot;
}
void mark_active(RaucSlot *slot, GError **error)
{
RaucSlotStatus *slot_state;
GError *ierror = NULL;
GDateTime *now;
gboolean res;
g_return_if_fail(slot);
g_return_if_fail(error == NULL || *error == NULL);
load_slot_status(slot);
slot_state = slot->status;
res = r_boot_set_primary(slot, &ierror);
if (!res) {
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_MARK_BOOTABLE,
"failed to activate slot %s: %s", slot->name, ierror->message);
g_error_free(ierror);
return;
}
g_free(slot_state->activated_timestamp);
now = g_date_time_new_now_utc();
slot_state->activated_timestamp = g_date_time_format(now, "%Y-%m-%dT%H:%M:%SZ");
slot_state->activated_count++;
g_date_time_unref(now);
res = save_slot_status(slot, &ierror);
if (!res) {
g_set_error(error, R_INSTALL_ERROR, R_INSTALL_ERROR_FAILED, "%s", ierror->message);
g_error_free(ierror);
return;
}
}
gboolean mark_run(const gchar *state,
const gchar *slot_identifier,
gchar **slot_name,
gchar **message)
{
RaucSlot *slot = NULL;
GError *ierror = NULL;
gboolean res;
g_assert(slot_name == NULL || *slot_name == NULL);
g_assert(message != NULL && *message == NULL);
slot = get_slot_by_identifier(slot_identifier, &ierror);
if (ierror) {
res = FALSE;
*message = g_strdup(ierror->message);
goto out;
}
if (!g_strcmp0(state, "good")) {
res = r_boot_set_state(slot, TRUE, &ierror);
*message = res ? g_strdup_printf("marked slot %s as good", slot->name) : g_strdup(ierror->message);
} else if (!g_strcmp0(state, "bad")) {
res = r_boot_set_state(slot, FALSE, &ierror);
*message = res ? g_strdup_printf("marked slot %s as bad", slot->name) : g_strdup(ierror->message);
} else if (!g_strcmp0(state, "active")) {
mark_active(slot, &ierror);
if (!ierror) {
res = TRUE;
*message = g_strdup_printf("activated slot %s", slot->name);
} else if (g_error_matches(ierror, R_INSTALL_ERROR, R_INSTALL_ERROR_MARK_BOOTABLE)) {
res = FALSE;
*message = g_strdup(ierror->message);
} else if (g_error_matches(ierror, R_INSTALL_ERROR, R_INSTALL_ERROR_FAILED)) {
res = TRUE;
*message = g_strdup_printf("activated slot %s, but failed to write status file: %s",
slot->name, ierror->message);
} else {
res = FALSE;
*message = g_strdup_printf("unexpected error while trying to activate slot %s: %s",
slot->name, ierror->message);
}
g_clear_error(&ierror);
} else {
res = FALSE;
*message = g_strdup_printf("unknown subcommand %s", state);
}
out:
if (res && slot_name)
*slot_name = g_strdup(slot->name);
g_clear_error(&ierror);
return res;
}