-
Notifications
You must be signed in to change notification settings - Fork 34
/
post9p.go
146 lines (136 loc) · 2.71 KB
/
post9p.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
package main
import (
"fmt"
"net"
"os"
"os/exec"
"os/user"
"strings"
)
var chattyfuse bool
func post9pservice(fd *os.File, name string, mtpt string) int {
var (
err error
ns, addr string
conn *net.UnixConn
)
if name == "" && mtpt == "" {
fd.Close()
panic("nothing to do")
return -1
}
if name != "" {
if strings.Index(name, "!") != -1 {
addr = name
} else {
ns = getns()
if ns == "" {
return -1
}
addr = fmt.Sprintf("unix!%s/%s", ns, name)
}
if addr == "" {
return -1
}
cmd := exec.Command("9pserve", "-lv", addr)
cmd.Stdin = fd
cmd.Stdout = fd
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
panic("Failed to start 9pserve")
}
fd.Close()
// 9pserve will fork into the background. Wait for that.
err = cmd.Wait()
if err != nil {
panic("Failed to start 9pserve")
}
if mtpt != "" {
// reopen
s := strings.Split(addr, "!")
unixaddr, err := net.ResolveUnixAddr(s[0], s[1])
if err != nil {
panic(fmt.Sprintf("ResolveUnixAddr: %v", err))
}
conn, err = net.DialUnix(s[0], unixaddr, nil)
if err != nil {
panic(fmt.Sprintf("cannot reopen for mount: %v", err))
}
}
}
if mtpt != "" {
cmd := exec.Command("mount9p", "-", mtpt)
if conn == nil {
cmd.Stdout = fd
} else {
fd, err = conn.File()
if err != nil {
panic("Bad mtpt connection")
}
}
err := cmd.Start()
if err != nil {
if chattyfuse {
cmd = exec.Command("9pfuse", "-D", "-", mtpt)
} else {
cmd = exec.Command("9pfuse", "-", mtpt)
}
cmd.Stdout = fd
err = cmd.Start()
if err != nil {
panic(fmt.Sprintf("failed to run 9pfuse: %v", err))
}
}
fd.Close()
}
return 0
}
func getns() string {
ns := os.Getenv("NAMESPACE")
if ns == "" {
var err error
ns, err = nsfromdisplay()
if err != nil {
acmeerror(ns, err)
}
}
if ns == "" {
panic("$NAMESPACE not set")
}
return ns
}
func getuser() string {
user, err := user.Current()
if err != nil {
return "Wile E. Coyote"
}
return user.Username
}
func nsfromdisplay() (ns string, err error) {
disp := os.Getenv("DISPLAY")
if disp == "" {
disp = ":0.0"
}
disp = strings.TrimSuffix(disp, ".0")
disp = strings.Replace(disp, "/", "_", -1)
ns = fmt.Sprintf("/tmp/ns.%s.%s", getuser(), disp)
err = os.Mkdir(ns, 0700)
if err == nil {
return ns, nil
}
// See if it's already there
f, err := os.Open(ns)
if err != nil {
return "", fmt.Errorf("Can't open namespace %s: %v", ns, err)
}
defer f.Close()
s, err := f.Stat()
if err != nil {
return "", fmt.Errorf("Can't stat namespace %s: %v", ns, err)
}
if !s.IsDir() || s.Mode()&0777 != 0700 { // || !isme(d->uid)
return "", fmt.Errorf("Bad namespace %s: %v", ns, err)
}
return ns, nil
}