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
}
func Launch(arg string) error {
func Launch(arg string, env []string) error {
idx, name, err := parseProfileArg(arg)
if err != nil {
return err
@ -59,6 +59,7 @@ func Launch(arg string) error {
resp, err := clientSend(&LaunchMsg{
Index: idx,
Name: name,
Env: env,
})
if err != nil {
return err

@ -76,7 +76,7 @@ func initialize() *daemonState {
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()})
}
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 {
d.Warning("launch of %s failed: %v", p.Name, err)
return m.Respond(&ErrorMsg{err.Error()})

@ -12,10 +12,11 @@ import (
"github.com/subgraph/oz"
"github.com/subgraph/oz/fs"
"github.com/subgraph/oz/xpra"
"github.com/subgraph/oz/network"
"github.com/subgraph/oz/xpra"
"github.com/op/go-logging"
"github.com/subgraph/oz/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.Dir = "/"
@ -69,18 +70,22 @@ func createInitCommand(name, chroot string, uid uint32, display int, stn *networ
}
if stn.Ip != "" {
cmd.Env = append(cmd.Env, "INIT_ADDR=" + stn.Ip)
cmd.Env = append(cmd.Env, "INIT_VHOST=" + stn.VethHost)
cmd.Env = append(cmd.Env, "INIT_VGUEST=" + stn.VethGuest)
cmd.Env = append(cmd.Env, "INIT_GATEWAY=" + stn.Gateway.String() + "/" + stn.Class)
cmd.Env = append(cmd.Env, "INIT_ADDR="+stn.Ip)
cmd.Env = append(cmd.Env, "INIT_VHOST="+stn.VethHost)
cmd.Env = append(cmd.Env, "INIT_VGUEST="+stn.VethGuest)
cmd.Env = append(cmd.Env, "INIT_GATEWAY="+stn.Gateway.String()+"/"+stn.Class)
}
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
}
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))
if err != nil {
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)
pp, err := cmd.StderrPipe()
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)
}
sbox := &Sandbox{
daemon: d,
id: d.nextSboxId,

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

@ -16,19 +16,21 @@ import (
"github.com/subgraph/oz"
"github.com/subgraph/oz/fs"
"github.com/subgraph/oz/ipc"
"github.com/subgraph/oz/xpra"
"github.com/subgraph/oz/network"
"github.com/subgraph/oz/xpra"
"github.com/kr/pty"
"github.com/op/go-logging"
)
const SocketAddress = "/tmp/oz-init-control"
const EnvPrefix = "INIT_ENV_"
type initState struct {
log *logging.Logger
profile *oz.Profile
config *oz.Config
launchEnv []string
uid int
gid int
user *user.User
@ -124,9 +126,19 @@ func parseArgs() *initState {
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{
log: log,
config: config,
launchEnv: env,
profile: p,
uid: uid,
gid: gid,
@ -235,7 +247,7 @@ func (st *initState) readXpraOutput(r io.ReadCloser) {
}
func (st *initState) launchApplication() {
cmd := exec.Command(st.profile.Path + ".unsafe")
cmd := exec.Command(st.profile.Path /*+ ".unsafe"*/)
stdout, err := cmd.StdoutPipe()
if err != nil {
st.log.Warning("Failed to create stdout pipe: %v", err)
@ -251,9 +263,9 @@ func (st *initState) launchApplication() {
Uid: uint32(st.uid),
Gid: uint32(st.gid),
}
cmd.Env = []string{
cmd.Env = append(st.launchEnv,
fmt.Sprintf("DISPLAY=:%d", st.display),
}
)
if err := cmd.Start(); err != nil {
st.log.Warning("Failed to start application (%s): %v", st.profile.Path, err)
return
@ -311,7 +323,7 @@ func (st *initState) handleRunShell(rs *RunShellMsg, msg *ipc.Message) error {
}
}
if st.profile.XServer.Enabled {
cmd.Env = append(cmd.Env, "DISPLAY=:" + strconv.Itoa(st.display))
cmd.Env = append(cmd.Env, "DISPLAY=:"+strconv.Itoa(st.display))
}
cmd.Env = append(cmd.Env, "PATH=/usr/bin:/bin")
cmd.Env = append(cmd.Env, fmt.Sprintf("PS1=[%s] $ ", st.profile.Name))

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

Loading…
Cancel
Save