1f08c3bdfSopenharmony_ci/******************************************************************************/
2f08c3bdfSopenharmony_ci/* Copyright (c) Crackerjack Project., 2007                                   */
3f08c3bdfSopenharmony_ci/*                                                                            */
4f08c3bdfSopenharmony_ci/* This program is free software;  you can redistribute it and/or modify      */
5f08c3bdfSopenharmony_ci/* it under the terms of the GNU General Public License as published by       */
6f08c3bdfSopenharmony_ci/* the Free Software Foundation; either version 2 of the License, or          */
7f08c3bdfSopenharmony_ci/* (at your option) any later version.                                        */
8f08c3bdfSopenharmony_ci/*                                                                            */
9f08c3bdfSopenharmony_ci/* This program is distributed in the hope that it will be useful,            */
10f08c3bdfSopenharmony_ci/* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
11f08c3bdfSopenharmony_ci/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
12f08c3bdfSopenharmony_ci/* the GNU General Public License for more details.                           */
13f08c3bdfSopenharmony_ci/*                                                                            */
14f08c3bdfSopenharmony_ci/* You should have received a copy of the GNU General Public License          */
15f08c3bdfSopenharmony_ci/* along with this program;  if not, write to the Free Software Foundation,   */
16f08c3bdfSopenharmony_ci/* Inc., 59 Temple Place, Suite TEST_SIG0, Boston, MA 02111-1307 USA          */
17f08c3bdfSopenharmony_ci/*                                                                            */
18f08c3bdfSopenharmony_ci/* History:     Porting from Crackerjack to LTP is done by                    */
19f08c3bdfSopenharmony_ci/*              Manas Kumar Nayak <maknayak@in.ibm.com>                       */
20f08c3bdfSopenharmony_ci/******************************************************************************/
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci/******************************************************************************/
23f08c3bdfSopenharmony_ci/* Description: This tests the rt_sigprocmask() syscall                       */
24f08c3bdfSopenharmony_ci/*		rt_sigprocmask changes the list of currently blocked signals. */
25f08c3bdfSopenharmony_ci/*		The set value stores the signal mask of the pending signals.  */
26f08c3bdfSopenharmony_ci/*		The previous action on the signal is saved in oact. The value */
27f08c3bdfSopenharmony_ci/*		of how indicates how the call should behave; its values are   */
28f08c3bdfSopenharmony_ci/*		as follows:						      */
29f08c3bdfSopenharmony_ci/*									      */
30f08c3bdfSopenharmony_ci/*		SIG_BLOCK						      */
31f08c3bdfSopenharmony_ci/*		    The set of blocked signals is the union of the current set*/
32f08c3bdfSopenharmony_ci/*		    and the set argument.				      */
33f08c3bdfSopenharmony_ci/*		SIG_UNBLOCK						      */
34f08c3bdfSopenharmony_ci/*		    The signals in set are removed from the current set of    */
35f08c3bdfSopenharmony_ci/*		    blocked signals. It is okay to unblock a signal that is   */
36f08c3bdfSopenharmony_ci/*		    not blocked.					      */
37f08c3bdfSopenharmony_ci/*		SIG_SETMASK						      */
38f08c3bdfSopenharmony_ci/*		    The set of blocked signals is set to the set argument.    */
39f08c3bdfSopenharmony_ci/*		    sigsetsize should indicate the size of a sigset_t type.   */
40f08c3bdfSopenharmony_ci/******************************************************************************/
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci#include <stdio.h>
43f08c3bdfSopenharmony_ci#include <signal.h>
44f08c3bdfSopenharmony_ci#include <errno.h>
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci#include "test.h"
47f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
48f08c3bdfSopenharmony_ci#include "lapi/rt_sigaction.h"
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cichar *TCID = "rt_sigprocmask01";
51f08c3bdfSopenharmony_cistatic int testno;
52f08c3bdfSopenharmony_ciint TST_TOTAL = 8;
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cistatic volatile sig_atomic_t sig_count;
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci#define TEST_SIG SIGRTMIN+1
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void cleanup(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	tst_rmdir();
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic void setup(void)
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	TEST_PAUSE;
66f08c3bdfSopenharmony_ci	tst_tmpdir();
67f08c3bdfSopenharmony_ci}
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_civoid sig_handler(int sig)
70f08c3bdfSopenharmony_ci{
71f08c3bdfSopenharmony_ci	sig_count++;
72f08c3bdfSopenharmony_ci}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ciint main(int ac, char **av)
75f08c3bdfSopenharmony_ci{
76f08c3bdfSopenharmony_ci	struct sigaction act, oact;
77f08c3bdfSopenharmony_ci	memset(&act, 0, sizeof(act));
78f08c3bdfSopenharmony_ci	memset(&oact, 0, sizeof(oact));
79f08c3bdfSopenharmony_ci	act.sa_handler = sig_handler;
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	sigset_t set, oset;
82f08c3bdfSopenharmony_ci	int lc;
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	setup();
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); ++lc) {
89f08c3bdfSopenharmony_ci		tst_count = 0;
90f08c3bdfSopenharmony_ci		for (testno = 0; testno < TST_TOTAL; ++testno) {
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci			if (sigemptyset(&set) < 0)
93f08c3bdfSopenharmony_ci				tst_brkm(TFAIL | TERRNO, cleanup,
94f08c3bdfSopenharmony_ci					 "sigemptyset call failed");
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci			if (sigaddset(&set, TEST_SIG) < 0)
97f08c3bdfSopenharmony_ci				tst_brkm(TFAIL | TERRNO, cleanup,
98f08c3bdfSopenharmony_ci					 "sigaddset call failed");
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci			/* call rt_sigaction() */
101f08c3bdfSopenharmony_ci			TEST(ltp_rt_sigaction(TEST_SIG, &act, &oact,
102f08c3bdfSopenharmony_ci						SIGSETSIZE));
103f08c3bdfSopenharmony_ci			if (TEST_RETURN < 0)
104f08c3bdfSopenharmony_ci				tst_brkm(TFAIL | TTERRNO, cleanup,
105f08c3bdfSopenharmony_ci					 "rt_sigaction call failed");
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci			/* call rt_sigprocmask() to block signal#TEST_SIG */
108f08c3bdfSopenharmony_ci			TEST(tst_syscall(__NR_rt_sigprocmask, SIG_BLOCK, &set,
109f08c3bdfSopenharmony_ci				     &oset, SIGSETSIZE));
110f08c3bdfSopenharmony_ci			if (TEST_RETURN == -1)
111f08c3bdfSopenharmony_ci				tst_brkm(TFAIL | TTERRNO, cleanup,
112f08c3bdfSopenharmony_ci					 "rt_sigprocmask call failed");
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci			/* Make sure that the masked process is indeed
115f08c3bdfSopenharmony_ci			 * masked. */
116f08c3bdfSopenharmony_ci			if (kill(getpid(), TEST_SIG) < 0)
117f08c3bdfSopenharmony_ci				tst_brkm(TFAIL | TERRNO, cleanup,
118f08c3bdfSopenharmony_ci					 "call to kill() failed");
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ci			if (sig_count) {
121f08c3bdfSopenharmony_ci				tst_brkm(TFAIL | TERRNO, cleanup,
122f08c3bdfSopenharmony_ci					 "rt_sigprocmask() failed to change "
123f08c3bdfSopenharmony_ci					 "the process's signal mask");
124f08c3bdfSopenharmony_ci			} else {
125f08c3bdfSopenharmony_ci				/* call rt_sigpending() */
126f08c3bdfSopenharmony_ci				TEST(tst_syscall(__NR_rt_sigpending, &oset,
127f08c3bdfSopenharmony_ci					     SIGSETSIZE));
128f08c3bdfSopenharmony_ci				if (TEST_RETURN == -1)
129f08c3bdfSopenharmony_ci					tst_brkm(TFAIL | TTERRNO, cleanup,
130f08c3bdfSopenharmony_ci						 "rt_sigpending call failed");
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci				TEST(sigismember(&oset, TEST_SIG));
133f08c3bdfSopenharmony_ci				if (TEST_RETURN == 0)
134f08c3bdfSopenharmony_ci					tst_brkm(TFAIL | TTERRNO,
135f08c3bdfSopenharmony_ci						 cleanup,
136f08c3bdfSopenharmony_ci						 "sigismember call failed");
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci				/* call rt_sigprocmask() to unblock
139f08c3bdfSopenharmony_ci				 * signal#TEST_SIG */
140f08c3bdfSopenharmony_ci				TEST(tst_syscall(__NR_rt_sigprocmask,
141f08c3bdfSopenharmony_ci					     SIG_UNBLOCK, &set, &oset,
142f08c3bdfSopenharmony_ci					     SIGSETSIZE));
143f08c3bdfSopenharmony_ci				if (TEST_RETURN == -1)
144f08c3bdfSopenharmony_ci					tst_brkm(TFAIL | TTERRNO,
145f08c3bdfSopenharmony_ci						 cleanup,
146f08c3bdfSopenharmony_ci						 "rt_sigprocmask call failed");
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_ci				if (sig_count) {
149f08c3bdfSopenharmony_ci					tst_resm(TPASS,
150f08c3bdfSopenharmony_ci						 "rt_sigprocmask "
151f08c3bdfSopenharmony_ci						 "functionality passed");
152f08c3bdfSopenharmony_ci					break;
153f08c3bdfSopenharmony_ci				} else {
154f08c3bdfSopenharmony_ci					tst_brkm(TFAIL | TERRNO,
155f08c3bdfSopenharmony_ci						 cleanup,
156f08c3bdfSopenharmony_ci						 "rt_sigprocmask "
157f08c3bdfSopenharmony_ci						 "functionality failed");
158f08c3bdfSopenharmony_ci				}
159f08c3bdfSopenharmony_ci			}
160f08c3bdfSopenharmony_ci
161f08c3bdfSopenharmony_ci		}
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci		tst_count++;
164f08c3bdfSopenharmony_ci
165f08c3bdfSopenharmony_ci	}
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_ci	cleanup();
168f08c3bdfSopenharmony_ci	tst_exit();
169f08c3bdfSopenharmony_ci}
170