package container

import (
	"context"
	"encoding/gob"
	"errors"
	"fmt"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"path/filepath"
	"slices"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	. "syscall"
	"time"

	"hakurei.app/check"
	"hakurei.app/container/seccomp"
	"hakurei.app/ext"
	"hakurei.app/fhs"
	"hakurei.app/internal/params"
	"hakurei.app/message"
)

const (
	/* intermediateHostPath is the pathname of the intermediate tmpfs mount point.

	This path might seem like a weird choice, however there are many good reasons to use it:
	- The contents of this path is never exposed to the container:
	  The tmpfs root established here effectively becomes anonymous after pivot_root.
	- It is safe to assume this path exists and is a directory:
	  This program will not work correctly without a proper /proc and neither will most others.
	- This path belongs to the container init:
	  The container init is not any more privileged or trusted than the rest of the container.
	- This path is only accessible by init and root:
	  The container init sets SUID_DUMP_DISABLE and terminates if that fails.

	It should be noted that none of this should become relevant at any point
	since the resulting intermediate root tmpfs should be effectively anonymous. */
	intermediateHostPath = fhs.Proc + "self/fd"

	// setupEnv is the name of the environment variable holding the string
	// representation of the read end file descriptor of the setup params pipe.
	setupEnv = "HAKUREI_SETUP"

	// exitUnexpectedWait4 is the exit code if wait4 returns an unexpected errno.
	exitUnexpectedWait4 = 2
)

type (
	// Ops is a collection of [Op].
	Ops []Op

	// Op is a generic setup step ran inside the container init.
	// Implementations of this interface are sent as a stream of gobs.
	Op interface {
		// early is called in host root.
		early(state *setupState, k syscallDispatcher) error
		// apply is called in intermediate root.
		apply(state *setupState, k syscallDispatcher) error
		// late is called right before starting the initial process.
		late(state *setupState, k syscallDispatcher) error

		// prefix returns a log message prefix, and whether this Op prints no
		// identifying message on its own.
		prefix() (string, bool)

		Is(op Op) bool
		Valid() bool
		fmt.Stringer
	}

	// setupState persists context between Ops.
	setupState struct {
		nonrepeatable uintptr

		// Whether early reaping has concluded. Must only be accessed in the
		// wait4 loop.
		processConcluded bool
		// Process to syscall.WaitStatus populated in the wait4 loop. Freed
		// after early reaping concludes.
		process map[int]WaitStatus
		// Synchronises access to process.
		processMu sync.RWMutex

		*Params
		context.Context
		message.Msg
	}
)

// terminated returns whether the specified pid has been reaped, and its
// syscall.WaitStatus if it had. This is only usable by [Op].
func (state *setupState) terminated(pid int) (wstatus WaitStatus, ok bool) {
	state.processMu.RLock()
	wstatus, ok = state.process[pid]
	state.processMu.RUnlock()
	return
}

// Grow grows the slice Ops points to using [slices.Grow].
func (f *Ops) Grow(n int) { *f = slices.Grow(*f, n) }

const (
	nrAutoEtc = 1 << iota
	nrAutoRoot
)

// OpRepeatError is returned applying a repeated nonrepeatable [Op].
type OpRepeatError string

func (e OpRepeatError) Error() string { return string(e) + " is not repeatable" }

// OpStateError indicates an impossible internal state has been reached in an [Op].
type OpStateError string

func (o OpStateError) Error() string { return "impossible " + string(o) + " state reached" }

// initParams are params passed from parent.
type initParams struct {
	Params

	HostUid, HostGid int
	// extra files count
	Count int
	// verbosity pass through
	Verbose bool
}

// Init is called if the current process is the container init.
func Init(msg message.Msg) { initEntrypoint(direct{}, msg) }

