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