Cleanup of checking for recursing sandboxes from oz-client

master
xSmurf 10 years ago
parent b52ed64e7a
commit 2d0b33ce7e

@ -4,11 +4,13 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
"os/user" "os/user"
"path"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -22,7 +24,6 @@ import (
"github.com/kr/pty" "github.com/kr/pty"
"github.com/op/go-logging" "github.com/op/go-logging"
"path"
) )
const EnvPrefix = "INIT_ENV_" const EnvPrefix = "INIT_ENV_"
@ -234,6 +235,9 @@ func (st *initState) runInit() {
st.xpraReady.Wait() st.xpraReady.Wait()
st.log.Info("XPRA started") st.log.Info("XPRA started")
fsbx := path.Join("/tmp", "oz-sandbox")
err = ioutil.WriteFile(fsbx, []byte(st.profile.Name), 0644)
os.Stderr.WriteString("OK\n") os.Stderr.WriteString("OK\n")
go st.processSignals(sigs, s) go st.processSignals(sigs, s)

@ -3,7 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -18,39 +20,36 @@ import (
type fnRunType func() type fnRunType func()
var runFunc fnRunType var runFunc fnRunType
var runBasename string
func init() { func init() {
runBasename = path.Base(os.Args[0]) switch path.Base(os.Args[0]) {
switch runBasename {
case "oz": case "oz":
runFunc = runApplication runFunc = runApplication
default: default:
runFunc = runSandbox runFunc = runSandboxed
} }
} }
func main() { func main() {
runFunc() if err := checkRecursingSandbox(); err != nil {
} fmt.Fprintf(os.Stderr, "%s\n", err)
func runSandbox() {
hostname, _ := os.Hostname()
if runBasename == hostname {
fmt.Fprintf(os.Stderr, "Cannot launch from inside a sandbox.\n")
os.Exit(1) os.Exit(1)
} }
name := "0" runFunc()
cpath := os.Args[0] }
if !filepath.IsAbs(os.Args[0]) {
// TODO: Check for executable in path... func runSandboxed() {
name = cpath apath := os.Args[0]
cpath = "" if !filepath.IsAbs(apath) {
epath, err := exec.LookPath(apath)
apath = epath
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot find executable for `%s`: %v\n", apath, err)
os.Exit(1)
}
} }
err := daemon.Launch(name, cpath, os.Args[1:], os.Environ(), false) if err := daemon.Launch("0", apath, os.Args[1:], os.Environ(), false); err != nil {
if err != nil {
fmt.Fprintf(os.Stderr, "launch command failed: %v.\n", err) fmt.Fprintf(os.Stderr, "launch command failed: %v.\n", err)
os.Exit(1) os.Exit(1)
} }
@ -204,7 +203,7 @@ func handleKill(c *cli.Context) {
} }
if c.Args()[0] == "all" { if c.Args()[0] == "all" {
if err := daemon.KillAllSandboxes(); err != nil { if err := daemon.KillAllSandboxes(); err != nil {
fmt.Fprintf(os.Stderr, "Kill command failed:", err) fmt.Fprintf(os.Stderr, "Kill command failed: %s.\n", err)
os.Exit(1) os.Exit(1)
} }
return return
@ -215,7 +214,7 @@ func handleKill(c *cli.Context) {
os.Exit(1) os.Exit(1)
} }
if err := daemon.KillSandbox(id); err != nil { if err := daemon.KillSandbox(id); err != nil {
fmt.Fprintf(os.Stderr, "Kill command failed:", err) fmt.Fprintf(os.Stderr, "Kill command failed: %s.\n", err)
os.Exit(1) os.Exit(1)
} }
@ -231,3 +230,27 @@ func handleLogs(c *cli.Context) {
fmt.Println(ll) fmt.Println(ll)
} }
} }
func checkRecursingSandbox() error {
hostname, _ := os.Hostname()
fsbox := path.Join("/tmp", "oz-sandbox")
bsbox, err := ioutil.ReadFile(fsbox)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Unknown error checking for sandbox file: %v")
}
ssbox := string(bsbox)
if ssbox != "" {
if path.Base(os.Args[0]) == "oz" {
return fmt.Errorf("Cannot run oz client inside of existing sandbox!")
}
if path.Base(os.Args[0]) == hostname {
// TODO: We should just exec cmd+suffix here
return fmt.Errorf("Cannot recursively launch sandbox `%s`!", hostname)
}
// TODO: Attempting to launch sandboxed application in another sandbox.
// Send back to daemon for launching from host.
return fmt.Errorf("Cannot run a sandbox from inside a running sandbox!")
}
return nil
}

Loading…
Cancel
Save