Sat, 07 Nov 2020 10:30:02 +0800
Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.sjavac.server; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.File; |
aoqi@0 | 29 | import java.io.FileNotFoundException; |
aoqi@0 | 30 | import java.io.IOException; |
aoqi@0 | 31 | import java.io.RandomAccessFile; |
aoqi@0 | 32 | import java.nio.channels.ClosedChannelException; |
aoqi@0 | 33 | import java.nio.channels.FileChannel; |
aoqi@0 | 34 | import java.nio.channels.FileLock; |
aoqi@0 | 35 | import java.nio.channels.FileLockInterruptionException; |
aoqi@0 | 36 | import com.sun.tools.sjavac.Log; |
aoqi@0 | 37 | |
aoqi@0 | 38 | /** |
aoqi@0 | 39 | * The PortFile class mediates access to a short binary file containing the tcp/ip port (for the localhost) |
aoqi@0 | 40 | * and a cookie necessary for the server answering on that port. The file can be locked using file system |
aoqi@0 | 41 | * primitives to avoid race conditions when several javac clients are started at the same. Note that file |
aoqi@0 | 42 | * system locking is not always supported on a all operating systems and/or file systems. |
aoqi@0 | 43 | * |
aoqi@0 | 44 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 45 | * If you write code that depends on this, you do so at your own |
aoqi@0 | 46 | * risk. This code and its internal interfaces are subject to change |
aoqi@0 | 47 | * or deletion without notice.</b></p> |
aoqi@0 | 48 | */ |
aoqi@0 | 49 | class PortFile { |
aoqi@0 | 50 | |
aoqi@0 | 51 | // Port file format: |
aoqi@0 | 52 | // byte ordering: high byte first = big endian |
aoqi@0 | 53 | // Magic nr, 4 byte int, first in file. |
aoqi@0 | 54 | private final static int magicNr = 0x1174; |
aoqi@0 | 55 | // Followed by a 4 byte int, with the port nr. |
aoqi@0 | 56 | // Followed by a 8 byte long, with cookie nr. |
aoqi@0 | 57 | |
aoqi@0 | 58 | private String filename; |
aoqi@0 | 59 | private File file; |
aoqi@0 | 60 | private File stopFile; |
aoqi@0 | 61 | private RandomAccessFile rwfile; |
aoqi@0 | 62 | private FileChannel channel; |
aoqi@0 | 63 | private FileLock lock; |
aoqi@0 | 64 | |
aoqi@0 | 65 | private boolean containsPortInfo; |
aoqi@0 | 66 | private int serverPort; |
aoqi@0 | 67 | private long serverCookie; |
aoqi@0 | 68 | private int myServerPort; |
aoqi@0 | 69 | private long myServerCookie; |
aoqi@0 | 70 | |
aoqi@0 | 71 | /** |
aoqi@0 | 72 | * Create a new portfile. |
aoqi@0 | 73 | * @param filename is the path to the file. |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | public PortFile(String fn) throws FileNotFoundException |
aoqi@0 | 76 | { |
aoqi@0 | 77 | filename = fn; |
aoqi@0 | 78 | file = new File(filename); |
aoqi@0 | 79 | stopFile = new File(filename+".stop"); |
aoqi@0 | 80 | rwfile = new RandomAccessFile(file, "rw"); |
aoqi@0 | 81 | // The rwfile should only be readable by the owner of the process |
aoqi@0 | 82 | // and no other! How do we do that on a RandomAccessFile? |
aoqi@0 | 83 | channel = rwfile.getChannel(); |
aoqi@0 | 84 | containsPortInfo = false; |
aoqi@0 | 85 | lock = null; |
aoqi@0 | 86 | } |
aoqi@0 | 87 | |
aoqi@0 | 88 | /** |
aoqi@0 | 89 | * Lock the port file. |
aoqi@0 | 90 | */ |
aoqi@0 | 91 | void lock() throws IOException { |
aoqi@0 | 92 | lock = channel.lock(); |
aoqi@0 | 93 | } |
aoqi@0 | 94 | |
aoqi@0 | 95 | /** |
aoqi@0 | 96 | * Read the values from the port file in the file system. |
aoqi@0 | 97 | * Expects the port file to be locked. |
aoqi@0 | 98 | */ |
aoqi@0 | 99 | public void getValues() { |
aoqi@0 | 100 | containsPortInfo = false; |
aoqi@0 | 101 | if (lock == null) { |
aoqi@0 | 102 | // Not locked, remain ignorant about port file contents. |
aoqi@0 | 103 | return; |
aoqi@0 | 104 | } |
aoqi@0 | 105 | try { |
aoqi@0 | 106 | if (rwfile.length()>0) { |
aoqi@0 | 107 | rwfile.seek(0); |
aoqi@0 | 108 | int nr = rwfile.readInt(); |
aoqi@0 | 109 | serverPort = rwfile.readInt(); |
aoqi@0 | 110 | serverCookie = rwfile.readLong(); |
aoqi@0 | 111 | |
aoqi@0 | 112 | if (nr == magicNr) { |
aoqi@0 | 113 | containsPortInfo = true; |
aoqi@0 | 114 | } else { |
aoqi@0 | 115 | containsPortInfo = false; |
aoqi@0 | 116 | } |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } catch (Exception e) { |
aoqi@0 | 119 | containsPortInfo = false; |
aoqi@0 | 120 | } |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** |
aoqi@0 | 124 | * Did the locking and getValues succeed? |
aoqi@0 | 125 | */ |
aoqi@0 | 126 | public boolean containsPortInfo() { |
aoqi@0 | 127 | return containsPortInfo; |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | /** |
aoqi@0 | 131 | * If so, then we can acquire the tcp/ip port on localhost. |
aoqi@0 | 132 | */ |
aoqi@0 | 133 | public int getPort() { |
aoqi@0 | 134 | assert(containsPortInfo); |
aoqi@0 | 135 | return serverPort; |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | /** |
aoqi@0 | 139 | * If so, then we can acquire the server cookie. |
aoqi@0 | 140 | */ |
aoqi@0 | 141 | public long getCookie() { |
aoqi@0 | 142 | assert(containsPortInfo); |
aoqi@0 | 143 | return serverCookie; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | /** |
aoqi@0 | 147 | * Store the values into the locked port file. |
aoqi@0 | 148 | */ |
aoqi@0 | 149 | public void setValues(int port, long cookie) throws IOException { |
aoqi@0 | 150 | assert(lock != null); |
aoqi@0 | 151 | rwfile.seek(0); |
aoqi@0 | 152 | // Write the magic nr that identifes a port file. |
aoqi@0 | 153 | rwfile.writeInt(magicNr); |
aoqi@0 | 154 | rwfile.writeInt(port); |
aoqi@0 | 155 | rwfile.writeLong(cookie); |
aoqi@0 | 156 | myServerPort = port; |
aoqi@0 | 157 | myServerCookie = cookie; |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | /** |
aoqi@0 | 161 | * Delete the port file. |
aoqi@0 | 162 | */ |
aoqi@0 | 163 | public void delete() throws IOException { |
aoqi@0 | 164 | // Access to file must be closed before deleting. |
aoqi@0 | 165 | rwfile.close(); |
aoqi@0 | 166 | // Now delete. |
aoqi@0 | 167 | file.delete(); |
aoqi@0 | 168 | } |
aoqi@0 | 169 | |
aoqi@0 | 170 | /** |
aoqi@0 | 171 | * Is the port file still there? |
aoqi@0 | 172 | */ |
aoqi@0 | 173 | public boolean exists() throws IOException { |
aoqi@0 | 174 | return file.exists(); |
aoqi@0 | 175 | } |
aoqi@0 | 176 | |
aoqi@0 | 177 | /** |
aoqi@0 | 178 | * Is a stop file there? |
aoqi@0 | 179 | */ |
aoqi@0 | 180 | public boolean markedForStop() throws IOException { |
aoqi@0 | 181 | if (stopFile.exists()) { |
aoqi@0 | 182 | try { |
aoqi@0 | 183 | stopFile.delete(); |
aoqi@0 | 184 | } catch (Exception e) |
aoqi@0 | 185 | {} |
aoqi@0 | 186 | return true; |
aoqi@0 | 187 | } |
aoqi@0 | 188 | return false; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | /** |
aoqi@0 | 192 | * Unlock the port file. |
aoqi@0 | 193 | */ |
aoqi@0 | 194 | public void unlock() throws IOException { |
aoqi@0 | 195 | assert(lock != null); |
aoqi@0 | 196 | lock.release(); |
aoqi@0 | 197 | lock = null; |
aoqi@0 | 198 | } |
aoqi@0 | 199 | |
aoqi@0 | 200 | /** |
aoqi@0 | 201 | * Wait for the port file to contain values that look valid. |
aoqi@0 | 202 | * Return true, if a-ok, false if the valid values did not materialize within 5 seconds. |
aoqi@0 | 203 | */ |
aoqi@0 | 204 | public synchronized boolean waitForValidValues() throws IOException, FileNotFoundException { |
aoqi@0 | 205 | for (int tries = 0; tries < 50; tries++) { |
aoqi@0 | 206 | lock(); |
aoqi@0 | 207 | getValues(); |
aoqi@0 | 208 | unlock(); |
aoqi@0 | 209 | if (containsPortInfo) { |
aoqi@0 | 210 | Log.debug("Found valid values in port file after waiting "+(tries*100)+"ms"); |
aoqi@0 | 211 | return true; |
aoqi@0 | 212 | } |
aoqi@0 | 213 | try { |
aoqi@0 | 214 | Thread.sleep(100); |
aoqi@0 | 215 | } catch (InterruptedException e) |
aoqi@0 | 216 | {} |
aoqi@0 | 217 | } |
aoqi@0 | 218 | Log.debug("Gave up waiting for valid values in port file"); |
aoqi@0 | 219 | return false; |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | /** |
aoqi@0 | 223 | * Check if the portfile still contains my values, assuming that I am the server. |
aoqi@0 | 224 | */ |
aoqi@0 | 225 | public synchronized boolean stillMyValues() throws IOException, FileNotFoundException { |
aoqi@0 | 226 | for (;;) { |
aoqi@0 | 227 | try { |
aoqi@0 | 228 | lock(); |
aoqi@0 | 229 | getValues(); |
aoqi@0 | 230 | unlock(); |
aoqi@0 | 231 | if (containsPortInfo) { |
aoqi@0 | 232 | if (serverPort == myServerPort && |
aoqi@0 | 233 | serverCookie == myServerCookie) { |
aoqi@0 | 234 | // Everything is ok. |
aoqi@0 | 235 | return true; |
aoqi@0 | 236 | } |
aoqi@0 | 237 | // Someone has overwritten the port file. |
aoqi@0 | 238 | // Probably another javac server, lets quit. |
aoqi@0 | 239 | return false; |
aoqi@0 | 240 | } |
aoqi@0 | 241 | // Something else is wrong with the portfile. Lets quit. |
aoqi@0 | 242 | return false; |
aoqi@0 | 243 | } catch (FileLockInterruptionException e) { |
aoqi@0 | 244 | continue; |
aoqi@0 | 245 | } |
aoqi@0 | 246 | catch (ClosedChannelException e) { |
aoqi@0 | 247 | // The channel has been closed since sjavac is exiting. |
aoqi@0 | 248 | return false; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | } |
aoqi@0 | 251 | } |
aoqi@0 | 252 | |
aoqi@0 | 253 | /** |
aoqi@0 | 254 | * Return the name of the port file. |
aoqi@0 | 255 | */ |
aoqi@0 | 256 | public String getFilename() { |
aoqi@0 | 257 | return filename; |
aoqi@0 | 258 | } |
aoqi@0 | 259 | } |