1 /*
2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
884 }
885
886 /**
887 * Try to acquire the appropriate file lock (user or system). If
888 * the initial attempt fails, several more attempts are made using
889 * an exponential backoff strategy. If all attempts fail, this method
890 * returns false.
891 * @throws SecurityException if file access denied.
892 */
893 private boolean lockFile(boolean shared) throws SecurityException{
894 boolean usernode = isUserNode();
895 int[] result;
896 int errorCode = 0;
897 File lockFile = (usernode ? userLockFile : systemLockFile);
898 long sleepTime = INIT_SLEEP_TIME;
899 for (int i = 0; i < MAX_ATTEMPTS; i++) {
900 try {
901 int perm = (usernode? USER_READ_WRITE: USER_RW_ALL_READ);
902 result = lockFile0(lockFile.getCanonicalPath(), perm, shared);
903
904 errorCode = result[ERROR_CODE];
905 if (result[LOCK_HANDLE] != 0) {
906 if (usernode) {
907 userRootLockHandle = result[LOCK_HANDLE];
908 } else {
909 systemRootLockHandle = result[LOCK_HANDLE];
910 }
911 return true;
912 }
913 } catch(IOException e) {
914 // // If at first, you don't succeed...
915 }
916
917 try {
918 Thread.sleep(sleepTime);
919 } catch(InterruptedException e) {
920 checkLockFile0ErrorCode(errorCode);
921 return false;
922 }
923 sleepTime *= 2;
924 }
925 checkLockFile0ErrorCode(errorCode);
926 return false;
927 }
928
929 /**
930 * Checks if unlockFile0() returned an error. Throws a SecurityException,
931 * if access denied. Logs a warning otherwise.
932 */
|
1 /*
2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
884 }
885
886 /**
887 * Try to acquire the appropriate file lock (user or system). If
888 * the initial attempt fails, several more attempts are made using
889 * an exponential backoff strategy. If all attempts fail, this method
890 * returns false.
891 * @throws SecurityException if file access denied.
892 */
893 private boolean lockFile(boolean shared) throws SecurityException{
894 boolean usernode = isUserNode();
895 int[] result;
896 int errorCode = 0;
897 File lockFile = (usernode ? userLockFile : systemLockFile);
898 long sleepTime = INIT_SLEEP_TIME;
899 for (int i = 0; i < MAX_ATTEMPTS; i++) {
900 try {
901 int perm = (usernode? USER_READ_WRITE: USER_RW_ALL_READ);
902 result = lockFile0(lockFile.getCanonicalPath(), perm, shared);
903
904 if (result != null) {
905 errorCode = result[ERROR_CODE];
906 if (result[LOCK_HANDLE] != 0) {
907 if (usernode) {
908 userRootLockHandle = result[LOCK_HANDLE];
909 } else {
910 systemRootLockHandle = result[LOCK_HANDLE];
911 }
912 return true;
913 }
914 }
915 } catch(IOException e) {
916 // // If at first, you don't succeed...
917 }
918
919 try {
920 Thread.sleep(sleepTime);
921 } catch(InterruptedException e) {
922 checkLockFile0ErrorCode(errorCode);
923 return false;
924 }
925 sleepTime *= 2;
926 }
927 checkLockFile0ErrorCode(errorCode);
928 return false;
929 }
930
931 /**
932 * Checks if unlockFile0() returned an error. Throws a SecurityException,
933 * if access denied. Logs a warning otherwise.
934 */
|