1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"@(#)rm_lnkcnt_zero_file.c	1.3	07/05/25 SMI"
28 
29 /*
30  * --------------------------------------------------------------------
31  * The purpose of this test is to see if the bug reported (#4723351) for
32  * UFS exists when using a ZFS file system.
33  * --------------------------------------------------------------------
34  *
35  */
36 #define	_REENTRANT 1
37 #include <stdio.h>
38 #include <fcntl.h>
39 #include <pthread.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 
46 static const int TRUE = 1;
47 static char *filebase;
48 
49 static int
50 pickidx()
51 {
52 	return (random() % 1000);
53 }
54 
55 /* ARGSUSED */
56 static void *
57 mover(void *a)
58 {
59 	char buf[256];
60 	int idx, ret;
61 
62 	while (TRUE) {
63 		idx = pickidx();
64 		(void) sprintf(buf, "%s.%03d", filebase, idx);
65 		ret = rename(filebase, buf);
66 		if (ret < 0 && errno != ENOENT)
67 			(void) perror("renaming file");
68 	}
69 
70 	return (NULL);
71 }
72 
73 /* ARGSUSED */
74 static void *
75 cleaner(void *a)
76 {
77 	char buf[256];
78 	int idx, ret;
79 
80 	while (TRUE) {
81 		idx = pickidx();
82 		(void) sprintf(buf, "%s.%03d", filebase, idx);
83 		ret = remove(buf);
84 		if (ret < 0 && errno != ENOENT)
85 			(void) perror("removing file");
86 	}
87 
88 	return (NULL);
89 }
90 
91 static void *
92 writer(void *a)
93 {
94 	int *fd = (int *)a;
95 
96 	while (TRUE) {
97 		(void) close (*fd);
98 		*fd = open(filebase, O_APPEND | O_RDWR | O_CREAT, 0644);
99 		if (*fd < 0)
100 			perror("refreshing file");
101 		(void) write(*fd, "test\n", 5);
102 	}
103 
104 	return (NULL);
105 }
106 
107 int
108 main(int argc, char **argv)
109 {
110 	int fd;
111 	pthread_t tid;
112 
113 	if (argc == 1) {
114 		(void) printf("Usage: %s <filebase>\n", argv[0]);
115 		exit(-1);
116 	}
117 
118 	filebase = argv[1];
119 	fd = open(filebase, O_APPEND | O_RDWR | O_CREAT, 0644);
120 	if (fd < 0) {
121 		perror("creating test file");
122 		exit(-1);
123 	}
124 
125 	if (pthread_setconcurrency(4)) {	/* 3 threads + main */
126 		fprintf(stderr, "failed to set concurrency\n");
127 		exit(-1);
128 	}
129 	(void) pthread_create(&tid, NULL, mover, NULL);
130 	(void) pthread_create(&tid, NULL, cleaner, NULL);
131 	(void) pthread_create(&tid, NULL, writer, (void *) &fd);
132 
133 	while (TRUE) {
134 		int ret;
135 		struct stat st;
136 
137 		ret = stat(filebase, &st);
138 		if (ret == 0 && (st.st_nlink > 2 || st.st_nlink < 1)) {
139 			(void) printf("st.st_nlink = %d, exiting\n", \
140 			    (int)st.st_nlink);
141 			exit(0);
142 		}
143 		(void) sleep(1);
144 	}
145 
146 	return (0);
147 }
148