core: rework serialization

Let's be more careful with what we serialize: let's ensure we never
serialize strings that are longer than LONG_LINE_MAX, so that we know we
can read them back with read_line(…, LONG_LINE_MAX, …) safely.

In order to implement this all serialization functions are move to
serialize.[ch], and internally will do line size checks. We'd rather
skip a serialization line (with a loud warning) than write an overly
long line out. Of course, this is just a second level protection, after
all the data we serialize shouldn't be this long in the first place.

While we are at it also clean up logging: while serializing make sure to
always log about errors immediately. Also, (void)ify all calls we don't
expect errors in (or catch errors as part of the general
fflush_and_check() at the end.
This commit is contained in:
Lennart Poettering
2018-10-26 10:52:41 +02:00
parent cdd620e346
commit d68c645bd3
31 changed files with 519 additions and 462 deletions
+5 -4
View File
@@ -8,6 +8,7 @@
#include "load-dropin.h"
#include "log.h"
#include "scope.h"
#include "serialize.h"
#include "special.h"
#include "string-table.h"
#include "string-util.h"
@@ -402,11 +403,11 @@ static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
assert(f);
assert(fds);
unit_serialize_item(u, f, "state", scope_state_to_string(s->state));
unit_serialize_item(u, f, "was-abandoned", yes_no(s->was_abandoned));
(void) serialize_item(f, "state", scope_state_to_string(s->state));
(void) serialize_bool(f, "was-abandoned", s->was_abandoned);
if (s->controller)
unit_serialize_item(u, f, "controller", s->controller);
(void) serialize_item(f, "controller", s->controller);
return 0;
}
@@ -441,7 +442,7 @@ static int scope_deserialize_item(Unit *u, const char *key, const char *value, F
r = free_and_strdup(&s->controller, value);
if (r < 0)
log_oom();
return log_oom();
} else
log_unit_debug(u, "Unknown serialization key: %s", key);