1*e7679a5cSAlexey Dobriyan /*
2*e7679a5cSAlexey Dobriyan * Copyright (c) 2024 Alexey Dobriyan <adobriyan@gmail.com>
3*e7679a5cSAlexey Dobriyan *
4*e7679a5cSAlexey Dobriyan * Permission to use, copy, modify, and distribute this software for any
5*e7679a5cSAlexey Dobriyan * purpose with or without fee is hereby granted, provided that the above
6*e7679a5cSAlexey Dobriyan * copyright notice and this permission notice appear in all copies.
7*e7679a5cSAlexey Dobriyan *
8*e7679a5cSAlexey Dobriyan * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*e7679a5cSAlexey Dobriyan * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*e7679a5cSAlexey Dobriyan * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*e7679a5cSAlexey Dobriyan * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*e7679a5cSAlexey Dobriyan * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*e7679a5cSAlexey Dobriyan * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*e7679a5cSAlexey Dobriyan * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*e7679a5cSAlexey Dobriyan */
16*e7679a5cSAlexey Dobriyan /* Test that kernel thread is reported as such. */
17*e7679a5cSAlexey Dobriyan #undef NDEBUG
18*e7679a5cSAlexey Dobriyan #include <assert.h>
19*e7679a5cSAlexey Dobriyan #include <errno.h>
20*e7679a5cSAlexey Dobriyan #include <fcntl.h>
21*e7679a5cSAlexey Dobriyan #include <string.h>
22*e7679a5cSAlexey Dobriyan #include <unistd.h>
23*e7679a5cSAlexey Dobriyan
main(void)24*e7679a5cSAlexey Dobriyan int main(void)
25*e7679a5cSAlexey Dobriyan {
26*e7679a5cSAlexey Dobriyan /*
27*e7679a5cSAlexey Dobriyan * The following solutions don't really work:
28*e7679a5cSAlexey Dobriyan *
29*e7679a5cSAlexey Dobriyan * 1) jit kernel module which creates kernel thread:
30*e7679a5cSAlexey Dobriyan * test becomes arch-specific,
31*e7679a5cSAlexey Dobriyan * problems with mandatory module signing,
32*e7679a5cSAlexey Dobriyan * problems with lockdown mode,
33*e7679a5cSAlexey Dobriyan * doesn't work with CONFIG_MODULES=n at all,
34*e7679a5cSAlexey Dobriyan * kthread creation API is formally unstable internal kernel API,
35*e7679a5cSAlexey Dobriyan * need a mechanism to report test kernel thread's PID back,
36*e7679a5cSAlexey Dobriyan *
37*e7679a5cSAlexey Dobriyan * 2) ksoftirqd/0 and kswapd0 look like stable enough kernel threads,
38*e7679a5cSAlexey Dobriyan * but their PIDs are unstable.
39*e7679a5cSAlexey Dobriyan *
40*e7679a5cSAlexey Dobriyan * Check against kthreadd which always seem to exist under pid 2.
41*e7679a5cSAlexey Dobriyan */
42*e7679a5cSAlexey Dobriyan int fd = open("/proc/2/status", O_RDONLY);
43*e7679a5cSAlexey Dobriyan assert(fd >= 0);
44*e7679a5cSAlexey Dobriyan
45*e7679a5cSAlexey Dobriyan char buf[4096];
46*e7679a5cSAlexey Dobriyan ssize_t rv = read(fd, buf, sizeof(buf));
47*e7679a5cSAlexey Dobriyan assert(0 <= rv && rv < sizeof(buf));
48*e7679a5cSAlexey Dobriyan buf[rv] = '\0';
49*e7679a5cSAlexey Dobriyan
50*e7679a5cSAlexey Dobriyan assert(strstr(buf, "Kthread:\t1\n"));
51*e7679a5cSAlexey Dobriyan
52*e7679a5cSAlexey Dobriyan return 0;
53*e7679a5cSAlexey Dobriyan }
54