1*2cd36fb3SAlexey Dobriyan /*
2*2cd36fb3SAlexey Dobriyan * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
3*2cd36fb3SAlexey Dobriyan *
4*2cd36fb3SAlexey Dobriyan * Permission to use, copy, modify, and distribute this software for any
5*2cd36fb3SAlexey Dobriyan * purpose with or without fee is hereby granted, provided that the above
6*2cd36fb3SAlexey Dobriyan * copyright notice and this permission notice appear in all copies.
7*2cd36fb3SAlexey Dobriyan *
8*2cd36fb3SAlexey Dobriyan * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*2cd36fb3SAlexey Dobriyan * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*2cd36fb3SAlexey Dobriyan * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*2cd36fb3SAlexey Dobriyan * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*2cd36fb3SAlexey Dobriyan * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*2cd36fb3SAlexey Dobriyan * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*2cd36fb3SAlexey Dobriyan * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*2cd36fb3SAlexey Dobriyan */
16*2cd36fb3SAlexey Dobriyan // Test that /proc/thread-self gives correct TGID/PID.
17*2cd36fb3SAlexey Dobriyan #undef NDEBUG
18*2cd36fb3SAlexey Dobriyan #include <assert.h>
19*2cd36fb3SAlexey Dobriyan #include <sched.h>
20*2cd36fb3SAlexey Dobriyan #include <stdio.h>
21*2cd36fb3SAlexey Dobriyan #include <unistd.h>
22*2cd36fb3SAlexey Dobriyan #include <sys/mman.h>
23*2cd36fb3SAlexey Dobriyan #include <sys/wait.h>
24*2cd36fb3SAlexey Dobriyan
25*2cd36fb3SAlexey Dobriyan #include "proc.h"
26*2cd36fb3SAlexey Dobriyan
f(void * arg)27*2cd36fb3SAlexey Dobriyan int f(void *arg)
28*2cd36fb3SAlexey Dobriyan {
29*2cd36fb3SAlexey Dobriyan char buf1[64], buf2[64];
30*2cd36fb3SAlexey Dobriyan pid_t pid, tid;
31*2cd36fb3SAlexey Dobriyan ssize_t rv;
32*2cd36fb3SAlexey Dobriyan
33*2cd36fb3SAlexey Dobriyan pid = sys_getpid();
34*2cd36fb3SAlexey Dobriyan tid = sys_gettid();
35*2cd36fb3SAlexey Dobriyan snprintf(buf1, sizeof(buf1), "%u/task/%u", pid, tid);
36*2cd36fb3SAlexey Dobriyan
37*2cd36fb3SAlexey Dobriyan rv = readlink("/proc/thread-self", buf2, sizeof(buf2));
38*2cd36fb3SAlexey Dobriyan assert(rv == strlen(buf1));
39*2cd36fb3SAlexey Dobriyan buf2[rv] = '\0';
40*2cd36fb3SAlexey Dobriyan assert(streq(buf1, buf2));
41*2cd36fb3SAlexey Dobriyan
42*2cd36fb3SAlexey Dobriyan if (arg)
43*2cd36fb3SAlexey Dobriyan exit(0);
44*2cd36fb3SAlexey Dobriyan return 0;
45*2cd36fb3SAlexey Dobriyan }
46*2cd36fb3SAlexey Dobriyan
main(void)47*2cd36fb3SAlexey Dobriyan int main(void)
48*2cd36fb3SAlexey Dobriyan {
49*2cd36fb3SAlexey Dobriyan const int PAGE_SIZE = sysconf(_SC_PAGESIZE);
50*2cd36fb3SAlexey Dobriyan pid_t pid;
51*2cd36fb3SAlexey Dobriyan void *stack;
52*2cd36fb3SAlexey Dobriyan
53*2cd36fb3SAlexey Dobriyan /* main thread */
54*2cd36fb3SAlexey Dobriyan f((void *)0);
55*2cd36fb3SAlexey Dobriyan
56*2cd36fb3SAlexey Dobriyan stack = mmap(NULL, 2 * PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
57*2cd36fb3SAlexey Dobriyan assert(stack != MAP_FAILED);
58*2cd36fb3SAlexey Dobriyan /* side thread */
59*2cd36fb3SAlexey Dobriyan pid = clone(f, stack + PAGE_SIZE, CLONE_THREAD|CLONE_SIGHAND|CLONE_VM, (void *)1);
60*2cd36fb3SAlexey Dobriyan assert(pid > 0);
61*2cd36fb3SAlexey Dobriyan pause();
62*2cd36fb3SAlexey Dobriyan
63*2cd36fb3SAlexey Dobriyan return 0;
64*2cd36fb3SAlexey Dobriyan }
65