// Copyright (c) 2026 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

package cmdwrapper

import (
	"os"
	"os/exec"
	"syscall"
)

// setPdeathsig arranges for the child to be SIGKILL'd if the wrapper dies
// unexpectedly (e.g. OOM, SIGKILL of the wrapper itself). Without this, the
// child would be reparented to init and continue running — which for
// felix/kube-controllers means stale datastore connections that outlive
// the pod sandbox.
func setPdeathsig(cmd *exec.Cmd) {
	if cmd.SysProcAttr == nil {
		cmd.SysProcAttr = &syscall.SysProcAttr{}
	}
	cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL
}

// shouldIgnoreSignal returns true for signals that should not be forwarded
// to the wrapped child. SIGCHLD is emitted when the child exits; cmd.Wait
// reaps it, so forwarding would be meaningless (and racy).
func shouldIgnoreSignal(s os.Signal) bool {
	return s == syscall.SIGCHLD
}
