1#!/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright (c) 2018, 2019 by Delphix. All rights reserved.
15#
16
17typeset -a disk_array=($(find_disks $DISKS))
18
19typeset -r DISK1=${disk_array[0]}
20typeset -r DISK2=${disk_array[1]}
21typeset -r DISK3=${disk_array[2]}
22
23#
24# When the condition it is waiting for becomes true, 'zfs wait' should return
25# promptly. We want to enforce this, but any check will be racey because it will
26# take some small but indeterminate amount of time for the waiting thread to be
27# woken up and for the process to exit.
28#
29# To deal with this, we provide a grace period after the condition becomes true
30# during which 'zfs wait' can exit. If it hasn't exited by the time the grace
31# period expires we assume something is wrong and fail the test. While there is
32# no value that can really be correct, the idea is we choose something large
33# enough that it shouldn't cause issues in practice.
34#
35typeset -r WAIT_EXIT_GRACE=2.0
36
37function proc_exists # pid
38{
39	ps -p $1 >/dev/null
40}
41
42function proc_must_exist # pid
43{
44	proc_exists $1 || log_fail "zpool process exited too soon"
45}
46
47function proc_must_not_exist # pid
48{
49	proc_exists $1 && log_fail "zpool process took too long to exit"
50}
51
52function get_time
53{
54	date +'%H:%M:%S'
55}
56
57function kill_if_running
58{
59	typeset pid=$1
60	[[ $pid ]] && proc_exists $pid && log_must kill -s TERM $pid
61}
62
63# Log a command and then start it running in the background
64function log_bkgrnd
65{
66	log_note "$(get_time) Starting cmd in background '$@'"
67	"$@" &
68}
69
70# Check that a background process has completed and exited with a status of 0
71function bkgrnd_proc_succeeded
72{
73	typeset pid=$1
74
75	log_must sleep $WAIT_EXIT_GRACE
76
77	proc_must_not_exist $pid
78	wait $pid || log_fail "process exited with status $?"
79	log_note "$(get_time) wait completed successfully"
80}
81