162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission to use, copy, modify, and distribute this software for any
562306a36Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
662306a36Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
962306a36Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1062306a36Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1162306a36Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1262306a36Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1362306a36Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1462306a36Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci// Test that /proc/thread-self gives correct TGID/PID.
1762306a36Sopenharmony_ci#undef NDEBUG
1862306a36Sopenharmony_ci#include <assert.h>
1962306a36Sopenharmony_ci#include <sched.h>
2062306a36Sopenharmony_ci#include <stdio.h>
2162306a36Sopenharmony_ci#include <unistd.h>
2262306a36Sopenharmony_ci#include <sys/mman.h>
2362306a36Sopenharmony_ci#include <sys/wait.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include "proc.h"
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ciint f(void *arg)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	char buf1[64], buf2[64];
3062306a36Sopenharmony_ci	pid_t pid, tid;
3162306a36Sopenharmony_ci	ssize_t rv;
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	pid = sys_getpid();
3462306a36Sopenharmony_ci	tid = sys_gettid();
3562306a36Sopenharmony_ci	snprintf(buf1, sizeof(buf1), "%u/task/%u", pid, tid);
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	rv = readlink("/proc/thread-self", buf2, sizeof(buf2));
3862306a36Sopenharmony_ci	assert(rv == strlen(buf1));
3962306a36Sopenharmony_ci	buf2[rv] = '\0';
4062306a36Sopenharmony_ci	assert(streq(buf1, buf2));
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	if (arg)
4362306a36Sopenharmony_ci		exit(0);
4462306a36Sopenharmony_ci	return 0;
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciint main(void)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	const int PAGE_SIZE = sysconf(_SC_PAGESIZE);
5062306a36Sopenharmony_ci	pid_t pid;
5162306a36Sopenharmony_ci	void *stack;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	/* main thread */
5462306a36Sopenharmony_ci	f((void *)0);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	stack = mmap(NULL, 2 * PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
5762306a36Sopenharmony_ci	assert(stack != MAP_FAILED);
5862306a36Sopenharmony_ci	/* side thread */
5962306a36Sopenharmony_ci	pid = clone(f, stack + PAGE_SIZE, CLONE_THREAD|CLONE_SIGHAND|CLONE_VM, (void *)1);
6062306a36Sopenharmony_ci	assert(pid > 0);
6162306a36Sopenharmony_ci	pause();
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	return 0;
6462306a36Sopenharmony_ci}
65