Skip to content

Commit

Permalink
Removed dependency on GNU Parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
hazimeh committed Jul 8, 2020
1 parent 7950f12 commit d0d8f5e
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions tools/captain/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ shopt -s nullglob
rm -f "$LOCKDIR"/*
shopt -u nullglob

export PARALLELDIR=~/.parallel/semaphores
export MUX_TAR=magma_tar
export MUX_CID=magma_cid

get_next_cid()
{
Expand Down Expand Up @@ -80,11 +81,27 @@ get_next_cid()
}
export -f get_next_cid

mutex()
{
##
# Pre-requirements:
# - $1: the mutex ID (file descriptor)
# - $2..N: command to run
##
trap 'rm -f "$LOCKDIR/$mux"' EXIT
mux=$1
shift
(
flock -xF 200 &> /dev/null
"${@}"
) 200>"$LOCKDIR/$mux"
}
export -f mutex

start_campaign()
{
launch_campaign()
{

export SHARED="$CAMPAIGN_CACHEDIR/$CACHECID"
mkdir -p "$SHARED" && chmod 777 "$SHARED"

Expand All @@ -99,7 +116,7 @@ start_campaign()

if [ -z $NO_ARCHIVE ]; then
# only one tar job runs at a time, to prevent out-of-storage errors
sem --id "magma_tar" --fg -j 1 \
mutex $MUX_TAR \
tar -cf "${CAMPAIGN_ARDIR}/${ARCID}/${TARBALL_BASENAME}.tar" -C "$SHARED" . &>/dev/null && \
rm -rf "$SHARED"
else
Expand All @@ -111,10 +128,10 @@ start_campaign()

while : ; do
export CAMPAIGN_CACHEDIR="$CACHEDIR/$FUZZER/$TARGET/$PROGRAM"
export CACHECID=$(sem --id magma_cid --fg -j 1 \
export CACHECID=$(mutex $MUX_CID \
get_next_cid "$CAMPAIGN_CACHEDIR")
export CAMPAIGN_ARDIR="$ARDIR/$FUZZER/$TARGET/$PROGRAM"
export ARCID=$(sem --id magma_cid --fg -j 1 \
export ARCID=$(mutex $MUX_CID \
get_next_cid "$CAMPAIGN_ARDIR")

errno_lock=69
Expand Down Expand Up @@ -164,16 +181,13 @@ allocate_workers()
done
exit 0
}
trap cleanup SIGTERM SIGINT
trap cleanup SIGINT

while [ $NUMWORKERS -gt 0 ]; do
for i in $WORKER_POOL; do
if [ -f "$LOCKDIR/magma_cpu_$i" ]; then
continue
else
if ( set -o noclobber; > "$LOCKDIR/magma_cpu_$i" ) &>/dev/null; then
export WORKERSET="$WORKERSET,$i"
export NUMWORKERS=$(( NUMWORKERS - 1 ))
touch "$LOCKDIR/magma_cpu_$i"
allocate_workers
return
fi
Expand All @@ -199,6 +213,7 @@ fi
cleanup()
{
trap 'echo Cleaning up...' SIGINT
echo_time "Waiting for jobs to finish"
for job in `jobs -p`; do
if ! wait $job; then
continue
Expand Down

0 comments on commit d0d8f5e

Please sign in to comment.