diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-11-14 08:14:46 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-11-14 08:33:19 +0900 |
| commit | 690a0ed0d68e270e60bfe9c7cd575c4e986de83d (patch) | |
| tree | be14195bb5b4e64ddfceb19a4a09d0c5f9f3cec3 /ldd/exec.go | |
| parent | a9d72a5eb19827ded874e359a99f91cc4d91f4b3 (diff) | |
ldd: decode from reader
This should reduce memory footprint of the parsing process and allow decoding part of the stream.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'ldd/exec.go')
| -rw-r--r-- | ldd/exec.go | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/ldd/exec.go b/ldd/exec.go index c52eff1c..d91cd652 100644 --- a/ldd/exec.go +++ b/ldd/exec.go @@ -3,6 +3,7 @@ package ldd import ( "bytes" "context" + "errors" "io" "os" "os/exec" @@ -16,11 +17,14 @@ import ( "hakurei.app/message" ) -var ( - msgStatic = []byte("Not a valid dynamic program") - msgStaticGlibc = []byte("not a dynamic executable") +const ( + // msgStaticSuffix is the suffix of message printed to stderr by musl on a statically linked program. + msgStaticSuffix = ": Not a valid dynamic program" + // msgStaticGlibc is a substring of the message printed to stderr by glibc on a statically linked program. + msgStaticGlibc = "not a dynamic executable" ) +// Exec runs ldd(1) in a restrictive [container] and connects it to a [Decoder], returning resulting entries. func Exec(ctx context.Context, msg message.Msg, p string) ([]*Entry, error) { const ( lddName = "ldd" @@ -41,14 +45,20 @@ func Exec(ctx context.Context, msg message.Msg, p string) ([]*Entry, error) { z.Hostname = "hakurei-" + lddName z.SeccompFlags |= seccomp.AllowMultiarch z.SeccompPresets |= std.PresetStrict - stdout, stderr := new(bytes.Buffer), new(bytes.Buffer) - z.Stdout = stdout + stderr := new(bytes.Buffer) z.Stderr = stderr z. Bind(fhs.AbsRoot, fhs.AbsRoot, 0). Proc(fhs.AbsProc). Dev(fhs.AbsDev, false) + var d *Decoder + if r, err := z.StdoutPipe(); err != nil { + return nil, err + } else { + d = NewDecoder(r) + } + if err := z.Start(); err != nil { return nil, err } @@ -56,15 +66,18 @@ func Exec(ctx context.Context, msg message.Msg, p string) ([]*Entry, error) { if err := z.Serve(); err != nil { return nil, err } + + entries, decodeErr := d.Decode() if err := z.Wait(); err != nil { m := stderr.Bytes() - if bytes.Contains(m, append([]byte(p+": "), msgStatic...)) || - bytes.Contains(m, msgStaticGlibc) { + if bytes.Contains(m, []byte(msgStaticSuffix)) || bytes.Contains(m, []byte(msgStaticGlibc)) { return nil, nil } + + if decodeErr != nil { + return nil, errors.Join(decodeErr, err) + } return nil, err } - - v := stdout.Bytes() - return Parse(v) + return entries, decodeErr } |
