Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / tty / tty_tstamp_update.c
blob9e1a40f5db178bf78921a3c76f93373907ec37f9
1 // SPDX-License-Identifier: GPL-2.0
3 #include <errno.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
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)
19 return false;
21 if (strncmp(tty, "/dev/tty", MIN_TTY_PATH_LEN) == 0 ||
22 strncmp(tty, "/dev/pts", MIN_TTY_PATH_LEN) == 0)
23 return true;
25 return false;
28 static int write_dev_tty(void)
30 FILE *f;
31 int r = 0;
33 f = fopen("/dev/tty", "r+");
34 if (!f)
35 return -errno;
37 r = fprintf(f, "hello, world!\n");
38 if (r != strlen("hello, world!\n"))
39 r = -EIO;
41 fclose(f);
42 return r;
45 int main(int argc, char **argv)
47 int r;
48 char tty[PATH_MAX] = {};
49 struct stat st1, st2;
50 int result = KSFT_FAIL;
52 ksft_print_header();
53 ksft_set_plan(1);
55 r = readlink("/proc/self/fd/0", tty, PATH_MAX);
56 if (r < 0) {
57 ksft_print_msg("readlink on /proc/self/fd/0 failed: %m\n");
58 goto out;
61 if (!tty_valid(tty)) {
62 ksft_print_msg("invalid tty path '%s'\n", tty);
63 result = KSFT_SKIP;
64 goto out;
68 r = stat(tty, &st1);
69 if (r < 0) {
70 ksft_print_msg("stat failed on tty path '%s': %m\n", tty);
71 goto out;
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 */
76 sleep(10);
78 r = write_dev_tty();
79 if (r < 0) {
80 ksft_print_msg("failed to write to /dev/tty: %s\n",
81 strerror(-r));
82 goto out;
85 r = stat(tty, &st2);
86 if (r < 0) {
87 ksft_print_msg("stat failed on tty path '%s': %m\n", tty);
88 goto out;
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");
95 goto out;
98 ksft_print_msg(
99 "timestamps of terminal '%s' updated after write to /dev/tty\n", tty);
100 result = KSFT_PASS;
102 out:
103 ksft_test_result_report(result, "tty_tstamp_update\n");
105 ksft_finished();