You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
4.7 KiB

package daemon
import (
"fmt"
"os/user"
"syscall"
"github.com/subgraph/oz"
"github.com/subgraph/oz/fs"
10 years ago
"github.com/subgraph/oz/ipc"
"github.com/subgraph/oz/network"
"github.com/op/go-logging"
)
type daemonState struct {
10 years ago
log *logging.Logger
config *oz.Config
10 years ago
profiles oz.Profiles
sandboxes []*Sandbox
nextSboxId int
nextDisplay int
10 years ago
memBackend *logging.ChannelMemoryBackend
backends []logging.Backend
network *network.HostNetwork
}
func Main() {
d := initialize()
err := runServer(
d.log,
d.handlePing,
d.handleListProfiles,
d.handleLaunch,
d.handleInitBridgeNetwork,
d.handleListSandboxes,
d.handleClean,
d.handleLogs,
)
if err != nil {
d.log.Warning("Error running server: %v", err)
}
}
func initialize() *daemonState {
d := &daemonState{}
d.initializeLogging()
var config *oz.Config
config, err := oz.LoadConfig(oz.DefaultConfigPath)
if err != nil {
d.log.Info("Could not load config file (%s), using default config", oz.DefaultConfigPath)
config = oz.NewDefaultConfig()
}
d.log.Info("Oz Global Config: %+v", config)
d.config = config
ps, err := oz.LoadProfiles(config.ProfileDir)
if err != nil {
d.log.Fatalf("Failed to load profiles: %v", err)
}
d.Debug("%d profiles loaded", len(ps))
d.profiles = ps
oz.ReapChildProcs(d.log, d.handleChildExit)
d.nextSboxId = 1
d.nextDisplay = 100
d.log.Info("Initializing bridge networking")
htn, err := network.BridgeInit(d.log)
if err != nil {
d.log.Fatalf("Failed to initialize bridge networking: %+v", err)
}
d.network = htn
return d
}
func (d *daemonState) handleChildExit(pid int, wstatus syscall.WaitStatus) {
d.Debug("Child process pid=%d exited with status %d", pid, wstatus.ExitStatus())
10 years ago
for _, sbox := range d.sandboxes {
if sbox.init.Process.Pid == pid {
sbox.remove()
return
}
}
d.Notice("No sandbox found with oz-init pid = %d", pid)
}
func runServer(log *logging.Logger, args ...interface{}) error {
10 years ago
s, err := ipc.NewServer(SocketName, messageFactory, log, args...)
if err != nil {
return err
}
return s.Run()
}
10 years ago
func (d *daemonState) handlePing(msg *PingMsg, m *ipc.Message) error {
d.Debug("received ping with data [%s]", msg.Data)
return m.Respond(&PingMsg{msg.Data})
}
10 years ago
func (d *daemonState) handleListProfiles(msg *ListProfilesMsg, m *ipc.Message) error {
r := new(ListProfilesResp)
index := 1
10 years ago
for _, p := range d.profiles {
r.Profiles = append(r.Profiles, Profile{Index: index, Name: p.Name, Path: p.Path})
index += 1
}
return m.Respond(r)
}
func (d *daemonState) handleLaunch(msg *LaunchMsg, m *ipc.Message) error {
d.Debug("Launch message received: %+v", msg)
10 years ago
p, err := d.getProfileByIdxOrName(msg.Index, msg.Name)
if err != nil {
return m.Respond(&ErrorMsg{err.Error()})
}
d.Debug("Would launch %s", p.Name)
10 years ago
_, err = d.launch(p, m.Ucred.Uid, m.Ucred.Gid)
if err != nil {
d.Warning("launch of %s failed: %v", p.Name, err)
return m.Respond(&ErrorMsg{err.Error()})
}
return m.Respond(&OkMsg{})
}
func (d *daemonState) handleInitBridgeNetwork(msg *InitNetworkMsg, m *ipc.Message) error {
d.Debug("Network bridge init message received: %+v", msg)
return m.Respond(&OkMsg{})
}
func (d *daemonState) getProfileByIdxOrName(index int, name string) (*oz.Profile, error) {
if len(name) == 0 {
if index < 1 || index > len(d.profiles) {
return nil, fmt.Errorf("not a valid profile index (%d)", index)
}
return d.profiles[index-1], nil
}
10 years ago
for _, p := range d.profiles {
if p.Name == name {
10 years ago
return p, nil
}
}
return nil, fmt.Errorf("could not find profile name '%s'", name)
}
func (d *daemonState) handleListSandboxes(list *ListSandboxesMsg, msg *ipc.Message) error {
r := new(ListSandboxesResp)
for _, sb := range d.sandboxes {
r.Sandboxes = append(r.Sandboxes, SandboxInfo{Id: sb.id, Address: sb.addr, Profile: sb.profile.Name})
}
return msg.Respond(r)
}
func (d *daemonState) handleClean(clean *CleanMsg, msg *ipc.Message) error {
10 years ago
p, err := d.getProfileByIdxOrName(clean.Index, clean.Name)
if err != nil {
return msg.Respond(&ErrorMsg{err.Error()})
}
for _, sb := range d.sandboxes {
if sb.profile.Name == p.Name {
errmsg := fmt.Sprintf("Cannot clean profile '%s' because there are sandboxes running for this profile", p.Name)
return msg.Respond(&ErrorMsg{errmsg})
}
}
// XXX
10 years ago
u, _ := user.Current()
fs := fs.NewFromProfile(p, u, d.config.SandboxPath, d.log)
if err := fs.Cleanup(); err != nil {
return msg.Respond(&ErrorMsg{err.Error()})
}
return msg.Respond(&OkMsg{})
}
func (d *daemonState) handleLogs(logs *LogsMsg, msg *ipc.Message) error {
for n := d.memBackend.Head(); n != nil; n = n.Next() {
s := n.Record.Formatted(0)
msg.Respond(&LogData{Lines: []string{s}})
}
if logs.Follow {
d.followLogs(msg)
return nil
}
msg.Respond(&OkMsg{})
return nil
}