Skip to content

Commit 80843db

Browse files
committed
replace syscall with unix/windows packages where possible
from the 1.22 release notes: The syscall package has been frozen since Go 1.4 and was marked as deprecated in Go 1.11 [...] However, some non-deprecated functionality requires use of the syscall package [...] To avoid unnecessary complaints[...] no longer marked as deprecated. The package remains frozen to most new functionality, and new code remains encouraged to use golang.org/x/sys/unix or golang.org/x/sys/windows where possible.
1 parent 3532e87 commit 80843db

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

cmd/crowdsec-cli/dashboard.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"path/filepath"
1313
"strconv"
1414
"strings"
15-
"syscall"
1615
"unicode"
1716

1817
"github.com/AlecAivazis/survey/v2"
1918
"github.com/pbnjay/memory"
2019
log "github.com/sirupsen/logrus"
2120
"github.com/spf13/cobra"
21+
"golang.org/x/sys/unix"
2222

2323
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
2424
"github.com/crowdsecurity/crowdsec/pkg/metabase"
@@ -457,7 +457,7 @@ func (cli *cliDashboard) chownDatabase(gid string) error {
457457

458458
if stat, err := os.Stat(cfg.DbConfig.DbPath); !os.IsNotExist(err) {
459459
info := stat.Sys()
460-
if err := os.Chown(cfg.DbConfig.DbPath, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
460+
if err := os.Chown(cfg.DbConfig.DbPath, int(info.(*unix.Stat_t).Uid), intID); err != nil {
461461
return fmt.Errorf("unable to chown sqlite db file '%s': %s", cfg.DbConfig.DbPath, err)
462462
}
463463
}
@@ -467,7 +467,7 @@ func (cli *cliDashboard) chownDatabase(gid string) error {
467467
file := cfg.DbConfig.DbPath + ext
468468
if stat, err := os.Stat(file); !os.IsNotExist(err) {
469469
info := stat.Sys()
470-
if err := os.Chown(file, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
470+
if err := os.Chown(file, int(info.(*unix.Stat_t).Uid), intID); err != nil {
471471
return fmt.Errorf("unable to chown sqlite db file '%s': %s", file, err)
472472
}
473473
}

cmd/crowdsec/win_service.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package main
88

99
import (
1010
"fmt"
11-
"syscall"
1211
"time"
1312

1413
log "github.com/sirupsen/logrus"
@@ -67,7 +66,7 @@ func runService(name string) error {
6766
// All the calls to logging before the logger is configured are pretty much useless, but we keep them for clarity
6867
err := eventlog.InstallAsEventCreate("CrowdSec", eventlog.Error|eventlog.Warning|eventlog.Info)
6968
if err != nil {
70-
if errno, ok := err.(syscall.Errno); ok {
69+
if errno, ok := err.(windows.Errno); ok {
7170
if errno == windows.ERROR_ACCESS_DENIED {
7271
log.Warnf("Access denied when installing event source, running as non-admin ?")
7372
} else {

pkg/acquisition/modules/wineventlog/wineventlog_windows.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"runtime"
88
"strings"
9-
"syscall"
109
"time"
1110

1211
"github.com/google/winops/winlog"
@@ -180,7 +179,7 @@ func (w *WinEventLogSource) getEvents(out chan types.Event, t *tomb.Tomb) error
180179
w.logger.Errorf("WaitForSingleObject failed: %s", err)
181180
return err
182181
}
183-
if status == syscall.WAIT_OBJECT_0 {
182+
if status == windows.WAIT_OBJECT_0 {
184183
renderedEvents, err := w.getXMLEvents(w.evtConfig, publisherCache, subscription, 500)
185184
if err == windows.ERROR_NO_MORE_ITEMS {
186185
windows.ResetEvent(w.evtConfig.SignalEvent)
@@ -225,7 +224,7 @@ func (w *WinEventLogSource) generateConfig(query string) (*winlog.SubscribeConfi
225224
return &config, fmt.Errorf("windows.CreateEvent failed: %v", err)
226225
}
227226
config.Flags = wevtapi.EvtSubscribeToFutureEvents
228-
config.Query, err = syscall.UTF16PtrFromString(query)
227+
config.Query, err = windows.UTF16PtrFromString(query)
229228
if err != nil {
230229
return &config, fmt.Errorf("syscall.UTF16PtrFromString failed: %v", err)
231230
}

pkg/csplugin/utils.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
"strconv"
1515
"strings"
1616
"syscall"
17+
18+
"golang.org/x/sys/unix"
1719
)
1820

19-
func CheckCredential(uid int, gid int) *syscall.SysProcAttr {
20-
return &syscall.SysProcAttr{
21+
func CheckCredential(uid int, gid int) *unix.SysProcAttr {
22+
return &unix.SysProcAttr{
2123
Credential: &syscall.Credential{
2224
Uid: uint32(uid),
2325
Gid: uint32(gid),
@@ -80,7 +82,7 @@ func getPluginTypeAndSubtypeFromPath(path string) (string, string, error) {
8082
return strings.Join(parts[:len(parts)-1], "-"), parts[len(parts)-1], nil
8183
}
8284

83-
func getProcessAttr(username string, groupname string) (*syscall.SysProcAttr, error) {
85+
func getProcessAttr(username string, groupname string) (*unix.SysProcAttr, error) {
8486
uid, err := getUID(username)
8587
if err != nil {
8688
return nil, err
@@ -90,7 +92,7 @@ func getProcessAttr(username string, groupname string) (*syscall.SysProcAttr, er
9092
return nil, err
9193
}
9294

93-
return &syscall.SysProcAttr{
95+
return &unix.SysProcAttr{
9496
Credential: &syscall.Credential{
9597
Uid: uid,
9698
Gid: gid,
@@ -116,7 +118,7 @@ func pluginIsValid(path string) error {
116118
if err != nil {
117119
return fmt.Errorf("while looking up the current uid: %w", err)
118120
}
119-
stat := details.Sys().(*syscall.Stat_t)
121+
stat := details.Sys().(*unix.Stat_t)
120122
if stat.Uid != currentUID {
121123
return fmt.Errorf("plugin at %s is not owned by user '%s'", path, currentUser.Username)
122124
}

pkg/csplugin/utils_windows.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
var (
22-
advapi32 = syscall.NewLazyDLL("advapi32.dll")
22+
advapi32 = windows.NewLazyDLL("advapi32.dll")
2323

2424
procGetAce = advapi32.NewProc("GetAce")
2525
)
@@ -149,7 +149,7 @@ func CheckPerms(path string) error {
149149
return nil
150150
}
151151

152-
func getProcessAtr() (*syscall.SysProcAttr, error) {
152+
func getProcessAtr() (*windows.SysProcAttr, error) {
153153
var procToken, token windows.Token
154154

155155
proc := windows.CurrentProcess()
@@ -195,7 +195,7 @@ func getProcessAtr() (*syscall.SysProcAttr, error) {
195195
}
196196

197197
return &windows.SysProcAttr{
198-
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
198+
CreationFlags: windows.CREATE_NEW_PROCESS_GROUP,
199199
Token: syscall.Token(token),
200200
}, nil
201201
}

pkg/types/getfstype_freebsd.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
package types
44

55
import (
6-
"fmt"
7-
"syscall"
6+
"fmt"
7+
8+
"golang.org/x/sys/unix"
89
)
910

1011
func GetFSType(path string) (string, error) {
11-
var fsStat syscall.Statfs_t
12+
var fsStat unix.Statfs_t
1213

13-
if err := syscall.Statfs(path, &fsStat); err != nil {
14+
if err := unix.Statfs(path, &fsStat); err != nil {
1415
return "", fmt.Errorf("failed to get filesystem type: %w", err)
1516
}
1617

pkg/types/getfstype_windows.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import (
44
"path/filepath"
55
"syscall"
66
"unsafe"
7+
8+
"golang.org/x/sys/windows"
79
)
810

911
func GetFSType(path string) (string, error) {
10-
kernel32, err := syscall.LoadLibrary("kernel32.dll")
12+
kernel32, err := windows.LoadLibrary("kernel32.dll")
1113
if err != nil {
1214
return "", err
1315
}
14-
defer syscall.FreeLibrary(kernel32)
16+
defer windows.FreeLibrary(kernel32)
1517

16-
getVolumeInformation, err := syscall.GetProcAddress(kernel32, "GetVolumeInformationW")
18+
getVolumeInformation, err := windows.GetProcAddress(kernel32, "GetVolumeInformationW")
1719
if err != nil {
1820
return "", err
1921
}
@@ -27,7 +29,7 @@ func GetFSType(path string) (string, error) {
2729
// Get the root path of the volume
2830
volumeRoot := filepath.VolumeName(absPath) + "\\"
2931

30-
volumeRootPtr, _ := syscall.UTF16PtrFromString(volumeRoot)
32+
volumeRootPtr, _ := windows.UTF16PtrFromString(volumeRoot)
3133

3234
var (
3335
fileSystemNameBuffer = make([]uint16, 260)
@@ -49,5 +51,5 @@ func GetFSType(path string) (string, error) {
4951
return "", err
5052
}
5153

52-
return syscall.UTF16ToString(fileSystemNameBuffer), nil
54+
return windows.UTF16ToString(fileSystemNameBuffer), nil
5355
}

0 commit comments

Comments
 (0)