forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
libs/native/src/main/java/org/elasticsearch/nativeaccess/FreebsdNativeAccess.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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.nativeaccess; | ||
|
||
import org.elasticsearch.nativeaccess.lib.BsdCLibrary; | ||
import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider; | ||
import org.elasticsearch.nativeaccess.lib.PosixCLibrary; | ||
|
||
public class FreebsdNativeAccess extends PosixNativeAccess { | ||
|
||
private final BsdCLibrary bsdLibc; | ||
static final int RLIMIT_NPROC = 7; | ||
|
||
// https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/sys/resource.h#L123 | ||
// https://man.freebsd.org/cgi/man.cgi?stat(2) | ||
// https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/sys/stat.h#L159 | ||
// Offset of st_size: 112 bytes | ||
// Offset of st_blocks: 120 bytes | ||
FreebsdNativeAccess(NativeLibraryProvider libraryProvider) { | ||
super("FreeBSD", libraryProvider, | ||
new PosixConstants( | ||
-1L, | ||
10, | ||
1, | ||
6, | ||
512, | ||
144, | ||
112, | ||
120 | ||
) | ||
); | ||
|
||
this.bsdLibc = libraryProvider.getLibrary(BsdCLibrary.class); | ||
} | ||
|
||
// https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/sys/resource.h#L110 | ||
@Override | ||
protected long getMaxThreads() { | ||
return getRLimit(RLIMIT_NPROC, "max number of threads"); | ||
} | ||
|
||
@Override | ||
protected void logMemoryLimitInstructions() { | ||
logger.warn("You can allow ElasticSearch to lock large amounts of RAM by setting the following in /etc/sysctl.conf:"); | ||
logger.warn("security.bsd.unprivileged_mlock=1\n"); | ||
logger.warn("You can also run the following command to modify the value immediately:"); | ||
logger.warn("sysctl security.bsd.unprivileged_mlock=1\n"); | ||
logger.warn("When running within a Jail, it's highly advisable to set:"); | ||
logger.warn("enforce_statfs = 1"); | ||
} | ||
|
||
@Override | ||
protected boolean nativePreallocate(int fd, long currentSize, long newSize) { | ||
final int rc = bsdLibc.posix_fallocate(fd, currentSize, newSize - currentSize); | ||
if (rc != 0) { | ||
logger.warn("posix_fallocate failed: " + libc.strerror(libc.errno())); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
// https://github.com/elastic/elasticsearch/blob/v8.15.5/server/src/main/java/org/elasticsearch/bootstrap/SystemCallFilter.java#L556 | ||
@Override | ||
public void tryInstallExecSandbox() { | ||
PosixCLibrary.RLimit limit = libc.newRLimit(); | ||
limit.rlim_cur(0); | ||
limit.rlim_max(0); | ||
if (libc.setrlimit(RLIMIT_NPROC, limit) != 0) { | ||
throw new UnsupportedOperationException("RLIMIT_NPROC unavailable: " + libc.strerror(libc.errno())); | ||
} | ||
|
||
logger.debug("FreeBSD RLIMIT_NPROC initialization successful"); | ||
|
||
execSandboxState = ExecSandboxState.ALL_THREADS; | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
libs/native/src/main/java/org/elasticsearch/nativeaccess/lib/BsdCLibrary.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,15 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.nativeaccess.lib; | ||
|
||
public non-sealed interface BsdCLibrary extends NativeLibrary { | ||
|
||
int posix_fallocate(int fd, long offset, long length); | ||
} |
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
39 changes: 39 additions & 0 deletions
39
libs/native/src/main21/java/org/elasticsearch/nativeaccess/jdk/JdkFreebsdCLibrary.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,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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.nativeaccess.jdk; | ||
|
||
import org.elasticsearch.nativeaccess.lib.BsdCLibrary; | ||
|
||
import java.lang.foreign.FunctionDescriptor; | ||
import java.lang.invoke.MethodHandle; | ||
|
||
import static java.lang.foreign.ValueLayout.JAVA_INT; | ||
import static java.lang.foreign.ValueLayout.JAVA_LONG; | ||
import static org.elasticsearch.nativeaccess.jdk.JdkPosixCLibrary.downcallHandleWithErrno; | ||
import static org.elasticsearch.nativeaccess.jdk.JdkPosixCLibrary.errnoState; | ||
|
||
public class JdkFreebsdCLibrary implements BsdCLibrary { | ||
|
||
private static final MethodHandle posix_allocate$mh = downcallHandleWithErrno( | ||
"posix_fallocate", | ||
FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_LONG, JAVA_LONG) | ||
); | ||
|
||
// https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate | ||
// https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/sys/fcntl.h#L390 | ||
@Override | ||
public int posix_fallocate(int fd, long offset, long length) { | ||
try { | ||
return (int) posix_allocate$mh.invokeExact(errnoState, fd, offset, length); | ||
} 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