blob: 90231d405d3c0687b6c3c73e1cb4c0034c2c4af9 [file] [log] [blame]
robbiewd6c7b1f2002-12-04 18:41:18 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewd6c7b1f2002-12-04 18:41:18 +000018 */
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
subrata_modakbdbaec52009-02-26 12:14:51 +000036#include <stdio.h>
robbiewa70576c2003-03-04 18:33:41 +000037#include <errno.h>
robbiewd6c7b1f2002-12-04 18:41:18 +000038#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"
robbiewd6c7b1f2002-12-04 18:41:18 +000048
49#define FAILED 0
50#define PASSED 1
51
52int local_flag = PASSED;
robbiewd6c7b1f2002-12-04 18:41:18 +000053
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020054char *TCID = "rename14";
55int TST_TOTAL = 1;
robbiewd6c7b1f2002-12-04 18:41:18 +000056/**************/
57
robbiewd6c7b1f2002-12-04 18:41:18 +000058#define RUNTIME 45
59
60int kidpid[2];
61int parent_pid;
62
Mike Frysingerc57fba52014-04-09 18:56:30 -040063int main(int argc, char *argv[])
robbiewd6c7b1f2002-12-04 18:41:18 +000064{
subrata_modak48f20312007-04-19 06:24:06 +000065 int pid;
robbiewd6c7b1f2002-12-04 18:41:18 +000066 sigset_t set;
67 struct sigaction act, oact;
subrata_modak56207ce2009-03-23 13:35:39 +000068 int term();
69 int al();
robbiewd6c7b1f2002-12-04 18:41:18 +000070 void dochild1();
71 void dochild2();
72
robbiewd34d5812005-07-11 22:28:09 +000073#ifdef UCLINUX
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020074 const char *msg;
robbiewd34d5812005-07-11 22:28:09 +000075
Garrett Cooper53740502010-12-16 00:04:01 -080076 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
robbiewd34d5812005-07-11 22:28:09 +000077 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +000078
79 maybe_run_child(&dochild1, "n", 1);
80 maybe_run_child(&dochild2, "n", 2);
81#endif
robbiewd6c7b1f2002-12-04 18:41:18 +000082 sigemptyset(&set);
subrata_modak56207ce2009-03-23 13:35:39 +000083 act.sa_handler = (void (*)())term;
robbiewd6c7b1f2002-12-04 18:41:18 +000084 act.sa_mask = set;
85 act.sa_flags = 0;
86 if (sigaction(SIGTERM, &act, &oact)) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010087 tst_brkm(TBROK, NULL, "Sigaction(SIGTERM)");
robbiewd6c7b1f2002-12-04 18:41:18 +000088 }
89
90 sigemptyset(&set);
subrata_modak56207ce2009-03-23 13:35:39 +000091 act.sa_handler = (void (*)())al;
robbiewd6c7b1f2002-12-04 18:41:18 +000092 act.sa_mask = set;
93 act.sa_flags = 0;
94 if (sigaction(SIGALRM, &act, 0)) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010095 tst_brkm(TBROK, NULL, "Sigaction(SIGALRM)");
subrata_modak4bb656a2009-02-26 12:02:09 +000096 }
robbiewd6c7b1f2002-12-04 18:41:18 +000097 parent_pid = getpid();
98 tst_tmpdir();
99/*--------------------------------------------------------------*/
100
robbiewd34d5812005-07-11 22:28:09 +0000101 pid = FORK_OR_VFORK();
robbiewd6c7b1f2002-12-04 18:41:18 +0000102 if (pid < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100103 tst_brkm(TBROK, NULL, "fork() returned %d", pid);
robbiewd6c7b1f2002-12-04 18:41:18 +0000104 }
105 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000106#ifdef UCLINUX
107 if (self_exec(argv[0], "n", 1) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000108 tst_resm(TBROK, "self_exec failed");
robbiewd34d5812005-07-11 22:28:09 +0000109 }
110#else
robbiewd6c7b1f2002-12-04 18:41:18 +0000111 dochild1();
robbiewd34d5812005-07-11 22:28:09 +0000112#endif
robbiewd6c7b1f2002-12-04 18:41:18 +0000113 }
114 kidpid[0] = pid;
robbiewd34d5812005-07-11 22:28:09 +0000115 pid = FORK_OR_VFORK();
robbiewd6c7b1f2002-12-04 18:41:18 +0000116 if (pid < 0) {
117 (void)kill(kidpid[0], SIGTERM);
118 (void)unlink("./rename14");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100119 tst_brkm(TBROK, NULL, "fork() returned %d", pid);
robbiewd6c7b1f2002-12-04 18:41:18 +0000120 }
121 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000122#ifdef UCLINUX
123 if (self_exec(argv[0], "n", 1) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000124 tst_resm(TBROK, "self_exec failed");
robbiewd34d5812005-07-11 22:28:09 +0000125 }
126#else
robbiewd6c7b1f2002-12-04 18:41:18 +0000127 dochild2();
robbiewd34d5812005-07-11 22:28:09 +0000128#endif
robbiewd6c7b1f2002-12-04 18:41:18 +0000129 }
130 kidpid[1] = pid;
131
132 alarm(RUNTIME);
133
134 /* Collect child processes. */
mreed109e9306f2007-02-28 16:17:16 +0000135 /* Wait for timeout */
136 pause();
subrata_modak4bb656a2009-02-26 12:02:09 +0000137
robbiewd6c7b1f2002-12-04 18:41:18 +0000138 kill(kidpid[0], SIGTERM);
139 kill(kidpid[1], SIGTERM);
140
subrata_modak8bf4e962008-03-14 16:22:27 +0000141 waitpid(kidpid[0], NULL, 0);
142 waitpid(kidpid[1], NULL, 0);
143
robbiewd6c7b1f2002-12-04 18:41:18 +0000144 unlink("./rename14");
145 unlink("./rename14xyz");
subrata_modak56207ce2009-03-23 13:35:39 +0000146 (local_flag == PASSED) ? tst_resm(TPASS, "Test Passed")
147 : tst_resm(TFAIL, "Test Failed");
robbiewd6c7b1f2002-12-04 18:41:18 +0000148
149 tst_rmdir();
Garrett Cooper53740502010-12-16 00:04:01 -0800150 tst_exit();
robbiewd6c7b1f2002-12-04 18:41:18 +0000151}
subrata_modak56207ce2009-03-23 13:35:39 +0000152
robbiewd6c7b1f2002-12-04 18:41:18 +0000153/* FUNCTIONS GO HERE */
154
Mike Frysingerc57fba52014-04-09 18:56:30 -0400155int term(void)
robbiewd6c7b1f2002-12-04 18:41:18 +0000156{
157 if (parent_pid != getpid())
158 exit(0);
159 if (kidpid[0])
subrata_modak56207ce2009-03-23 13:35:39 +0000160 return (kill(kidpid[0], SIGTERM));
robbiewd6c7b1f2002-12-04 18:41:18 +0000161 if (kidpid[1])
subrata_modak56207ce2009-03-23 13:35:39 +0000162 return (kill(kidpid[1], SIGTERM));
163 return 0;
robbiewd6c7b1f2002-12-04 18:41:18 +0000164}
165
Mike Frysingerc57fba52014-04-09 18:56:30 -0400166int al(void)
robbiewd6c7b1f2002-12-04 18:41:18 +0000167{
168 if (kidpid[0])
subrata_modak56207ce2009-03-23 13:35:39 +0000169 return (kill(kidpid[0], SIGTERM));
robbiewd6c7b1f2002-12-04 18:41:18 +0000170 if (kidpid[1])
subrata_modak56207ce2009-03-23 13:35:39 +0000171 return (kill(kidpid[1], SIGTERM));
172 return 0;
robbiewd6c7b1f2002-12-04 18:41:18 +0000173}
174
Mike Frysingerc57fba52014-04-09 18:56:30 -0400175void dochild1(void)
robbiewd6c7b1f2002-12-04 18:41:18 +0000176{
177 int fd;
178
179 for (;;) {
180 fd = creat("./rename14", 0666);
181 unlink("./rename14");
182 close(fd);
183 }
184}
185
Mike Frysingerc57fba52014-04-09 18:56:30 -0400186void dochild2(void)
robbiewd6c7b1f2002-12-04 18:41:18 +0000187{
188 for (;;) {
189 rename("./rename14", "./rename14xyz");
190 }
Chris Dearmanec6edca2012-10-17 19:54:01 -0700191}