From 7557a41199851929b97dd9cfd911f07386cf83b3 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Wed, 29 Nov 2017 22:38:39 -0500 Subject: [PATCH 1/2] Use private directory for temporary files This change ensures that the temporary directory used for java.io.tmpdir is a private temporary directory. To achieve this we use mktemp on macOS and Linux to give us a private temporary directory and the value of the environment variable TMP on Windows. For this to work with our packaging, we add java.io.tmpdir=${ES_TMPDIR} to our packaged jvm.options, we set ES_TMPDIR respectively in our startup scripts, and resolve the value of the template ${ES_TMPDIR} at startup. --- distribution/src/main/resources/bin/elasticsearch | 3 ++- distribution/src/main/resources/bin/elasticsearch-env | 4 ++++ distribution/src/main/resources/bin/elasticsearch-env.bat | 4 ++++ distribution/src/main/resources/bin/elasticsearch-service.bat | 2 +- distribution/src/main/resources/bin/elasticsearch.bat | 2 +- distribution/src/main/resources/config/jvm.options | 2 ++ 6 files changed, 14 insertions(+), 3 deletions(-) diff --git a/distribution/src/main/resources/bin/elasticsearch b/distribution/src/main/resources/bin/elasticsearch index 6602a9aa7ee36..6a63d2e9aa828 100755 --- a/distribution/src/main/resources/bin/elasticsearch +++ b/distribution/src/main/resources/bin/elasticsearch @@ -24,7 +24,8 @@ parse_jvm_options() { ES_JVM_OPTIONS="$ES_PATH_CONF"/jvm.options -ES_JAVA_OPTS="`parse_jvm_options "$ES_JVM_OPTIONS"` $ES_JAVA_OPTS" +JVM_OPTIONS=`parse_jvm_options "$ES_JVM_OPTIONS"` +ES_JAVA_OPTS="${JVM_OPTIONS//\$\{ES_TMPDIR\}/$ES_TMPDIR} $ES_JAVA_OPTS" # manual parsing to find out, if process should be detached if ! echo $* | grep -E '(^-d |-d$| -d |--daemonize$|--daemonize )' > /dev/null; then diff --git a/distribution/src/main/resources/bin/elasticsearch-env b/distribution/src/main/resources/bin/elasticsearch-env index 7e74195e60784..a7f3856f2ae92 100644 --- a/distribution/src/main/resources/bin/elasticsearch-env +++ b/distribution/src/main/resources/bin/elasticsearch-env @@ -73,3 +73,7 @@ if [ -z "$ES_PATH_CONF" ]; then echo "ES_PATH_CONF must be set to the configuration path" exit 1 fi + +if [ -z "$ES_TMPDIR" ]; then + ES_TMPDIR=`mktemp -d` +fi diff --git a/distribution/src/main/resources/bin/elasticsearch-env.bat b/distribution/src/main/resources/bin/elasticsearch-env.bat index 00cc7fa09804e..93a1b8d1330e5 100644 --- a/distribution/src/main/resources/bin/elasticsearch-env.bat +++ b/distribution/src/main/resources/bin/elasticsearch-env.bat @@ -49,3 +49,7 @@ set HOSTNAME=%COMPUTERNAME% if not defined ES_PATH_CONF ( set ES_PATH_CONF=!ES_HOME!\config ) + +if not defined ES_TMPDIR ( + set ES_TMPDIR=!TMP! +) diff --git a/distribution/src/main/resources/bin/elasticsearch-service.bat b/distribution/src/main/resources/bin/elasticsearch-service.bat index dfb854a47087e..cf714c27a0a23 100644 --- a/distribution/src/main/resources/bin/elasticsearch-service.bat +++ b/distribution/src/main/resources/bin/elasticsearch-service.bat @@ -104,7 +104,7 @@ if not "%ES_JAVA_OPTS%" == "" set ES_JAVA_OPTS=%ES_JAVA_OPTS: =;% @setlocal for /F "usebackq delims=" %%a in (`findstr /b \- "%ES_JVM_OPTIONS%" ^| findstr /b /v "\-server \-client"`) do set JVM_OPTIONS=!JVM_OPTIONS!%%a; -@endlocal & set ES_JAVA_OPTS=%JVM_OPTIONS%%ES_JAVA_OPTS% +@endlocal & set ES_JAVA_OPTS=%JVM_OPTIONS:${ES_TMPDIR}=!ES_TMPDIR!% %ES_JAVA_OPTS% if "%ES_JAVA_OPTS:~-1%"==";" set ES_JAVA_OPTS=%ES_JAVA_OPTS:~0,-1% diff --git a/distribution/src/main/resources/bin/elasticsearch.bat b/distribution/src/main/resources/bin/elasticsearch.bat index 51f2047679007..5c172d20b8230 100644 --- a/distribution/src/main/resources/bin/elasticsearch.bat +++ b/distribution/src/main/resources/bin/elasticsearch.bat @@ -47,7 +47,7 @@ set "ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options" rem extract the options from the JVM options file %ES_JVM_OPTIONS% rem such options are the lines beginning with '-', thus "findstr /b" for /F "usebackq delims=" %%a in (`findstr /b \- "%ES_JVM_OPTIONS%"`) do set JVM_OPTIONS=!JVM_OPTIONS! %%a -@endlocal & set ES_JAVA_OPTS=%JVM_OPTIONS% %ES_JAVA_OPTS% +@endlocal & set ES_JAVA_OPTS=%JVM_OPTIONS:${ES_TMPDIR}=!ES_TMPDIR!% %ES_JAVA_OPTS% %JAVA% %ES_JAVA_OPTS% -Delasticsearch -Des.path.home="%ES_HOME%" -Des.path.conf="%ES_PATH_CONF%" -cp "%ES_CLASSPATH%" "org.elasticsearch.bootstrap.Elasticsearch" !newparams! diff --git a/distribution/src/main/resources/config/jvm.options b/distribution/src/main/resources/config/jvm.options index 52bd4244bb853..f5d527eec876a 100644 --- a/distribution/src/main/resources/config/jvm.options +++ b/distribution/src/main/resources/config/jvm.options @@ -72,6 +72,8 @@ -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true +-Djava.io.tmpdir=${ES_TMPDIR} + ## heap dumps # generate a heap dump when an allocation from the Java heap fails From 7bdb17db2f3cc120365b521cb34401ad8a39f2a0 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 30 Nov 2017 13:54:13 -0500 Subject: [PATCH 2/2] Add elasticsearch to directory name --- distribution/src/main/resources/bin/elasticsearch-env | 6 +++++- distribution/src/main/resources/bin/elasticsearch-env.bat | 2 +- .../src/main/resources/bin/elasticsearch-service.bat | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/distribution/src/main/resources/bin/elasticsearch-env b/distribution/src/main/resources/bin/elasticsearch-env index a7f3856f2ae92..2b376bd47b33d 100644 --- a/distribution/src/main/resources/bin/elasticsearch-env +++ b/distribution/src/main/resources/bin/elasticsearch-env @@ -75,5 +75,9 @@ if [ -z "$ES_PATH_CONF" ]; then fi if [ -z "$ES_TMPDIR" ]; then - ES_TMPDIR=`mktemp -d` + if [ "`uname`" == "Darwin" ]; then + ES_TMPDIR=`mktemp -d -t elasticsearch` + else + ES_TMPDIR=`mktemp -d -t elasticsearch.XXXXXXXX` + fi fi diff --git a/distribution/src/main/resources/bin/elasticsearch-env.bat b/distribution/src/main/resources/bin/elasticsearch-env.bat index 93a1b8d1330e5..e80a8ce258e2f 100644 --- a/distribution/src/main/resources/bin/elasticsearch-env.bat +++ b/distribution/src/main/resources/bin/elasticsearch-env.bat @@ -51,5 +51,5 @@ if not defined ES_PATH_CONF ( ) if not defined ES_TMPDIR ( - set ES_TMPDIR=!TMP! + set ES_TMPDIR=!TMP!\elasticsearch ) diff --git a/distribution/src/main/resources/bin/elasticsearch-service.bat b/distribution/src/main/resources/bin/elasticsearch-service.bat index cf714c27a0a23..e8be9485349e4 100644 --- a/distribution/src/main/resources/bin/elasticsearch-service.bat +++ b/distribution/src/main/resources/bin/elasticsearch-service.bat @@ -104,7 +104,7 @@ if not "%ES_JAVA_OPTS%" == "" set ES_JAVA_OPTS=%ES_JAVA_OPTS: =;% @setlocal for /F "usebackq delims=" %%a in (`findstr /b \- "%ES_JVM_OPTIONS%" ^| findstr /b /v "\-server \-client"`) do set JVM_OPTIONS=!JVM_OPTIONS!%%a; -@endlocal & set ES_JAVA_OPTS=%JVM_OPTIONS:${ES_TMPDIR}=!ES_TMPDIR!% %ES_JAVA_OPTS% +@endlocal & set ES_JAVA_OPTS=%JVM_OPTIONS:${ES_TMPDIR}=!ES_TMPDIR!%%ES_JAVA_OPTS% if "%ES_JAVA_OPTS:~-1%"==";" set ES_JAVA_OPTS=%ES_JAVA_OPTS:~0,-1%