Skip to content

Commit 5d73ee7

Browse files
authored
Merge pull request #542 from tcutts/fix/launch-template-quoting
Fix/launch template unsafe quoting, and performance improvements
2 parents 7e73a55 + 03a249d commit 5d73ee7

9 files changed

Lines changed: 65 additions & 40 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#!/bin/bash
22

3-
source $(dirname "$0")/launcher_lib.sh
3+
source "${0%/*}/launcher_lib.sh"
44

55
_PSI_J_PROCESS_COUNT="$1"
66
shift
77

88
pre_launch
99

1010
set +e
11-
aprun -n $_PSI_J_PROCESS_COUNT "$@" 1>$_PSI_J_STDOUT 2>$_PSI_J_STDERR <$_PSI_J_STDIN
11+
aprun -n "$_PSI_J_PROCESS_COUNT" "$@" 1>"$_PSI_J_STDOUT" 2>"$_PSI_J_STDERR" <"$_PSI_J_STDIN"
1212
_PSI_J_EC=$?
1313
set -e
1414

1515
log "Command done: $_PSI_J_EC"
1616

1717
post_launch
1818

19-
exit $_PSI_J_EC
19+
exit "$_PSI_J_EC"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#!/bin/bash
22

3-
source $(dirname "$0")/launcher_lib.sh
3+
source "${0%/*}/launcher_lib.sh"
44

55
_PSI_J_PROCESS_COUNT="$1"
66
shift
77

88
pre_launch
99

1010
set +e
11-
jsrun -p $_PSI_J_PROCESS_COUNT --np 1 "$@" 1>$_PSI_J_STDOUT 2>$_PSI_J_STDERR <$_PSI_J_STDIN
11+
jsrun -p "$_PSI_J_PROCESS_COUNT" --np 1 "$@" 1>"$_PSI_J_STDOUT" 2>"$_PSI_J_STDERR" <"$_PSI_J_STDIN"
1212
_PSI_J_EC=$?
1313
set -e
1414

1515
log "Command done: $_PSI_J_EC"
1616

1717
post_launch
1818

19-
exit $_PSI_J_EC
19+
exit "$_PSI_J_EC"

src/psij/launchers/scripts/launcher_lib.sh

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
11
set -e
22

3+
# abspath Source - https://stackoverflow.com/a/23002317
4+
# Posted by Alexander Klimetschek, modified by community. See post 'Timeline' for change history
5+
# Retrieved 2026-03-26, License - CC BY-SA 3.0
6+
7+
function _psi_j_abspath() {
8+
if [[ $1 = /* ]]; then
9+
echo "$1"
10+
elif [[ $1 == */* ]]; then
11+
echo "$(cd "${1%/*}"; pwd)/${1##*/}"
12+
elif [ -n "$1" ]; then
13+
echo "$(pwd)/$1"
14+
fi
15+
}
16+
317
_PSI_J_JOB_ID="$1"
4-
_PSI_J_LOG_FILE="$2"
5-
_PSI_J_PRE_LAUNCH="$3"
6-
_PSI_J_POST_LAUNCH="$4"
7-
_PSI_J_STDIN="$5"
8-
_PSI_J_STDOUT="$6"
9-
_PSI_J_STDERR="$7"
18+
_PSI_J_LOG_FILE=$(_psi_j_abspath "$2")
19+
_PSI_J_PRE_LAUNCH=$(_psi_j_abspath "$3")
20+
_PSI_J_POST_LAUNCH=$(_psi_j_abspath "$4")
21+
_PSI_J_STDIN=$(_psi_j_abspath "$5")
22+
_PSI_J_STDOUT=$(_psi_j_abspath "$6")
23+
_PSI_J_STDERR=$(_psi_j_abspath "$7")
1024

1125
shift 7
1226

1327
if [ "$_PSI_J_LOG_FILE" == "" ]; then
1428
_PSI_J_LOG_FILE="/dev/null"
1529
fi
1630

