Skip to content

Commit 504afd7

Browse files
committed
core/manager: split out creation of serialization fd out to a helper
There is a slight change in behaviour: the user manager for root will create a temporary file in /run/systemd, not /tmp. I don't think this matters, but simplifies implementation.
1 parent 18f71a3 commit 504afd7

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

src/basic/fileio.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,25 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
13421342
return fd;
13431343
}
13441344

1345+
int open_serialization_fd(const char *ident) {
1346+
int fd = -1;
1347+
1348+
fd = memfd_create(ident, MFD_CLOEXEC);
1349+
if (fd < 0) {
1350+
const char *path;
1351+
1352+
path = getpid() == 1 ? "/run/systemd" : "/tmp";
1353+
fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
1354+
if (fd < 0)
1355+
return fd;
1356+
1357+
log_debug("Serializing %s to %s.", ident, path);
1358+
} else
1359+
log_debug("Serializing %s to memfd.", ident);
1360+
1361+
return fd;
1362+
}
1363+
13451364
int link_tmpfile(int fd, const char *path, const char *target) {
13461365

13471366
assert(fd >= 0);

src/basic/fileio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space)
8484

8585
int open_tmpfile_unlinkable(const char *directory, int flags);
8686
int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
87+
int open_serialization_fd(const char *ident);
8788

8889
int link_tmpfile(int fd, const char *path, const char *target);
8990

src/core/manager.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,22 +2437,14 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
24372437
}
24382438

24392439
int manager_open_serialization(Manager *m, FILE **_f) {
2440-
int fd = -1;
2440+
int fd;
24412441
FILE *f;
24422442

24432443
assert(_f);
24442444

2445-
fd = memfd_create("systemd-serialization", MFD_CLOEXEC);
2446-
if (fd < 0) {
2447-
const char *path;
2448-
2449-
path = MANAGER_IS_SYSTEM(m) ? "/run/systemd" : "/tmp";
2450-
fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
2451-
if (fd < 0)
2452-
return -errno;
2453-
log_debug("Serializing state to %s.", path);
2454-
} else
2455-
log_debug("Serializing state to memfd.");
2445+
fd = open_serialization_fd("systemd-state");
2446+
if (fd < 0)
2447+
return fd;
24562448

24572449
f = fdopen(fd, "w+");
24582450
if (!f) {
@@ -2461,7 +2453,6 @@ int manager_open_serialization(Manager *m, FILE **_f) {
24612453
}
24622454

24632455
*_f = f;
2464-
24652456
return 0;
24662457
}
24672458

src/test/test-fd-util.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,20 @@ static void test_same_fd(void) {
9494
assert_se(same_fd(b, a) == 0);
9595
}
9696

97+
static void test_open_serialization_fd(void) {
98+
_cleanup_close_ int fd = -1;
99+
100+
fd = open_serialization_fd("test");
101+
assert_se(fd >= 0);
102+
103+
write(fd, "test\n", 5);
104+
}
105+
97106
int main(int argc, char *argv[]) {
98107
test_close_many();
99108
test_close_nointr();
100109
test_same_fd();
110+
test_open_serialization_fd();
101111

102112
return 0;
103113
}

0 commit comments

Comments
 (0)