1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2002
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/* 11/12/2002   Port to LTP     robbiew@us.ibm.com */
21/* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
22
23/*
24 * NAME
25 *	rename14.c - create and rename files
26 *
27 * CALLS
28 *	create, unlink, rename
29 *
30 * ALGORITHM
31 *	Creates two processes.  One creates and unlinks a file.
32 *	The other renames that file.
33 *
34 */
35
36#include <stdio.h>
37#include <errno.h>
38#include <signal.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <sys/wait.h>
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <fcntl.h>
45
46#include "test.h"
47
48#define FAILED 0
49#define PASSED 1
50
51int local_flag = PASSED;
52
53char *TCID = "rename14";
54int TST_TOTAL = 1;
55
56#define RUNTIME	5
57
58int kidpid[2];
59int parent_pid;
60
61int term(void);
62int al(void);
63void dochild1(void);
64void dochild2(void);
65
66int main(int argc, char *argv[])
67{
68	int pid;
69	sigset_t set;
70	struct sigaction act, oact;
71
72	tst_parse_opts(argc, argv, NULL, NULL);
73
74	sigemptyset(&set);
75	act.sa_handler = (void (*)())term;
76	act.sa_mask = set;
77	act.sa_flags = 0;
78	if (sigaction(SIGTERM, &act, &oact)) {
79		tst_brkm(TBROK, NULL, "Sigaction(SIGTERM)");
80	}
81
82	sigemptyset(&set);
83	act.sa_handler = (void (*)())al;
84	act.sa_mask = set;
85	act.sa_flags = 0;
86	if (sigaction(SIGALRM, &act, 0)) {
87		tst_brkm(TBROK, NULL, "Sigaction(SIGALRM)");
88	}
89	parent_pid = getpid();
90	tst_tmpdir();
91
92	pid = FORK_OR_VFORK();
93	if (pid < 0)
94		tst_brkm(TBROK, NULL, "fork() returned %d", pid);
95	if (pid == 0)
96		dochild1();
97
98	kidpid[0] = pid;
99	pid = FORK_OR_VFORK();
100	if (pid < 0) {
101		(void)kill(kidpid[0], SIGTERM);
102		(void)unlink("./rename14");
103		tst_brkm(TBROK, NULL, "fork() returned %d", pid);
104	}
105	if (pid == 0)
106		dochild2();
107
108	kidpid[1] = pid;
109
110	alarm(RUNTIME);
111
112	/* Collect child processes. */
113	/* Wait for timeout */
114	pause();
115
116	kill(kidpid[0], SIGTERM);
117	kill(kidpid[1], SIGTERM);
118
119	waitpid(kidpid[0], NULL, 0);
120	waitpid(kidpid[1], NULL, 0);
121
122	unlink("./rename14");
123	unlink("./rename14xyz");
124	(local_flag == PASSED) ? tst_resm(TPASS, "Test Passed")
125	    : tst_resm(TFAIL, "Test Failed");
126
127	tst_rmdir();
128	tst_exit();
129}
130
131int term(void)
132{
133	if (parent_pid != getpid())
134		exit(0);
135	if (kidpid[0])
136		return (kill(kidpid[0], SIGTERM));
137	if (kidpid[1])
138		return (kill(kidpid[1], SIGTERM));
139	return 0;
140}
141
142int al(void)
143{
144	if (kidpid[0])
145		return (kill(kidpid[0], SIGTERM));
146	if (kidpid[1])
147		return (kill(kidpid[1], SIGTERM));
148	return 0;
149}
150
151void dochild1(void)
152{
153	int fd;
154
155	for (;;) {
156		fd = creat("./rename14", 0666);
157		unlink("./rename14");
158		close(fd);
159	}
160}
161
162void dochild2(void)
163{
164	for (;;)
165		rename("./rename14", "./rename14xyz");
166}
167