]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
creds: if newline is explicitly requested, skip tty check
authorMike Yuan <me@yhndnzj.com>
Wed, 25 Mar 2026 15:00:14 +0000 (16:00 +0100)
committerMike Yuan <me@yhndnzj.com>
Thu, 26 Mar 2026 15:00:42 +0000 (16:00 +0100)
Before this commit, the > 0 state of arg_newline tristate is
simply ignored.

Yes, this is a minor compat break, but I'd argue the previous
behavior was not useful as "yes" is treated the same as "auto".
An issue also reported that it was quite surprising.

Fixes #41348

src/creds/creds.c
test/units/TEST-54-CREDS.sh

index 9e1b2cfbcc46f4c32810f7ce24e9721786ae856d..3103686a9f8e3ecbf5e51d0a9c4b9878af4a8996 100644 (file)
@@ -398,8 +398,6 @@ static int transcode(
 }
 
 static int print_newline(FILE *f, const char *data, size_t l) {
-        int fd;
-
         assert(f);
         assert(data || l == 0);
 
@@ -411,10 +409,12 @@ static int print_newline(FILE *f, const char *data, size_t l) {
         if (l > 0 && data[l-1] == '\n')
                 return 0;
 
-        /* Don't bother unless this is a tty */
-        fd = fileno(f);
-        if (fd >= 0 && !isatty_safe(fd))
-                return 0;
+        /* If not explicitly requested, don't bother if the output is not a tty */
+        if (arg_newline < 0) {
+                int fd = fileno(f);
+                if (fd >= 0 && !isatty_safe(fd))
+                        return 0;
+        }
 
         if (fputc('\n', f) != '\n')
                 return log_error_errno(errno, "Failed to write trailing newline: %m");
index 0eaf8a2dfb0d80345dec98ac8b24a26868fee1e1..523046ec765b0d4b8113bf2c0d90a958792ca61b 100755 (executable)
@@ -114,10 +114,9 @@ run_with_cred_compare "mycred:" "" cat mycred
 run_with_cred_compare "mycred:\n" "\n" cat mycred
 run_with_cred_compare "mycred:foo" "foo" cat mycred
 run_with_cred_compare "mycred:foo" "foofoofoo" cat mycred mycred mycred
-# Note: --newline= does nothing when stdout is not a tty, which is the case here
-run_with_cred_compare "mycred:foo" "foo" --newline=yes cat mycred
-run_with_cred_compare "mycred:foo" "foo" --newline=no cat mycred
 run_with_cred_compare "mycred:foo" "foo" --newline=auto cat mycred
+run_with_cred_compare "mycred:foo" "foo" --newline=no cat mycred
+run_with_cred_compare "mycred:foo" "foo\n" --newline=yes cat mycred
 run_with_cred_compare "mycred:foo" "foo" --transcode=no cat mycred
 run_with_cred_compare "mycred:foo" "foo" --transcode=0 cat mycred
 run_with_cred_compare "mycred:foo" "foo" --transcode=false cat mycred