1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/limits.h>
12 #include "../kselftest.h"
14 #define MIN_TTY_PATH_LEN 8
16 static bool tty_valid(char *tty
)
18 if (strlen(tty
) < MIN_TTY_PATH_LEN
)
21 if (strncmp(tty
, "/dev/tty", MIN_TTY_PATH_LEN
) == 0 ||
22 strncmp(tty
, "/dev/pts", MIN_TTY_PATH_LEN
) == 0)
28 static int write_dev_tty(void)
33 f
= fopen("/dev/tty", "r+");
37 r
= fprintf(f
, "hello, world!\n");
38 if (r
!= strlen("hello, world!\n"))
45 int main(int argc
, char **argv
)
48 char tty
[PATH_MAX
] = {};
50 int result
= KSFT_FAIL
;
55 r
= readlink("/proc/self/fd/0", tty
, PATH_MAX
);
57 ksft_print_msg("readlink on /proc/self/fd/0 failed: %m\n");
61 if (!tty_valid(tty
)) {
62 ksft_print_msg("invalid tty path '%s'\n", tty
);
70 ksft_print_msg("stat failed on tty path '%s': %m\n", tty
);
74 /* We need to wait at least 8 seconds in order to observe timestamp change */
75 /* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fbf47635315ab308c9b58a1ea0906e711a9228de */
80 ksft_print_msg("failed to write to /dev/tty: %s\n",
87 ksft_print_msg("stat failed on tty path '%s': %m\n", tty
);
91 /* We wrote to the terminal so timestamps should have been updated */
92 if (st1
.st_atim
.tv_sec
== st2
.st_atim
.tv_sec
&&
93 st1
.st_mtim
.tv_sec
== st2
.st_mtim
.tv_sec
) {
94 ksft_print_msg("tty timestamps not updated\n");
99 "timestamps of terminal '%s' updated after write to /dev/tty\n", tty
);
103 ksft_test_result_report(result
, "tty_tstamp_update\n");