mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
188 lines
4.8 KiB
Go
188 lines
4.8 KiB
Go
//go:build linux && subprocess && chunkstore_integration
|
|
|
|
package main_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Preconditions are assertions: a privileged run is not non-root evidence.
|
|
func assertMiniUnprivileged(t *testing.T) {
|
|
t.Helper()
|
|
if os.Getuid() == 0 || os.Geteuid() == 0 {
|
|
t.Fatal("non-root qualification must run without uid 0")
|
|
}
|
|
p := "/run/sw-block/locks"
|
|
for {
|
|
_, err := os.Stat(p)
|
|
if err == nil {
|
|
break
|
|
}
|
|
if errors.Is(err, syscall.EACCES) || errors.Is(err, syscall.EPERM) {
|
|
t.Logf("non-root uid=%d euid=%d; host lock path %s is inaccessible: %v", os.Getuid(), os.Geteuid(), p, err)
|
|
return
|
|
}
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("host lock preflight: %v", err)
|
|
}
|
|
p = filepath.Dir(p)
|
|
}
|
|
if err := syscall.Access(p, 3); err == nil {
|
|
t.Fatalf("host lock path/ancestor %s is writable; non-root precondition is unproved", p)
|
|
} else if !errors.Is(err, syscall.EACCES) && !errors.Is(err, syscall.EPERM) {
|
|
t.Fatalf("host lock access preflight: %v", err)
|
|
}
|
|
t.Logf("non-root uid=%d euid=%d; host lock ancestor %s is not writable", os.Getuid(), os.Geteuid(), p)
|
|
}
|
|
|
|
type miniOutput struct {
|
|
mu sync.Mutex
|
|
b bytes.Buffer
|
|
}
|
|
|
|
func (b *miniOutput) Write(p []byte) (int, error) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
return b.b.Write(p)
|
|
}
|
|
func (b *miniOutput) String() string { b.mu.Lock(); defer b.mu.Unlock(); return b.b.String() }
|
|
|
|
type ownedMini struct {
|
|
cmd *exec.Cmd
|
|
done chan struct{}
|
|
err error
|
|
output miniOutput
|
|
stopOnce sync.Once
|
|
}
|
|
|
|
// All descendants inherit this dedicated group. Cleanup is registered before
|
|
// any assertion can fail; TERM, bounded join, KILL, bounded join. No name kill.
|
|
func startOwnedMini(t *testing.T, ctx context.Context, bin string, args ...string) *ownedMini {
|
|
t.Helper()
|
|
p := &ownedMini{cmd: exec.CommandContext(ctx, bin, args...), done: make(chan struct{})}
|
|
p.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
p.cmd.Stdout, p.cmd.Stderr = &p.output, &p.output
|
|
p.cmd.WaitDelay = 3 * time.Second
|
|
p.cmd.Cancel = func() error { return syscall.Kill(-p.cmd.Process.Pid, syscall.SIGTERM) }
|
|
if err := p.cmd.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
go func() { p.err = p.cmd.Wait(); close(p.done) }()
|
|
t.Cleanup(func() { p.stop(t) })
|
|
return p
|
|
}
|
|
func (p *ownedMini) join(t *testing.T, bound time.Duration) error {
|
|
t.Helper()
|
|
select {
|
|
case <-p.done:
|
|
return p.err
|
|
case <-time.After(bound):
|
|
t.Fatalf("owned process did not join in %s", bound)
|
|
return nil
|
|
}
|
|
}
|
|
func (p *ownedMini) stop(t *testing.T) {
|
|
p.stopOnce.Do(func() { p.stopGroup(t) })
|
|
}
|
|
func (p *ownedMini) stopGroup(t *testing.T) {
|
|
t.Helper()
|
|
pgid := p.cmd.Process.Pid
|
|
if pgid <= 1 {
|
|
t.Error("invalid owned process group")
|
|
return
|
|
}
|
|
_ = syscall.Kill(-pgid, syscall.SIGTERM)
|
|
select {
|
|
case <-p.done:
|
|
case <-time.After(2 * time.Second):
|
|
}
|
|
_ = syscall.Kill(-pgid, syscall.SIGKILL)
|
|
_ = p.join(t, 5*time.Second)
|
|
p.assertStopped(t)
|
|
}
|
|
func (p *ownedMini) assertStopped(t *testing.T) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
live := miniGroupLive(t, p.cmd.Process.Pid)
|
|
if len(live) == 0 {
|
|
return
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("owned group %d still has live descendants %v", p.cmd.Process.Pid, live)
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
}
|
|
func miniGroupLive(t *testing.T, pgid int) []int {
|
|
t.Helper()
|
|
entries, err := os.ReadDir("/proc")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var live []int
|
|
for _, entry := range entries {
|
|
pid, err := strconv.Atoi(entry.Name())
|
|
if err != nil {
|
|
continue
|
|
}
|
|
b, err := os.ReadFile(filepath.Join("/proc", entry.Name(), "stat"))
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
end := strings.LastIndexByte(string(b), ')')
|
|
if end < 0 {
|
|
t.Fatalf("invalid proc stat for %d", pid)
|
|
}
|
|
fields := strings.Fields(string(b[end+1:]))
|
|
if len(fields) < 3 {
|
|
t.Fatal("short proc stat")
|
|
}
|
|
group, err := strconv.Atoi(fields[2])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if group == pgid && fields[0] != "Z" && fields[0] != "X" {
|
|
live = append(live, pid)
|
|
}
|
|
}
|
|
return live
|
|
}
|
|
|
|
// An intentionally TERM-ignoring descendant exercises the safety net itself.
|
|
func TestMiniOwnedGroupCleanupStopsDescendants(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
p := startOwnedMini(t, ctx, "/bin/sh", "-c", "trap '' TERM; sleep 60 & echo ready; wait")
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for !strings.Contains(p.output.String(), "ready") {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("fixture did not start")
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
if live := miniGroupLive(t, p.cmd.Process.Pid); len(live) < 2 {
|
|
t.Fatalf("descendant fixture not reached: %v", live)
|
|
}
|
|
p.stop(t)
|
|
select {
|
|
case <-p.done:
|
|
default:
|
|
t.Fatal("leader not joined")
|
|
}
|
|
}
|