xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/gettid/gettid02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker /*\
6*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This test spawns multiple threads, then check for each one of them if the
9*49cdfc7eSAndroid Build Coastguard Worker  * parent ID is different AND if the thread ID is different from all the other
10*49cdfc7eSAndroid Build Coastguard Worker  * spwaned threads.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_pthread.h"
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define THREADS_NUM 10
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker static volatile pid_t tids[THREADS_NUM];
20*49cdfc7eSAndroid Build Coastguard Worker 
threaded(void * arg)21*49cdfc7eSAndroid Build Coastguard Worker static void *threaded(void *arg)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	int i = *(int *)arg;
24*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid, tid;
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	pid = tst_syscall(__NR_getpid);
27*49cdfc7eSAndroid Build Coastguard Worker 	tid = tst_syscall(__NR_gettid);
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(pid != tid,
30*49cdfc7eSAndroid Build Coastguard Worker 		"parent ID (%d) differs from thread[%d] ID (%d)",
31*49cdfc7eSAndroid Build Coastguard Worker 		pid, i, tid);
32*49cdfc7eSAndroid Build Coastguard Worker 	tids[i] = tid;
33*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker 
run(void)36*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t thread[THREADS_NUM];
39*49cdfc7eSAndroid Build Coastguard Worker 	int args[THREADS_NUM];
40*49cdfc7eSAndroid Build Coastguard Worker 	int error = 0;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	for (int i = 0; i < THREADS_NUM; i++) {
43*49cdfc7eSAndroid Build Coastguard Worker 		args[i] = i;
44*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_CREATE(&thread[i], NULL, threaded, &args[i]);
45*49cdfc7eSAndroid Build Coastguard Worker 	}
46*49cdfc7eSAndroid Build Coastguard Worker 	for (int i = 0; i < THREADS_NUM; i++)
47*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_JOIN(thread[i], NULL);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	for (int i = 0; i < THREADS_NUM; i++) {
50*49cdfc7eSAndroid Build Coastguard Worker 		for (int j = i + 1; j < THREADS_NUM; j++) {
51*49cdfc7eSAndroid Build Coastguard Worker 			if (tids[i] == tids[j]) {
52*49cdfc7eSAndroid Build Coastguard Worker 				tst_res(TINFO, "thread[%d] and thread[%d] have the same ID %d", i, j, tids[i]);
53*49cdfc7eSAndroid Build Coastguard Worker 				error = 1;
54*49cdfc7eSAndroid Build Coastguard Worker 			}
55*49cdfc7eSAndroid Build Coastguard Worker 		}
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	if (error)
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Some threads have the same TID");
60*49cdfc7eSAndroid Build Coastguard Worker 	else
61*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "All threads have a different TID");
62*49cdfc7eSAndroid Build Coastguard Worker }
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
65*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
66*49cdfc7eSAndroid Build Coastguard Worker };
67