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

This commit is contained in:
Alessandro Ros
2024-09-03 19:12:31 +02:00
committed by GitHub
parent 3b7b2305d6
commit abd34d8978
2 changed files with 65 additions and 6 deletions
+1 -1
View File
@@ -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
+64 -5
View File
@@ -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 (
@@ -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 {
@@ -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
}
@@ -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