Null-DRM-Official/apps/streamd/internal/supervisor/proc_unix.go
404errordeveloper 5b6f38c9cf
All checks were successful
release / build-and-upload (push) Successful in 6m56s
streamd v0.2.3: fix NRE Spectre crash under daemon (Linux PTY)
Wrap N_m3u8DL-RE with util-linux script so Spectre gets a real TTY; drop
subtitles; pin COLUMNS/LINES; kill process group on stop. Fixes immediate
'downloader exited before any media arrived' on serve start.
2026-10-10 19:54:41 +02:00

94 lines
2.3 KiB
Go

//go:build !windows
package supervisor
import (
"io"
"os"
"os/exec"
"strings"
"syscall"
)
func hideWindow(cmd *exec.Cmd) {}
func killProcessTree(cmd *exec.Cmd) error {
if cmd == nil || cmd.Process == nil {
return nil
}
pid := cmd.Process.Pid
// Negative PID signals the process group (script PTY wrapper + NRE).
if err := syscall.Kill(-pid, syscall.SIGKILL); err == nil {
return nil
}
return cmd.Process.Kill()
}
func processDead(pid int) bool {
p, err := os.FindProcess(pid)
if err != nil {
return true
}
err = p.Signal(syscall.Signal(0))
return err != nil
}
// startNRELogged starts NRE under a PTY so Spectre.Console sees a real terminal.
// Plain file/pipe redirects make N_m3u8DL-RE force ANSI UI with WindowWidth 0,
// which crashes ProgressRefreshThread (ArgumentException/RemoveRange).
//
// Uses util-linux `script` when available (PTY + typescript log). Falls back to
// attaching logFile as Stdout/Stderr. keepLogFD is false when script took over
// the log path (caller must not Close logFile again).
func startNRELogged(cmd *exec.Cmd, logFile *os.File) (pty io.Closer, keepLogFD bool, err error) {
scriptPath, err := exec.LookPath("script")
if err != nil {
cmd.Stdout = logFile
cmd.Stderr = logFile
if err := cmd.Start(); err != nil {
return nil, true, err
}
return nopCloser{}, true, nil
}
logPath := logFile.Name()
_ = logFile.Sync()
_ = logFile.Close()
inner := shellJoin(append([]string{cmd.Path}, cmd.Args[1:]...)...)
// -q quiet header, -a append (keep prior runs), -f flush so log tails live.
wrapped := exec.Command(scriptPath, "-q", "-a", "-f", "-c", inner, logPath)
wrapped.Env = cmd.Env
wrapped.Dir = cmd.Dir
if cmd.SysProcAttr != nil {
wrapped.SysProcAttr = cmd.SysProcAttr
} else {
wrapped.SysProcAttr = &syscall.SysProcAttr{}
}
// Own process group so Stop can signal the whole tree (script + NRE).
wrapped.SysProcAttr.Setpgid = true
*cmd = *wrapped
if err := cmd.Start(); err != nil {
return nil, false, err
}
return nopCloser{}, false, nil
}
func shellJoin(argv ...string) string {
parts := make([]string, len(argv))
for i, a := range argv {
parts[i] = shellQuote(a)
}
return strings.Join(parts, " ")
}
func shellQuote(s string) string {
if s == "" {
return "''"
}
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
}
type nopCloser struct{}
func (nopCloser) Close() error { return nil }