tmpfiles: do not truncate the file of a w entry

A w entry empties the file before it writes the argument.  With a file
that holds "abcdef":

    w /tmp/file - - - - XY

leaves "XY".  With systemd-tmpfiles the file holds "XYcdef", it writes
over the start of what is there.  For a file in /proc or /sys there is
no difference.

Open the file without O_TRUNC.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 17:13:53 +01:00
1 parent 309844fc11
commit aa38c72044
4 files changed
+26 -7

No files matched your search

+13 -4
View File
@@ -1760,11 +1760,20 @@ static void tmpfiles(char *line)
break;
for (size_t i = 0; i < gl.gl_pathc; i++) {
fp = fopen(gl.gl_pathv[i], plus ? "a" : "w");
if (fp) {
write_arg(fp, arg);
rc = fclose(fp);
int fd;
/* Not truncated, as in tmpfiles.d(5) */
fd = open(gl.gl_pathv[i], O_WRONLY | O_NOCTTY | O_CLOEXEC | (plus ? O_APPEND : 0));
if (fd < 0)
continue;
fp = fdopen(fd, plus ? "a" : "w");
if (!fp) {
close(fd);
continue;
}
write_arg(fp, arg);
rc = fclose(fp);
}
globfree(&gl);
break;