/
context.c
410 lines (318 loc) · 10.5 KB
/
context.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
#include <config.h>
#include <gio/gio.h>
#include "config_file.h"
#include "context.h"
#include "network.h"
#include "signature.h"
RaucContext *context = NULL;
static const gchar* get_cmdline_bootname(void) {
GRegex *regex = NULL;
GMatchInfo *match = NULL;
char *contents = NULL;
static const char *bootname = NULL;
if (bootname != NULL)
return bootname;
if (!g_file_get_contents("/proc/cmdline", &contents, NULL, NULL))
return NULL;
regex = g_regex_new("rauc\\.slot=(\\S+)", 0, 0, NULL);
if (g_regex_match(regex, contents, 0, &match)) {
bootname = g_match_info_fetch(match, 1);
goto out;
}
g_clear_pointer(&match, g_match_info_free);
g_clear_pointer(®ex, g_regex_unref);
/* For barebox, we check if the bootstate code set the active slot name
* in the command line */
if (g_strcmp0(context->config->system_bootloader, "barebox") == 0) {
regex = g_regex_new("(?:bootstate|bootchooser)\\.active=(\\S+)", 0, 0, NULL);
if (g_regex_match(regex, contents, 0, &match)) {
bootname = g_match_info_fetch(match, 1);
goto out;
}
g_clear_pointer(&match, g_match_info_free);
g_clear_pointer(®ex, g_regex_unref);
}
regex = g_regex_new("root=(\\S+)", 0, 0, NULL);
if (g_regex_match(regex, contents, 0, &match)) {
bootname = g_match_info_fetch(match, 1);
goto out;
}
out:
g_clear_pointer(&match, g_match_info_free);
g_clear_pointer(®ex, g_regex_unref);
g_clear_pointer(&contents, g_free);
return bootname;
}
static gboolean launch_and_wait_variables_handler(gchar *handler_name, GHashTable *variables, GError **error) {
GSubprocessLauncher *handlelaunch = NULL;
GSubprocess *handleproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
GHashTableIter iter;
gchar *key = NULL;
gchar *value = NULL;
GDataInputStream *datainstream = NULL;
GInputStream *instream;
gchar* outline;
handlelaunch = g_subprocess_launcher_new(G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_MERGE);
/* we copy the variables from the hashtable and add them to the
subprocess environment */
g_hash_table_iter_init(&iter, variables);
while (g_hash_table_iter_next(&iter, (gpointer)&key, (gpointer)&value)) {
g_subprocess_launcher_setenv(handlelaunch, g_strdup(key), g_strdup(value), 1);
}
handleproc = g_subprocess_launcher_spawn(
handlelaunch,
&ierror, handler_name,
NULL,
NULL);
if (!handleproc) {
g_propagate_error(error, ierror);
goto out;
}
instream = g_subprocess_get_stdout_pipe(handleproc);
datainstream = g_data_input_stream_new(instream);
do {
outline = g_data_input_stream_read_line(datainstream, NULL, NULL, NULL);
if (!outline)
continue;
if (g_str_has_prefix(outline, "RAUC_")) {
gchar **split = g_strsplit(outline, "=", 2);
if (g_strv_length(split) != 2)
continue;
g_hash_table_insert(variables, g_strdup(split[0]), g_strdup(split[1]));
g_strfreev(split);
}
} while (outline);
res = g_subprocess_wait_check(handleproc, NULL, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
res = TRUE;
out:
g_clear_object(&datainstream);
g_clear_object(&handleproc);
g_clear_object(&handlelaunch);
return res;
}
static void r_context_configure(void) {
gboolean res = TRUE;
GError *error = NULL;
g_assert_nonnull(context);
g_assert_false(context->busy);
g_clear_pointer(&context->config, free_config);
res = load_config(context->configpath, &context->config, &error);
if (!res && error->domain==g_file_error_quark()) {
g_debug("system config not found, using default values");
g_clear_error(&error);
res = default_config(&context->config);
}
if (!res) {
g_error("failed to initialize context: %s", error->message);
g_clear_error(&error);
}
if (context->config->systeminfo_handler &&
g_file_test(context->config->systeminfo_handler, G_FILE_TEST_EXISTS)) {
GError *ierror = NULL;
GHashTable *vars = NULL;
GHashTableIter iter;
gchar *key = NULL;
gchar *value = NULL;
vars = g_hash_table_new(g_str_hash, g_str_equal);
g_message("Getting Systeminfo: %s", context->config->systeminfo_handler);
res = launch_and_wait_variables_handler(context->config->systeminfo_handler, vars, &ierror);
if (!res) {
g_error("Failed to read system-info variables%s", ierror->message);
g_clear_error(&ierror);
}
g_hash_table_iter_init(&iter, vars);
while (g_hash_table_iter_next(&iter, (gpointer)&key, (gpointer)&value)) {
if (g_strcmp0(key, "RAUC_SYSTEM_SERIAL") == 0)
r_context_conf()->system_serial = g_strdup(value);
}
g_clear_pointer(&vars, g_hash_table_unref);
}
if (context->bootslot == NULL) {
context->bootslot = g_strdup(get_cmdline_bootname());
}
if (context->mountprefix) {
g_free(context->config->mount_prefix);
context->config->mount_prefix = g_strdup(context->mountprefix);
}
if (context->keyringpath) {
context->config->keyring_path = context->keyringpath;
}
context->pending = FALSE;
}
gboolean r_context_get_busy(void) {
if (context == NULL) {
return FALSE;
}
return context->busy;
}
void r_context_set_busy(gboolean busy) {
g_assert_nonnull(context);
g_assert(context->busy != busy);
if (!context->busy && context->pending)
r_context_configure();
context->busy = busy;
}
static void r_context_send_progress(gboolean op_finished, gboolean success) {
RaucProgressStep *step;
RaucProgressStep *iter_step;
gfloat percentage = 0;
/* last step already notified parent, ignore it */
GList *iter = g_list_next(context->progress);
/* "stack" should never be NULL at this point */
g_assert_nonnull(context->progress);
step = context->progress->data;
/* no step in list left means operation complete */
if (!iter)
percentage = step->percent_done;
/* sum up done percentages of all steps */
while (iter) {
iter_step = iter->data;
percentage = percentage + iter_step->percent_done;
iter = g_list_next(iter);
}
g_assert_cmpint(percentage, <=, 100);
/* call installer callback with percentage and message */
if (op_finished) {
if (success)
step->description = g_strdup_printf("%s done.",
step->description);
else
step->description = g_strdup_printf("%s failed.",
step->description);
}
/* handle missing callback gracefully */
if (context->progress_callback)
context->progress_callback(percentage, step->description,
g_list_length(context->progress));
}
void r_context_begin_step(const gchar *name, const gchar *description,
gint substeps) {
RaucProgressStep *step = g_new0(RaucProgressStep, 1);
RaucProgressStep *parent;
/* set properties */
step->name = name;
step->description = description;
step->substeps_total = substeps;
step->substeps_done = 0;
step->percent_done = 0;
step->last_explicit_percent = 0;
/* calculate percentage */
if (context->progress) {
parent = context->progress->data;
g_assert_cmpint(parent->substeps_total, >, 0);
/* nesting check */
if (parent->substeps_total == parent->substeps_done)
g_error("Step nesting wrong: %s contains %s exceeding step limit (%d/%d)",
parent->name, step->name,
parent->substeps_done + 1,
parent->substeps_total);
step->percent_total = parent->percent_total
/ parent->substeps_total;
g_assert_cmpint(step->percent_total, <=,
parent->percent_total);
g_assert_cmpint(step->percent_total, <=, 100);
} else {
/* root step */
step->percent_total = 100;
}
/* add step to "stack" */
context->progress = g_list_prepend(context->progress, step);
r_context_send_progress(FALSE, FALSE);
}
void r_context_end_step(const gchar *name, gboolean success) {
RaucProgressStep *step;
GList *step_element;
RaucProgressStep *parent;
/* "stack" should never be NULL at this point */
g_assert_nonnull(context->progress);
/* get element from "stack" */
step_element = context->progress;
step = step_element->data;
g_assert_nonnull(step);
step->percent_done = step->percent_total;
/* check number of substeps */
if (step->substeps_done > step->substeps_total)
g_error("Too many substeps: %s (%d/%d)",
step->name, step->substeps_done,
step->substeps_total);
if (success && step->substeps_done < step->substeps_total)
g_error("Not enough substeps: %s (%d/%d)",
step->name, step->substeps_done,
step->substeps_total);
/* mark substeps/percentage as done/complete in case of an error */
if (!success)
step->substeps_done = step->substeps_total;
/* ensure that progress step nesting is done correctly */
g_assert_cmpstr(step->name, ==, name);
/* increment step count and percentage on parent step */
if (g_list_next(context->progress)) {
parent = g_list_next(context->progress)->data;
parent->substeps_done++;
/* clean up explicit percentage */
if (step->last_explicit_percent != 0)
r_context_set_step_percentage(step->name, 100);
else
parent->percent_done = parent->percent_done
+ step->percent_done;
g_assert_cmpint(step->percent_done, <=,
parent->percent_done);
}
r_context_send_progress(TRUE, success);
context->progress = g_list_remove_link(context->progress,
step_element);
g_list_free(step_element);
g_free(step);
}
void r_context_set_step_percentage(const gchar *name, gint custom_percent) {
RaucProgressStep *step;
RaucProgressStep *parent;
gint percent_difference;
g_assert_nonnull(context->progress);
step = context->progress->data;
parent = g_list_next(context->progress)->data;
/* ensure that progress step nesting is done correctly */
g_assert_cmpstr(step->name, ==, name);
/* substeps and setting explicit percentage does not make sense */
g_assert_cmpint(step->substeps_total, ==, 0);
percent_difference = custom_percent - step->last_explicit_percent;
step->percent_done = step->percent_total
* (percent_difference / 100.0f);
/* pass to parent */
if (parent)
parent->percent_done = parent->percent_done
+ step->percent_done;
step->last_explicit_percent = custom_percent;
/* r_context_step_end sends 100% progress step */
if (custom_percent != 100)
r_context_send_progress(FALSE, FALSE);
}
void r_context_register_progress_callback(progress_callback progress_cb) {
g_assert_nonnull(progress_cb);
g_assert_null(context->progress_callback);
context->progress_callback = progress_cb;
}
RaucContext *r_context_conf(void) {
if (context == NULL) {
network_init();
signature_init();
context = g_new0(RaucContext, 1);
context->configpath = g_strdup("/etc/rauc/system.conf");
context->progress = NULL;
context->install_info = g_new0(RContextInstallationInfo, 1);
}
g_assert_false(context->busy);
context->pending = TRUE;
return context;
}
const RaucContext *r_context(void) {
g_assert_nonnull(context);
if (context->pending)
r_context_configure();
return context;
}