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
|
/*
* Copyright (c) 2021, 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 8238274
* @summary Potential leak file descriptor for SCTP
* @requires (os.family == "linux")
* @run main/othervm/timeout=250 CloseDescriptors
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.SctpServerChannel;
public class CloseDescriptors {
private static Selector selector;
private static final int LOOP = 10;
private static final int LIMIT_LINES = 2;
private static SelectorThread selThread;
private static boolean finished = false;
public static void main(String[] args) throws Exception {
if (!Util.isSCTPSupported()) {
System.out.println("SCTP protocol is not supported");
System.out.println("Test cannot be run");
return;
}
List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin");
Optional<Path> lsof = lsofDirs.stream()
.map(s -> Path.of(s, "lsof"))
.filter(f -> Files.isExecutable(f))
.findFirst();
if (!lsof.isPresent()) {
System.out.println("Cannot locate lsof in " + lsofDirs);
System.out.println("Test cannot be run");
return;
}
try (ServerSocket ss = new ServerSocket(0)) {
int port = ss.getLocalPort();
Server server = new Server(port);
server.start();
selector = Selector.open();
selThread = new SelectorThread();
selThread.start();
// give time for the server and selector to start
Thread.sleep(100);
for (int i = 0 ; i < 100 ; ++i) {
System.out.println(i);
doIt(port);
Thread.sleep(100);
}
System.out.println("end");
if (!check()) {
cleanup(port);
throw new RuntimeException("Failed: detected unclosed FD.");
}
cleanup(port);
server.join();
selThread.join();
}
}
private static void doIt(int port) throws Exception {
InetSocketAddress sa = new InetSocketAddress("localhost", port);
for (int i = 0 ; i < LOOP ; ++i) {
System.out.println(" " + i);
try (SctpChannel channel = SctpChannel.open(sa, 1, 1)) {
channel.configureBlocking(false);
SelectionKey key = selThread.regChannel(channel);
key.cancel();
selector.wakeup();
}
catch (Exception ex) {
ex.printStackTrace();
}
Thread.sleep(200);
}
}
private static boolean check() throws Exception {
long myPid = ProcessHandle.current().pid();
ProcessBuilder pb = new ProcessBuilder(
"lsof", "-U", "-a", "-w", "-p", Long.toString(myPid));
pb.redirectErrorStream(true);
Process p = pb.start();
p.waitFor();
if (p.exitValue() != 0) {
return false;
}
boolean result = true;
try (BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()))) {
int count = 0;
String line = br.readLine();
while (line != null) {
System.out.println(line);
count++;
if (count > LIMIT_LINES) {
result = false;
}
line = br.readLine();
}
}
return result;
}
private static void cleanup(int port) throws IOException {
finished = true;
InetSocketAddress sa = new InetSocketAddress("localhost", port);
SctpChannel channel = SctpChannel.open(sa, 1, 1);
channel.close();
}
private static class SelectorThread extends Thread {
private Object lock = new Object();
private SctpChannel channel;
private SelectionKey key;
public SelectionKey regChannel(SctpChannel ch) throws Exception {
synchronized (lock) {
channel = ch;
selector.wakeup();
lock.wait();
}
return key;
}
public void run() {
try {
while (!finished) {
selector.select(1000);
synchronized (lock) {
if (channel != null) {
key = channel.register(selector, SelectionKey.OP_READ);
channel = null;
lock.notify();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static class Server extends Thread {
private int port;
public Server(int port) { this.port = port; }
public void run() {
try {
SctpServerChannel ss = SctpServerChannel.open();
InetSocketAddress sa = new InetSocketAddress("localhost", port);
ss.bind(sa);
while (!finished) {
SctpChannel soc = ss.accept();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
|