2 * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 // Test that /proc/$KERNEL_THREAD/fd/ is empty.
19 #include <sys/syscall.h>
25 #include <sys/types.h>
32 #define PF_KHTREAD 0x00200000
35 * Test for kernel threadness atomically with openat().
37 * Return /proc/$PID/fd descriptor if process is kernel thread.
38 * Return -1 if a process is userspace process.
40 static int kernel_thread_fd(unsigned int pid
)
42 unsigned int flags
= 0;
47 snprintf(buf
, sizeof(buf
), "/proc/%u", pid
);
48 dir_fd
= open(buf
, O_RDONLY
|O_DIRECTORY
);
53 * Believe it or not, struct task_struct::flags is directly exposed
56 fd
= openat(dir_fd
, "stat", O_RDONLY
);
61 rv
= read(fd
, buf
, sizeof(buf
));
63 if (0 < rv
&& rv
<= sizeof(buf
)) {
64 unsigned long long flags_ull
;
68 assert(buf
[rv
- 1] == '\n');
71 /* Search backwards: ->comm can contain whitespace and ')'. */
72 for (i
= 0; i
< 43; i
++) {
73 p
= strrchr(buf
, ' ');
78 p
= strrchr(buf
, ' ');
81 flags_ull
= xstrtoull(p
+ 1, &end
);
83 assert(flags_ull
== (unsigned int)flags_ull
);
89 if (flags
& PF_KHTREAD
) {
90 fd
= openat(dir_fd
, "fd", O_RDONLY
|O_DIRECTORY
);
96 static void test_readdir(int fd
)
105 assert(streq(de
->d_name
, "."));
106 assert(de
->d_type
== DT_DIR
);
109 assert(streq(de
->d_name
, ".."));
110 assert(de
->d_type
== DT_DIR
);
116 static inline int sys_statx(int dirfd
, const char *pathname
, int flags
,
117 unsigned int mask
, void *stx
)
119 return syscall(SYS_statx
, dirfd
, pathname
, flags
, mask
, stx
);
122 static void test_lookup_fail(int fd
, const char *pathname
)
124 char stx
[256] __attribute__((aligned(8)));
127 rv
= sys_statx(fd
, pathname
, AT_SYMLINK_NOFOLLOW
, 0, (void *)stx
);
128 assert(rv
== -1 && errno
== ENOENT
);
131 static void test_lookup(int fd
)
137 for (i
= INT_MIN
; i
< INT_MIN
+ 1024; i
++) {
138 snprintf(buf
, sizeof(buf
), "%d", i
);
139 test_lookup_fail(fd
, buf
);
141 for (i
= -1024; i
< 1024; i
++) {
142 snprintf(buf
, sizeof(buf
), "%d", i
);
143 test_lookup_fail(fd
, buf
);
145 for (u
= INT_MAX
- 1024; u
< (unsigned int)INT_MAX
+ 1024; u
++) {
146 snprintf(buf
, sizeof(buf
), "%u", u
);
147 test_lookup_fail(fd
, buf
);
149 for (u
= UINT_MAX
- 1024; u
!= 0; u
++) {
150 snprintf(buf
, sizeof(buf
), "%u", u
);
151 test_lookup_fail(fd
, buf
);
161 * In theory this will loop indefinitely if kernel threads are exiled
164 * Start with kthreadd.
167 while ((fd
= kernel_thread_fd(pid
)) == -1 && pid
< 1024) {
170 /* EACCES if run as non-root. */