Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpi: throw an error when trying to run the 32-bit server on 64-bit OS and vice-versa #3721

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/staticsources/rpicamera/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *camera) initialize() error {
"LD_LIBRARY_PATH=" + dumpPath,
}

c.cmd = exec.Command(filepath.Join(dumpPath, "mtxrpicam"))
c.cmd = exec.Command(filepath.Join(dumpPath, executableName))
c.cmd.Stdout = os.Stdout
c.cmd.Stderr = os.Stderr
c.cmd.Env = env
Expand Down
69 changes: 64 additions & 5 deletions internal/staticsources/rpicamera/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
package rpicamera

import (
"debug/elf"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
)

//go:generate go run ./mtxrpicamdownloader

const (
dumpPrefix = "/dev/shm/mediamtx-rpicamera-"
libraryToCheckArchitecture = "libc.so.6"
dumpPrefix = "/dev/shm/mediamtx-rpicamera-"
executableName = "mtxrpicam"
)

var (
Expand All @@ -23,6 +28,55 @@ var (
dumpPath = ""
)

func getArchitecture(libPath string) (bool, error) {
f, err := os.Open(libPath)
if err != nil {
return false, err
}
defer f.Close()

ef, err := elf.NewFile(f)
if err != nil {
return false, err
}
defer ef.Close()

return (ef.FileHeader.Class == elf.ELFCLASS64), nil
}

func checkArchitecture() error {
byts, err := exec.Command("ldconfig", "-p").Output()
if err != nil {
return fmt.Errorf("ldconfig failed: %w", err)
}

for _, line := range strings.Split(string(byts), "\n") {
f := strings.Split(line, " => ")
if len(f) == 2 && strings.Contains(f[1], libraryToCheckArchitecture) {
is64, err := getArchitecture(f[1])
if err != nil {
return err
}

if runtime.GOARCH == "arm" {
if !is64 {
return nil
}
} else {
if is64 {
return nil
}
}
}
}

if runtime.GOARCH == "arm" {
return fmt.Errorf("the operating system is 64-bit, you need the 64-bit server version")
}

return fmt.Errorf("the operating system is 32-bit, you need the 32-bit server version")
}

func dumpEmbedFSRecursive(src string, dest string) error {
files, err := component.ReadDir(src)
if err != nil {
Expand Down Expand Up @@ -65,9 +119,14 @@ func dumpComponent() error {
return nil
}

err := checkArchitecture()
if err != nil {
return err
}

dumpPath = dumpPrefix + strconv.FormatInt(time.Now().UnixNano(), 10)

err := os.Mkdir(dumpPath, 0o755)
err = os.Mkdir(dumpPath, 0o755)
if err != nil {
return err
}
Expand All @@ -84,7 +143,7 @@ func dumpComponent() error {
return err
}

err = os.Chmod(filepath.Join(dumpPath, "mtxrpicam"), 0o755)
err = os.Chmod(filepath.Join(dumpPath, executableName), 0o755)
if err != nil {
os.RemoveAll(dumpPath)
return err
Expand Down
Loading