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