1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8048020
* @author Daniel Fuchs
* @summary Regression on java.util.logging.FileHandler.
* The fix is to avoid filling up the file system with zombie lock files.
*
* @run main/othervm CheckZombieLockTest WRITABLE CLOSE CLEANUP
* @run main/othervm CheckZombieLockTest CLEANUP
* @run main/othervm CheckZombieLockTest WRITABLE
* @run main/othervm CheckZombieLockTest CREATE_FIRST
* @run main/othervm CheckZombieLockTest CREATE_NEXT
* @run main/othervm CheckZombieLockTest CREATE_NEXT
* @run main/othervm CheckZombieLockTest CLEANUP
* @run main/othervm CheckZombieLockTest REUSE
* @run main/othervm CheckZombieLockTest CLEANUP
* @key randomness
*/
import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class CheckZombieLockTest {
private static final String WRITABLE_DIR = "writable-lockfile-dir";
private static volatile boolean supportsLocking = true;
static enum TestCase {
WRITABLE, // just verifies that we can create a file in our 'writable-lockfile-dir'
CLOSE, // checks that closing a FileHandler removes its lock file
CREATE_FIRST, // verifies that 'writable-lockfile-dir' contains no lock, then creates a first FileHandler.
CREATE_NEXT, // verifies that 'writable-lockfile-dir' contains a single lock, then creates the next FileHandler
REUSE, // verifies that zombie lock files can be reused
CLEANUP // removes "writable-lockfile-dir"
};
public static void main(String... args) throws IOException {
// we'll base all file creation attempts on the system temp directory,
// %t
File writableDir = setup();
System.out.println("Writable dir is: " + writableDir.getAbsolutePath());
// we now have one writable directory to work with:
// writableDir
if (args == null || args.length == 0) {
args = new String[] { "WRITABLE", "CLOSE", "CLEANUP" };
}
try {
runTests(writableDir, args);
} catch (RuntimeException | IOException | Error x) {
// some error occured: cleanup
delete(writableDir);
throw x;
}
}
/**
* @param writableDir in which log and lock file are created
* @throws SecurityException
* @throws RuntimeException
* @throws IOException
*/
private static void runTests(File writableDir, String... args) throws SecurityException,
RuntimeException, IOException {
for (String arg : args) {
switch(TestCase.valueOf(arg)) {
// Test 1: makes sure we can create FileHandler in writable directory
case WRITABLE: checkWritable(writableDir); break;
// Test 2: verifies that FileHandler.close() cleans up its lock file
case CLOSE: testFileHandlerClose(writableDir); break;
// Test 3: creates the first file handler
case CREATE_FIRST: testFileHandlerCreate(writableDir, true); break;
// Test 4, 5, ... creates the next file handler
case CREATE_NEXT: testFileHandlerCreate(writableDir, false); break;
// Checks that zombie lock files are reused appropriatly
case REUSE: testFileHandlerReuse(writableDir); break;
// Removes the writableDir
case CLEANUP: delete(writableDir); break;
default: throw new RuntimeException("No such test case: " + arg);
}
}
}
/**
* @param writableDir in which log and lock file are created
* @throws SecurityException
* @throws RuntimeException
* @throws IOException
*/
private static void checkWritable(File writableDir) throws SecurityException,
RuntimeException, IOException {
// Test 1: make sure we can create/delete files in the writable dir.
final File file = new File(writableDir, "test.txt");
if (!createFile(file, false)) {
throw new IOException("Can't create " + file + "\n\tUnable to run test");
} else {
delete(file);
}
}
private static FileHandler createFileHandler(File writableDir) throws SecurityException,
RuntimeException, IOException {
// Test 1: make sure we can create FileHandler in writable directory
try {
FileHandler handler = new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
handler.publish(new LogRecord(Level.INFO, handler.toString()));
handler.flush();
return handler;
} catch (IOException ex) {
throw new RuntimeException("Test failed: should have been able"
+ " to create FileHandler for " + "%t/" + WRITABLE_DIR
+ "/log.log in writable directory.", ex);
}
}
private static List<File> listLocks(File writableDir, boolean print)
throws IOException {
List<File> locks = new ArrayList<>();
for (File f : writableDir.listFiles()) {
if (print) {
System.out.println("Found file: " + f.getName());
}
if (f.getName().endsWith(".lck")) {
locks.add(f);
}
}
return locks;
}
private static void testFileHandlerClose(File writableDir) throws IOException {
File fakeLock = new File(writableDir, "log.log.lck");
if (!createFile(fakeLock, false)) {
throw new IOException("Can't create fake lock file: " + fakeLock);
}
try {
List<File> before = listLocks(writableDir, true);
System.out.println("before: " + before.size() + " locks found");
FileHandler handler = createFileHandler(writableDir);
System.out.println("handler created: " + handler);
List<File> after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found");
handler.close();
System.out.println("handler closed: " + handler);
List<File> afterClose = listLocks(writableDir, true);
System.out.println("after closing handler: " + afterClose.size() + " locks found");
afterClose.removeAll(before);
if (!afterClose.isEmpty()) {
throw new RuntimeException("Zombie lock file detected: " + afterClose);
}
} finally {
if (fakeLock.canRead()) delete(fakeLock);
}
List<File> finalLocks = listLocks(writableDir, false);
System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
private static void testFileHandlerReuse(File writableDir) throws IOException {
List<File> before = listLocks(writableDir, true);
System.out.println("before: " + before.size() + " locks found");
try {
if (!before.isEmpty()) {
throw new RuntimeException("Expected no lock file! Found: " + before);
}
} finally {
before.stream().forEach(CheckZombieLockTest::delete);
}
FileHandler handler1 = createFileHandler(writableDir);
System.out.println("handler created: " + handler1);
List<File> after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found");
if (after.size() != 1) {
throw new RuntimeException("Unexpected number of lock files found for "
+ handler1 + ": " + after);
}
final File lock = after.get(0);
after.clear();
handler1.close();
after = listLocks(writableDir, true);
System.out.println("after closing handler: " + after.size() + " locks found");
if (!after.isEmpty()) {
throw new RuntimeException("Unexpected number of lock files found for "
+ handler1 + ": " + after);
}
if (!createFile(lock, false)) {
throw new IOException("Can't create fake lock file: " + lock);
}
try {
before = listLocks(writableDir, true);
System.out.println("before: " + before.size() + " locks found");
if (before.size() != 1) {
throw new RuntimeException("Unexpected number of lock files found: "
+ before + " expected [" + lock + "].");
}
FileHandler handler2 = createFileHandler(writableDir);
System.out.println("handler created: " + handler2);
after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found");
after.removeAll(before);
if (!after.isEmpty()) {
throw new RuntimeException("Unexpected lock file found: " + after
+ "\n\t" + lock + " should have been reused");
}
handler2.close();
System.out.println("handler closed: " + handler2);
List<File> afterClose = listLocks(writableDir, true);
System.out.println("after closing handler: " + afterClose.size() + " locks found");
if (!afterClose.isEmpty()) {
throw new RuntimeException("Zombie lock file detected: " + afterClose);
}
if (supportsLocking) {
FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()),
StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
StandardOpenOption.WRITE);
try {
if (fc.tryLock() != null) {
System.out.println("locked: " + lock);
handler2 = createFileHandler(writableDir);
System.out.println("handler created: " + handler2);
after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size()
+ " locks found");
after.removeAll(before);
if (after.size() != 1) {
throw new RuntimeException("Unexpected lock files found: " + after
+ "\n\t" + lock + " should not have been reused");
}
} else {
throw new RuntimeException("Failed to lock: " + lock);
}
} finally {
delete(lock);
}
}
} finally {
List<File> finalLocks = listLocks(writableDir, false);
System.out.println("end: " + finalLocks.size() + " locks found");
delete(writableDir);
}
}
private static void testFileHandlerCreate(File writableDir, boolean first)
throws IOException {
List<File> before = listLocks(writableDir, true);
System.out.println("before: " + before.size() + " locks found");
try {
if (first && !before.isEmpty()) {
throw new RuntimeException("Expected no lock file! Found: " + before);
} else if (!first && before.size() != 1) {
throw new RuntimeException("Expected a single lock file! Found: " + before);
}
} finally {
before.stream().forEach(CheckZombieLockTest::delete);
}
FileHandler handler = createFileHandler(writableDir);
System.out.println("handler created: " + handler);
List<File> after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found");
if (after.size() != 1) {
throw new RuntimeException("Unexpected number of lock files found for "
+ handler + ": " + after);
}
}
/**
* Setup all the files and directories needed for the tests
*
* @return writable directory created that needs to be deleted when done
* @throws RuntimeException
*/
private static File setup() throws RuntimeException {
// First do some setup in the temporary directory (using same logic as
// FileHandler for %t pattern)
String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
if (tmpDir == null) {
tmpDir = System.getProperty("user.home");
}
File tmpOrHomeDir = new File(tmpDir);
// Create a writable directory here (%t/writable-lockfile-dir)
File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
if (!createFile(writableDir, true)) {
throw new RuntimeException("Test setup failed: unable to create"
+ " writable working directory "
+ writableDir.getAbsolutePath() );
}
// try to determine whether file locking is supported
final String uniqueFileName = UUID.randomUUID().toString()+".lck";
try {
FileChannel fc = FileChannel.open(Paths.get(writableDir.getAbsolutePath(),
uniqueFileName),
StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
StandardOpenOption.DELETE_ON_CLOSE);
try {
fc.tryLock();
} catch(IOException x) {
supportsLocking = false;
} finally {
fc.close();
}
} catch (IOException t) {
// should not happen
System.err.println("Failed to create new file " + uniqueFileName +
" in " + writableDir.getAbsolutePath());
throw new RuntimeException("Test setup failed: unable to run test", t);
}
return writableDir;
}
/**
* @param newFile
* @return true if file already exists or creation succeeded
*/
private static boolean createFile(File newFile, boolean makeDirectory) {
if (newFile.exists()) {
return true;
}
if (makeDirectory) {
return newFile.mkdir();
} else {
try {
return newFile.createNewFile();
} catch (IOException ioex) {
ioex.printStackTrace();
return false;
}
}
}
/*
* Recursively delete all files starting at specified file
*/
private static void delete(File f) {
if (f != null && f.isDirectory()) {
for (File c : f.listFiles())
delete(c);
}
if (!f.delete())
System.err.println(
"WARNING: unable to delete/cleanup writable test directory: "
+ f );
}
}
|