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 <wait.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 
46 /** LTP Port **/
47 #include "test.h"
48 
49 #define FAILED 0
50 #define PASSED 1
51 
52 int local_flag = PASSED;
53 
54 char *TCID = "rename14";
55 int TST_TOTAL = 1;
56 /**************/
57 
58 #define RUNTIME	45
59 
60 int kidpid[2];
61 int parent_pid;
62 
main(int argc,char * argv[])63 int main(int argc, char *argv[])
64 {
65 	int pid;
66 	sigset_t set;
67 	struct sigaction act, oact;
68 	int term();
69 	int al();
70 	void dochild1();
71 	void dochild2();
72 
73 #ifdef UCLINUX
74 
75 	tst_parse_opts(argc, argv, NULL, NULL);
76 
77 	maybe_run_child(&dochild1, "n", 1);
78 	maybe_run_child(&dochild2, "n", 2);
79 #endif
80 	sigemptyset(&set);
81 	act.sa_handler = (void (*)())term;
82 	act.sa_mask = set;
83 	act.sa_flags = 0;
84 	if (sigaction(SIGTERM, &act, &oact)) {
85 		tst_brkm(TBROK, NULL, "Sigaction(SIGTERM)");
86 	}
87 
88 	sigemptyset(&set);
89 	act.sa_handler = (void (*)())al;
90 	act.sa_mask = set;
91 	act.sa_flags = 0;
92 	if (sigaction(SIGALRM, &act, 0)) {
93 		tst_brkm(TBROK, NULL, "Sigaction(SIGALRM)");
94 	}
95 	parent_pid = getpid();
96 	tst_tmpdir();
97 /*--------------------------------------------------------------*/
98 
99 	pid = FORK_OR_VFORK();
100 	if (pid < 0) {
101 		tst_brkm(TBROK, NULL, "fork() returned %d", pid);
102 	}
103 	if (pid == 0) {
104 #ifdef UCLINUX
105 		if (self_exec(argv[0], "n", 1) < 0) {
106 			tst_resm(TBROK, "self_exec failed");
107 		}
108 #else
109 		dochild1();
110 #endif
111 	}
112 	kidpid[0] = pid;
113 	pid = FORK_OR_VFORK();
114 	if (pid < 0) {
115 		(void)kill(kidpid[0], SIGTERM);
116 		(void)unlink("./rename14");
117 		tst_brkm(TBROK, NULL, "fork() returned %d", pid);
118 	}
119 	if (pid == 0) {
120 #ifdef UCLINUX
121 		if (self_exec(argv[0], "n", 1) < 0) {
122 			tst_resm(TBROK, "self_exec failed");
123 		}
124 #else
125 		dochild2();
126 #endif
127 	}
128 	kidpid[1] = pid;
129 
130 	alarm(RUNTIME);
131 
132 	/* Collect child processes. */
133 	/* Wait for timeout */
134 	pause();
135 
136 	kill(kidpid[0], SIGTERM);
137 	kill(kidpid[1], SIGTERM);
138 
139 	waitpid(kidpid[0], NULL, 0);
140 	waitpid(kidpid[1], NULL, 0);
141 
142 	unlink("./rename14");
143 	unlink("./rename14xyz");
144 	(local_flag == PASSED) ? tst_resm(TPASS, "Test Passed")
145 	    : tst_resm(TFAIL, "Test Failed");
146 
147 	tst_rmdir();
148 	tst_exit();
149 }
150 
151 /* FUNCTIONS GO HERE */
152 
term(void)153 int term(void)
154 {
155 	if (parent_pid != getpid())
156 		exit(0);
157 	if (kidpid[0])
158 		return (kill(kidpid[0], SIGTERM));
159 	if (kidpid[1])
160 		return (kill(kidpid[1], SIGTERM));
161 	return 0;
162 }
163 
al(void)164 int al(void)
165 {
166 	if (kidpid[0])
167 		return (kill(kidpid[0], SIGTERM));
168 	if (kidpid[1])
169 		return (kill(kidpid[1], SIGTERM));
170 	return 0;
171 }
172 
dochild1(void)173 void dochild1(void)
174 {
175 	int fd;
176 
177 	for (;;) {
178 		fd = creat("./rename14", 0666);
179 		unlink("./rename14");
180 		close(fd);
181 	}
182 }
183 
dochild2(void)184 void dochild2(void)
185 {
186 	for (;;) {
187 		rename("./rename14", "./rename14xyz");
188 	}
189 }
190