Skip to content

Commit 4ab411c

Browse files
committed
Issue-70: Add timeout to zfs/zpool commands, except zfs send/receive commands
1 parent d5b1632 commit 4ab411c

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ before_install:
2626
- sudo apt-get update -y && sudo apt-get install -y libattr1-dev libblkid-dev linux-headers-$(uname -r) tree uuid-dev
2727
- mkdir -p $HOME/zfs
2828
- cd $HOME/zfs
29-
- [[ -d spl-$rel.tar.gz ]] || curl -L https://github.com/zfsonlinux/zfs/releases/download/zfs-$rel/spl-$rel.tar.gz | tar xz
30-
- [[ -d zfs-$rel.tar.gz ]] || curl -L https://github.com/zfsonlinux/zfs/releases/download/zfs-$rel/zfs-$rel.tar.gz | tar xz
29+
- "[[ -d spl-$rel.tar.gz ]] || curl -L https://github.com/zfsonlinux/zfs/releases/download/zfs-$rel/spl-$rel.tar.gz | tar xz"
30+
- "[[ -d zfs-$rel.tar.gz ]] || curl -L https://github.com/zfsonlinux/zfs/releases/download/zfs-$rel/zfs-$rel.tar.gz | tar xz"
3131
- (cd spl-$rel && ./configure --prefix=/usr && make && sudo make install)
3232
- (cd zfs-$rel && ./configure --prefix=/usr && make && sudo make install)
3333
- sudo modprobe zfs

utils.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,50 @@ package zfs
22

33
import (
44
"bytes"
5+
"context"
56
"errors"
67
"fmt"
78
"io"
9+
"os"
810
"os/exec"
911
"regexp"
1012
"runtime"
1113
"strconv"
1214
"strings"
15+
"time"
1316

1417
"github.com/pborman/uuid"
1518
)
1619

20+
const (
21+
cmdtimeoutEnv = "COMMAND_TIMEOUT"
22+
)
23+
1724
type command struct {
1825
Command string
1926
Stdin io.Reader
2027
Stdout io.Writer
28+
timeout *time.Duration
2129
}
2230

23-
func (c *command) Run(arg ...string) ([][]string, error) {
31+
func getCommandTimeout() *time.Duration {
32+
value := os.Getenv(cmdtimeoutEnv)
33+
if timeout, err := time.ParseDuration(value); value != "" && err == nil {
34+
return &timeout
35+
}
36+
return nil
37+
}
2438

25-
cmd := exec.Command(c.Command, arg...)
39+
func (c *command) Run(arg ...string) ([][]string, error) {
40+
var cmd *exec.Cmd
2641

42+
if c.timeout == nil {
43+
cmd = exec.Command(c.Command, arg...)
44+
} else {
45+
ctx, cancel := context.WithTimeout(context.TODO(), *c.timeout)
46+
defer cancel()
47+
cmd = exec.CommandContext(ctx, c.Command, arg...)
48+
}
2749
var stdout, stderr bytes.Buffer
2850

2951
if c.Stdout == nil {

zfs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func SetLogger(l Logger) {
111111

112112
// zfs is a helper function to wrap typical calls to zfs.
113113
func zfs(arg ...string) ([][]string, error) {
114-
c := command{Command: "zfs"}
114+
c := command{Command: "zfs", timeout: getCommandTimeout()}
115115
return c.Run(arg...)
116116
}
117117

zpool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Zpool struct {
2929

3030
// zpool is a helper function to wrap typical calls to zpool.
3131
func zpool(arg ...string) ([][]string, error) {
32-
c := command{Command: "zpool"}
32+
c := command{Command: "zpool", timeout: getCommandTimeout()}
3333
return c.Run(arg...)
3434
}
3535

0 commit comments

Comments
 (0)