]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use isatty_safe() more 34044/head
authorLennart Poettering <lennart@poettering.net>
Tue, 20 Aug 2024 08:33:52 +0000 (10:33 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Aug 2024 09:11:53 +0000 (11:11 +0200)
19 files changed:
src/analyze/analyze-srk.c
src/basic/terminal-util.c
src/basic/user-util.c
src/busctl/busctl.c
src/core/exec-invoke.c
src/dissect/dissect.c
src/journal/journald.c
src/nspawn/nspawn.c
src/partition/repart.c
src/run/run.c
src/shared/ask-password-agent.c
src/shared/ask-password-api.c
src/shared/exec-util.c
src/shared/format-table.c
src/shared/polkit-agent.c
src/storagetm/storagetm.c
src/test/test-format-table.c
src/varlinkctl/varlinkctl.c
src/vmspawn/vmspawn-register.c

index acfd8b0c47ff70b13897af3f771613e94a03f70c..adbc51316afd47772897c78400206211e4e9eb22 100644 (file)
@@ -33,7 +33,7 @@ int verb_srk(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to marshal SRK: %m");
 
-        if (isatty(STDOUT_FILENO))
+        if (isatty_safe(STDOUT_FILENO))
                 return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                        "Refusing to write binary data to TTY, please redirect output to file.");
 
index d6de92ee89d6c2acc2b23f111ba834b291f28b10..a97312c9bf4117f840b96dbdbb520fd6c4f9168b 100644 (file)
@@ -1052,8 +1052,8 @@ bool on_tty(void) {
 
         if (cached_on_tty < 0)
                 cached_on_tty =
-                        isatty(STDOUT_FILENO) > 0 &&
-                        isatty(STDERR_FILENO) > 0;
+                        isatty_safe(STDOUT_FILENO) &&
+                        isatty_safe(STDERR_FILENO);
 
         return cached_on_tty;
 }
index 6bdf5bf1cdc9a1536b6cde2bc5edf5e11507ca1c..858d712189631a1388c0e83ab88c39518fa46023 100644 (file)
@@ -27,6 +27,7 @@
 #include "random-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "terminal-util.h"
 #include "user-util.h"
 #include "utf8.h"
 
@@ -120,7 +121,7 @@ char* getlogname_malloc(void) {
         uid_t uid;
         struct stat st;
 
-        if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
+        if (isatty_safe(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
                 uid = st.st_uid;
         else
                 uid = getuid();
index 3735337a800d430f77d3ed121b1ceb196bdfa504..1b0b72085e6465e900053ef8e8b47ec9bd8c42ef 100644 (file)
@@ -1401,7 +1401,7 @@ static int verb_capture(int argc, char **argv, void *userdata) {
                 "busctl (systemd) " PROJECT_VERSION_FULL " (Git " GIT_VERSION ")";
         int r;
 
-        if (isatty(STDOUT_FILENO))
+        if (isatty_safe(STDOUT_FILENO))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Refusing to write message data to console, please redirect output to a file.");
 
index 6bef4e7675e2e05fcba38e000c3451ef4c618117..a867480300e848a7b7ff176253df66696a3c25fd 100644 (file)
@@ -354,7 +354,7 @@ static int setup_input(
                         return -errno;
 
                 /* Try to make this the controlling tty, if it is a tty */
-                if (isatty(STDIN_FILENO))
+                if (isatty_safe(STDIN_FILENO))
                         (void) ioctl(STDIN_FILENO, TIOCSCTTY, context->std_input == EXEC_INPUT_TTY_FORCE);
 
                 return STDIN_FILENO;
index 1ea2f248d8e917eb15a21a30311747c84334ef9b..1cad45c8ff94b08091aa3333b9241a3aaa2ad025 100644 (file)
@@ -718,7 +718,7 @@ static int parse_argv(int argc, char *argv[]) {
         if (!IN_SET(arg_action, ACTION_DISSECT, ACTION_LIST, ACTION_MTREE, ACTION_COPY_FROM, ACTION_COPY_TO, ACTION_DISCOVER, ACTION_VALIDATE) && geteuid() != 0)
                 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be root.");
 
-        SET_FLAG(arg_flags, DISSECT_IMAGE_ALLOW_INTERACTIVE_AUTH, isatty(STDIN_FILENO));
+        SET_FLAG(arg_flags, DISSECT_IMAGE_ALLOW_INTERACTIVE_AUTH, isatty_safe(STDIN_FILENO));
 
         return 1;
 }
@@ -1639,7 +1639,7 @@ static int action_list_or_mtree_or_copy_or_make_archive(DissectedImage *m, LoopD
 
                         r = sym_archive_write_open_FILE(a, f);
                 } else {
-                        if (isatty(STDOUT_FILENO))
+                        if (isatty_safe(STDOUT_FILENO))
                                 return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Refusing to write archive to TTY.");
 
                         r = sym_archive_write_open_fd(a, STDOUT_FILENO);
@@ -1999,12 +1999,12 @@ static int action_validate(void) {
         if (r < 0)
                 return r;
 
-        if (isatty(STDOUT_FILENO) && emoji_enabled())
+        if (isatty_safe(STDOUT_FILENO) && emoji_enabled())
                 printf("%s ", special_glyph(SPECIAL_GLYPH_SPARKLES));
 
         printf("%sOK%s", ansi_highlight_green(), ansi_normal());
 
-        if (isatty(STDOUT_FILENO) && emoji_enabled())
+        if (isatty_safe(STDOUT_FILENO) && emoji_enabled())
                 printf(" %s", special_glyph(SPECIAL_GLYPH_SPARKLES));
 
         putc('\n', stdout);
index 87ed22e7fe91c441a591a32e2914871e27dc10aa..84f44c46b44f794ab482b489f15feb6c412bee3b 100644 (file)
@@ -36,7 +36,7 @@ static int run(int argc, char *argv[]) {
                  * daemon when it comes to logging hence LOG_TARGET_AUTO won't do the right thing for
                  * us. Hence explicitly log to the console if we're started from a console or to kmsg
                  * otherwise. */
-                log_target = isatty(STDERR_FILENO) ? LOG_TARGET_CONSOLE : LOG_TARGET_KMSG;
+                log_target = isatty_safe(STDERR_FILENO) ? LOG_TARGET_CONSOLE : LOG_TARGET_KMSG;
 
                 log_set_prohibit_ipc(true); /* better safe than sorry */
                 log_set_target(log_target);
index 4a9693f3b71201acbba15f069d90b1daa6567f9d..8c68a2058c5f3f0dca4cf99d6b6ebc46db35e9d5 100644 (file)
@@ -290,7 +290,7 @@ static int handle_arg_console(const char *arg) {
         else if (streq(arg, "passive"))
                 arg_console_mode = CONSOLE_PASSIVE;
         else if (streq(arg, "pipe")) {
-                if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))
+                if (isatty_safe(STDIN_FILENO) && isatty(STDOUT_FILENO))
                         log_full(arg_quiet ? LOG_DEBUG : LOG_NOTICE,
                                  "Console mode 'pipe' selected, but standard input/output are connected to an interactive TTY. "
                                  "Most likely you want to use 'interactive' console mode for proper interactivity and shell job control. "
@@ -298,7 +298,7 @@ static int handle_arg_console(const char *arg) {
 
                 arg_console_mode = CONSOLE_PIPE;
         } else if (streq(arg, "autopipe")) {
-                if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))
+                if (isatty_safe(STDIN_FILENO) && isatty(STDOUT_FILENO))
                         arg_console_mode = CONSOLE_INTERACTIVE;
                 else
                         arg_console_mode = CONSOLE_PIPE;
@@ -5883,7 +5883,7 @@ static int run(int argc, char *argv[]) {
         umask(0022);
 
         if (arg_console_mode < 0)
-                arg_console_mode = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) ?
+                arg_console_mode = isatty_safe(STDIN_FILENO) && isatty(STDOUT_FILENO) ?
                                    CONSOLE_INTERACTIVE : CONSOLE_READ_ONLY;
 
         if (arg_console_mode == CONSOLE_PIPE) /* if we pass STDERR on to the container, don't add our own logs into it too */
index 7db493cafc323ef24861bfcec3251fcb503e2c06..6f03161702940310d1da67cc61f8a2792c1a505c 100644 (file)
@@ -7701,7 +7701,7 @@ static int parse_argv(int argc, char *argv[]) {
                         return log_oom();
         }
 
-        if (arg_pretty < 0 && isatty(STDOUT_FILENO))
+        if (arg_pretty < 0 && isatty_safe(STDOUT_FILENO))
                 arg_pretty = true;
 
         if (arg_architecture >= 0) {
index 2db60e6bb8f497adc6be8c821d6ca9a2517777a9..7fd76ebced56a75437498e47e004ceb8230a2fa8 100644 (file)
@@ -674,7 +674,7 @@ static int parse_argv(int argc, char *argv[]) {
                 /* If we both --pty and --pipe are specified we'll automatically pick --pty if we are connected fully
                  * to a TTY and pick direct fd passing otherwise. This way, we automatically adapt to usage in a shell
                  * pipeline, but we are neatly interactive with tty-level isolation otherwise. */
-                arg_stdio = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) ?
+                arg_stdio = isatty_safe(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) ?
                         ARG_STDIO_PTY :
                         ARG_STDIO_DIRECT;
 
@@ -908,7 +908,7 @@ static int parse_argv_sudo_mode(int argc, char *argv[]) {
         arg_wait = true;
         arg_aggressive_gc = true;
 
-        arg_stdio = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) ? ARG_STDIO_PTY : ARG_STDIO_DIRECT;
+        arg_stdio = isatty_safe(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) ? ARG_STDIO_PTY : ARG_STDIO_DIRECT;
         arg_expand_environment = false;
         arg_send_sighup = true;
 
@@ -1176,7 +1176,7 @@ static int transient_service_set_properties(sd_bus_message *m, const char *pty_p
                 if (r < 0)
                         return bus_log_create_error(r);
 
-                send_term = isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO);
+                send_term = isatty_safe(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO);
         }
 
         if (send_term) {
index 60281d3ae94c5d8de2ff6042a24967d6ff91114c..e425f825db805fe58ab754860963e68257834681 100644 (file)
@@ -8,6 +8,7 @@
 #include "exec-util.h"
 #include "log.h"
 #include "process-util.h"
+#include "terminal-util.h"
 
 static pid_t agent_pid = 0;
 
@@ -17,9 +18,8 @@ int ask_password_agent_open(void) {
         if (agent_pid > 0)
                 return 0;
 
-        /* We check STDIN here, not STDOUT, since this is about input,
-         * not output */
-        if (!isatty(STDIN_FILENO))
+        /* We check STDIN here, not STDOUT, since this is about input, not output */
+        if (!isatty_safe(STDIN_FILENO))
                 return 0;
 
         if (!is_main_thread())
index 778b31274d7ed83cc33da3bb39594c9cb9dd407e..7706c3c2719a02a72a6a56dec01ed3b8753f811a 100644 (file)
@@ -988,14 +988,14 @@ int ask_password_auto(
 
         if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) &&
             req && req->keyring &&
-            (FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) || !isatty(STDIN_FILENO)) &&
+            (FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) || !isatty_safe(STDIN_FILENO)) &&
             FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT)) {
                 r = ask_password_keyring(req, flags, ret);
                 if (r != -ENOKEY)
                         return r;
         }
 
-        if (!FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) && isatty(STDIN_FILENO))
+        if (!FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) && isatty_safe(STDIN_FILENO))
                 return ask_password_tty(-EBADF, req, until, flags, NULL, ret);
 
         if (!FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT))
index 213d660a2e2904b203d5d3d2d1b00891049e2545..870f8f66d82430f0039972502c45b4e7a50cfc6a 100644 (file)
@@ -555,8 +555,8 @@ int _fork_agent(const char *name, const int except[], size_t n_except, pid_t *re
 
         /* In the child: */
 
-        stdout_is_tty = isatty(STDOUT_FILENO);
-        stderr_is_tty = isatty(STDERR_FILENO);
+        stdout_is_tty = isatty_safe(STDOUT_FILENO);
+        stderr_is_tty = isatty_safe(STDERR_FILENO);
 
         if (!stdout_is_tty || !stderr_is_tty) {
                 int fd;
index ee641d1144429801ee5081a37dc65b6a15f5afdb..4cf4b67f6db161890ec932241e7cf87ea36fb6d4 100644 (file)
@@ -2391,7 +2391,7 @@ int table_print(Table *t, FILE *f) {
                 if (t->width != 0 && t->width != SIZE_MAX)
                         table_effective_width = t->width;
                 else if (t->width == 0 ||
-                         ((pass > 0 || !any_soft) && (pager_have() || !isatty(STDOUT_FILENO))))
+                         ((pass > 0 || !any_soft) && (pager_have() || !isatty_safe(STDOUT_FILENO))))
                         table_effective_width = table_requested_width;
                 else
                         table_effective_width = MIN(table_requested_width, columns());
index 2f38965cf276459b224c109912a124e57e0712f3..92e576c9d259d0048196338cbc44a16d3f6a02fd 100644 (file)
@@ -14,6 +14,7 @@
 #include "polkit-agent.h"
 #include "process-util.h"
 #include "stdio-util.h"
+#include "terminal-util.h"
 #include "time-util.h"
 
 #if ENABLE_POLKIT
@@ -31,7 +32,7 @@ int polkit_agent_open(void) {
                 return 0;
 
         /* We check STDIN here, not STDOUT, since this is about input, not output */
-        if (!isatty(STDIN_FILENO))
+        if (!isatty_safe(STDIN_FILENO))
                 return 0;
 
         if (!is_main_thread())
index 910746dc346a223d5094b6c4ed44d9c4998a6acc..ca8e886d37de6202edbfefd2fcda11512dad6b92 100644 (file)
@@ -1065,7 +1065,7 @@ static int on_display_refresh(sd_event_source *s, uint64_t usec, void *userdata)
 
         c->display_refresh_scheduled = false;
 
-        if (isatty(STDERR_FILENO))
+        if (isatty_safe(STDERR_FILENO))
                 fputs(ANSI_HOME_CLEAR, stderr);
 
         /* If we have both IPv4 and IPv6, we display IPv4 info via Plymouth, since it doesn't have much
@@ -1247,7 +1247,7 @@ static int run(int argc, char* argv[]) {
         if (r < 0)
                 return log_error_errno(r, "Failed to subscribe to RTM_DELADDR events: %m");
 
-        if (isatty(STDIN_FILENO))
+        if (isatty_safe(STDIN_FILENO))
                 log_info("Hit Ctrl-C to exit target mode.");
 
         _unused_ _cleanup_(notify_on_cleanup) const char *notify_message =
index 41621dbb5b36621ac181e8a865514dea3364e333..f074c5b6f7a660af484ae2591c26d879a448952c 100644 (file)
@@ -7,6 +7,7 @@
 #include "json-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "terminal-util.h"
 #include "tests.h"
 #include "time-util.h"
 
@@ -562,7 +563,7 @@ TEST(table) {
         assert_se(table_format(t, &formatted) >= 0);
         printf("%s\n", formatted);
 
-        if (isatty(STDOUT_FILENO))
+        if (isatty_safe(STDOUT_FILENO))
                 assert_se(streq(formatted,
                                 "no   a long f… no   a long f… a long fi…\n"
                                 "no   fäää      no   fäää      fäää\n"
index aae6032da562fbba4c5111f1efa16cc82652f122..77cecdbf4e9fdf236b1a459792ee7a634a00e569 100644 (file)
@@ -504,7 +504,7 @@ static int verb_call(int argc, char *argv[], void *userdata) {
                 /* <argv[4]> is correct, as dispatch_verb() shifts arguments by one for the verb. */
                 r = sd_json_parse_with_source(parameter, source, 0, &jp, &line, &column);
         } else {
-                if (isatty(STDIN_FILENO) > 0 && !arg_quiet)
+                if (isatty_safe(STDIN_FILENO) && !arg_quiet)
                         log_notice("Expecting method call parameter JSON object on standard input. (Provide empty string or {} for no parameters.)");
 
                 source = "<stdin>";
index 474cbd5df9a709f87172071d35cc9594255a7a91..20e6def360fb0f9b5043ab94a0e8468f5e630999 100644 (file)
@@ -12,6 +12,7 @@
 #include "process-util.h"
 #include "socket-util.h"
 #include "string-util.h"
+#include "terminal-util.h"
 #include "vmspawn-register.h"
 #include "varlink-util.h"
 
@@ -73,7 +74,7 @@ int register_machine(
                         SD_JSON_BUILD_PAIR_CONDITION(!!directory, "rootDirectory", SD_JSON_BUILD_STRING(directory)),
                         SD_JSON_BUILD_PAIR_CONDITION(!!address, "sshAddress", SD_JSON_BUILD_STRING(address)),
                         SD_JSON_BUILD_PAIR_CONDITION(!!key_path, "sshPrivateKeyPath", SD_JSON_BUILD_STRING(key_path)),
-                        SD_JSON_BUILD_PAIR_CONDITION(isatty(STDIN_FILENO), "allowInteractiveAuthentication", SD_JSON_BUILD_BOOLEAN(true)),
+                        SD_JSON_BUILD_PAIR_CONDITION(isatty_safe(STDIN_FILENO), "allowInteractiveAuthentication", SD_JSON_BUILD_BOOLEAN(true)),
                         SD_JSON_BUILD_PAIR_CONDITION(!keep_unit, "allocateUnit", SD_JSON_BUILD_BOOLEAN(true)));
 }