Added `all` option to kill command

master
xSmurf 10 years ago
parent 993b89c0c8
commit 082cfa105a

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
"github.com/subgraph/oz/ipc" "github.com/subgraph/oz/ipc"
) )
@ -59,7 +59,7 @@ func Launch(arg, cpath string, args, env []string, noexec bool) error {
return err return err
} }
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
resp, err := clientSend(&LaunchMsg{ resp, err := clientSend(&LaunchMsg{
Index: idx, Index: idx,
Name: name, Name: name,
@ -106,6 +106,10 @@ func Clean(arg string) error {
} }
} }
func KillAllSanodboxes() error {
return KillSandbox(-1)
}
func KillSandbox(id int) error { func KillSandbox(id int) error {
resp, err := clientSend(&KillSandboxMsg{Id: id}) resp, err := clientSend(&KillSandboxMsg{Id: id})
if err != nil { if err != nil {

@ -189,12 +189,20 @@ func (d *daemonState) sanitizeEnvironment(p *oz.Profile, oldEnv []string) ([]str
} }
func (d *daemonState) handleKillSandbox(msg *KillSandboxMsg, m *ipc.Message) error { func (d *daemonState) handleKillSandbox(msg *KillSandboxMsg, m *ipc.Message) error {
sbox := d.sandboxById(msg.Id) if msg.Id == -1 {
if sbox == nil { for _, sb := range d.sandboxes {
return m.Respond(&ErrorMsg{fmt.Sprintf("no sandbox found with id = %d", msg.Id)}) if err := sb.init.Process.Signal(os.Interrupt); err != nil {
} return m.Respond(&ErrorMsg{fmt.Sprintf("failed to send interrupt signal: %v", err)})
if err := sbox.init.Process.Signal(os.Interrupt); err != nil { }
return m.Respond(&ErrorMsg{fmt.Sprintf("failed to send interrupt signal: %v", err)}) }
} else {
sbox := d.sandboxById(msg.Id)
if sbox == nil {
return m.Respond(&ErrorMsg{fmt.Sprintf("no sandbox found with id = %d", msg.Id)})
}
if err := sbox.init.Process.Signal(os.Interrupt); err != nil {
return m.Respond(&ErrorMsg{fmt.Sprintf("failed to send interrupt signal: %v", err)})
}
} }
return m.Respond(&OkMsg{}) return m.Respond(&OkMsg{})
} }

@ -120,12 +120,15 @@ func (d *daemonState) launch(p *oz.Profile, msg *LaunchMsg, uid, gid uint32, log
init: cmd, init: cmd,
cred: &syscall.Credential{Uid: uid, Gid: gid}, cred: &syscall.Credential{Uid: uid, Gid: gid},
fs: fs, fs: fs,
addr: path.Join(fs.Root(), "tmp", "oz-init-control"), addr: path.Join(fs.Root(), ozinit.SocketAddress),
stderr: pp, stderr: pp,
network: stn, network: stn,
} }
wgNet := new(sync.WaitGroup)
if p.Networking.Nettype == network.TYPE_BRIDGE { if p.Networking.Nettype == network.TYPE_BRIDGE {
defer wgNet.Done()
wgNet.Add(1)
if err := network.NetInit(stn, d.network, cmd.Process.Pid, log); err != nil { if err := network.NetInit(stn, d.network, cmd.Process.Pid, log); err != nil {
cmd.Process.Kill() cmd.Process.Kill()
fs.Cleanup() fs.Cleanup()
@ -138,6 +141,8 @@ func (d *daemonState) launch(p *oz.Profile, msg *LaunchMsg, uid, gid uint32, log
if p.Networking.Nettype != network.TYPE_HOST && len(p.Networking.Sockets) > 0 { if p.Networking.Nettype != network.TYPE_HOST && len(p.Networking.Sockets) > 0 {
go func() { go func() {
defer wgNet.Done()
wgNet.Add(1)
sbox.ready.Wait() sbox.ready.Wait()
err := network.ProxySetup(sbox.init.Process.Pid, p.Networking.Sockets, d.log, sbox.ready) err := network.ProxySetup(sbox.init.Process.Pid, p.Networking.Sockets, d.log, sbox.ready)
if err != nil { if err != nil {
@ -149,6 +154,7 @@ func (d *daemonState) launch(p *oz.Profile, msg *LaunchMsg, uid, gid uint32, log
if !msg.Noexec { if !msg.Noexec {
go func () { go func () {
sbox.ready.Wait() sbox.ready.Wait()
wgNet.Wait()
go sbox.launchProgram(msg.Path, msg.Pwd, msg.Args, log) go sbox.launchProgram(msg.Path, msg.Pwd, msg.Args, log)
}() }()
} }

@ -8,6 +8,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
"os/signal"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -21,7 +22,6 @@ import (
"github.com/kr/pty" "github.com/kr/pty"
"github.com/op/go-logging" "github.com/op/go-logging"
"os/signal"
) )
const SocketAddress = "/tmp/oz-init-control" const SocketAddress = "/tmp/oz-init-control"

@ -214,16 +214,22 @@ func handleClean(c *cli.Context) {
func handleKill(c *cli.Context) { func handleKill(c *cli.Context) {
if len(c.Args()) == 0 { if len(c.Args()) == 0 {
fmt.Println("Need a sandbox id to kill\n") fmt.Errorf("Need a sandbox id to kill\n")
os.Exit(1) os.Exit(1)
} }
if c.Args()[0] == "all" {
if err := daemon.KillAllSanodboxes(); err != nil {
fmt.Errorf("Kill command failed:", err)
}
return
}
id, err := strconv.Atoi(c.Args()[0]) id, err := strconv.Atoi(c.Args()[0])
if err != nil { if err != nil {
fmt.Printf("Could not parse id value %s\n", c.Args()[0]) fmt.Errorf("Could not parse id value %s\n", c.Args()[0])
os.Exit(1) os.Exit(1)
} }
if err := daemon.KillSandbox(id); err != nil { if err := daemon.KillSandbox(id); err != nil {
fmt.Println("Kill command failed:", err) fmt.Errorf("Kill command failed:", err)
} }
} }

Loading…
Cancel
Save