xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sigaltstack/sigaltstack02.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 /*
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker  * Ported by John George (2001)
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Verify that sigaltstack() fails with return value -1 and set expected errno:
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL on invalid value.
15*49cdfc7eSAndroid Build Coastguard Worker  * - ENOMEM on stack is < MINSIGSTKSZ.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_FLAGS	9999
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static stack_t sigstk;
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
26*49cdfc7eSAndroid Build Coastguard Worker 	int flag;
27*49cdfc7eSAndroid Build Coastguard Worker 	int size;
28*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
29*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
30*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
31*49cdfc7eSAndroid Build Coastguard Worker 	{INVAL_FLAGS, SIGSTKSZ, "Invalid Flag value", EINVAL},
32*49cdfc7eSAndroid Build Coastguard Worker 	/* use value low enough for all kernel versions
33*49cdfc7eSAndroid Build Coastguard Worker 	 * avoid using MINSIGSTKSZ defined by glibc as it could be different
34*49cdfc7eSAndroid Build Coastguard Worker 	 * from the one in kernel ABI
35*49cdfc7eSAndroid Build Coastguard Worker 	 */
36*49cdfc7eSAndroid Build Coastguard Worker 	{0, (2048 - 1), "alternate stack is < MINSIGSTKSZ", ENOMEM} };
37*49cdfc7eSAndroid Build Coastguard Worker 
check_sigaltstack(unsigned int nr)38*49cdfc7eSAndroid Build Coastguard Worker static void check_sigaltstack(unsigned int nr)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &tcases[nr];
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	sigstk.ss_size = tc->size;
43*49cdfc7eSAndroid Build Coastguard Worker 	sigstk.ss_flags = tc->flag;
44*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(sigaltstack(&sigstk, NULL), tc->exp_errno, "%s", tc->desc);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)47*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	sigstk.ss_sp = SAFE_MALLOC(SIGSTKSZ);
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)52*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	free(sigstk.ss_sp);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
58*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
59*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
60*49cdfc7eSAndroid Build Coastguard Worker 	.test = check_sigaltstack,
61*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
62*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1
63*49cdfc7eSAndroid Build Coastguard Worker };
64