6 Commits
1.5.1 ... 1.6.1

Author SHA1 Message Date
Clayton Craft
731a805a9e getBinaryDeps: properly handle circular lib dependencies
This moves the recursive bit outside of getBinaryDeps and properly
handles circular dependencies.
2023-02-09 12:42:06 -08:00
Clayton Craft
b90624d7dd getBinaryDeps: move recursive bit to new function 2023-02-09 12:41:34 -08:00
Oliver Smith
2a75cf9b4e getInitfsExtraFiles: add scripts from hooks-extra (MR 27)
Put scripts from /etc/postmarketos-mkinitfs/hooks-extra into the extra
initramfs instead of the regular one, similar to how it is possible with
files listed in /etc/postmarketos-mkinitfs/files-extra.

This way we will be able to launch hooks not only very early in the
initramfs as it's currently the case. But also later on after the
initramfs-extra was extracted, and more files are available. ondev2 will
make use of this feature.
2023-01-17 08:12:03 +01:00
Oliver Smith
d52cc16c88 getBinaryDeps: search in /usr/lib/expect* (MR 28)
Instead of only searching for shared libraries in /usr/lib and /lib,
also search in /usr/lib/expect* (currently the expect binary links
against /usr/lib/expect5.45.4/libexpect5.45.4.so).

I've also considered searching /usr/lib recursively, but that would be a
major performance hit.

Expect gets added to the initramfs-extra in a script that runs the
ondev2 testsuite inside qemu.
2023-01-16 22:55:36 -08:00
Oliver Smith
112b572dc2 getFile: print exact error from os.Stat (MR 28)
Pass the exact error message down and make formatting of the message
consistent.
2023-01-16 22:55:30 -08:00
Oliver Smith
0c0a85f3bb getHookFiles: print exact error from getFiles (MR 28)
Instead of assuming that the error is "unable to find file", print the
actual error from getFiles. I just had a situation where the file
exists, but a dependency couldn't be found.

Before:
  generateInitfsExtra: getHookFiles: unable to find file "/usr/bin/expect" required by "/etc/postmarketos-mkinitfs/files-extra/ondev2-test.files"

After:
  generateInitfsExtra: getHookFiles: unable to add file "/usr/bin/expect" required by "/etc/postmarketos-mkinitfs/files-extra/ondev2-test.files": getBinaryDeps: unable to locate dependency for "/usr/bin/expect": libexpect5.45.4.so
2023-01-16 22:55:18 -08:00

135
main.go
View File

