Skip to content

Commit d9d0f56

Browse files
authored
replace syscall with unix/windows packages where possible (#3032)
* replace syscall with unix/windows 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. * interface conversion: interface {} is *syscall.Stat_t, not *unix.Stat_t * update lint config
1 parent 4e0cc46 commit d9d0f56

9 files changed

Lines changed: 31 additions & 28 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ linters:
143143
# See https://github.com/kisielk/errcheck#excluding-functions for details.
144144
exclude-functions:
145145
- (*bytes.Buffer).ReadFrom # TODO
146-
- syscall.FreeLibrary
146+
- golang.org/x/sys/windows.FreeLibrary
147147
- golang.org/x/sys/windows.CloseHandle
148148
- golang.org/x/sys/windows.ResetEvent
149149
- (*golang.org/x/sys/windows/svc/eventlog.Log).Info

cmd/crowdsec/win_service.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package main
99
import (
1010
"context"
1111
"fmt"
12-
"syscall"
1312
"time"
1413

1514
log "github.com/sirupsen/logrus"
@@ -75,7 +74,7 @@ func runService(name string, sd *StateDumper) error {
7574
// All the calls to logging before the logger is configured are pretty much useless, but we keep them for clarity
7675
err := eventlog.InstallAsEventCreate("CrowdSec", eventlog.Error|eventlog.Warning|eventlog.Info)
7776
if err != nil {
78-
if errno, ok := err.(syscall.Errno); ok { //nolint:errorlint
77+
if errno, ok := err.(windows.Errno); ok { //nolint:errorlint
7978
if errno == windows.ERROR_ACCESS_DENIED {
8079
log.Warnf("Access denied when installing event source, running as non-admin?")
8180
} else {

pkg/acquisition/modules/wineventlog/config_windows.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/url"
99
"strconv"
1010
"strings"
11-
"syscall"
1211

1312
yaml "github.com/goccy/go-yaml"
1413
"github.com/google/winops/winlog"
@@ -117,15 +116,15 @@ func (s *Source) generateConfig(query string, live bool) (*winlog.SubscribeConfi
117116
}
118117
config.Flags = wevtapi.EvtSubscribeToFutureEvents
119118
} else {
120-
config.ChannelPath, err = syscall.UTF16PtrFromString(s.config.EventFile)
119+
config.ChannelPath, err = windows.UTF16PtrFromString(s.config.EventFile)
121120
if err != nil {
122-
return &config, fmt.Errorf("syscall.UTF16PtrFromString failed: %v", err)
121+
return &config, fmt.Errorf("windows.UTF16PtrFromString failed: %v", err)
123122
}
124123
config.Flags = wevtapi.EvtQueryFilePath | wevtapi.EvtQueryForwardDirection
125124
}
126-
config.Query, err = syscall.UTF16PtrFromString(query)
125+
config.Query, err = windows.UTF16PtrFromString(query)
127126
if err != nil {
128-
return &config, fmt.Errorf("syscall.UTF16PtrFromString failed: %v", err)
127+
return &config, fmt.Errorf("windows.UTF16PtrFromString failed: %v", err)
129128
}
130129

131130
return &config, nil

pkg/acquisition/modules/wineventlog/run_windows.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"syscall"
87
"time"
98

109
"github.com/google/winops/winlog"
@@ -88,7 +87,7 @@ func (s *Source) getEvents(out chan pipeline.Event, t *tomb.Tomb) error {
8887
s.logger.Errorf("WaitForSingleObject failed: %s", err)
8988
return err
9089
}
91-
if status == syscall.WAIT_OBJECT_0 {
90+
if status == windows.WAIT_OBJECT_0 {
9291
renderedEvents, err := s.getXMLEvents(s.evtConfig, publisherCache, subscription, 500)
9392
if errors.Is(err, windows.ERROR_NO_MORE_ITEMS) {
9493
windows.ResetEvent(s.evtConfig.SignalEvent)

pkg/csplugin/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"strconv"
1616
"strings"
1717
"syscall"
18+
19+
"golang.org/x/sys/unix"
1820
)
1921

2022
func (pb *PluginBroker) CreateCmd(ctx context.Context, binaryPath string) (*exec.Cmd, error) {
@@ -72,7 +74,7 @@ func getPluginTypeAndSubtypeFromPath(path string) (string, string, error) {
7274
return strings.Join(parts[:len(parts)-1], "-"), parts[len(parts)-1], nil
7375
}
7476

75-
func getProcessAttr(username string, groupname string) (*syscall.SysProcAttr, error) {
77+
func getProcessAttr(username string, groupname string) (*unix.SysProcAttr, error) {
7678
uid, err := getUID(username)
7779
if err != nil {
7880
return nil, err
@@ -82,7 +84,7 @@ func getProcessAttr(username string, groupname string) (*syscall.SysProcAttr, er
8284
return nil, err
8385
}
8486

85-
return &syscall.SysProcAttr{
87+
return &unix.SysProcAttr{
8688
Credential: &syscall.Credential{
8789
Uid: uid,
8890
Gid: gid,

pkg/csplugin/utils_windows.go

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

2222
var (
23-
advapi32 = syscall.NewLazyDLL("advapi32.dll")
23+
advapi32 = windows.NewLazyDLL("advapi32.dll")
2424
procGetAce = advapi32.NewProc("GetAce")
2525
)
2626

@@ -155,7 +155,7 @@ func CheckPerms(path string) error {
155155
return nil
156156
}
157157

158-
func getProcessAttr() (*syscall.SysProcAttr, error) {
158+
func getProcessAttr() (*windows.SysProcAttr, error) {
159159
var procToken, token windows.Token
160160

161161
proc := windows.CurrentProcess()
@@ -201,7 +201,7 @@ func getProcessAttr() (*syscall.SysProcAttr, error) {
201201
}
202202

203203
return &windows.SysProcAttr{
204-
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
204+
CreationFlags: windows.CREATE_NEW_PROCESS_GROUP,
205205
Token: syscall.Token(token),
206206
}, nil
207207
}

pkg/fsutil/getfstype_freebsd.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
package fsutil
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/fsutil/getfstype_openbsd.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
package fsutil
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/fsutil/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)