Skip to content

Commit 73d5a61

Browse files
committed
Gather all syscall use in the osutil helper
Makes it easier to add windows portability Signed-off-by: Anders F Björklund <[email protected]>
1 parent 6410674 commit 73d5a61

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

cmd/limactl/stop.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10-
"syscall"
1110
"time"
1211

1312
hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events"
1413
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
14+
"github.com/lima-vm/lima/pkg/osutil"
1515
"github.com/lima-vm/lima/pkg/store"
1616
"github.com/lima-vm/lima/pkg/store/filenames"
1717
"github.com/sirupsen/logrus"
@@ -65,7 +65,7 @@ func stopInstanceGracefully(inst *store.Instance) error {
6565

6666
begin := time.Now() // used for logrus propagation
6767
logrus.Infof("Sending SIGINT to hostagent process %d", inst.HostAgentPID)
68-
if err := syscall.Kill(inst.HostAgentPID, syscall.SIGINT); err != nil {
68+
if err := osutil.SysKill(inst.HostAgentPID, osutil.SigInt); err != nil {
6969
logrus.Error(err)
7070
}
7171

@@ -106,7 +106,7 @@ func waitForHostAgentTermination(ctx context.Context, inst *store.Instance, begi
106106
func stopInstanceForcibly(inst *store.Instance) {
107107
if inst.QemuPID > 0 {
108108
logrus.Infof("Sending SIGKILL to the QEMU process %d", inst.QemuPID)
109-
if err := syscall.Kill(inst.QemuPID, syscall.SIGKILL); err != nil {
109+
if err := osutil.SysKill(inst.QemuPID, osutil.SigKill); err != nil {
110110
logrus.Error(err)
111111
}
112112
} else {
@@ -115,7 +115,7 @@ func stopInstanceForcibly(inst *store.Instance) {
115115

116116
if inst.HostAgentPID > 0 {
117117
logrus.Infof("Sending SIGKILL to the host agent process %d", inst.HostAgentPID)
118-
if err := syscall.Kill(inst.HostAgentPID, syscall.SIGKILL); err != nil {
118+
if err := osutil.SysKill(inst.HostAgentPID, osutil.SigKill); err != nil {
119119
logrus.Error(err)
120120
}
121121
} else {

pkg/networks/reconcile/reconcile.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"runtime"
1010
"strings"
1111
"sync"
12-
"syscall"
1312
"time"
1413

1514
"github.com/lima-vm/lima/pkg/networks"
@@ -94,7 +93,7 @@ func makeVarRun(config *networks.NetworksConfig) error {
9493
if err != nil {
9594
return err
9695
}
97-
stat, ok := fi.Sys().(*syscall.Stat_t)
96+
stat, ok := osutil.SysStat(fi)
9897
if !ok {
9998
// should never happen
10099
return fmt.Errorf("could not retrieve stat buffer for %q", config.Paths.VarRun)

pkg/networks/validate.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"path/filepath"
99
"reflect"
1010
"strings"
11-
"syscall"
1211

1312
"github.com/lima-vm/lima/pkg/osutil"
1413
)
@@ -73,7 +72,7 @@ func validatePath(path string, allowDaemonGroupWritable bool) error {
7372
if (fi.Mode() & fs.ModeSymlink) != 0 {
7473
return fmt.Errorf("%s %q is a symlink", file, path)
7574
}
76-
stat, ok := fi.Sys().(*syscall.Stat_t)
75+
stat, ok := osutil.SysStat(fi)
7776
if !ok {
7877
// should never happen
7978
return fmt.Errorf("could not retrieve stat buffer for %q", path)

pkg/osutil/osutil_linux.go

+28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
package osutil
22

3+
import (
4+
"io/fs"
5+
"syscall"
6+
)
7+
38
// UnixPathMax is the value of UNIX_PATH_MAX.
49
const UnixPathMax = 108
10+
11+
// Stat is a selection of syscall.Stat_t
12+
type Stat struct {
13+
Uid uint32
14+
Gid uint32
15+
}
16+
17+
func SysStat(fi fs.FileInfo) (Stat, bool) {
18+
stat, ok := fi.Sys().(*syscall.Stat_t)
19+
return Stat{Uid: stat.Uid, Gid: stat.Gid}, ok
20+
}
21+
22+
// SigInt is the value of SIGINT.
23+
const SigInt = Signal(syscall.SIGINT)
24+
25+
// SigKill is the value of SIGKILL.
26+
const SigKill = Signal(syscall.SIGKILL)
27+
28+
type Signal syscall.Signal
29+
30+
func SysKill(pid int, sig Signal) error {
31+
return syscall.Kill(pid, syscall.Signal(sig))
32+
}

pkg/osutil/osutil_others.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1-
//go:build !linux
2-
// +build !linux
1+
//go:build !linux && !windows
2+
// +build !linux,!windows
33

44
package osutil
55

6+
import (
7+
"io/fs"
8+
"syscall"
9+
)
10+
611
// UnixPathMax is the value of UNIX_PATH_MAX.
712
const UnixPathMax = 104
13+
14+
// Stat is a selection of syscall.Stat_t
15+
type Stat struct {
16+
Uid uint32
17+
Gid uint32
18+
}
19+
20+
func SysStat(fi fs.FileInfo) (Stat, bool) {
21+
stat, ok := fi.Sys().(*syscall.Stat_t)
22+
return Stat{Uid: stat.Uid, Gid: stat.Gid}, ok
23+
}
24+
25+
// SigInt is the value of SIGINT.
26+
const SigInt = Signal(syscall.SIGINT)
27+
28+
// SigKill is the value of SIGKILL.
29+
const SigKill = Signal(syscall.SIGKILL)
30+
31+
type Signal syscall.Signal
32+
33+
func SysKill(pid int, sig Signal) error {
34+
return syscall.Kill(pid, syscall.Signal(sig))
35+
}

0 commit comments

Comments
 (0)