pass all environment from ./oz to applications launched from oz-init

(+ gofmt)
master
brl 10 years ago
parent dee514bd4e
commit d1bb0fdeec

@ -51,7 +51,7 @@ func ListSandboxes() ([]SandboxInfo, error) {
return body.Sandboxes, nil return body.Sandboxes, nil
} }
func Launch(arg string) error { func Launch(arg string, env []string) error {
idx, name, err := parseProfileArg(arg) idx, name, err := parseProfileArg(arg)
if err != nil { if err != nil {
return err return err
@ -59,6 +59,7 @@ func Launch(arg string) error {
resp, err := clientSend(&LaunchMsg{ resp, err := clientSend(&LaunchMsg{
Index: idx, Index: idx,
Name: name, Name: name,
Env: env,
}) })
if err != nil { if err != nil {
return err return err

@ -76,7 +76,7 @@ func initialize() *daemonState {
network.NetPrint(d.log) network.NetPrint(d.log)
break; break
} }
} }
@ -125,7 +125,7 @@ func (d *daemonState) handleLaunch(msg *LaunchMsg, m *ipc.Message) error {
return m.Respond(&ErrorMsg{err.Error()}) return m.Respond(&ErrorMsg{err.Error()})
} }
d.Debug("Would launch %s", p.Name) d.Debug("Would launch %s", p.Name)
_, err = d.launch(p, m.Ucred.Uid, m.Ucred.Gid, d.log) _, err = d.launch(p, msg.Env, m.Ucred.Uid, m.Ucred.Gid, d.log)
if err != nil { if err != nil {
d.Warning("launch of %s failed: %v", p.Name, err) d.Warning("launch of %s failed: %v", p.Name, err)
return m.Respond(&ErrorMsg{err.Error()}) return m.Respond(&ErrorMsg{err.Error()})

@ -12,10 +12,11 @@ import (
"github.com/subgraph/oz" "github.com/subgraph/oz"
"github.com/subgraph/oz/fs" "github.com/subgraph/oz/fs"
"github.com/subgraph/oz/xpra"
"github.com/subgraph/oz/network" "github.com/subgraph/oz/network"
"github.com/subgraph/oz/xpra"
"github.com/op/go-logging" "github.com/op/go-logging"
"github.com/subgraph/oz/oz-init"
) )
const initPath = "/usr/local/bin/oz-init" const initPath = "/usr/local/bin/oz-init"
@ -46,7 +47,7 @@ func findSandbox(id int) *Sandbox {
} }
*/ */
func createInitCommand(name, chroot string, uid uint32, display int, stn *network.SandboxNetwork, nettype string) *exec.Cmd { func createInitCommand(name, chroot string, env []string, uid uint32, display int, stn *network.SandboxNetwork, nettype string) *exec.Cmd {
cmd := exec.Command(initPath) cmd := exec.Command(initPath)
cmd.Dir = "/" cmd.Dir = "/"
@ -77,10 +78,14 @@ func createInitCommand(name, chroot string, uid uint32, display int, stn *networ
cmd.Env = append(cmd.Env, fmt.Sprintf("INIT_DISPLAY=%d", display)) cmd.Env = append(cmd.Env, fmt.Sprintf("INIT_DISPLAY=%d", display))
for _, e := range env {
cmd.Env = append(cmd.Env, ozinit.EnvPrefix+e)
}
return cmd return cmd
} }
func (d *daemonState) launch(p *oz.Profile, uid, gid uint32, log *logging.Logger) (*Sandbox, error) { func (d *daemonState) launch(p *oz.Profile, env []string, uid, gid uint32, log *logging.Logger) (*Sandbox, error) {
u, err := user.LookupId(fmt.Sprintf("%d", uid)) u, err := user.LookupId(fmt.Sprintf("%d", uid))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to lookup user for uid=%d: %v", uid, err) return nil, fmt.Errorf("failed to lookup user for uid=%d: %v", uid, err)
@ -103,7 +108,7 @@ func (d *daemonState) launch(p *oz.Profile, uid, gid uint32, log *logging.Logger
} }
} }
cmd := createInitCommand(p.Name, fs.Root(), uid, display, stn, p.Networking.Nettype) cmd := createInitCommand(p.Name, fs.Root(), env, uid, display, stn, p.Networking.Nettype)
log.Debug("Command environment: %+v", cmd.Env) log.Debug("Command environment: %+v", cmd.Env)
pp, err := cmd.StderrPipe() pp, err := cmd.StderrPipe()
if err != nil { if err != nil {
@ -117,7 +122,6 @@ func (d *daemonState) launch(p *oz.Profile, uid, gid uint32, log *logging.Logger
return nil, fmt.Errorf("Unable to start process: %+v", err) return nil, fmt.Errorf("Unable to start process: %+v", err)
} }
sbox := &Sandbox{ sbox := &Sandbox{
daemon: d, daemon: d,
id: d.nextSboxId, id: d.nextSboxId,

@ -33,6 +33,7 @@ type ListProfilesResp struct {
type LaunchMsg struct { type LaunchMsg struct {
Index int "Launch" Index int "Launch"
Name string Name string
Env []string
} }
type ListSandboxesMsg struct { type ListSandboxesMsg struct {

@ -16,19 +16,21 @@ import (
"github.com/subgraph/oz" "github.com/subgraph/oz"
"github.com/subgraph/oz/fs" "github.com/subgraph/oz/fs"
"github.com/subgraph/oz/ipc" "github.com/subgraph/oz/ipc"
"github.com/subgraph/oz/xpra"
"github.com/subgraph/oz/network" "github.com/subgraph/oz/network"
"github.com/subgraph/oz/xpra"
"github.com/kr/pty" "github.com/kr/pty"
"github.com/op/go-logging" "github.com/op/go-logging"
) )
const SocketAddress = "/tmp/oz-init-control" const SocketAddress = "/tmp/oz-init-control"
const EnvPrefix = "INIT_ENV_"
type initState struct { type initState struct {
log *logging.Logger log *logging.Logger
profile *oz.Profile profile *oz.Profile
config *oz.Config config *oz.Config
launchEnv []string
uid int uid int
gid int gid int
user *user.User user *user.User
@ -124,9 +126,19 @@ func parseArgs() *initState {
stn.Gateway = gateway stn.Gateway = gateway
} }
env := []string{}
for _, e := range os.Environ() {
if strings.HasPrefix(e, EnvPrefix) {
e = e[len(EnvPrefix):]
log.Debug("Adding (%s) to launch environment", e)
env = append(env, e)
}
}
return &initState{ return &initState{
log: log, log: log,
config: config, config: config,
launchEnv: env,
profile: p, profile: p,
uid: uid, uid: uid,
gid: gid, gid: gid,
@ -235,7 +247,7 @@ func (st *initState) readXpraOutput(r io.ReadCloser) {
} }
func (st *initState) launchApplication() { func (st *initState) launchApplication() {
cmd := exec.Command(st.profile.Path + ".unsafe") cmd := exec.Command(st.profile.Path /*+ ".unsafe"*/)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
st.log.Warning("Failed to create stdout pipe: %v", err) st.log.Warning("Failed to create stdout pipe: %v", err)
@ -251,9 +263,9 @@ func (st *initState) launchApplication() {
Uid: uint32(st.uid), Uid: uint32(st.uid),
Gid: uint32(st.gid), Gid: uint32(st.gid),
} }
cmd.Env = []string{ cmd.Env = append(st.launchEnv,
fmt.Sprintf("DISPLAY=:%d", st.display), fmt.Sprintf("DISPLAY=:%d", st.display),
} )
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
st.log.Warning("Failed to start application (%s): %v", st.profile.Path, err) st.log.Warning("Failed to start application (%s): %v", st.profile.Path, err)
return return

@ -71,10 +71,9 @@ func handleLaunch(c *cli.Context) {
fmt.Println("Argument needed to launch command") fmt.Println("Argument needed to launch command")
os.Exit(1) os.Exit(1)
} }
err := daemon.Launch(c.Args()[0]) err := daemon.Launch(c.Args()[0], os.Environ())
if err != nil { if err != nil {
fmt.Printf("launch command failed: %v\n", err) fmt.Printf("launch command failed: %v\n", err)
} }
} }

Loading…
Cancel
Save