aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/pipe.go
blob: 2a55305a281f06db846a6aa7c578b9d9e4c75f73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package helper

import (
	"errors"
	"io"
	"os"
	"os/exec"

	"git.ophivana.moe/security/fortify/internal/proc"
)

type pipes struct {
	args io.WriterTo

	statP [2]*os.File
	argsP [2]*os.File

	ready chan error

	cmd *exec.Cmd
}

func (p *pipes) pipe() error {
	if p.statP[0] != nil || p.statP[1] != nil ||
		p.argsP[0] != nil || p.argsP[1] != nil {
		panic("attempted to pipe twice")
	}
	if p.args == nil {
		panic("attempted to pipe without args")
	}

	// create pipes
	if pr, pw, err := os.Pipe(); err != nil {
		return err
	} else {
		p.argsP[0], p.argsP[1] = pr, pw
	}

	// create status pipes if ready signal is requested
	if p.ready != nil {
		if pr, pw, err := os.Pipe(); err != nil {
			return err
		} else {
			p.statP[0], p.statP[1] = pr, pw
		}
	}

	return nil
}

// calls pipe to create pipes and sets them up as ExtraFiles, returning their fd
func (p *pipes) prepareCmd(cmd *exec.Cmd) (argsFd, statFd int, err error) {
	argsFd, statFd = -1, -1
	if err = p.pipe(); err != nil {
		return
	}

	// save a reference of cmd for future use
	p.cmd = cmd

	argsFd = int(proc.ExtraFile(cmd, p.argsP[0]))
	if p.ready != nil {
		statFd = int(proc.ExtraFile(cmd, p.statP[1]))
	}

	return
}

func (p *pipes) readyWriteArgs() error {
	statsP, argsP := p.statP[0], p.argsP[1]

	// write arguments and close args pipe
	if _, err := p.args.WriteTo(argsP); err != nil {
		if err1 := p.cmd.Process.Kill(); err1 != nil {
			// should be unreachable
			panic(err1.Error())
		}
		return err
	} else {
		if err = argsP.Close(); err != nil {
			if err1 := p.cmd.Process.Kill(); err1 != nil {
				// should be unreachable
				panic(err1.Error())
			}
			return err
		}
	}

	if p.ready != nil {
		// monitor stat pipe
		go func() {
			n, err := statsP.Read(make([]byte, 1))
			switch n {
			case -1:
				if err1 := p.cmd.Process.Kill(); err1 != nil {
					// should be unreachable
					panic(err1.Error())
				}
				// ensure error is not nil
				if err == nil {
					err = ErrStatusFault
				}
				p.ready <- err
			case 0:
				// ensure error is not nil
				if err == nil {
					err = ErrStatusRead
				}
				p.ready <- err
			case 1:
				p.ready <- nil
			default:
				panic("unreachable") // unexpected read count
			}
		}()
	}

	return nil
}

func (p *pipes) mustClosePipes() {
	if err := p.argsP[0].Close(); err != nil && !errors.Is(err, os.ErrClosed) {
		// unreachable
		panic(err.Error())
	}
	if err := p.argsP[1].Close(); err != nil && !errors.Is(err, os.ErrClosed) {
		// unreachable
		panic(err.Error())
	}

	if p.ready != nil {
		if err := p.statP[0].Close(); err != nil && !errors.Is(err, os.ErrClosed) {
			// unreachable
			panic(err.Error())
		}
		if err := p.statP[1].Close(); err != nil && !errors.Is(err, os.ErrClosed) {
			// unreachable
			panic(err.Error())
		}
	}
}

func (p *pipes) closeStatus() error {
	if p.ready == nil {
		panic("attempted to close helper with no status pipe")
	}

	return p.statP[0].Close()
}