]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/serialize.c
d0f86a83f475102d7d21b652a92a239d11cc98b8
[thirdparty/systemd.git] / src / shared / serialize.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "env-util.h"
5 #include "escape.h"
6 #include "fileio.h"
7 #include "parse-util.h"
8 #include "serialize.h"
9 #include "strv.h"
10
11 int serialize_item(FILE *f, const char *key, const char *value) {
12 assert(f);
13 assert(key);
14
15 if (!value)
16 return 0;
17
18 /* Make sure that anything we serialize we can also read back again with read_line() with a maximum line size
19 * of LONG_LINE_MAX. This is a safety net only. All code calling us should filter this out earlier anyway. */
20 if (strlen(key) + 1 + strlen(value) + 1 > LONG_LINE_MAX) {
21 log_warning("Attempted to serialize overly long item '%s', refusing.", key);
22 return -EINVAL;
23 }
24
25 fputs(key, f);
26 fputc('=', f);
27 fputs(value, f);
28 fputc('\n', f);
29
30 return 1;
31 }
32
33 int serialize_item_escaped(FILE *f, const char *key, const char *value) {
34 _cleanup_free_ char *c = NULL;
35
36 assert(f);
37 assert(key);
38
39 if (!value)
40 return 0;
41
42 c = cescape(value);
43 if (!c)
44 return log_oom();
45
46 return serialize_item(f, key, c);
47 }
48
49 int serialize_item_format(FILE *f, const char *key, const char *format, ...) {
50 char buf[LONG_LINE_MAX];
51 va_list ap;
52 int k;
53
54 assert(f);
55 assert(key);
56 assert(format);
57
58 va_start(ap, format);
59 k = vsnprintf(buf, sizeof(buf), format, ap);
60 va_end(ap);
61
62 if (k < 0 || (size_t) k >= sizeof(buf) || strlen(key) + 1 + k + 1 > LONG_LINE_MAX) {
63 log_warning("Attempted to serialize overly long item '%s', refusing.", key);
64 return -EINVAL;
65 }
66
67 fputs(key, f);
68 fputc('=', f);
69 fputs(buf, f);
70 fputc('\n', f);
71
72 return 1;
73 }
74
75 int serialize_fd(FILE *f, FDSet *fds, const char *key, int fd) {
76 int copy;
77
78 assert(f);
79 assert(key);
80
81 if (fd < 0)
82 return 0;
83
84 copy = fdset_put_dup(fds, fd);
85 if (copy < 0)
86 return log_error_errno(copy, "Failed to add file descriptor to serialization set: %m");
87
88 return serialize_item_format(f, key, "%i", copy);
89 }
90
91 int serialize_usec(FILE *f, const char *key, usec_t usec) {
92 assert(f);
93 assert(key);
94
95 if (usec == USEC_INFINITY)
96 return 0;
97
98 return serialize_item_format(f, key, USEC_FMT, usec);
99 }
100
101 int serialize_dual_timestamp(FILE *f, const char *name, const dual_timestamp *t) {
102 assert(f);
103 assert(name);
104 assert(t);
105
106 if (!dual_timestamp_is_set(t))
107 return 0;
108
109 return serialize_item_format(f, name, USEC_FMT " " USEC_FMT, t->realtime, t->monotonic);
110 }
111
112 int serialize_strv(FILE *f, const char *key, char **l) {
113 int ret = 0, r;
114 char **i;
115
116 /* Returns the first error, or positive if anything was serialized, 0 otherwise. */
117
118 STRV_FOREACH(i, l) {
119 r = serialize_item_escaped(f, key, *i);
120 if ((ret >= 0 && r < 0) ||
121 (ret == 0 && r > 0))
122 ret = r;
123 }
124
125 return ret;
126 }
127
128 int deserialize_usec(const char *value, usec_t *ret) {
129 int r;
130
131 assert(value);
132
133 r = safe_atou64(value, ret);
134 if (r < 0)
135 return log_debug_errno(r, "Failed to parse usec value \"%s\": %m", value);
136
137 return 0;
138 }
139
140 int deserialize_dual_timestamp(const char *value, dual_timestamp *t) {
141 uint64_t a, b;
142 int r, pos;
143
144 assert(value);
145 assert(t);
146
147 pos = strspn(value, WHITESPACE);
148 if (value[pos] == '-')
149 return -EINVAL;
150 pos += strspn(value + pos, DIGITS);
151 pos += strspn(value + pos, WHITESPACE);
152 if (value[pos] == '-')
153 return -EINVAL;
154
155 r = sscanf(value, "%" PRIu64 "%" PRIu64 "%n", &a, &b, &pos);
156 if (r != 2) {
157 log_debug("Failed to parse dual timestamp value \"%s\".", value);
158 return -EINVAL;
159 }
160
161 if (value[pos] != '\0')
162 /* trailing garbage */
163 return -EINVAL;
164
165 t->realtime = a;
166 t->monotonic = b;
167
168 return 0;
169 }
170
171 int deserialize_environment(const char *value, char ***list) {
172 _cleanup_free_ char *unescaped = NULL;
173 int r;
174
175 assert(value);
176 assert(list);
177
178 /* Changes the *environment strv inline. */
179
180 r = cunescape(value, 0, &unescaped);
181 if (r < 0)
182 return log_error_errno(r, "Failed to unescape: %m");
183
184 r = strv_env_replace(list, unescaped);
185 if (r < 0)
186 return log_error_errno(r, "Failed to append environment variable: %m");
187
188 unescaped = NULL; /* now part of 'list' */
189 return 0;
190 }