-
Notifications
You must be signed in to change notification settings - Fork 4
/
bang.go
204 lines (179 loc) · 4.11 KB
/
bang.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package bang
import (
"fmt"
"io"
neturl "net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/platinasystems/goes/cmd"
"github.com/platinasystems/goes/external/flags"
"github.com/platinasystems/goes/external/parms"
"github.com/platinasystems/goes/lang"
"github.com/platinasystems/url"
)
var tmpDir string
var namespaces = []struct {
name string
bits uintptr
}{
{"-m", syscall.CLONE_NEWNS},
{"-u", syscall.CLONE_NEWUTS},
{"-i", syscall.CLONE_NEWNET},
{"-p", syscall.CLONE_NEWPID},
// {"-c", syscall.CLONE_NEWCGROUP},
{"-u", syscall.CLONE_NEWUSER},
}
var parmlist = map[string]struct{}{
"-cd": {},
"-chroot": {},
}
type Command struct{}
func (Command) String() string { return "!" }
func (Command) Usage() string {
return "! COMMAND [-m] [-u] [-i] [-p] [-u] [-cd DIR] [-chroot DIR] [ARGS]... [&]"
}
func (Command) Apropos() lang.Alt {
return lang.Alt{
lang.EnUS: "run an external command",
}
}
func (Command) Man() lang.Alt {
return lang.Alt{
lang.EnUS: `
DESCRIPTION
Sh-bang!
Command executes in background if last argument ends with '&'.
The standard i/o redirections apply.
OPTIONS
-m create in new mount namespace
-u create in new UTS namespace
-i create in new network namespace
-p create in new PID namespace
-u create in new user namespace
-cd DIR change directory to DIR to run command
-chroot DIR change root directory to DIR to run command`,
}
}
func (Command) Kind() cmd.Kind { return cmd.DontFork }
func (Command) Main(args ...string) error {
var background bool
opts := args
args = []string{}
for i := 0; i < len(opts); i++ {
if strings.HasPrefix(opts[i], "-") {
// skip over next argument
if _, found := parmlist[opts[i]]; found {
i = i + 1
}
} else {
args = opts[i:]
opts = opts[:i]
break
}
}
if len(args) == 0 {
return fmt.Errorf("COMMAND: missing")
}
parms, opts := parms.New(opts,
"-chroot",
"-cd")
flags, opts := flags.New(opts,
"-m",
"-u",
"-i",
"-p",
"-c",
"-u")
if len(opts) > 0 {
return fmt.Errorf("Unexpected %v\n", opts)
}
if n := len(args); args[n-1] == "&" {
background = true
args = args[:n-1]
}
fp := args[0]
var u *neturl.URL
if parms.ByName["-chroot"] == "" {
var err error
fp, u, err = url.FilePathFromUrl(args[0])
if err != nil {
return fmt.Errorf("Error from url.FilePathFromUrl(%s): %w",
args[0], err)
}
} else {
p := filepath.Join(parms.ByName["-chroot"], args[0])
if _, err := os.Stat(p); err != nil {
return fmt.Errorf("Unable to stat %s: %w",
p, err)
}
}
execpath := args[0]
command := args[0]
if fp == "" {
tmpDir = "/var/run/goes/bang-" + strconv.Itoa(os.Getppid())
err := os.MkdirAll(tmpDir, 0755)
if err != nil {
return fmt.Errorf("Error in os.MkdirAll(%s): %w", tmpDir, err)
}
execpath, command, err = loadNetExec(u)
if err != nil {
return fmt.Errorf("Error from loadNetExec(%v): %w",
u, err)
}
}
cmd := exec.Command(execpath, args[1:]...)
cmd.Args[0] = command
cmd.Dir = parms.ByName["-cd"]
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
unshareFlags := uintptr(0)
for _, flag := range namespaces {
if flags.ByName[flag.name] {
unshareFlags |= flag.bits
}
}
cmd.SysProcAttr = &syscall.SysProcAttr{
Chroot: parms.ByName["-chroot"],
Unshareflags: unshareFlags,
}
if background {
go func() {
err := cmd.Run()
if err != nil {
fmt.Fprintln(os.Stderr, cmd.Args[0], ": ", err)
}
}()
return nil
} else {
return cmd.Run()
}
}
func loadNetExec(u *neturl.URL) (execpath, command string, err error) {
command = path.Base(u.String())
execpath = filepath.Join(tmpDir, command)
r, err := url.OpenUrl(u)
if err != nil {
return "", "", fmt.Errorf("Error from url.OpenUrl(%v): %w",
u, err)
}
f, err := os.OpenFile(execpath,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return "", "", err
}
defer f.Close()
_, err = io.Copy(f, r)
if err != nil {
return "", "", err
}
return
}