| 1 |
|
| 2 |
|
| 3 |
package service |
| 4 |
|
| 5 |
import ( |
| 6 |
"expvar" |
| 7 |
"fmt" |
| 8 |
"gopkg.in/hlandau/easyconfig.v1/cflag" |
| 9 |
"gopkg.in/hlandau/service.v2/gsptcall" |
| 10 |
"gopkg.in/hlandau/svcutils.v1/exepath" |
| 11 |
"io" |
| 12 |
"net/http" |
| 13 |
_ "net/http/pprof" |
| 14 |
"os" |
| 15 |
"os/signal" |
| 16 |
"runtime/pprof" |
| 17 |
"sync" |
| 18 |
"syscall" |
| 19 |
"time" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
var ( |
| 25 |
fg = cflag.NewGroup(nil, "service") |
| 26 |
cpuProfileFlag = cflag.String(fg, "cpuprofile", "", "Write CPU profile to file") |
| 27 |
debugServerAddrFlag = cflag.String(fg, "debugserveraddr", "", "Address for debug server to listen on (do not specify a public address) (default: disabled)") |
| 28 |
) |
| 29 |
|
| 30 |
type nullWriter struct{} |
| 31 |
|
| 32 |
func (nw nullWriter) Write(p []byte) (n int, err error) { |
| 33 |
return len(p), nil |
| 34 |
} |
| 35 |
|
| 36 |
func init() { |
| 37 |
expvar.NewString("service.startTime").Set(time.Now().String()) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
func Main(info *Info) { |
| 43 |
info.main() |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
type Manager interface { |
| 50 |
|
| 51 |
|
| 52 |
DropPrivileges() error |
| 53 |
|
| 54 |
|
| 55 |
SetStarted() |
| 56 |
|
| 57 |
|
| 58 |
StopChan() <-chan struct{} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
SetStatus(status string) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
type Runnable interface { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
Start() error |
| 71 |
|
| 72 |
|
| 73 |
Stop() error |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
type StatusSource interface { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
StatusChan() <-chan string |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
type Info struct { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
Name string |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
RunFunc func(smgr Manager) error |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
NewFunc func() (Runnable, error) |
| 111 |
|
| 112 |
Title string |
| 113 |
Description string |
| 114 |
|
| 115 |
AllowRoot bool |
| 116 |
DefaultChroot string |
| 117 |
NoBanSuid bool |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
systemd bool |
| 122 |
|
| 123 |
|
| 124 |
pidFileName string |
| 125 |
pidFile io.Closer |
| 126 |
} |
| 127 |
|
| 128 |
func (info *Info) main() { |
| 129 |
err := info.maine() |
| 130 |
if err != nil { |
| 131 |
fmt.Fprintf(os.Stderr, "Error in service: %+v\n", err) |
| 132 |
os.Exit(1) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func (info *Info) maine() error { |
| 137 |
if info.Name == "" { |
| 138 |
info.Name = exepath.ProgramName |
| 139 |
} else if exepath.ProgramNameSetter == "default" { |
| 140 |
exepath.ProgramName = info.Name |
| 141 |
exepath.ProgramNameSetter = "service" |
| 142 |
} |
| 143 |
|
| 144 |
if info.Name == "" { |
| 145 |
panic("service name must be specified") |
| 146 |
} |
| 147 |
if info.Title == "" { |
| 148 |
info.Title = info.Name |
| 149 |
} |
| 150 |
if info.Description == "" { |
| 151 |
info.Description = info.Title |
| 152 |
} |
| 153 |
|
| 154 |
err := info.commonPre() |
| 155 |
if err != nil { |
| 156 |
return err |
| 157 |
} |
| 158 |
|
| 159 |
err = info.setRunFunc() |
| 160 |
if err != nil { |
| 161 |
return err |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
if cpuProfileFlag.Value() != "" { |
| 166 |
f, err := os.Create(cpuProfileFlag.Value()) |
| 167 |
if err != nil { |
| 168 |
return err |
| 169 |
} |
| 170 |
pprof.StartCPUProfile(f) |
| 171 |
defer f.Close() |
| 172 |
defer pprof.StopCPUProfile() |
| 173 |
} |
| 174 |
|
| 175 |
err = info.serviceMain() |
| 176 |
|
| 177 |
return err |
| 178 |
} |
| 179 |
|
| 180 |
func (info *Info) commonPre() error { |
| 181 |
if debugServerAddrFlag != nil && debugServerAddrFlag.Value() != "" { |
| 182 |
go func() { |
| 183 |
err := http.ListenAndServe(debugServerAddrFlag.Value(), nil) |
| 184 |
if err != nil { |
| 185 |
fmt.Fprintf(os.Stderr, "Couldn't start debug server: %+v\n", err) |
| 186 |
} |
| 187 |
}() |
| 188 |
} |
| 189 |
return nil |
| 190 |
} |
| 191 |
|
| 192 |
func (info *Info) setRunFunc() error { |
| 193 |
if info.RunFunc != nil { |
| 194 |
return nil |
| 195 |
} |
| 196 |
|
| 197 |
if info.NewFunc == nil { |
| 198 |
panic("either RunFunc or NewFunc must be specified") |
| 199 |
} |
| 200 |
|
| 201 |
info.RunFunc = func(smgr Manager) error { |
| 202 |
|
| 203 |
r, err := info.NewFunc() |
| 204 |
if err != nil { |
| 205 |
return err |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
getStatusChan := func() <-chan string { |
| 210 |
return nil |
| 211 |
} |
| 212 |
if ss, ok := r.(StatusSource); ok { |
| 213 |
getStatusChan = func() <-chan string { |
| 214 |
return ss.StatusChan() |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
err = smgr.DropPrivileges() |
| 220 |
if err != nil { |
| 221 |
return err |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
err = r.Start() |
| 226 |
if err != nil { |
| 227 |
return err |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
smgr.SetStarted() |
| 232 |
smgr.SetStatus(info.Name + ": running ok") |
| 233 |
|
| 234 |
|
| 235 |
loop: |
| 236 |
for { |
| 237 |
select { |
| 238 |
case statusMsg := <-getStatusChan(): |
| 239 |
smgr.SetStatus(info.Name + ": " + statusMsg) |
| 240 |
|
| 241 |
case <-smgr.StopChan(): |
| 242 |
break loop |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
return r.Stop() |
| 248 |
} |
| 249 |
|
| 250 |
return nil |
| 251 |
} |
| 252 |
|
| 253 |
type ihandler struct { |
| 254 |
info *Info |
| 255 |
stopChan chan struct{} |
| 256 |
statusMutex sync.Mutex |
| 257 |
statusNotifyChan chan struct{} |
| 258 |
startedChan chan struct{} |
| 259 |
status string |
| 260 |
started bool |
| 261 |
stopping bool |
| 262 |
dropped bool |
| 263 |
} |
| 264 |
|
| 265 |
func (h *ihandler) SetStarted() { |
| 266 |
if !h.dropped { |
| 267 |
panic("service must call DropPrivileges before calling SetStarted") |
| 268 |
} |
| 269 |
|
| 270 |
select { |
| 271 |
case h.startedChan <- struct{}{}: |
| 272 |
default: |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
func (h *ihandler) StopChan() <-chan struct{} { |
| 277 |
return h.stopChan |
| 278 |
} |
| 279 |
|
| 280 |
func (h *ihandler) SetStatus(status string) { |
| 281 |
h.statusMutex.Lock() |
| 282 |
h.status = status |
| 283 |
h.statusMutex.Unlock() |
| 284 |
|
| 285 |
select { |
| 286 |
case <-h.statusNotifyChan: |
| 287 |
default: |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
func (h *ihandler) updateStatus() { |
| 292 |
|
| 293 |
if h.info.systemd { |
| 294 |
s := "" |
| 295 |
if h.started { |
| 296 |
s += "READY=1\n" |
| 297 |
} |
| 298 |
if h.status != "" { |
| 299 |
s += "STATUS=" + h.status + "\n" |
| 300 |
} |
| 301 |
systemdUpdateStatus(s) |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
if h.status != "" { |
| 306 |
gsptcall.SetProcTitle(h.status) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func (info *Info) runInteractively() error { |
| 311 |
smgr := ihandler{ |
| 312 |
info: info, |
| 313 |
stopChan: make(chan struct{}), |
| 314 |
statusNotifyChan: make(chan struct{}, 1), |
| 315 |
startedChan: make(chan struct{}, 1), |
| 316 |
} |
| 317 |
|
| 318 |
doneChan := make(chan error) |
| 319 |
go func() { |
| 320 |
err := info.RunFunc(&smgr) |
| 321 |
doneChan <- err |
| 322 |
}() |
| 323 |
|
| 324 |
sig := make(chan os.Signal, 1) |
| 325 |
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) |
| 326 |
|
| 327 |
var exitErr error |
| 328 |
|
| 329 |
loop: |
| 330 |
for { |
| 331 |
select { |
| 332 |
case <-sig: |
| 333 |
if !smgr.stopping { |
| 334 |
smgr.stopping = true |
| 335 |
close(smgr.stopChan) |
| 336 |
smgr.updateStatus() |
| 337 |
} |
| 338 |
case <-smgr.startedChan: |
| 339 |
if !smgr.started { |
| 340 |
smgr.started = true |
| 341 |
smgr.updateStatus() |
| 342 |
} |
| 343 |
case <-smgr.statusNotifyChan: |
| 344 |
smgr.updateStatus() |
| 345 |
case exitErr = <-doneChan: |
| 346 |
break loop |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
return exitErr |
| 351 |
} |
| 352 |
|
| 353 |
e |