death to trailing whitespace
[openafs.git] / src / tests / rename-under-feet.c
1 /*
2  * Copyright (c) 2000 - 2001 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <signal.h>
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/wait.h>
47 #include <unistd.h>
48 #include <dirent.h>
49
50 #include <err.h>
51
52 #define RETSIGTYPE void
53
54 static void
55 emkdir(const char *path, mode_t mode)
56 {
57     int ret = mkdir(path, mode);
58     if (ret < 0)
59         err(1, "mkdir %s", path);
60 }
61
62 static pid_t child_pid;
63
64 static sig_atomic_t term_sig = 0;
65
66 static RETSIGTYPE
67 child_sigterm(int signo)
68 {
69     term_sig = 1;
70 }
71
72 static int
73 child_chdir(const char *path)
74 {
75     int ret;
76     int pfd[2];
77
78     ret = pipe(pfd);
79     if (ret < 0)
80         err(1, "pipe");
81
82     child_pid = fork();
83     if (child_pid < 0)
84         err(1, "fork");
85     if (child_pid != 0) {
86         close(pfd[1]);
87         return pfd[0];
88     } else {
89         char buf[256];
90         struct sigaction sa;
91         FILE *fp;
92
93         sa.sa_handler = child_sigterm;
94         sigfillset(&sa.sa_mask);
95         sa.sa_flags = 0;
96         sigaction(SIGTERM, &sa, NULL);
97
98         close(pfd[0]);
99         ret = chdir(path);
100         if (ret < 0)
101             err(1, "chdir %s", path);
102         ret = write(pfd[1], "", 1);
103         if (ret != 1)
104             err(1, "write");
105         while (!term_sig)
106             pause();
107 #if 0
108         if (getcwd(buf, sizeof(buf)) == NULL)
109             err(1, "getcwd");
110 #endif
111         fp = fdopen(4, "w");
112         if (fp != NULL)
113             fprintf(fp, "child: cwd = %s\n", buf);
114         exit(0);
115     }
116 }
117
118 static void
119 kill_child(void)
120 {
121     kill(child_pid, SIGTERM);
122 }
123
124 int
125 main(int argc, char **argv)
126 {
127     struct stat sb;
128     int ret;
129     int fd;
130     char buf[1];
131     int status;
132
133
134     emkdir("one", 0777);
135     emkdir("two", 0777);
136     emkdir("one/a", 0777);
137
138     fd = child_chdir("one/a");
139     atexit(kill_child);
140     ret = read(fd, buf, 1);
141     if (ret < 0)
142         err(1, "read");
143     if (ret == 0)
144         errx(1, "EOF on read");
145
146     ret = rename("one/a", "two/a");
147     if (ret < 0)
148         err(1, "rename one/a two");
149     ret = lstat("two/a", &sb);
150     if (ret < 0)
151         err(1, "lstat two/a");
152     ret = lstat("one/a", &sb);
153     if (ret != -1 || errno != ENOENT)
154         errx(1, "one/a still exists");
155     kill_child();
156     waitpid(child_pid, &status, 0);
157     ret = lstat("one/a", &sb);
158     if (ret != -1 || errno != ENOENT)
159         errx(1, "one/a still exists after child");
160     if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
161         rmdir("one/a");
162         rmdir("two/a");
163         rmdir("one");
164         rmdir("two");
165         return 0;
166     } else
167         return 1;
168 }