1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2013 Red Hat, Inc.
4 */
5
6 #include "config.h"
7 #include <check.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/ptrace.h>
13 #include <sys/resource.h>
14 #include <sys/wait.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <libevdev/libevdev.h>
18
19 #include "test-common.h"
20
21 static int
is_debugger_attached(void)22 is_debugger_attached(void)
23 {
24 int rc = 1;
25 /*
26 * FreeBSD does not support PTRACE_ATTACH, disable attaching a debugger
27 * on FreeBSD by skipping the rest of the function and just return 1.
28 */
29 #ifndef __FreeBSD__
30 int status;
31 int pid = fork();
32
33 if (pid == -1)
34 return 0;
35
36 if (pid == 0) {
37 int ppid = getppid();
38 if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) == 0) {
39 waitpid(ppid, NULL, 0);
40 ptrace(PTRACE_CONT, NULL, NULL);
41 ptrace(PTRACE_DETACH, ppid, NULL, NULL);
42 rc = 0;
43 }
44 _exit(rc);
45 } else {
46 waitpid(pid, &status, 0);
47 rc = WEXITSTATUS(status);
48 }
49
50 #endif /* !__FreeBSD__ */
51 return rc;
52 }
53
54 static bool
device_nodes_exist(void)55 device_nodes_exist(void)
56 {
57 struct stat st;
58 int rc;
59
60 rc = stat("/dev/uinput", &st);
61 if (rc == -1 && errno == ENOENT)
62 return false;
63
64 rc = stat("/dev/input", &st);
65 if (rc == -1 && errno == ENOENT)
66 return false;
67
68 /* Any issues but ENOENT we just let the test suite blow up later */
69 return true;
70 }
71
72 extern const struct libevdev_test __start_test_section, __stop_test_section;
73
main(void)74 int main(void)
75 {
76 const struct libevdev_test *t;
77 const struct rlimit corelimit = {0, 0};
78 int failed;
79
80 for (t = &__start_test_section; t < &__stop_test_section; t++) {
81 if (t->needs_root_privileges) {
82 if (getenv("LIBEVDEV_SKIP_ROOT_TESTS"))
83 return 77;
84
85 if (getuid() != 0) {
86 fprintf(stderr, "This test needs to run as root\n");
87 return 77;
88 }
89 if (!device_nodes_exist()) {
90 fprintf(stderr, "This test needs /dev/input and /dev/uinput to exist\n");
91 return 77;
92 }
93
94 break;
95 }
96 }
97
98 if (is_debugger_attached())
99 setenv("CK_FORK", "no", 0);
100
101 if (setrlimit(RLIMIT_CORE, &corelimit) != 0)
102 perror("WARNING: Core dumps not disabled. Reason");
103
104 libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
105
106 SRunner *sr = srunner_create(NULL);
107 for (t = &__start_test_section; t < &__stop_test_section; t++) {
108 srunner_add_suite(sr, t->setup());
109 }
110
111 srunner_run_all(sr, CK_NORMAL);
112
113 failed = srunner_ntests_failed(sr);
114 srunner_free(sr);
115
116 return failed;
117 }
118