func initEntrypoint(k syscallDispatcher, msg message.Msg) {
	k.lockOSThread()

	if msg == nil {
		panic("attempting to call initEntrypoint with nil msg")
	}

	if k.getpid() != 1 {
		k.fatal(msg, "this process must run as pid 1")
	}

	if err := k.setPtracer(0); err != nil {
		msg.Verbosef("cannot enable ptrace protection via Yama LSM: %v", err)
		// not fatal: this program has no additional privileges at initial program start
	}

	var (
		param      initParams
		closeSetup func() error
		setupFd    int
	)
	if f, err := k.receive(setupEnv, &param, &setupFd); err != nil {
		if errors.Is(err, EBADF) {
			k.fatal(msg, "invalid setup descriptor")
		}
		if errors.Is(err, params.ErrReceiveEnv) {
			k.fatal(msg, setupEnv+" not set")
		}

		k.fatalf(msg, "cannot decode init setup payload: %v", err)
	} else {
		if param.Ops == nil {
			k.fatal(msg, "invalid setup parameters")
		}
		if param.ParentPerm == 0 {
			param.ParentPerm = 0755
		}

		msg.SwapVerbose(param.Verbose)
		msg.Verbose("received setup parameters")
		closeSetup = f
	}

	if !param.HostNet {
		ctx, cancel := signal.NotifyContext(context.Background(), CancelSignal,
			os.Interrupt, SIGTERM, SIGQUIT)
		defer cancel() // for panics
		k.mustLoopback(ctx, msg)
		cancel()
	}

	uid, gid := param.Uid, param.Gid
	if param.InitAsRoot {
		uid, gid = 0, 0
	}

	// write uid/gid map here so parent does not need to set dumpable
	if err := k.setDumpable(ext.SUID_DUMP_USER); err != nil {
		k.fatalf(msg, "cannot set SUID_DUMP_USER: %v", err)
	}
	if err := k.writeFile(
		fhs.Proc+"self/uid_map",
		[]byte(strconv.Itoa(uid)+" "+strconv.Itoa(param.HostUid)+" 1\n"),
		0,
	); err != nil {
		k.fatalf(msg, "%v", err)
	}
	if err := k.writeFile(
		fhs.Proc+"self/setgroups",
		[]byte("deny\n"),
		0,
	); err != nil && !os.IsNotExist(err) {
		k.fatalf(msg, "%v", err)
	}
	if err := k.writeFile(fhs.Proc+"self/gid_map",
		[]byte(strconv.Itoa(gid)+" "+strconv.Itoa(param.HostGid)+" 1\n"),
		0,
	); err != nil {
		k.fatalf(msg, "%v", err)
	}
	if err := k.setDumpable(ext.SUID_DUMP_DISABLE); err != nil {
		k.fatalf(msg, "cannot set SUID_DUMP_DISABLE: %v", err)
	}

	oldmask := k.umask(0)
	if param.Hostname != "" {
		if err := k.sethostname([]byte(param.Hostname)); err != nil {
			k.fatalf(msg, "cannot set hostname: %v", err)
		}
	}

	// cache sysctl before pivot_root
	lastcap := k.lastcap(msg)

	if err := k.mount(zeroString, fhs.Root, zeroString, MS_SILENT|MS_SLAVE|MS_REC, zeroString); err != nil {
		k.fatalf(msg, "cannot make / rslave: %v", optionalErrorUnwrap(err))
	}

	ctx, cancel := context.WithCancel(context.Background())
	state := &setupState{process: make(map[int]WaitStatus), Params: &param.Params, Msg: msg, Context: ctx}
	defer cancel()

	if err := k.mount(SourceTmpfsRootfs, intermediateHostPath, FstypeTmpfs, MS_NODEV|MS_NOSUID, zeroString); err != nil {
		k.fatalf(msg, "cannot mount intermediate root: %v", optionalErrorUnwrap(err))
	}
	if err := k.chdir(intermediateHostPath); err != nil {
		k.fatalf(msg, "cannot enter intermediate host path: %v", err)
	}

	if len(param.Binfmt) > 0 {
		for i, e := range param.Binfmt {
			if pathname, err := k.evalSymlinks(e.Interpreter.String()); err != nil {
				k.fatal(msg, err)
			} else if param.Binfmt[i].Interpreter, err = check.NewAbs(pathname); err != nil {
				k.fatal(msg, err)
			}
		}
	}

	/* early is called right before pivot_root into intermediate root;
	this step is mostly for gathering information that would otherwise be
	difficult to obtain via library functions after pivot_root, and
	implementations are expected to avoid changing the state of the mount
	namespace */
	for i, op := range *param.Ops {
		if op == nil || !op.Valid() {
			k.fatalf(msg, "invalid op at index %d", i)
		}

		if err := op.early(state, k); err != nil {
			if m, ok := messageFromError(err); ok {
				k.fatal(msg, m)
			} else {
				k.fatalf(msg, "cannot prepare op at index %d: %v", i, err)
			}
		}
	}

	if err := k.mkdir(sysrootDir, 0755); err != nil {
		k.fatalf(msg, "%v", err)
	}
	if err := k.mount(sysrootDir, sysrootDir, zeroString, MS_SILENT|MS_BIND|MS_REC, zeroString); err != nil {
		k.fatalf(msg, "cannot bind sysroot: %v", optionalErrorUnwrap(err))
	}

	if err := k.mkdir(hostDir, 0755); err != nil {
		k.fatalf(msg, "%v", err)
	}
	// pivot_root uncovers intermediateHostPath in hostDir
	if err := k.pivotRoot(intermediateHostPath, hostDir); err != nil {
		k.fatalf(msg, "cannot pivot into intermediate root: %v", err)
	}
	if err := k.chdir(fhs.Root); err != nil {
		k.fatalf(msg, "cannot enter intermediate root: %v", err)
	}

	/* apply is called right after pivot_root and entering the new root. This
	step sets up the container filesystem, and implementations are expected to
	keep the host root and sysroot mount points intact but otherwise can do
	whatever they need to. Calling chdir is allowed but discouraged. */
	for i, op := range *param.Ops {
		// ops already checked during early setup
		if prefix, ok := op.prefix(); ok {
			msg.Verbosef("%s %s", prefix, op)
		}
		if err := op.apply(state, k); err != nil {
			if m, ok := messageFromError(err); ok {
				k.fatal(msg, m)
			} else {
				k.fatalf(msg, "cannot apply op at index %d: %v", i, err)
			}
		}
	}

	if len(param.Binfmt) > 0 {
		const interpreter = "/interpreter"

		if param.BinfmtPath == nil {
			param.BinfmtPath = fhs.AbsProcSys.Append("fs/binfmt_misc")
		}
		binfmt := sysrootPath + param.BinfmtPath.String()
		if err := k.mkdirAll(binfmt, 0); err != nil {
			k.fatal(msg, err)
		}
		if err := k.mount(
			SourceBinfmtMisc,
			binfmt,
			FstypeBinfmtMisc,
			MS_NOSUID|MS_NOEXEC|MS_NODEV,
			zeroString,
		); err != nil {
			k.fatal(msg, err)
		}

		var buf strings.Builder
		buf.Grow(1920)

		register := binfmt + "/register"
		for i, e := range param.Binfmt {
			if err := k.symlink(hostPath+e.Interpreter.String(), interpreter); err != nil {
				k.fatal(msg, err)
			} else if err = k.writeFile(register, []byte(":"+
				strconv.Itoa(i)+":"+
				"M:"+
				strconv.Itoa(int(e.Offset))+":"+
				escapeBinfmt(&buf, e.Magic)+":"+
				escapeBinfmt(&buf, e.Mask)+":"+
				interpreter+":"+
				"F"), 0); err != nil {
				k.fatal(msg, err)
			} else if err = k.remove(interpreter); err != nil {
				k.fatal(msg, err)
			}
		}
	}

	// setup requiring host root complete at this point
	if err := k.mount(hostDir, hostDir, zeroString, MS_SILENT|MS_REC|MS_PRIVATE, zeroString); err != nil {
		k.fatalf(msg, "cannot make host root rprivate: %v", optionalErrorUnwrap(err))
	}
	if err := k.unmount(hostDir, MNT_DETACH); err != nil {
		k.fatalf(msg, "cannot unmount host root: %v", err)
	}

	{
		var fd int
		if err := ext.IgnoringEINTR(func() (err error) {
			fd, err = k.open(fhs.Root, O_DIRECTORY|O_RDONLY, 0)
			return
		}); err != nil {
			k.fatalf(msg, "cannot open intermediate root: %v", err)
		}
		if err := k.chdir(sysrootPath); err != nil {
			k.fatalf(msg, "cannot enter sysroot: %v", err)
		}

		if err := k.pivotRoot(".", "."); err != nil {
			k.fatalf(msg, "cannot pivot into sysroot: %v", err)
		}
		if err := k.fchdir(fd); err != nil {
			k.fatalf(msg, "cannot re-enter intermediate root: %v", err)
		}
		if err := k.unmount(".", MNT_DETACH); err != nil {
			k.fatalf(msg, "cannot unmount intermediate root: %v", err)
		}
		if err := k.chdir(fhs.Root); err != nil {
			k.fatalf(msg, "cannot enter root: %v", err)
		}

		if err := k.close(fd); err != nil {
			k.fatalf(msg, "cannot close intermediate root: %v", err)
		}
	}

	var keepCaps []uintptr
	if param.Privileged {
		keepCaps = append(keepCaps, CAP_SYS_ADMIN, CAP_SETPCAP)
	}
	if param.InitAsRoot {
		keepCaps = append(keepCaps, CAP_SETFCAP)
	}

	if err := k.capAmbientClearAll(); err != nil {
		k.fatalf(msg, "cannot clear the ambient capability set: %v", err)
	}
	for i := range lastcap + 1 {
		if slices.Contains(keepCaps, i) {
			continue
		}
		if err := k.capBoundingSetDrop(i); err != nil {
			k.fatalf(msg, "cannot drop capability from bounding set: %v", err)
		}
	}

	var keep [2]uint32
	for _, c := range keepCaps {
		keep[capToIndex(c)] |= capToMask(c)
	}

	if err := k.capset(
		&capHeader{_LINUX_CAPABILITY_VERSION_3, 0},
		&[2]capData{{keep[0], keep[0], keep[0]}, {keep[1], keep[1], keep[1]}},
	); err != nil {
		k.fatalf(msg, "cannot capset: %v", err)
	}

	for _, c := range keepCaps {
		if err := k.capAmbientRaise(c); err != nil {
			k.fatalf(msg, "cannot raise %#x: %v", c, err)
		}
	}

	if !param.SeccompDisable {
		rules := param.SeccompRules
		if len(rules) == 0 { // non-empty rules slice always overrides presets
			msg.Verbosef("resolving presets %#x", param.SeccompPresets)
			rules = seccomp.Preset(param.SeccompPresets, param.SeccompFlags)
		}
		if err := k.seccompLoad(rules, param.SeccompFlags); err != nil {
			// this also indirectly asserts PR_SET_NO_NEW_PRIVS
			k.fatalf(msg, "cannot load syscall filter: %v", err)
		}
		msg.Verbosef("%d filter rules loaded", len(rules))
	} else {
		msg.Verbose("syscall filter not configured")
	}

	extraFiles := make([]*os.File, param.Count)
	for i := range extraFiles {
		// setup fd is placed before all extra files
		extraFiles[i] = k.newFile(uintptr(setupFd+1+i), "extra file "+strconv.Itoa(i))
	}
	k.umask(oldmask)

	// winfo represents an exited process from wait4.
	type winfo struct {
		wpid    int
		wstatus WaitStatus
	}

	// info is closed as the wait4 thread terminates
	// when there are no longer any processes left to reap
	info := make(chan winfo, 1)

	// whether initial process has started
	var initialProcessStarted atomic.Bool

	k.new(func(k syscallDispatcher) {
		k.lockOSThread()

	wait4:
		var (
			err     error
			wpid    = -2
			wstatus WaitStatus

			// whether initial process has started
			started bool
		)

		// keep going until no child process is left
		for wpid != -1 {
			if err != nil {
				break
			}

			if wpid != -2 {
				if !state.processConcluded {
					state.processMu.Lock()
					if state.process == nil {
						// early reaping has already concluded at this point
						state.processConcluded = true
						info <- winfo{wpid, wstatus}
					} else {
						// initial process has not yet been created, and the
						// info channel is not yet being received from
						state.process[wpid] = wstatus
					}
					state.processMu.Unlock()
				} else {
					info <- winfo{wpid, wstatus}
				}
			}

			if !started {
				started = initialProcessStarted.Load()
			}

			err = EINTR
			for errors.Is(err, EINTR) {
				wpid, err = k.wait4(-1, &wstatus, 0, nil)
			}
		}

		if !errors.Is(err, ECHILD) {
			k.printf(msg, "unexpected wait4 response: %v", err)
		} else if !started {
			// initial process has not yet been reached and all daemons
			// terminated or none were started in the first place
			time.Sleep(500 * time.Microsecond)
			goto wait4
		}

		close(info)
	})

	// called right before startup of initial process, all state changes to the
	// current process is prohibited during late
	for i, op := range *param.Ops {
		// ops already checked during early setup
		if err := op.late(state, k); err != nil {
			if m, ok := messageFromError(err); ok {
				k.fatal(msg, m)
			} else if errors.Is(err, context.DeadlineExceeded) {
				k.fatalf(msg, "%s deadline exceeded", op.String())
			} else {
				k.fatalf(msg, "cannot complete op at index %d: %v", i, err)
			}
		}
	}
	// early reaping has concluded, this must happen before initial process is created
	state.processMu.Lock()
	state.process = nil
	state.processMu.Unlock()

	if err := closeSetup(); err != nil {
		k.fatalf(msg, "cannot close setup pipe: %v", err)
	}

	cmd := exec.Command(param.Path.String())
	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
	cmd.Args = param.Args
	cmd.Env = param.Env
	cmd.ExtraFiles = extraFiles
	cmd.Dir = param.Dir.String()

	if param.InitAsRoot {
		cmd.SysProcAttr = &SysProcAttr{
			Cloneflags:  CLONE_NEWUSER,
			UidMappings: []SysProcIDMap{{ContainerID: param.Uid, HostID: 0, Size: 1}},
			GidMappings: []SysProcIDMap{{ContainerID: param.Gid, HostID: 0, Size: 1}},
		}
	}

	msg.Verbosef("starting initial process %s", param.Path)
	if err := k.start(cmd); err != nil {
		k.fatalf(msg, "%v", err)
	}
	initialProcessStarted.Store(true)

	// handle signals to dump withheld messages
	sig := make(chan os.Signal, 2)
	k.notify(sig, CancelSignal,
		os.Interrupt, SIGTERM, SIGQUIT)

	// closed after residualProcessTimeout has elapsed after initial process death
	timeout := make(chan struct{})

	r := exitUnexpectedWait4
	for {
		select {
		case s := <-sig:
			if s == CancelSignal && param.ForwardCancel && cmd.Process != nil {
				msg.Verbose("forwarding context cancellation")
				if err := k.signal(cmd, os.Interrupt); err != nil && !errors.Is(err, os.ErrProcessDone) {
					k.printf(msg, "cannot forward cancellation: %v", err)
				}
				continue
			}

			if s == SIGTERM || s == SIGQUIT {
				msg.Verbosef("got %s, forwarding to initial process", s.String())
				if err := k.signal(cmd, s); err != nil {
					k.printf(msg, "cannot forward signal: %v", err)
				}
				continue
			}

			msg.Verbosef("got %s", s.String())
			msg.BeforeExit()
			k.exit(0)

		case w, ok := <-info:
			if !ok {
				msg.BeforeExit()
				k.exit(r)
				continue // unreachable
			}

			if w.wpid == cmd.Process.Pid {
				// cancel Op context early
				cancel()

				// start timeout early
				go func() { time.Sleep(param.AdoptWaitDelay); close(timeout) }()

				// close initial process files; this also keeps them alive
				for _, f := range extraFiles {
					if err := f.Close(); err != nil {
						msg.Verbose(err.Error())
					}
				}

				switch {
				case w.wstatus.Exited():
					r = w.wstatus.ExitStatus()
					msg.Verbosef("initial process exited with code %d", w.wstatus.ExitStatus())

				case w.wstatus.Signaled():
					r = 128 + int(w.wstatus.Signal())
					msg.Verbosef("initial process exited with signal %s", w.wstatus.Signal())

				default:
					r = 255
					msg.Verbosef("initial process exited with status %#x", w.wstatus)
				}
			}

		case <-timeout:
			k.printf(msg, "timeout exceeded waiting for lingering processes")
			msg.BeforeExit()
			k.exit(r)
		}
	}
}

// initName is the prefix used by log.std in the init process.
const initName = "init"

var _ = func() struct{} {
	for _, v := range []any{
		(*AutoEtcOp)(nil),
		(*AutoRootOp)(nil),
		(*BindMountOp)(nil),
		(*DaemonOp)(nil),
		(*MkdirOp)(nil),
		(*MountDevOp)(nil),
		(*MountOverlayOp)(nil),
		(*MountProcOp)(nil),
		(*MountTmpfsOp)(nil),
		(*RemountOp)(nil),
		(*SymlinkOp)(nil),
		(*TmpfileOp)(nil),
	} {
		gob.Register(v)
	}

	if len(os.Args) == 1 && filepath.Base(os.Args[0]) == initName {
		log.SetPrefix(initName + ": ")
		log.SetFlags(0)
		msg := message.New(log.Default())

		Init(msg)
		msg.BeforeExit()
		os.Exit(0)
	}
	return struct{}{}
}()

// TryArgv0 is a noop.
//
// Deprecated: init is now implemented as an import side effect.
func TryArgv0(_ message.Msg) {}
