Skip to content

Commit

Permalink
builder/util: Add ChrootShell() function
Browse files Browse the repository at this point in the history
Intended to replace ChrootExecStdin() in order to maintain more control
over how we chroot in the environment.
  • Loading branch information
joebonrichie committed Sep 15, 2024
1 parent 8314405 commit aebc307
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,68 @@ func ChrootExecStdin(notif PidNotifier, dir, command string) error {
return c.Wait()
}

func ChrootShell(notif PidNotifier, dir, command, workdir string) error {

// Hold an fd for the og root
fd, err := os.Open("/")
if err != nil {
return err
}

// Remember our working directory
wd, err := os.Getwd()
if err != nil {
return err
}

// Ensure chroot directory is available
if err := os.Chdir(dir); err != nil {
return err
}

if err := syscall.Chroot(dir); err != nil {
fd.Close()
return err
}

if err := os.Chdir("/"); err != nil {
return err
}

// Spawn a shell
args := []string{"--login"}
c := exec.Command("/bin/bash", args...)
c.Stdout = os.Stdout
c.Stderr = os.Stdout
c.Stdin = os.Stdin
c.Env = ChrootEnvironment
c.Dir = workdir

if err := c.Start(); err != nil {
goto CLEANUP
}

notif.SetActivePID(c.Process.Pid)

if err := c.Wait(); err != nil {
goto CLEANUP
}

CLEANUP:
// Return to our original root and working directory
defer fd.Close()
if err := fd.Chdir(); err != nil {
return err
}
if err := syscall.Chroot("."); err != nil {
return err
}
if err := os.Chdir(wd); err != nil {
return err
}
return err
}

func StartSccache(dir string) {
var buf bytes.Buffer

Expand Down

0 comments on commit aebc307

Please sign in to comment.