Skip to content

Commit

Permalink
Merge pull request #912 from afbjorklund/syscall-windows
Browse files Browse the repository at this point in the history
Gather all syscall use in the osutil helper
  • Loading branch information
AkihiroSuda authored Jun 20, 2022
2 parents 30c5594 + fa6eff1 commit e9ba19b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cmd/limactl/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/networks/reconcile/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"runtime"
"strings"
"sync"
"syscall"
"time"

"github.com/lima-vm/lima/pkg/networks"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pkg/networks/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"reflect"
"strings"
"syscall"

"github.com/lima-vm/lima/pkg/osutil"
)
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions pkg/osutil/osutil_linux.go
Original file line number Diff line number Diff line change
@@ -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))
}
32 changes: 30 additions & 2 deletions pkg/osutil/osutil_others.go
Original file line number Diff line number Diff line change
@@ -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))
}
31 changes: 31 additions & 0 deletions pkg/osutil/osutil_windows.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit e9ba19b

Please sign in to comment.