Skip to content

Commit

Permalink
Add systemd native access (elastic#106151)
Browse files Browse the repository at this point in the history
This commit moves systemd access to the NativeAccess lib.

relates elastic#104876
  • Loading branch information
rjernst committed Mar 12, 2024
1 parent d8da8fa commit 10dcb8e
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
import org.elasticsearch.nativeaccess.lib.SystemdLibrary;

import java.util.Map;

public class JnaNativeLibraryProvider extends NativeLibraryProvider {
public JnaNativeLibraryProvider() {
super("jna", Map.of(PosixCLibrary.class, JnaPosixCLibrary::new));
super("jna", Map.of(PosixCLibrary.class, JnaPosixCLibrary::new, SystemdLibrary.class, JnaSystemdLibrary::new));
}
}
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);
}
}
2 changes: 1 addition & 1 deletion libs/native/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
requires org.elasticsearch.base;
requires org.elasticsearch.logging;

exports org.elasticsearch.nativeaccess to org.elasticsearch.server;
exports org.elasticsearch.nativeaccess to org.elasticsearch.server, org.elasticsearch.systemd;
// allows jna to implement a library provider, and ProviderLocator to load it
exports org.elasticsearch.nativeaccess.lib to org.elasticsearch.nativeaccess.jna, org.elasticsearch.base;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ protected AbstractNativeAccess(String name) {
String getName() {
return name;
}

@Override
public Systemd systemd() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@
package org.elasticsearch.nativeaccess;

import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
import org.elasticsearch.nativeaccess.lib.SystemdLibrary;

class LinuxNativeAccess extends PosixNativeAccess {

Systemd systemd;

LinuxNativeAccess(NativeLibraryProvider libraryProvider) {
super("Linux", libraryProvider);
this.systemd = new Systemd(libraryProvider.getLibrary(SystemdLibrary.class));
}

@Override
public Systemd systemd() {
return systemd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ static NativeAccess instance() {
* @return true if running as root, or false if unsure
*/
boolean definitelyRunningAsRoot();

Systemd systemd();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ public boolean definitelyRunningAsRoot() {
logger.warn("Cannot check if running as root because native access is not available");
return false;
}

@Override
public Systemd systemd() {
logger.warn("Cannot get systemd access because native access is not available");
return null;
}
}
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
package org.elasticsearch.nativeaccess.lib;

/** A marker interface for libraries that can be loaded by {@link org.elasticsearch.nativeaccess.lib.NativeLibraryProvider} */
public sealed interface NativeLibrary permits PosixCLibrary {}
public sealed interface NativeLibrary permits PosixCLibrary, SystemdLibrary {}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
import org.elasticsearch.nativeaccess.lib.SystemdLibrary;

import java.util.Map;

public class JdkNativeLibraryProvider extends NativeLibraryProvider {

public JdkNativeLibraryProvider() {
super("jdk", Map.of(PosixCLibrary.class, JdkPosixCLibrary::new));
super("jdk", Map.of(PosixCLibrary.class, JdkPosixCLibrary::new, SystemdLibrary.class, JdkSystemdLibrary::new));
}
}
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);
}
}
}
4 changes: 4 additions & 0 deletions modules/systemd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ esplugin {
classname 'org.elasticsearch.systemd.SystemdPlugin'
}

dependencies {
implementation project(':libs:elasticsearch-native')
}

2 changes: 1 addition & 1 deletion modules/systemd/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
requires org.elasticsearch.xcontent;
requires org.apache.logging.log4j;
requires org.apache.lucene.core;
requires com.sun.jna;
requires org.elasticsearch.nativeaccess;
}

This file was deleted.

0 comments on commit 10dcb8e

Please sign in to comment.