1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2002
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/* 11/12/2002   Port to LTP     robbiew@us.ibm.com */
21f08c3bdfSopenharmony_ci/* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci/*
24f08c3bdfSopenharmony_ci * NAME
25f08c3bdfSopenharmony_ci *	rename14.c - create and rename files
26f08c3bdfSopenharmony_ci *
27f08c3bdfSopenharmony_ci * CALLS
28f08c3bdfSopenharmony_ci *	create, unlink, rename
29f08c3bdfSopenharmony_ci *
30f08c3bdfSopenharmony_ci * ALGORITHM
31f08c3bdfSopenharmony_ci *	Creates two processes.  One creates and unlinks a file.
32f08c3bdfSopenharmony_ci *	The other renames that file.
33f08c3bdfSopenharmony_ci *
34f08c3bdfSopenharmony_ci */
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#include <stdio.h>
37f08c3bdfSopenharmony_ci#include <errno.h>
38f08c3bdfSopenharmony_ci#include <signal.h>
39f08c3bdfSopenharmony_ci#include <stdlib.h>
40f08c3bdfSopenharmony_ci#include <unistd.h>
41f08c3bdfSopenharmony_ci#include <sys/wait.h>
42f08c3bdfSopenharmony_ci#include <sys/types.h>
43f08c3bdfSopenharmony_ci#include <sys/stat.h>
44f08c3bdfSopenharmony_ci#include <fcntl.h>
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci#include "test.h"
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci#define FAILED 0
49f08c3bdfSopenharmony_ci#define PASSED 1
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ciint local_flag = PASSED;
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cichar *TCID = "rename14";
54f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci#define RUNTIME	5
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ciint kidpid[2];
59f08c3bdfSopenharmony_ciint parent_pid;
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ciint term(void);
62f08c3bdfSopenharmony_ciint al(void);
63f08c3bdfSopenharmony_civoid dochild1(void);
64f08c3bdfSopenharmony_civoid dochild2(void);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ciint main(int argc, char *argv[])
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	int pid;
69f08c3bdfSopenharmony_ci	sigset_t set;
70f08c3bdfSopenharmony_ci	struct sigaction act, oact;
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	sigemptyset(&set);
75f08c3bdfSopenharmony_ci	act.sa_handler = (void (*)())term;
76f08c3bdfSopenharmony_ci	act.sa_mask = set;
77f08c3bdfSopenharmony_ci	act.sa_flags = 0;
78f08c3bdfSopenharmony_ci	if (sigaction(SIGTERM, &act, &oact)) {
79f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "Sigaction(SIGTERM)");
80f08c3bdfSopenharmony_ci	}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	sigemptyset(&set);
83f08c3bdfSopenharmony_ci	act.sa_handler = (void (*)())al;
84f08c3bdfSopenharmony_ci	act.sa_mask = set;
85f08c3bdfSopenharmony_ci	act.sa_flags = 0;
86f08c3bdfSopenharmony_ci	if (sigaction(SIGALRM, &act, 0)) {
87f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "Sigaction(SIGALRM)");
88f08c3bdfSopenharmony_ci	}
89f08c3bdfSopenharmony_ci	parent_pid = getpid();
90f08c3bdfSopenharmony_ci	tst_tmpdir();
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	pid = FORK_OR_VFORK();
93f08c3bdfSopenharmony_ci	if (pid < 0)
94f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "fork() returned %d", pid);
95f08c3bdfSopenharmony_ci	if (pid == 0)
96f08c3bdfSopenharmony_ci		dochild1();
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	kidpid[0] = pid;
99f08c3bdfSopenharmony_ci	pid = FORK_OR_VFORK();
100f08c3bdfSopenharmony_ci	if (pid < 0) {
101f08c3bdfSopenharmony_ci		(void)kill(kidpid[0], SIGTERM);
102f08c3bdfSopenharmony_ci		(void)unlink("./rename14");
103f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "fork() returned %d", pid);
104f08c3bdfSopenharmony_ci	}
105f08c3bdfSopenharmony_ci	if (pid == 0)
106f08c3bdfSopenharmony_ci		dochild2();
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	kidpid[1] = pid;
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	alarm(RUNTIME);
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci	/* Collect child processes. */
113f08c3bdfSopenharmony_ci	/* Wait for timeout */
114f08c3bdfSopenharmony_ci	pause();
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	kill(kidpid[0], SIGTERM);
117f08c3bdfSopenharmony_ci	kill(kidpid[1], SIGTERM);
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_ci	waitpid(kidpid[0], NULL, 0);
120f08c3bdfSopenharmony_ci	waitpid(kidpid[1], NULL, 0);
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	unlink("./rename14");
123f08c3bdfSopenharmony_ci	unlink("./rename14xyz");
124f08c3bdfSopenharmony_ci	(local_flag == PASSED) ? tst_resm(TPASS, "Test Passed")
125f08c3bdfSopenharmony_ci	    : tst_resm(TFAIL, "Test Failed");
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci	tst_rmdir();
128f08c3bdfSopenharmony_ci	tst_exit();
129f08c3bdfSopenharmony_ci}
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ciint term(void)
132f08c3bdfSopenharmony_ci{
133f08c3bdfSopenharmony_ci	if (parent_pid != getpid())
134f08c3bdfSopenharmony_ci		exit(0);
135f08c3bdfSopenharmony_ci	if (kidpid[0])
136f08c3bdfSopenharmony_ci		return (kill(kidpid[0], SIGTERM));
137f08c3bdfSopenharmony_ci	if (kidpid[1])
138f08c3bdfSopenharmony_ci		return (kill(kidpid[1], SIGTERM));
139f08c3bdfSopenharmony_ci	return 0;
140f08c3bdfSopenharmony_ci}
141f08c3bdfSopenharmony_ci
142f08c3bdfSopenharmony_ciint al(void)
143f08c3bdfSopenharmony_ci{
144f08c3bdfSopenharmony_ci	if (kidpid[0])
145f08c3bdfSopenharmony_ci		return (kill(kidpid[0], SIGTERM));
146f08c3bdfSopenharmony_ci	if (kidpid[1])
147f08c3bdfSopenharmony_ci		return (kill(kidpid[1], SIGTERM));
148f08c3bdfSopenharmony_ci	return 0;
149f08c3bdfSopenharmony_ci}
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_civoid dochild1(void)
152f08c3bdfSopenharmony_ci{
153f08c3bdfSopenharmony_ci	int fd;
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ci	for (;;) {
156f08c3bdfSopenharmony_ci		fd = creat("./rename14", 0666);
157f08c3bdfSopenharmony_ci		unlink("./rename14");
158f08c3bdfSopenharmony_ci		close(fd);
159f08c3bdfSopenharmony_ci	}
160f08c3bdfSopenharmony_ci}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_civoid dochild2(void)
163f08c3bdfSopenharmony_ci{
164f08c3bdfSopenharmony_ci	for (;;)
165f08c3bdfSopenharmony_ci		rename("./rename14", "./rename14xyz");
166f08c3bdfSopenharmony_ci}
167