1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
3f08c3bdfSopenharmony_ci * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci * (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci * the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci * along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * Verify that, pause() returns -1 and sets errno to EINTR after receipt of a
22f08c3bdfSopenharmony_ci * signal which is caught by the calling process. Also, verify that the calling
23f08c3bdfSopenharmony_ci * process will resume execution from the point of suspension.
24f08c3bdfSopenharmony_ci */
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#include <unistd.h>
27f08c3bdfSopenharmony_ci#include <errno.h>
28f08c3bdfSopenharmony_ci#include <fcntl.h>
29f08c3bdfSopenharmony_ci#include <sys/wait.h>
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#include "test.h"
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cichar *TCID = "pause02";
34f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
35f08c3bdfSopenharmony_cistatic pid_t cpid;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic void do_child(void);
38f08c3bdfSopenharmony_cistatic void setup(void);
39f08c3bdfSopenharmony_cistatic void cleanup(void);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ciint main(int ac, char **av)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	int lc;
44f08c3bdfSopenharmony_ci	int status;
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci#ifdef UCLINUX
49f08c3bdfSopenharmony_ci	maybe_run_child(&do_child, "");
50f08c3bdfSopenharmony_ci#endif
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	setup();
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
55f08c3bdfSopenharmony_ci		tst_count = 0;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci		cpid = FORK_OR_VFORK();
58f08c3bdfSopenharmony_ci		switch (cpid) {
59f08c3bdfSopenharmony_ci		case -1:
60f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "fork() failed");
61f08c3bdfSopenharmony_ci		break;
62f08c3bdfSopenharmony_ci		case 0:
63f08c3bdfSopenharmony_ci#ifdef UCLINUX
64f08c3bdfSopenharmony_ci			if (self_exec(av[0], "") < 0)
65f08c3bdfSopenharmony_ci				tst_brkm(TBROK, cleanup, "self_exec failed");
66f08c3bdfSopenharmony_ci#else
67f08c3bdfSopenharmony_ci			do_child();
68f08c3bdfSopenharmony_ci#endif
69f08c3bdfSopenharmony_ci		break;
70f08c3bdfSopenharmony_ci		default:
71f08c3bdfSopenharmony_ci		break;
72f08c3bdfSopenharmony_ci		}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci		/*
75f08c3bdfSopenharmony_ci		 * Wait for child to enter pause().
76f08c3bdfSopenharmony_ci		 */
77f08c3bdfSopenharmony_ci		TST_PROCESS_STATE_WAIT(cleanup, cpid, 'S');
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci		/*
80f08c3bdfSopenharmony_ci		 * Send the SIGINT signal now, so that child
81f08c3bdfSopenharmony_ci		 * returns from pause and resumes execution.
82f08c3bdfSopenharmony_ci		 */
83f08c3bdfSopenharmony_ci		kill(cpid, SIGINT);
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci		wait(&status);
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci		if (WIFEXITED(status)) {
88f08c3bdfSopenharmony_ci			if (WEXITSTATUS(status) == 0)
89f08c3bdfSopenharmony_ci				tst_resm(TPASS, "pause was interrupted correctly");
90f08c3bdfSopenharmony_ci			else
91f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "pause was interrupted but the "
92f08c3bdfSopenharmony_ci				                "retval and/or errno was wrong");
93f08c3bdfSopenharmony_ci			continue;
94f08c3bdfSopenharmony_ci		}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci		if (WIFSIGNALED(status)) {
97f08c3bdfSopenharmony_ci			switch (WTERMSIG(status)) {
98f08c3bdfSopenharmony_ci			case SIGALRM:
99f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "Timeout: SIGINT wasn't received by child");
100f08c3bdfSopenharmony_ci			break;
101f08c3bdfSopenharmony_ci			default:
102f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "Child killed by signal");
103f08c3bdfSopenharmony_ci			}
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci			continue;
106f08c3bdfSopenharmony_ci		}
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "Pause was not interrupted");
109f08c3bdfSopenharmony_ci	}
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	cleanup();
112f08c3bdfSopenharmony_ci	tst_exit();
113f08c3bdfSopenharmony_ci}
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_cistatic void sig_handle(int sig)
116f08c3bdfSopenharmony_ci{
117f08c3bdfSopenharmony_ci}
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_cistatic void do_child(void)
120f08c3bdfSopenharmony_ci{
121f08c3bdfSopenharmony_ci	/* avoid LTP framework to do whatever it likes */
122f08c3bdfSopenharmony_ci	signal(SIGALRM, SIG_DFL);
123f08c3bdfSopenharmony_ci
124f08c3bdfSopenharmony_ci	if (signal(SIGINT, sig_handle) == SIG_ERR) {
125f08c3bdfSopenharmony_ci		fprintf(stderr, "Child: Failed to setup signal handler\n");
126f08c3bdfSopenharmony_ci		exit(1);
127f08c3bdfSopenharmony_ci	}
128f08c3bdfSopenharmony_ci
129f08c3bdfSopenharmony_ci	/* Commit suicide after 10 seconds */
130f08c3bdfSopenharmony_ci	alarm(10);
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci	TEST(pause());
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci	if (TEST_RETURN == -1) {
135f08c3bdfSopenharmony_ci		if (TEST_ERRNO == EINTR)
136f08c3bdfSopenharmony_ci			exit(0);
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci		fprintf(stderr, "Child: Pause returned -1 but errno is %d (%s)\n",
139f08c3bdfSopenharmony_ci		        TEST_ERRNO, strerror(TEST_ERRNO));
140f08c3bdfSopenharmony_ci		exit(1);
141f08c3bdfSopenharmony_ci	}
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci	fprintf(stderr, "Child: Pause returned %ld\n", TEST_RETURN);
144f08c3bdfSopenharmony_ci	exit(1);
145f08c3bdfSopenharmony_ci}
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_cistatic void setup(void)
148f08c3bdfSopenharmony_ci{
149f08c3bdfSopenharmony_ci	tst_sig(FORK, DEF_HANDLER, cleanup);
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_ci	TEST_PAUSE;
152f08c3bdfSopenharmony_ci}
153f08c3bdfSopenharmony_ci
154f08c3bdfSopenharmony_cistatic void cleanup(void)
155f08c3bdfSopenharmony_ci{
156f08c3bdfSopenharmony_ci}
157