mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-04-24 23:27:25 -04:00
Add native access library (#105100)
Elasticsearch requires access to some native functions. Historically this has been achieved with the JNA library. However, JNA is a complicated, magical library, and has caused various problems booting Elasticsearch over the years. The new Java Foreign Function and Memory API allows access to call native functions directly from Java. It also has the advantage of tight integration with hotspot which can improve performance of these functions (though performance of Elasticsearch's native calls has never been much of an issue since they are mostly at boot time). This commit adds a new native lib that is internal to Elasticsearch. It is built to use the foreign function api starting with Java 21, and continue using JNA with Java versions below that. Only one function, checking whether Elasticsearch is running as root, is migrated. Future changes will migrate other native functions.
This commit is contained in:
parent
0022005e17
commit
6375e9f443
33 changed files with 844 additions and 25 deletions
|
@ -55,6 +55,7 @@ public class InternalDistributionModuleCheckTaskProvider {
|
|||
"org.elasticsearch.grok",
|
||||
"org.elasticsearch.logging",
|
||||
"org.elasticsearch.lz4",
|
||||
"org.elasticsearch.nativeaccess",
|
||||
"org.elasticsearch.plugin",
|
||||
"org.elasticsearch.plugin.analysis",
|
||||
"org.elasticsearch.pluginclassloader",
|
||||
|
|
|
@ -67,6 +67,7 @@ final class SystemJvmOptions {
|
|||
* explore alternatives. See org.elasticsearch.xpack.searchablesnapshots.preallocate.Preallocate.
|
||||
*/
|
||||
"--add-opens=java.base/java.io=org.elasticsearch.preallocate",
|
||||
maybeEnableNativeAccess(),
|
||||
maybeOverrideDockerCgroup(distroType),
|
||||
maybeSetActiveProcessorCount(nodeSettings),
|
||||
setReplayFile(distroType, isHotspot),
|
||||
|
@ -119,4 +120,11 @@ final class SystemJvmOptions {
|
|||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static String maybeEnableNativeAccess() {
|
||||
if (Runtime.version().feature() >= 21) {
|
||||
return "--enable-native-access=org.elasticsearch.nativeaccess";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ configure(subprojects - project('elasticsearch-log4j')) {
|
|||
&& false == depProject.path.equals(':libs:elasticsearch-core')
|
||||
&& false == depProject.path.equals(':libs:elasticsearch-plugin-api')
|
||||
&& false == depProject.path.equals(':libs:elasticsearch-logging')
|
||||
&& false == depProject.path.equals(':libs:elasticsearch-native')
|
||||
&& depProject.path.startsWith(':libs')
|
||||
&& depProject.name.startsWith('elasticsearch-')) {
|
||||
throw new InvalidUserDataException("projects in :libs "
|
||||
|
|
|
@ -14,7 +14,7 @@ module org.elasticsearch.base {
|
|||
|
||||
exports org.elasticsearch.core;
|
||||
exports org.elasticsearch.jdk;
|
||||
exports org.elasticsearch.core.internal.provider to org.elasticsearch.xcontent;
|
||||
exports org.elasticsearch.core.internal.provider to org.elasticsearch.xcontent, org.elasticsearch.nativeaccess;
|
||||
|
||||
uses ModuleQualifiedExportsService;
|
||||
}
|
||||
|
|
39
libs/native/build.gradle
Normal file
39
libs/native/build.gradle
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import org.elasticsearch.gradle.transform.UnzipTransform
|
||||
import org.elasticsearch.gradle.internal.GenerateProviderManifest
|
||||
import org.elasticsearch.gradle.internal.precommit.CheckForbiddenApisTask
|
||||
import org.gradle.api.internal.artifacts.ArtifactAttributes
|
||||
|
||||
import java.util.stream.Collectors
|
||||
|
||||
apply plugin: 'elasticsearch.publish'
|
||||
apply plugin: 'elasticsearch.build'
|
||||
apply plugin: 'elasticsearch.mrjar'
|
||||
apply plugin: 'elasticsearch.embedded-providers'
|
||||
|
||||
embeddedProviders {
|
||||
impl 'native-access-jna', project(':libs:elasticsearch-native:jna')
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api project(':libs:elasticsearch-core')
|
||||
api project(':libs:elasticsearch-logging')
|
||||
testImplementation(project(":test:framework")) {
|
||||
exclude group: 'org.elasticsearch', module: 'elasticsearch-native'
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType(CheckForbiddenApisTask).configureEach {
|
||||
replaceSignatureFiles 'jdk-signatures'
|
||||
}
|
||||
|
||||
tasks.named('forbiddenApisMain21').configure {
|
||||
ignoreMissingClasses = true
|
||||
}
|
31
libs/native/jna/build.gradle
Normal file
31
libs/native/jna/build.gradle
Normal file
|
@ -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.
|
||||
*/
|
||||
|
||||
apply plugin: 'elasticsearch.java'
|
||||
|
||||
base {
|
||||
archivesName = "native-access-jna"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly project(':libs:elasticsearch-core')
|
||||
compileOnly project(':libs:elasticsearch-native')
|
||||
// TODO: this will become an implementation dep onces jna is removed from server
|
||||
compileOnly "net.java.dev.jna:jna:${versions.jna}"
|
||||
|
||||
testImplementation(project(":test:framework")) {
|
||||
exclude group: 'org.elasticsearch', module: 'elasticsearch-native'
|
||||
}
|
||||
}
|
||||
|
||||
tasks.named('forbiddenApisMain').configure {
|
||||
replaceSignatureFiles 'jdk-signatures'
|
||||
}
|
||||
|
||||
// not published, so no need for javadoc
|
||||
tasks.named("javadoc").configure { enabled = false }
|
177
libs/native/jna/licences/jna-LICENSE.txt
Normal file
177
libs/native/jna/licences/jna-LICENSE.txt
Normal file
|
@ -0,0 +1,177 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
1
libs/native/jna/licences/jna-NOTICE.txt
Normal file
1
libs/native/jna/licences/jna-NOTICE.txt
Normal file
|
@ -0,0 +1 @@
|
|||
|
19
libs/native/jna/src/main/java/module-info.java
Normal file
19
libs/native/jna/src/main/java/module-info.java
Normal file
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import org.elasticsearch.nativeaccess.jna.JnaNativeLibraryProvider;
|
||||
import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
|
||||
|
||||
module org.elasticsearch.nativeaccess.jna {
|
||||
requires org.elasticsearch.base;
|
||||
requires org.elasticsearch.nativeaccess;
|
||||
requires org.elasticsearch.logging;
|
||||
requires com.sun.jna;
|
||||
|
||||
provides NativeLibraryProvider with JnaNativeLibraryProvider;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* 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 org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
|
||||
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class JnaNativeLibraryProvider extends NativeLibraryProvider {
|
||||
public JnaNativeLibraryProvider() {
|
||||
super("jna", Map.of(PosixCLibrary.class, JnaPosixCLibrary::new));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* 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.PosixCLibrary;
|
||||
|
||||
class JnaPosixCLibrary implements PosixCLibrary {
|
||||
|
||||
private interface NativeFunctions extends Library {
|
||||
int geteuid();
|
||||
}
|
||||
|
||||
private final NativeFunctions functions;
|
||||
|
||||
JnaPosixCLibrary() {
|
||||
this.functions = Native.load("c", NativeFunctions.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int geteuid() {
|
||||
return functions.geteuid();
|
||||
}
|
||||
}
|
28
libs/native/src/main/java/module-info.java
Normal file
28
libs/native/src/main/java/module-info.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import org.elasticsearch.jdk.ModuleQualifiedExportsService;
|
||||
import org.elasticsearch.nativeaccess.exports.NativeAccessModuleExportsService;
|
||||
import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
|
||||
|
||||
module org.elasticsearch.nativeaccess {
|
||||
requires org.elasticsearch.base;
|
||||
requires org.elasticsearch.logging;
|
||||
|
||||
exports org.elasticsearch.nativeaccess to org.elasticsearch.server;
|
||||
// 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;
|
||||
|
||||
uses NativeLibraryProvider;
|
||||
|
||||
// allows qualified exports from this module to modules not in the boot layer, ie jna
|
||||
exports org.elasticsearch.nativeaccess.exports to org.elasticsearch.base;
|
||||
|
||||
provides ModuleQualifiedExportsService with NativeAccessModuleExportsService;
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
abstract class AbstractNativeAccess implements NativeAccess {
|
||||
|
||||
protected static final Logger logger = LogManager.getLogger(NativeAccess.class);
|
||||
|
||||
private final String name;
|
||||
|
||||
protected AbstractNativeAccess(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* 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.nativeaccess.lib.NativeLibraryProvider;
|
||||
|
||||
class LinuxNativeAccess extends PosixNativeAccess {
|
||||
LinuxNativeAccess(NativeLibraryProvider libraryProvider) {
|
||||
super("Linux", libraryProvider);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* 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.nativeaccess.lib.NativeLibraryProvider;
|
||||
|
||||
class MacNativeAccess extends PosixNativeAccess {
|
||||
|
||||
MacNativeAccess(NativeLibraryProvider libraryProvider) {
|
||||
super("MacOS", libraryProvider);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Provides access to native functionality needed by Elastisearch.
|
||||
*/
|
||||
public interface NativeAccess {
|
||||
|
||||
/**
|
||||
* Get the one and only instance of {@link NativeAccess} which is specific to the running platform and JVM.
|
||||
*/
|
||||
static NativeAccess instance() {
|
||||
return NativeAccessHolder.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether this JVM is running as the root user.
|
||||
*
|
||||
* @return true if running as root, or false if unsure
|
||||
*/
|
||||
boolean definitelyRunningAsRoot();
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* 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.NativeLibraryProvider;
|
||||
|
||||
class NativeAccessHolder {
|
||||
|
||||
protected static final Logger logger = LogManager.getLogger(NativeAccess.class);
|
||||
|
||||
static final NativeAccess INSTANCE;
|
||||
|
||||
static {
|
||||
var libProvider = NativeLibraryProvider.instance();
|
||||
var os = System.getProperty("os.name");
|
||||
|
||||
AbstractNativeAccess inst = null;
|
||||
try {
|
||||
if (os.startsWith("Linux")) {
|
||||
inst = new LinuxNativeAccess(libProvider);
|
||||
} else if (os.startsWith("Mac OS")) {
|
||||
inst = new MacNativeAccess(libProvider);
|
||||
} else if (os.startsWith("Windows")) {
|
||||
inst = new WindowsNativeAccess(libProvider);
|
||||
} else {
|
||||
logger.warn("Unsupported OS [" + os + "]. Native methods will be disabled.");
|
||||
}
|
||||
} catch (LinkageError e) {
|
||||
logger.warn("Unable to load native provider. Native methods will be disabled.", e);
|
||||
}
|
||||
if (inst == null) {
|
||||
inst = new NoopNativeAccess();
|
||||
} else {
|
||||
logger.info("Using [" + libProvider.getName() + "] native provider and native methods for [" + inst.getName() + "]");
|
||||
}
|
||||
INSTANCE = inst;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
class NoopNativeAccess extends AbstractNativeAccess {
|
||||
|
||||
NoopNativeAccess() {
|
||||
super("noop");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean definitelyRunningAsRoot() {
|
||||
logger.warn("Cannot check if running as root because native access is not available");
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* 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.nativeaccess.lib.NativeLibraryProvider;
|
||||
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
|
||||
|
||||
abstract class PosixNativeAccess extends AbstractNativeAccess {
|
||||
|
||||
protected final PosixCLibrary libc;
|
||||
|
||||
PosixNativeAccess(String name, NativeLibraryProvider libraryProvider) {
|
||||
super(name);
|
||||
this.libc = libraryProvider.getLibrary(PosixCLibrary.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean definitelyRunningAsRoot() {
|
||||
return libc.geteuid() == 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* 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.nativeaccess.lib.NativeLibraryProvider;
|
||||
|
||||
class WindowsNativeAccess extends AbstractNativeAccess {
|
||||
|
||||
WindowsNativeAccess(NativeLibraryProvider libraryProvider) {
|
||||
super("Windows");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean definitelyRunningAsRoot() {
|
||||
return false; // don't know
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* 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.exports;
|
||||
|
||||
import org.elasticsearch.jdk.ModuleQualifiedExportsService;
|
||||
|
||||
public class NativeAccessModuleExportsService extends ModuleQualifiedExportsService {
|
||||
@Override
|
||||
protected void addExports(String pkg, Module target) {
|
||||
module.addExports(pkg, target);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addOpens(String pkg, Module target) {
|
||||
module.addOpens(pkg, target);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
/** A marker interface for libraries that can be loaded by {@link org.elasticsearch.nativeaccess.lib.NativeLibraryProvider} */
|
||||
public sealed interface NativeLibrary permits PosixCLibrary {}
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import org.elasticsearch.core.internal.provider.ProviderLocator;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Allows loading native library mappings.
|
||||
*/
|
||||
public abstract class NativeLibraryProvider {
|
||||
|
||||
private final String name;
|
||||
private final Map<Class<? extends NativeLibrary>, Supplier<NativeLibrary>> libraries;
|
||||
|
||||
protected NativeLibraryProvider(String name, Map<Class<? extends NativeLibrary>, Supplier<NativeLibrary>> libraries) {
|
||||
this.name = name;
|
||||
this.libraries = libraries;
|
||||
|
||||
// ensure impls actually provide all necessary libraries
|
||||
for (Class<?> libClass : NativeLibrary.class.getPermittedSubclasses()) {
|
||||
if (libraries.containsKey(libClass) == false) {
|
||||
throw new IllegalStateException(getClass().getSimpleName() + " missing implementation for " + libClass.getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the one and only instance of {@link NativeLibraryProvider} that is specific to the running JDK version.
|
||||
*/
|
||||
public static NativeLibraryProvider instance() {
|
||||
return Holder.INSTANCE;
|
||||
}
|
||||
|
||||
/** Returns a human-understandable name for this provider */
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an instance of the given library class.
|
||||
* @param cls The library class to create
|
||||
* @return An instance of the class
|
||||
*/
|
||||
public <T extends NativeLibrary> T getLibrary(Class<T> cls) {
|
||||
Supplier<?> libraryCtor = libraries.get(cls);
|
||||
Object library = libraryCtor.get();
|
||||
assert library != null;
|
||||
assert cls.isAssignableFrom(library.getClass());
|
||||
return cls.cast(library);
|
||||
}
|
||||
|
||||
private static NativeLibraryProvider loadProvider() {
|
||||
final int runtimeVersion = Runtime.version().feature();
|
||||
if (runtimeVersion >= 21) {
|
||||
return loadJdkImpl(runtimeVersion);
|
||||
}
|
||||
return loadJnaImpl();
|
||||
}
|
||||
|
||||
private static NativeLibraryProvider loadJdkImpl(int runtimeVersion) {
|
||||
try {
|
||||
var lookup = MethodHandles.lookup();
|
||||
var clazz = lookup.findClass("org.elasticsearch.nativeaccess.jdk.JdkNativeLibraryProvider");
|
||||
var constructor = lookup.findConstructor(clazz, MethodType.methodType(void.class));
|
||||
try {
|
||||
return (NativeLibraryProvider) constructor.invoke();
|
||||
} catch (Throwable t) {
|
||||
throw new AssertionError(t);
|
||||
}
|
||||
} catch (NoSuchMethodException | IllegalAccessException e) {
|
||||
throw new LinkageError("NativeLibraryProvider for Java " + runtimeVersion + " has a bad constructor", e);
|
||||
} catch (ClassNotFoundException cnfe) {
|
||||
throw new LinkageError("NativeLibraryProvider is missing for Java " + runtimeVersion, cnfe);
|
||||
}
|
||||
}
|
||||
|
||||
private static NativeLibraryProvider loadJnaImpl() {
|
||||
return new ProviderLocator<>("native-access-jna", NativeLibraryProvider.class, "org.elasticsearch.nativeaccess.jna", Set.of())
|
||||
.get();
|
||||
}
|
||||
|
||||
private static final class Holder {
|
||||
private Holder() {}
|
||||
|
||||
static final NativeLibraryProvider INSTANCE = loadProvider();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Provides access to methods in libc.so available on POSIX systems.
|
||||
*/
|
||||
public non-sealed interface PosixCLibrary extends NativeLibrary {
|
||||
|
||||
/**
|
||||
* Gets the effective userid of the current process.
|
||||
*
|
||||
* @return the effective user id
|
||||
* @see <a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/geteuid.html">geteuid</a>
|
||||
*/
|
||||
int geteuid();
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* 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.NativeLibraryProvider;
|
||||
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class JdkNativeLibraryProvider extends NativeLibraryProvider {
|
||||
|
||||
public JdkNativeLibraryProvider() {
|
||||
super("jdk", Map.of(PosixCLibrary.class, JdkPosixCLibrary::new));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* 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.logging.LogManager;
|
||||
import org.elasticsearch.logging.Logger;
|
||||
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
|
||||
|
||||
import java.lang.foreign.FunctionDescriptor;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
|
||||
import static java.lang.foreign.ValueLayout.JAVA_INT;
|
||||
import static org.elasticsearch.nativeaccess.jdk.LinkerHelper.downcallHandle;
|
||||
|
||||
class JdkPosixCLibrary implements PosixCLibrary {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(JdkPosixCLibrary.class);
|
||||
|
||||
private static final MethodHandle geteuid$mh = downcallHandle("geteuid", FunctionDescriptor.of(JAVA_INT));
|
||||
|
||||
@Override
|
||||
public int geteuid() {
|
||||
try {
|
||||
return (int) geteuid$mh.invokeExact();
|
||||
} catch (Throwable t) {
|
||||
throw new AssertionError(t);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* 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.FunctionDescriptor;
|
||||
import java.lang.foreign.Linker;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.SymbolLookup;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
|
||||
/**
|
||||
* Utility methods for calling into the native linker.
|
||||
*/
|
||||
class LinkerHelper {
|
||||
private static final Linker LINKER = Linker.nativeLinker();
|
||||
private static final SymbolLookup SYMBOL_LOOKUP;
|
||||
private static final MethodHandles.Lookup MH_LOOKUP = MethodHandles.publicLookup();
|
||||
|
||||
static {
|
||||
// We first check the loader lookup, which contains libs loaded by System.load and System.loadLibrary.
|
||||
// If the symbol isn't found there, we fall back to the default lookup, which is "common libraries" for
|
||||
// the platform, typically eg libc
|
||||
SymbolLookup loaderLookup = SymbolLookup.loaderLookup();
|
||||
SYMBOL_LOOKUP = (name) -> loaderLookup.find(name).or(() -> LINKER.defaultLookup().find(name));
|
||||
}
|
||||
|
||||
static MemorySegment functionAddress(String function) {
|
||||
return SYMBOL_LOOKUP.find(function).orElseThrow(() -> new LinkageError("Native function " + function + " could not be found"));
|
||||
}
|
||||
|
||||
static MethodHandle downcallHandle(String function, FunctionDescriptor functionDescriptor, Linker.Option... options) {
|
||||
return LINKER.downcallHandle(functionAddress(function), functionDescriptor, options);
|
||||
}
|
||||
|
||||
static MethodHandle upcallHandle(Class<?> clazz, String methodName, FunctionDescriptor functionDescriptor) {
|
||||
try {
|
||||
return MH_LOOKUP.findVirtual(clazz, methodName, functionDescriptor.toMethodType());
|
||||
} catch (Throwable t) {
|
||||
throw new AssertionError(t);
|
||||
}
|
||||
}
|
||||
|
||||
static <T> MemorySegment upcallStub(MethodHandle mh, T instance, FunctionDescriptor functionDescriptor, Arena arena) {
|
||||
try {
|
||||
mh = mh.bindTo(instance);
|
||||
return LINKER.upcallStub(mh, functionDescriptor, arena);
|
||||
} catch (Throwable t) {
|
||||
throw new AssertionError(t);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -67,6 +67,8 @@ dependencies {
|
|||
api "org.apache.logging.log4j:log4j-api:${versions.log4j}"
|
||||
api "org.apache.logging.log4j:log4j-core:${versions.log4j}"
|
||||
|
||||
// access to native functions
|
||||
implementation project(':libs:elasticsearch-native')
|
||||
api "net.java.dev.jna:jna:${versions.jna}"
|
||||
|
||||
api "co.elastic.logging:log4j2-ecs-layout:${versions.ecsLogging}"
|
||||
|
|
|
@ -20,6 +20,7 @@ module org.elasticsearch.server {
|
|||
|
||||
requires org.elasticsearch.cli;
|
||||
requires org.elasticsearch.base;
|
||||
requires org.elasticsearch.nativeaccess;
|
||||
requires org.elasticsearch.geo;
|
||||
requires org.elasticsearch.lz4;
|
||||
requires org.elasticsearch.pluginclassloader;
|
||||
|
|
|
@ -37,6 +37,7 @@ import org.elasticsearch.monitor.jvm.HotThreads;
|
|||
import org.elasticsearch.monitor.jvm.JvmInfo;
|
||||
import org.elasticsearch.monitor.os.OsProbe;
|
||||
import org.elasticsearch.monitor.process.ProcessProbe;
|
||||
import org.elasticsearch.nativeaccess.NativeAccess;
|
||||
import org.elasticsearch.node.Node;
|
||||
import org.elasticsearch.node.NodeValidationException;
|
||||
|
||||
|
@ -278,7 +279,7 @@ class Elasticsearch {
|
|||
final Logger logger = LogManager.getLogger(Elasticsearch.class);
|
||||
|
||||
// check if the user is running as root, and bail
|
||||
if (Natives.definitelyRunningAsRoot()) {
|
||||
if (NativeAccess.instance().definitelyRunningAsRoot()) {
|
||||
throw new RuntimeException("can not run elasticsearch as root");
|
||||
}
|
||||
|
||||
|
|
|
@ -43,8 +43,6 @@ final class JNACLibrary {
|
|||
|
||||
static native int mlockall(int flags);
|
||||
|
||||
static native int geteuid();
|
||||
|
||||
/** corresponds to struct rlimit */
|
||||
public static final class Rlimit extends Structure implements Structure.ByReference {
|
||||
public NativeLong rlim_cur = new NativeLong(0);
|
||||
|
|
|
@ -153,19 +153,6 @@ class JNANatives {
|
|||
}
|
||||
}
|
||||
|
||||
/** Returns true if user is root, false if not, or if we don't know */
|
||||
static boolean definitelyRunningAsRoot() {
|
||||
if (Constants.WINDOWS) {
|
||||
return false; // don't know
|
||||
}
|
||||
try {
|
||||
return JNACLibrary.geteuid() == 0;
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
// this will have already been logged by Kernel32Library, no need to repeat it
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void tryVirtualLock() {
|
||||
JNAKernel32Library kernel = JNAKernel32Library.getInstance();
|
||||
Pointer process = null;
|
||||
|
|
|
@ -49,14 +49,6 @@ final class Natives {
|
|||
JNANatives.tryMlockall();
|
||||
}
|
||||
|
||||
static boolean definitelyRunningAsRoot() {
|
||||
if (JNA_AVAILABLE == false) {
|
||||
logger.warn("cannot check if running as root because JNA is not available");
|
||||
return false;
|
||||
}
|
||||
return JNANatives.definitelyRunningAsRoot();
|
||||
}
|
||||
|
||||
static void tryVirtualLock() {
|
||||
if (JNA_AVAILABLE == false) {
|
||||
logger.warn("cannot virtual lock because JNA is not available");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue