xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/syscall/syscall01.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) International Business Machines Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  * 01/02/2003	Port to LTP [email protected]
5*49cdfc7eSAndroid Build Coastguard Worker  * 06/30/2001	Port to Linux [email protected]
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*
9*49cdfc7eSAndroid Build Coastguard Worker  * Basic test for syscall().
10*49cdfc7eSAndroid Build Coastguard Worker  */
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
13*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
verify_getpid(void)20*49cdfc7eSAndroid Build Coastguard Worker static void verify_getpid(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker 	pid_t p1, p2;
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker 	p1 = getpid();
25*49cdfc7eSAndroid Build Coastguard Worker 	p2 = syscall(SYS_getpid);
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	if (p1 == p2) {
28*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "getpid() == syscall(SYS_getpid)");
29*49cdfc7eSAndroid Build Coastguard Worker 	} else {
30*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "getpid() = %i, syscall(SYS_getpid) = %i",
31*49cdfc7eSAndroid Build Coastguard Worker 			p1, p2);
32*49cdfc7eSAndroid Build Coastguard Worker 	}
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker 
verify_getuid(void)35*49cdfc7eSAndroid Build Coastguard Worker static void verify_getuid(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	uid_t u1, u2;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	u1 = getuid();
40*49cdfc7eSAndroid Build Coastguard Worker #ifdef SYS_getuid32
41*49cdfc7eSAndroid Build Coastguard Worker 	u2 = syscall(SYS_getuid32);
42*49cdfc7eSAndroid Build Coastguard Worker #else
43*49cdfc7eSAndroid Build Coastguard Worker 	u2 = syscall(SYS_getuid);
44*49cdfc7eSAndroid Build Coastguard Worker #endif
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (u1 == u2) {
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "getuid() == syscall(SYS_getuid)");
48*49cdfc7eSAndroid Build Coastguard Worker 	} else {
49*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "getuid() = %i, syscall(SYS_getuid) = %i",
50*49cdfc7eSAndroid Build Coastguard Worker 			u1, u2);
51*49cdfc7eSAndroid Build Coastguard Worker 	}
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker 
verify_getgid(void)54*49cdfc7eSAndroid Build Coastguard Worker static void verify_getgid(void)
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker 	gid_t g1, g2;
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	g1 = getgid();
59*49cdfc7eSAndroid Build Coastguard Worker #ifdef SYS_getgid32
60*49cdfc7eSAndroid Build Coastguard Worker 	g2 = syscall(SYS_getgid32);
61*49cdfc7eSAndroid Build Coastguard Worker #else
62*49cdfc7eSAndroid Build Coastguard Worker 	g2 = syscall(SYS_getgid);
63*49cdfc7eSAndroid Build Coastguard Worker #endif
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	if (g1 == g2) {
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "getgid() == syscall(SYS_getgid)");
67*49cdfc7eSAndroid Build Coastguard Worker 	} else {
68*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "getgid() = %i, syscall(SYS_getgid) = %i",
69*49cdfc7eSAndroid Build Coastguard Worker 			g1, g2);
70*49cdfc7eSAndroid Build Coastguard Worker 	}
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker static void (*tcases[])(void) = {
75*49cdfc7eSAndroid Build Coastguard Worker 	verify_getpid,
76*49cdfc7eSAndroid Build Coastguard Worker 	verify_getuid,
77*49cdfc7eSAndroid Build Coastguard Worker 	verify_getgid,
78*49cdfc7eSAndroid Build Coastguard Worker };
79*49cdfc7eSAndroid Build Coastguard Worker 
verify_syscall(unsigned int n)80*49cdfc7eSAndroid Build Coastguard Worker static void verify_syscall(unsigned int n)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	tcases[n]();
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
86*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_syscall,
87*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
88*49cdfc7eSAndroid Build Coastguard Worker };
89*49cdfc7eSAndroid Build Coastguard Worker 
90