Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using sudo if the script is run by root #79

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions bin/create-java-perf-map.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -e
#set -x

source $(dirname $0)/sudo-wrap.sh

CUR_DIR=`pwd`
PID=$1
OPTIONS=$2
Expand Down Expand Up @@ -37,9 +39,9 @@ fi


if [[ "$LINUX" == "1" ]]; then
sudo rm $PERF_MAP_FILE -f
(cd $PERF_MAP_DIR/out && sudo -u \#$TARGET_UID -g \#$TARGET_GID $JAVA_HOME/bin/java -cp $ATTACH_JAR_PATH:$JAVA_HOME/lib/tools.jar net.virtualvoid.perf.AttachOnce $PID "$OPTIONS")
sudo chown root:root $PERF_MAP_FILE
runasroot rm $PERF_MAP_FILE -f
(cd $PERF_MAP_DIR/out && runasroot -u \#$TARGET_UID -g \#$TARGET_GID $JAVA_HOME/bin/java -cp $ATTACH_JAR_PATH:$JAVA_HOME/lib/tools.jar net.virtualvoid.perf.AttachOnce $PID "$OPTIONS")
runasroot chown root:root $PERF_MAP_FILE
else
rm -f $PERF_MAP_FILE
(cd $PERF_MAP_DIR/out && $JAVA_HOME/bin/java -cp $ATTACH_JAR_PATH:$JAVA_HOME/lib/tools.jar net.virtualvoid.perf.AttachOnce $PID "$OPTIONS")
Expand Down
3 changes: 2 additions & 1 deletion bin/perf-java-flames
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh
set -e
#set -x
source $(dirname $0)/sudo-wrap.sh

PID=$1

Expand Down Expand Up @@ -30,6 +31,6 @@ if [ -z "$PERF_FLAME_OPTS" ]; then
fi

$PERF_MAP_DIR/bin/perf-java-record-stack $*
sudo perf script -i $PERF_DATA_FILE > $STACKS
runasroot perf script -i $PERF_DATA_FILE > $STACKS
$FLAMEGRAPH_DIR/stackcollapse-perf.pl $PERF_COLLAPSE_OPTS $STACKS | tee $COLLAPSED | $FLAMEGRAPH_DIR/flamegraph.pl $PERF_FLAME_OPTS > $PERF_FLAME_OUTPUT
echo "Flame graph SVG written to PERF_FLAME_OUTPUT='`readlink -f $PERF_FLAME_OUTPUT`'."
3 changes: 2 additions & 1 deletion bin/perf-java-record-stack
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
set -e
#set -x
source $(dirname $0)/sudo-wrap.sh

PERF_MAP_DIR=$(dirname $(readlink -f $0))/..
PID=$1
Expand All @@ -22,5 +23,5 @@ if [ -z "$PERF_DATA_FILE" ]; then
fi

echo "Recording events for $PERF_RECORD_SECONDS seconds (adapt by setting PERF_RECORD_SECONDS)"
sudo perf record -F $PERF_RECORD_FREQ -o $PERF_DATA_FILE -g -p $* -- sleep $PERF_RECORD_SECONDS
runasroot perf record -F $PERF_RECORD_FREQ -o $PERF_DATA_FILE -g -p $* -- sleep $PERF_RECORD_SECONDS
$PERF_MAP_DIR/bin/create-java-perf-map.sh $PID "$PERF_MAP_OPTIONS"
3 changes: 2 additions & 1 deletion bin/perf-java-report-stack
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
set -e
#set -x
source $(dirname $0)/sudo-wrap.sh

PID=$1
PERF_MAP_DIR=$(dirname $(readlink -f $0))/..
Expand All @@ -14,4 +15,4 @@ if [ -z "$PERF_DATA_FILE" ]; then
fi

$PERF_MAP_DIR/bin/perf-java-record-stack $*
sudo perf report -i $PERF_DATA_FILE
runasroot perf report -i $PERF_DATA_FILE
26 changes: 26 additions & 0 deletions bin/sudo-wrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

function runasroot() {
WANT_UID=0
if [[ "$1" == "-u" ]]; then
WANT_UID=$2
shift 2
fi

if [[ "$1" == "-g" ]]; then
WANT_GID=$2
shift 2
fi

if [[ "\#$EUID" == "${WANT_UID}" || "\#$EGID" == "${WANT_GID}" ]]; then
command -v sudo
if [[ $? -eq 0 ]]; then
sudo -u ${WANT_UID} -g ${WANT_GID} $@
else
echo "sudo is missing"
exit 1
fi
else
$@
fi
}