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