@@ -165,7 +165,7 @@ func getHookFiles(filesdir string) (files []string, err error) {
s := bufio.NewScanner(f) s := bufio.NewScanner(f)
for s.Scan() { for s.Scan() {
if filelist, err := getFiles([]string{s.Text()}, true); err != nil { if filelist, err := getFiles([]string{s.Text()}, true); err != nil {
return nil, fmt.Errorf("getHookFiles: unable to find file %q required by %q", s.Text(), path) return nil, fmt.Errorf("getHookFiles: unable to add file %q required by %q: %w", s.Text(), path, err)
} else { } else {
files = append(files, filelist...) files = append(files, filelist...)
} }
@@ -177,8 +177,62 @@ func getHookFiles(filesdir string) (files []string, err error) {
return files, nil return files, nil
} }
func getDeps(file string, parents map[string]struct{}) (files []string, err error) {
if _, found := parents[file]; found {
return
}
// get dependencies for binaries
fd, err := elf.Open(file)
if err != nil {
return nil, fmt.Errorf("getDeps: unable to open elf binary %q: %w", file, err)
}
libs, _ := fd.ImportedLibraries()
fd.Close()
files = append(files, file)
parents[file] = struct{}{}
if len(libs) == 0 {
return
}
// we don't recursively search these paths for performance reasons
libdirGlobs := []string{
"/usr/lib",
"/lib",
"/usr/lib/expect*",
}
for _, lib := range libs {
found := false
findDepLoop:
for _, libdirGlob := range libdirGlobs {
libdirs, _ := filepath.Glob(libdirGlob)
for _, libdir := range libdirs {
path := filepath.Join(libdir, lib)
if _, err := os.Stat(path); err == nil {
binaryDepFiles, err := getDeps(path, parents)
if err != nil {
return nil, err
}
files = append(files, binaryDepFiles...)
files = append(files, path)
found = true
break findDepLoop
}
}
}
if !found {
return nil, fmt.Errorf("getDeps: unable to locate dependency for %q: %s", file, lib)
}
}
return
}
// Recursively list all dependencies for a given ELF binary // Recursively list all dependencies for a given ELF binary
func getBinaryDeps(file string) (files []string, err error) { func getBinaryDeps(file string) ([]string, error) {
// if file is a symlink, resolve dependencies for target // if file is a symlink, resolve dependencies for target
fileStat, err := os.Lstat(file) fileStat, err := os.Lstat(file)
if err != nil { if err != nil {
@@ -194,52 +248,14 @@ func getBinaryDeps(file string) (files []string, err error) {
if !filepath.IsAbs(target) { if !filepath.IsAbs(target) {
target, err = misc.RelativeSymlinkTargetToDir(target, filepath.Dir(file)) target, err = misc.RelativeSymlinkTargetToDir(target, filepath.Dir(file))
if err != nil { if err != nil {
return files, err return nil, err
} }
} }
binaryDepFiles, err := getBinaryDeps(target) file = target
if err != nil {
return files, err
}
files = append(files, binaryDepFiles...)
return files, err
} }
// get dependencies for binaries return getDeps(file, make(map[string]struct{}))
fd, err := elf.Open(file)
if err != nil {
return nil, fmt.Errorf("getBinaryDeps: unable to open elf binary %q: %w", file, err)
}
libs, _ := fd.ImportedLibraries()
fd.Close()
files = append(files, file)
if len(libs) == 0 {
return files, err
}
libdirs := []string{"/usr/lib", "/lib"}
for _, lib := range libs {
found := false
for _, libdir := range libdirs {
path := filepath.Join(libdir, lib)
if _, err := os.Stat(path); err == nil {
binaryDepFiles, err := getBinaryDeps(path)
if err != nil {
return files, err
}
files = append(files, binaryDepFiles...)
files = append(files, path)
found = true
break
}
}
if !found {
return nil, fmt.Errorf("getBinaryDeps: unable to locate dependency for %q: %s", file, lib)
}
}
return
} }
func getFiles(list []string, required bool) (files []string, err error) { func getFiles(list []string, required bool) (files []string, err error) {
@@ -275,7 +291,7 @@ func getFile(file string, required bool) (files []string, err error) {
fileInfo, err := os.Stat(file) fileInfo, err := os.Stat(file)
if err != nil { if err != nil {
if required { if required {
return files, errors.New("getFile: File does not exist :" + file) return files, fmt.Errorf("getFile: failed to stat file %q: %w", file, err)
} }
return files, nil return files, nil
} }
@@ -426,11 +442,16 @@ func getFdeFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
return return
} }
func getHookScripts() (files []string) { func getHookScripts(scriptsdir string) (files []string, err error) {
scripts, _ := filepath.Glob("/etc/postmarketos-mkinitfs/hooks/*.sh") fileInfo, err := os.ReadDir(scriptsdir)
files = append(files, scripts...) if err != nil {
return nil, fmt.Errorf("getHookScripts: unable to read hook script dir: %w", err)
return }
for _, file := range fileInfo {
path := filepath.Join(scriptsdir, file.Name())
files = append(files, path)
}
return files, nil
} }
func getInitfsExtraFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) { func getInitfsExtraFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
@@ -466,6 +487,15 @@ func getInitfsExtraFiles(devinfo deviceinfo.DeviceInfo) (files []string, err err
} }
} }
if exists("/etc/postmarketos-mkinitfs/hooks-extra") {
log.Println("- Including extra hook scripts")
if hookScripts, err := getHookScripts("/etc/postmarketos-mkinitfs/hooks-extra"); err != nil {
return nil, err
} else {
files = append(files, hookScripts...)
}
}
if exists("/usr/bin/osk-sdl") { if exists("/usr/bin/osk-sdl") {
log.Println("- Including FDE support") log.Println("- Including FDE support")
if fdeFiles, err := getFdeFiles(devinfo); err != nil { if fdeFiles, err := getFdeFiles(devinfo); err != nil {
@@ -506,9 +536,14 @@ func getInitfsFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
} }
} }
if exists("/etc/postmarketos-mkinitfs/hooks") {
log.Println("- Including hook scripts") log.Println("- Including hook scripts")
hookScripts := getHookScripts() if hookScripts, err := getHookScripts("/etc/postmarketos-mkinitfs/hooks"); err != nil {
return nil, err
} else {
files = append(files, hookScripts...) files = append(files, hookScripts...)
}
}
log.Println("- Including required binaries") log.Println("- Including required binaries")
if filelist, err := getFiles(requiredFiles, true); err != nil { if filelist, err := getFiles(requiredFiles, true); err != nil {