diff --git a/cmd/limactl/stop.go b/cmd/limactl/stop.go index 8faadf47b2cd..bf5ecd1480b9 100644 --- a/cmd/limactl/stop.go +++ b/cmd/limactl/stop.go @@ -7,11 +7,11 @@ import ( "os" "path/filepath" "strings" - "syscall" "time" hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events" networks "github.com/lima-vm/lima/pkg/networks/reconcile" + "github.com/lima-vm/lima/pkg/osutil" "github.com/lima-vm/lima/pkg/store" "github.com/lima-vm/lima/pkg/store/filenames" "github.com/sirupsen/logrus" @@ -65,7 +65,7 @@ func stopInstanceGracefully(inst *store.Instance) error { begin := time.Now() // used for logrus propagation logrus.Infof("Sending SIGINT to hostagent process %d", inst.HostAgentPID) - if err := syscall.Kill(inst.HostAgentPID, syscall.SIGINT); err != nil { + if err := osutil.SysKill(inst.HostAgentPID, osutil.SigInt); err != nil { logrus.Error(err) } @@ -106,7 +106,7 @@ func waitForHostAgentTermination(ctx context.Context, inst *store.Instance, begi func stopInstanceForcibly(inst *store.Instance) { if inst.QemuPID > 0 { logrus.Infof("Sending SIGKILL to the QEMU process %d", inst.QemuPID) - if err := syscall.Kill(inst.QemuPID, syscall.SIGKILL); err != nil { + if err := osutil.SysKill(inst.QemuPID, osutil.SigKill); err != nil { logrus.Error(err) } } else { @@ -115,7 +115,7 @@ func stopInstanceForcibly(inst *store.Instance) { if inst.HostAgentPID > 0 { logrus.Infof("Sending SIGKILL to the host agent process %d", inst.HostAgentPID) - if err := syscall.Kill(inst.HostAgentPID, syscall.SIGKILL); err != nil { + if err := osutil.SysKill(inst.HostAgentPID, osutil.SigKill); err != nil { logrus.Error(err) } } else { diff --git a/pkg/networks/reconcile/reconcile.go b/pkg/networks/reconcile/reconcile.go index 6c6218f50a54..e4aa983ca6d3 100644 --- a/pkg/networks/reconcile/reconcile.go +++ b/pkg/networks/reconcile/reconcile.go @@ -9,7 +9,6 @@ import ( "runtime" "strings" "sync" - "syscall" "time" "github.com/lima-vm/lima/pkg/networks" @@ -94,7 +93,7 @@ func makeVarRun(config *networks.NetworksConfig) error { if err != nil { return err } - stat, ok := fi.Sys().(*syscall.Stat_t) + stat, ok := osutil.SysStat(fi) if !ok { // should never happen return fmt.Errorf("could not retrieve stat buffer for %q", config.Paths.VarRun) diff --git a/pkg/networks/validate.go b/pkg/networks/validate.go index 94dc613bc9e5..0d77371f8336 100644 --- a/pkg/networks/validate.go +++ b/pkg/networks/validate.go @@ -8,7 +8,6 @@ import ( "path/filepath" "reflect" "strings" - "syscall" "github.com/lima-vm/lima/pkg/osutil" ) @@ -73,7 +72,7 @@ func validatePath(path string, allowDaemonGroupWritable bool) error { if (fi.Mode() & fs.ModeSymlink) != 0 { return fmt.Errorf("%s %q is a symlink", file, path) } - stat, ok := fi.Sys().(*syscall.Stat_t) + stat, ok := osutil.SysStat(fi) if !ok { // should never happen return fmt.Errorf("could not retrieve stat buffer for %q", path) diff --git a/pkg/osutil/osutil_linux.go b/pkg/osutil/osutil_linux.go index 5ceaf43b54b5..817ef663bd54 100644 --- a/pkg/osutil/osutil_linux.go +++ b/pkg/osutil/osutil_linux.go @@ -1,4 +1,32 @@ package osutil +import ( + "io/fs" + "syscall" +) + // UnixPathMax is the value of UNIX_PATH_MAX. const UnixPathMax = 108 + +// Stat is a selection of syscall.Stat_t +type Stat struct { + Uid uint32 + Gid uint32 +} + +func SysStat(fi fs.FileInfo) (Stat, bool) { + stat, ok := fi.Sys().(*syscall.Stat_t) + return Stat{Uid: stat.Uid, Gid: stat.Gid}, ok +} + +// SigInt is the value of SIGINT. +const SigInt = Signal(syscall.SIGINT) + +// SigKill is the value of SIGKILL. +const SigKill = Signal(syscall.SIGKILL) + +type Signal syscall.Signal + +func SysKill(pid int, sig Signal) error { + return syscall.Kill(pid, syscall.Signal(sig)) +} diff --git a/pkg/osutil/osutil_others.go b/pkg/osutil/osutil_others.go index ec5eda734aa0..d61e2d7cd729 100644 --- a/pkg/osutil/osutil_others.go +++ b/pkg/osutil/osutil_others.go @@ -1,7 +1,35 @@ -//go:build !linux -// +build !linux +//go:build !linux && !windows +// +build !linux,!windows package osutil +import ( + "io/fs" + "syscall" +) + // UnixPathMax is the value of UNIX_PATH_MAX. const UnixPathMax = 104 + +// Stat is a selection of syscall.Stat_t +type Stat struct { + Uid uint32 + Gid uint32 +} + +func SysStat(fi fs.FileInfo) (Stat, bool) { + stat, ok := fi.Sys().(*syscall.Stat_t) + return Stat{Uid: stat.Uid, Gid: stat.Gid}, ok +} + +// SigInt is the value of SIGINT. +const SigInt = Signal(syscall.SIGINT) + +// SigKill is the value of SIGKILL. +const SigKill = Signal(syscall.SIGKILL) + +type Signal syscall.Signal + +func SysKill(pid int, sig Signal) error { + return syscall.Kill(pid, syscall.Signal(sig)) +} diff --git a/pkg/osutil/osutil_windows.go b/pkg/osutil/osutil_windows.go new file mode 100644 index 000000000000..711e82d28c74 --- /dev/null +++ b/pkg/osutil/osutil_windows.go @@ -0,0 +1,31 @@ +package osutil + +import ( + "fmt" + "io/fs" +) + +// UnixPathMax is the value of UNIX_PATH_MAX. +const UnixPathMax = 108 + +// Stat is a selection of syscall.Stat_t +type Stat struct { + Uid uint32 + Gid uint32 +} + +func SysStat(fi fs.FileInfo) (Stat, bool) { + return Stat{Uid: 0, Gid: 0}, false +} + +// SigInt is the value of SIGINT. +const SigInt = Signal(2) + +// SigKill is the value of SIGKILL. +const SigKill = Signal(9) + +type Signal int + +func SysKill(pid int, sig Signal) error { + return fmt.Errorf("unimplemented") +}