Skip to content

Commit

Permalink
Combine try/catch blocks
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <widdis@gmail.com>
  • Loading branch information
dbwiddis committed Sep 30, 2022
1 parent 50cd0be commit 8b35d6d
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions server/src/main/java/org/opensearch/bootstrap/JNANatives.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ static String rlimitToString(long value) {

/** Returns true if user is root, false if not, or if we don't know */
static boolean definitelyRunningAsRoot() {
if (Constants.WINDOWS) {
JNAKernel32Library kernel32 = JNAKernel32Library.getInstance();
JNAAdvapi32Library advapi32 = JNAAdvapi32Library.getInstance();
Pointer process = null;
try {
try {
if (Constants.WINDOWS) {
JNAKernel32Library kernel32 = JNAKernel32Library.getInstance();
JNAAdvapi32Library advapi32 = JNAAdvapi32Library.getInstance();

// Fetch a pseudo handle for the current process.
// The pseudo handle need not be closed when it is no longer needed (calling CloseHandle is a no-op).
process = kernel32.GetCurrentProcess();
Pointer process = kernel32.GetCurrentProcess();
PointerByReference hToken = new PointerByReference();
// Fetch the process token for the current process, for which we know we have the access rights
if (!advapi32.OpenProcessToken(process, TOKEN_QUERY, hToken)) {
Expand All @@ -223,15 +223,11 @@ static boolean definitelyRunningAsRoot() {
} finally {
kernel32.CloseHandle(hToken.getValue());
}
} catch (final UnsatisfiedLinkError e) {
return false; // don't know
}
}
try {
// effective user ID of process
// For unix-based systems, check effective user ID of process
return JNACLibrary.geteuid() == 0;
} catch (UnsatisfiedLinkError e) {
// this will have already been logged by Kernel32Library, no need to repeat it
// this will have already been logged by Native Library, no need to repeat it
return false;
}
}
Expand Down

0 comments on commit 8b35d6d

Please sign in to comment.