diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index d09b0dad..4a441085 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -321,6 +321,9 @@ All relevant changes are documented in this file. line were dropped as a comment. As in tmpfiles.d(5), only a line that starts with `#` is a comment. **Note:** a comment at the end of an entry is now read as its argument +- Fix tmpfiles `w` entries for a regular file, the file was truncated. + As in tmpfiles.d(5), the argument is now written over the start of what + the file holds - Fix logins refused after boot on a system with systemd's tmpfiles.d files installed. They create `/run/nologin` at boot, and on systemd it is removed by systemd-user-sessions. Finit now removes it when leaving diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index eb948a6d..a2617dcc 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -39,7 +39,4 @@ Missing options: `--root`, `--replace`, `--purge`, `--graceful`, and by that age, the age is not read * The `%T` and `%V` specifiers are always `/tmp` and `/var/tmp`, they do not look at `$TMPDIR` -* A `w` entry truncates the file, systemd writes over the start of - what is there. It only matters for a regular file, not in `/proc` - or `/sys` * `mksubsys()` in `src/util.c` is unused diff --git a/src/tmpfiles.c b/src/tmpfiles.c index fffe8af0..08a5d748 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -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; diff --git a/test/tmpfiles-arg.sh b/test/tmpfiles-arg.sh index 489dd067..f2b9b3d8 100755 --- a/test/tmpfiles-arg.sh +++ b/test/tmpfiles-arg.sh @@ -65,6 +65,16 @@ run "/libexec/finit/tmpfiles --create $CONF" assert "First match" "$(texec cat $DIR/w1 | tr '\n' ' ')" = "one two" assert "Second match" "$(texec cat $DIR/w2 | tr '\n' ' ')" = "one two" +sep +say 'w writes over the start of what is there, w+ adds to the end' +run "printf abcdef > $DIR/w1" +run "cat > $CONF" <<'CONF' +w /tmp/arg/w1 - - - - XY +w+ /tmp/arg/w1 - - - - Z +CONF +run "/libexec/finit/tmpfiles --create $CONF" +assert "Written in place" "$(texec cat $DIR/w1)" = "XYcdefZ" + sep say 'Escapes, and a # that is not at the start of the line' run "cat > $CONF" <<'CONF'