forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add systemd native access (elastic#106151)
This commit moves systemd access to the NativeAccess lib. relates elastic#104876
- Loading branch information
Showing
17 changed files
with
281 additions
and
123 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
31 changes: 31 additions & 0 deletions
31
libs/native/jna/src/main/java/org/elasticsearch/nativeaccess/jna/JnaSystemdLibrary.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,31 @@ | ||
/* | ||
* 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.jna; | ||
|
||
import com.sun.jna.Library; | ||
import com.sun.jna.Native; | ||
|
||
import org.elasticsearch.nativeaccess.lib.SystemdLibrary; | ||
|
||
class JnaSystemdLibrary implements SystemdLibrary { | ||
private interface NativeFunctions extends Library { | ||
int sd_notify(int unset_environment, String state); | ||
} | ||
|
||
private final NativeFunctions functions; | ||
|
||
JnaSystemdLibrary() { | ||
this.functions = Native.load("libsystemd.so.0", NativeFunctions.class); | ||
} | ||
|
||
@Override | ||
public int sd_notify(int unset_environment, String state) { | ||
return functions.sd_notify(unset_environment, state); | ||
} | ||
} |
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
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
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
55 changes: 55 additions & 0 deletions
55
libs/native/src/main/java/org/elasticsearch/nativeaccess/Systemd.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,55 @@ | ||
/* | ||
* 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.SystemdLibrary; | ||
|
||
import java.util.Locale; | ||
|
||
public class Systemd { | ||
private static final Logger logger = LogManager.getLogger(Systemd.class); | ||
|
||
private final SystemdLibrary lib; | ||
|
||
Systemd(SystemdLibrary lib) { | ||
this.lib = lib; | ||
} | ||
|
||
/** | ||
* Notify systemd that the process is ready. | ||
* | ||
* @throws RuntimeException on failure to notify systemd | ||
*/ | ||
public void notify_ready() { | ||
notify("READY=1", false); | ||
} | ||
|
||
public void notify_extend_timeout(long seconds) { | ||
notify("EXTEND_TIMEOUT_USEC=" + (seconds * 1000000), true); | ||
} | ||
|
||
public void notify_stopping() { | ||
notify("STOPPING=1", true); | ||
} | ||
|
||
private void notify(String state, boolean warnOnError) { | ||
int rc = lib.sd_notify(0, state); | ||
logger.trace("sd_notify({}, {}) returned [{}]", 0, state, rc); | ||
if (rc < 0) { | ||
String message = String.format(Locale.ROOT, "sd_notify(%d, %s) returned error [%d]", 0, state, rc); | ||
if (warnOnError) { | ||
logger.warn(message); | ||
} else { | ||
throw new RuntimeException(message); | ||
} | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
libs/native/src/main/java/org/elasticsearch/nativeaccess/lib/SystemdLibrary.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,13 @@ | ||
/* | ||
* 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.lib; | ||
|
||
public non-sealed interface SystemdLibrary extends NativeLibrary { | ||
int sd_notify(int unset_environment, String state); | ||
} |
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
65 changes: 65 additions & 0 deletions
65
libs/native/src/main21/java/org/elasticsearch/nativeaccess/jdk/JdkSystemdLibrary.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,65 @@ | ||
/* | ||
* 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 org.elasticsearch.nativeaccess.lib.SystemdLibrary; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.FunctionDescriptor; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.invoke.MethodHandle; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import static java.lang.foreign.ValueLayout.ADDRESS; | ||
import static java.lang.foreign.ValueLayout.JAVA_INT; | ||
import static org.elasticsearch.nativeaccess.jdk.LinkerHelper.downcallHandle; | ||
|
||
class JdkSystemdLibrary implements SystemdLibrary { | ||
static { | ||
System.load(findLibSystemd()); | ||
} | ||
|
||
// On some systems libsystemd does not have a non-versioned symlink. System.loadLibrary only knows how to find | ||
// non-versioned library files. So we must manually check the library path to find what we need. | ||
static String findLibSystemd() { | ||
final String libsystemd = "libsystemd.so.0"; | ||
String libpath = System.getProperty("java.library.path"); | ||
for (String basepathStr : libpath.split(":")) { | ||
var basepath = Paths.get(basepathStr); | ||
if (Files.exists(basepath) == false) { | ||
continue; | ||
} | ||
try (var stream = Files.walk(basepath)) { | ||
var foundpath = stream.filter(Files::isDirectory).map(p -> p.resolve(libsystemd)).filter(Files::exists).findAny(); | ||
if (foundpath.isPresent()) { | ||
return foundpath.get().toAbsolutePath().toString(); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
|
||
} | ||
throw new UnsatisfiedLinkError("Could not find " + libsystemd + " in java.library.path: " + libpath); | ||
} | ||
|
||
private static final MethodHandle sd_notify$mh = downcallHandle("sd_notify", FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS)); | ||
|
||
@Override | ||
public int sd_notify(int unset_environment, String state) { | ||
try (Arena arena = Arena.ofConfined()) { | ||
MemorySegment nativeState = arena.allocateUtf8String(state); | ||
return (int) sd_notify$mh.invokeExact(unset_environment, nativeState); | ||
} catch (Throwable t) { | ||
throw new AssertionError(t); | ||
} | ||
} | ||
} |
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: 0 additions & 38 deletions
38
modules/systemd/src/main/java/org/elasticsearch/systemd/Libsystemd.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.