-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Windows native functions into NativeAccess (#108873)
Elasticsearch uses a couple windows specific functions, specifically gettting a short path, and registering a console control handler for shutdown notification. This commit moves this functionality from the existing jna natives into NativeAccess. relates #104876
- Loading branch information
Showing
15 changed files
with
290 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
libs/native/src/main/java/org/elasticsearch/nativeaccess/WindowsFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.nativeaccess; | ||
|
||
import org.elasticsearch.logging.LogManager; | ||
import org.elasticsearch.logging.Logger; | ||
import org.elasticsearch.nativeaccess.lib.Kernel32Library; | ||
|
||
/** | ||
* Native functions specific to the Windows operating system. | ||
*/ | ||
public class WindowsFunctions { | ||
private static final Logger logger = LogManager.getLogger(Systemd.class); | ||
|
||
private final Kernel32Library kernel; | ||
|
||
WindowsFunctions(Kernel32Library kernel) { | ||
this.kernel = kernel; | ||
} | ||
|
||
/** | ||
* Retrieves the short path form of the specified path. | ||
* | ||
* @param path the path | ||
* @return the short path name, or the original path name if unsupported or unavailable | ||
*/ | ||
public String getShortPathName(String path) { | ||
String longPath = "\\\\?\\" + path; | ||
// first we get the length of the buffer needed | ||
final int length = kernel.GetShortPathNameW(longPath, null, 0); | ||
if (length == 0) { | ||
logger.warn("failed to get short path name: {}", kernel.GetLastError()); | ||
return path; | ||
} | ||
final char[] shortPath = new char[length]; | ||
// knowing the length of the buffer, now we get the short name | ||
if (kernel.GetShortPathNameW(longPath, shortPath, length) > 0) { | ||
assert shortPath[length - 1] == '\0'; | ||
return new String(shortPath, 0, length - 1); | ||
} else { | ||
logger.warn("failed to get short path name: {}", kernel.GetLastError()); | ||
return path; | ||
} | ||
} | ||
|
||
/** | ||
* Adds a Console Ctrl Handler for Windows. On non-windows this is a noop. | ||
* | ||
* @return true if the handler is correctly set | ||
*/ | ||
public boolean addConsoleCtrlHandler(ConsoleCtrlHandler handler) { | ||
return kernel.SetConsoleCtrlHandler(dwCtrlType -> { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("console control handler received event [{}]", dwCtrlType); | ||
} | ||
return handler.handle(dwCtrlType); | ||
}, true); | ||
} | ||
|
||
/** | ||
* Windows callback for console events | ||
* | ||
* @see <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms683242%28v=vs.85%29.aspx">HandlerRoutine docs</a> | ||
*/ | ||
public interface ConsoleCtrlHandler { | ||
|
||
int CTRL_CLOSE_EVENT = 2; | ||
|
||
/** | ||
* Handles the Ctrl event. | ||
* | ||
* @param code the code corresponding to the Ctrl sent. | ||
* @return true if the handler processed the event, false otherwise. If false, the next handler will be called. | ||
*/ | ||
boolean handle(int code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
libs/native/src/main21/java/org/elasticsearch/nativeaccess/jdk/ArenaUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.nativeaccess.jdk; | ||
|
||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.MemoryLayout; | ||
import java.lang.foreign.MemorySegment; | ||
import java.nio.charset.Charset; | ||
|
||
import static java.lang.foreign.ValueLayout.JAVA_BYTE; | ||
|
||
/** | ||
* Utility methods to act on Arena apis which have changed in subsequent JDK releases. | ||
*/ | ||
class ArenaUtil { | ||
|
||
/** | ||
* Allocate an array of the given memory layout. | ||
*/ | ||
static MemorySegment allocate(Arena arena, MemoryLayout layout, int count) { | ||
return arena.allocateArray(layout, count); | ||
} | ||
|
||
/** | ||
* Allocate and copy the given string into native memory. | ||
*/ | ||
static MemorySegment allocateFrom(Arena arena, String str, Charset charset) { | ||
return arena.allocateArray(JAVA_BYTE, str.getBytes(charset)); | ||
} | ||
|
||
private ArenaUtil() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
libs/native/src/main22/java/org/elasticsearch/nativeaccess/jdk/ArenaUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.nativeaccess.jdk; | ||
|
||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.MemoryLayout; | ||
import java.lang.foreign.MemorySegment; | ||
import java.nio.charset.Charset; | ||
|
||
public class ArenaUtil { | ||
|
||
/** | ||
* Allocate an array of the given memory layout. | ||
*/ | ||
static MemorySegment allocate(Arena arena, MemoryLayout layout, int count) { | ||
return arena.allocate(layout, count); | ||
} | ||
|
||
/** | ||
* Allocate and copy the given string into native memory. | ||
*/ | ||
static MemorySegment allocateFrom(Arena arena, String str, Charset charset) { | ||
return arena.allocateFrom(str, charset); | ||
} | ||
|
||
private ArenaUtil() {} | ||
} |
Oops, something went wrong.