Skip to content

Commit 573840a

Browse files
Ming Leiaxboe
authored andcommitted
selftests: ublk: make sure _add_ublk_dev can return in sub-shell
Detach ublk daemon from the starting process completely by double-fork and clearing its process group, so that `_add_ublk_dev` can return from sub-shell. Then it is more friendly for writing shell test script for adding/recovering ublk device. Prepare for running ublk test in parallel. Signed-off-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 8d31a7e commit 573840a

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

tools/testing/selftests/ublk/kublk.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,8 @@ static int ublk_send_dev_event(const struct dev_ctx *ctx, int dev_id)
654654
if (write(evtfd, &id, sizeof(id)) != sizeof(id))
655655
return -EINVAL;
656656

657+
close(evtfd);
658+
657659
return 0;
658660
}
659661

@@ -889,24 +891,40 @@ static int cmd_dev_add(struct dev_ctx *ctx)
889891
exit(-1);
890892
}
891893

892-
setsid();
893894
res = fork();
894895
if (res == 0) {
896+
int res2;
897+
898+
setsid();
899+
res2 = fork();
900+
if (res2 == 0) {
901+
/* prepare for detaching */
902+
close(STDIN_FILENO);
903+
close(STDOUT_FILENO);
904+
close(STDERR_FILENO);
895905
run:
896-
res = __cmd_dev_add(ctx);
897-
return res;
906+
res = __cmd_dev_add(ctx);
907+
return res;
908+
} else {
909+
/* detached from the foreground task */
910+
exit(EXIT_SUCCESS);
911+
}
898912
} else if (res > 0) {
899913
uint64_t id;
914+
int exit_code = EXIT_FAILURE;
900915

901916
res = read(ctx->_evtfd, &id, sizeof(id));
902917
close(ctx->_evtfd);
903918
if (res == sizeof(id) && id != ERROR_EVTFD_DEVID) {
904919
ctx->dev_id = id - 1;
905-
return __cmd_dev_list(ctx);
920+
if (__cmd_dev_list(ctx) >= 0)
921+
exit_code = EXIT_SUCCESS;
906922
}
907-
exit(EXIT_FAILURE);
923+
/* wait for child and detach from it */
924+
wait(NULL);
925+
exit(exit_code);
908926
} else {
909-
return res;
927+
exit(EXIT_FAILURE);
910928
}
911929
}
912930

tools/testing/selftests/ublk/test_common.sh

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ _have_feature()
170170
}
171171

172172
_add_ublk_dev() {
173-
local kublk_temp;
174173
local dev_id;
175174

176175
if [ ! -c /dev/ublk-control ]; then
@@ -182,17 +181,17 @@ _add_ublk_dev() {
182181
fi
183182
fi
184183

185-
kublk_temp=$(mktemp /tmp/kublk-XXXXXX)
186-
if ! "${UBLK_PROG}" add "$@" > "${kublk_temp}" 2>&1; then
184+
if ! dev_id=$("${UBLK_PROG}" add "$@" | grep "dev id" | awk -F '[ :]' '{print $3}'); then
187185
echo "fail to add ublk dev $*"
188-
rm -f "${kublk_temp}"
189186
return 255
190187
fi
191-
192-
dev_id=$(grep "dev id" "${kublk_temp}" | awk -F '[ :]' '{print $3}')
193188
udevadm settle
194-
rm -f "${kublk_temp}"
195-
echo "${dev_id}"
189+
190+
if [[ "$dev_id" =~ ^[0-9]+$ ]]; then
191+
echo "${dev_id}"
192+
else
193+
return 255
194+
fi
196195
}
197196

198197
# kill the ublk daemon and return ublk device state

tools/testing/selftests/ublk/test_stress_01.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
55
TID="stress_01"
66
ERR_CODE=0
7-
DEV_ID=-1
87

98
ublk_io_and_remove()
109
{
1110
local size=$1
11+
local dev_id
1212
shift 1
1313

14-
DEV_ID=$(_add_ublk_dev "$@")
14+
dev_id=$(_add_ublk_dev "$@")
1515
_check_add_dev $TID $?
1616

1717
[ "$UBLK_TEST_QUIET" -eq 0 ] && echo "run ublk IO vs. remove device(ublk add $*)"
18-
if ! __run_io_and_remove "${DEV_ID}" "${size}" "no"; then
19-
echo "/dev/ublkc${DEV_ID} isn't removed"
18+
if ! __run_io_and_remove "$dev_id" "${size}" "no"; then
19+
echo "/dev/ublkc$dev_id isn't removed"
2020
exit 255
2121
fi
2222
}

tools/testing/selftests/ublk/test_stress_02.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
55
TID="stress_02"
66
ERR_CODE=0
7-
DEV_ID=-1
87

98
ublk_io_and_kill_daemon()
109
{
1110
local size=$1
11+
local dev_id
1212
shift 1
1313

14-
DEV_ID=$(_add_ublk_dev "$@")
14+
dev_id=$(_add_ublk_dev "$@")
1515
_check_add_dev $TID $?
1616

1717
[ "$UBLK_TEST_QUIET" -eq 0 ] && echo "run ublk IO vs kill ublk server(ublk add $*)"
18-
if ! __run_io_and_remove "${DEV_ID}" "${size}" "yes"; then
19-
echo "/dev/ublkc${DEV_ID} isn't removed res ${res}"
18+
if ! __run_io_and_remove "$dev_id" "${size}" "yes"; then
19+
echo "/dev/ublkc$dev_id isn't removed res ${res}"
2020
exit 255
2121
fi
2222
}

0 commit comments

Comments
 (0)