17-
ts() {
18-
while read LINE; do
19-
TZ=UTC TS=`date '+%Y-%m-%d %H:%M:%S'`
20-
echo "$TS $_PSI_J_JOB_ID $LINE"
21-
done
22-
}
31+
if [ "${BASH_VERSINFO[0]}" -gt 4 ] || { [ "${BASH_VERSINFO[0]}" -eq 4 ] && [ "${BASH_VERSINFO[1]}" -ge 2 ]; }; then
32+
ts() {
33+
local TZ=UTC
34+
while read LINE; do
35+
printf -v TS '%(%Y-%m-%d %H:%M:%S)T' -1
36+
echo "$TS $_PSI_J_JOB_ID $LINE"
37+
done
38+
}
39+
else
40+
ts() {
41+
local TZ=UTC
42+
while read LINE; do
43+
TS=$(date '+%Y-%m-%d %H:%M:%S')
44+
echo "$TS $_PSI_J_JOB_ID $LINE"
45+
done
46+
}
47+
fi
2348

2449
log() {
2550
echo "$@" >&3

src/psij/launchers/scripts/mpi_launch.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
source $(dirname "$0")/launcher_lib.sh
3+
source "${0%/*}/launcher_lib.sh"
44

55
_PSI_J_PROCESS_COUNT="$1"
66
shift
@@ -27,13 +27,13 @@ filter_out_5() {
2727

2828
set +e
2929
if [ "$IS_OPENMPI_5" == "1" ]; then
30-
mpirun --oversubscribe --output TAG -n $_PSI_J_PROCESS_COUNT "$@" \
31-
1> >(filter_out_5 > $_PSI_J_STDOUT) 2> >(filter_out_5 > $_PSI_J_STDERR) <$_PSI_J_STDIN
30+
mpirun --oversubscribe --output TAG -n "$_PSI_J_PROCESS_COUNT" "$@" \
31+
1> >(filter_out_5 > "$_PSI_J_STDOUT") 2> >(filter_out_5 > "$_PSI_J_STDERR") <"$_PSI_J_STDIN"
3232
elif [ "$IS_OPENMPI" == "1" ]; then
33-
mpirun --oversubscribe --tag-output -q -n $_PSI_J_PROCESS_COUNT "$@" \
34-
1> >(filter_out > "$_PSI_J_STDOUT") 2> >(filter_out > $_PSI_J_STDERR) <$_PSI_J_STDIN
33+
mpirun --oversubscribe --tag-output -q -n "$_PSI_J_PROCESS_COUNT" "$@" \
34+
1> >(filter_out > "$_PSI_J_STDOUT") 2> >(filter_out > "$_PSI_J_STDERR") <"$_PSI_J_STDIN"
3535
else
36-
mpirun -n $_PSI_J_PROCESS_COUNT "$@" 1>$_PSI_J_STDOUT 2>$_PSI_J_STDERR <$_PSI_J_STDIN
36+
mpirun -n "$_PSI_J_PROCESS_COUNT" "$@" 1>"$_PSI_J_STDOUT" 2>"$_PSI_J_STDERR" <"$_PSI_J_STDIN"
3737
fi
3838
_PSI_J_EC=$?
3939
set -e
@@ -42,4 +42,4 @@ log "Command done: $_PSI_J_EC"
4242

4343
post_launch
4444

45-
exit $_PSI_J_EC
45+
exit "$_PSI_J_EC"

src/psij/launchers/scripts/multi_launch.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
source $(dirname "$0")/launcher_lib.sh
3+
source "${0%/*}/launcher_lib.sh"
44

55
pre_launch
66

@@ -9,8 +9,8 @@ _PSI_J_PROCESS_COUNT="$1"
99
shift
1010
export _PSI_J_PROCESS_COUNT
1111

12-
for INDEX in $(seq 1 1 $_PSI_J_PROCESS_COUNT); do
13-
_PSI_J_PROCESS_INDEX_=$INDEX "$@" 1>>$_PSI_J_STDOUT 2>>$_PSI_J_STDERR <$_PSI_J_STDIN &
12+
for INDEX in $(seq 1 1 "$_PSI_J_PROCESS_COUNT"); do
13+
_PSI_J_PROCESS_INDEX_=$INDEX "$@" 1>>"$_PSI_J_STDOUT" 2>>"$_PSI_J_STDERR" <"$_PSI_J_STDIN" &
1414
PIDS="$PIDS $!"
1515
done
1616

src/psij/launchers/scripts/single_launch.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/bin/bash
22

3-
source $(dirname "$0")/launcher_lib.sh
3+
source "${0%/*}/launcher_lib.sh"
44

55
pre_launch
66

77
set +e
8-
"$@" 1>$_PSI_J_STDOUT 2>$_PSI_J_STDERR <$_PSI_J_STDIN
8+
"$@" 1>"$_PSI_J_STDOUT" 2>"$_PSI_J_STDERR" <"$_PSI_J_STDIN"
99
_PSI_J_EC=$?
1010
set -e
1111
log "Command done: $_PSI_J_EC"

src/psij/launchers/scripts/srun_launch.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22

3-
source $(dirname "$0")/launcher_lib.sh
3+
source "${0%/*}/launcher_lib.sh"
44

55
_PSI_J_PROCESS_COUNT="$1"
66
shift
77

88
pre_launch
99

1010
set +e
11-
srun "$@" 1>$_PSI_J_STDOUT 2>$_PSI_J_STDERR <$_PSI_J_STDIN
11+
srun "$@" 1>"$_PSI_J_STDOUT" 2>"$_PSI_J_STDERR" <"$_PSI_J_STDIN"
1212
_PSI_J_EC=$?
1313
set -e
1414

tests/plugins1/_batch_test/test/launcher.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
source $(dirname "$0")/launcher_lib.sh
3+
source "${0%/*}/launcher_lib.sh"
44

55
pre_launch
66

@@ -18,20 +18,20 @@ export _PSI_J_PPN
1818
EXECUTABLE="$1"
1919
shift
2020
if [ "$EXECUTABLE" == "/bin/hostname" ]; then
21-
EXECUTABLE=$(dirname "$0")/hostname
21+
EXECUTABLE="${0%/*}/hostname"
2222
fi
2323

2424
log "Running stuff"
25-
log "STDOUT: $_PSI_J_STDOUT"
26-
log "STDERR: $_PSI_J_STDERR"
25+
log "STDOUT: \"$_PSI_J_STDOUT\""
26+
log "STDERR: \"$_PSI_J_STDERR\""
2727

28-
for NODE in $(seq 1 1 $_PSI_J_NODE_COUNT); do
28+
for NODE in $(seq 1 1 "$_PSI_J_NODE_COUNT"); do
2929
log "Node: $NODE"
30-
for NODE_PROC in $(seq 1 1 $_PSI_J_PPN); do
30+
for NODE_PROC in $(seq 1 1 "$_PSI_J_PPN"); do
3131
log "Node proc: $NODE_PROC"
3232
INDEX=$(($NODE * $_PSI_J_PPN + $NODE_PROC))
3333
log "Index: $INDEX"
34-
_PSI_J_NODE_INDEX_=$NODE _PSI_J_PROCESS_INDEX_=$INDEX "$EXECUTABLE" "$@" 1>>$_PSI_J_STDOUT 2>>$_PSI_J_STDERR <$_PSI_J_STDIN &
34+
_PSI_J_NODE_INDEX_=$NODE _PSI_J_PROCESS_INDEX_=$INDEX "$EXECUTABLE" "$@" 1>>"$_PSI_J_STDOUT" 2>>"$_PSI_J_STDERR" <"$_PSI_J_STDIN" &
3535
PIDS="$PIDS $!"
3636
done
3737
done

tests/test_nodefile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if [ "$EXPECTED_N_NODES" == "" ]; then
88
exit 3
99
fi
1010

11-
ACTUAL_N_NODES=`cat "$PSIJ_NODEFILE" | wc -l`
11+
ACTUAL_N_NODES=$(wc -l < "$PSIJ_NODEFILE" | tr -d ' ')
1212

1313
if [ "$EXPECTED_N_NODES" != "$ACTUAL_N_NODES" ]; then
1414
echo "Invalid node file. Expected $EXPECTED_N_NODES nodes, but got $ACTUAL_N_NODES."

0 commit comments

Comments
 (0)