Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace syscall with unix/windows packages where possible #3032

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"path/filepath"
"strconv"
"strings"
"syscall"
"unicode"

"github.com/AlecAivazis/survey/v2"
"github.com/pbnjay/memory"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/metabase"
Expand Down Expand Up @@ -457,7 +457,7 @@

if stat, err := os.Stat(cfg.DbConfig.DbPath); !os.IsNotExist(err) {
info := stat.Sys()
if err := os.Chown(cfg.DbConfig.DbPath, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
if err := os.Chown(cfg.DbConfig.DbPath, int(info.(*unix.Stat_t).Uid), intID); err != nil {

Check warning on line 460 in cmd/crowdsec-cli/dashboard.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L460

Added line #L460 was not covered by tests
return fmt.Errorf("unable to chown sqlite db file '%s': %s", cfg.DbConfig.DbPath, err)
}
}
Expand All @@ -467,7 +467,7 @@
file := cfg.DbConfig.DbPath + ext
if stat, err := os.Stat(file); !os.IsNotExist(err) {
info := stat.Sys()
if err := os.Chown(file, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
if err := os.Chown(file, int(info.(*unix.Stat_t).Uid), intID); err != nil {

Check warning on line 470 in cmd/crowdsec-cli/dashboard.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L470

Added line #L470 was not covered by tests
return fmt.Errorf("unable to chown sqlite db file '%s': %s", file, err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/crowdsec/win_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import (
"fmt"
"syscall"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -67,7 +66,7 @@
// All the calls to logging before the logger is configured are pretty much useless, but we keep them for clarity
err := eventlog.InstallAsEventCreate("CrowdSec", eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
if errno, ok := err.(syscall.Errno); ok {
if errno, ok := err.(windows.Errno); ok {

Check warning on line 69 in cmd/crowdsec/win_service.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/win_service.go#L69

Added line #L69 was not covered by tests
if errno == windows.ERROR_ACCESS_DENIED {
log.Warnf("Access denied when installing event source, running as non-admin ?")
} else {
Expand Down
5 changes: 2 additions & 3 deletions pkg/acquisition/modules/wineventlog/wineventlog_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"runtime"
"strings"
"syscall"
"time"

"github.com/google/winops/winlog"
Expand Down Expand Up @@ -180,7 +179,7 @@ func (w *WinEventLogSource) getEvents(out chan types.Event, t *tomb.Tomb) error
w.logger.Errorf("WaitForSingleObject failed: %s", err)
return err
}
if status == syscall.WAIT_OBJECT_0 {
if status == windows.WAIT_OBJECT_0 {
renderedEvents, err := w.getXMLEvents(w.evtConfig, publisherCache, subscription, 500)
if err == windows.ERROR_NO_MORE_ITEMS {
windows.ResetEvent(w.evtConfig.SignalEvent)
Expand Down Expand Up @@ -225,7 +224,7 @@ func (w *WinEventLogSource) generateConfig(query string) (*winlog.SubscribeConfi
return &config, fmt.Errorf("windows.CreateEvent failed: %v", err)
}
config.Flags = wevtapi.EvtSubscribeToFutureEvents
config.Query, err = syscall.UTF16PtrFromString(query)
config.Query, err = windows.UTF16PtrFromString(query)
if err != nil {
return &config, fmt.Errorf("syscall.UTF16PtrFromString failed: %v", err)
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/csplugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
"strconv"
"strings"
"syscall"

"golang.org/x/sys/unix"
)

func CheckCredential(uid int, gid int) *syscall.SysProcAttr {
return &syscall.SysProcAttr{
func CheckCredential(uid int, gid int) *unix.SysProcAttr {
return &unix.SysProcAttr{

Check warning on line 22 in pkg/csplugin/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/csplugin/utils.go#L21-L22

Added lines #L21 - L22 were not covered by tests
Credential: &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
Expand Down Expand Up @@ -80,7 +82,7 @@
return strings.Join(parts[:len(parts)-1], "-"), parts[len(parts)-1], nil
}

func getProcessAttr(username string, groupname string) (*syscall.SysProcAttr, error) {
func getProcessAttr(username string, groupname string) (*unix.SysProcAttr, error) {
uid, err := getUID(username)
if err != nil {
return nil, err
Expand All @@ -90,7 +92,7 @@
return nil, err
}

return &syscall.SysProcAttr{
return &unix.SysProcAttr{
Credential: &syscall.Credential{
Uid: uid,
Gid: gid,
Expand Down
6 changes: 3 additions & 3 deletions pkg/csplugin/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

var (
advapi32 = syscall.NewLazyDLL("advapi32.dll")
advapi32 = windows.NewLazyDLL("advapi32.dll")

procGetAce = advapi32.NewProc("GetAce")
)
Expand Down Expand Up @@ -149,7 +149,7 @@ func CheckPerms(path string) error {
return nil
}

func getProcessAtr() (*syscall.SysProcAttr, error) {
func getProcessAtr() (*windows.SysProcAttr, error) {
var procToken, token windows.Token

proc := windows.CurrentProcess()
Expand Down Expand Up @@ -195,7 +195,7 @@ func getProcessAtr() (*syscall.SysProcAttr, error) {
}

return &windows.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
CreationFlags: windows.CREATE_NEW_PROCESS_GROUP,
Token: syscall.Token(token),
}, nil
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/types/getfstype_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
package types

import (
"fmt"
"syscall"
"fmt"

"golang.org/x/sys/unix"
)

func GetFSType(path string) (string, error) {
var fsStat syscall.Statfs_t
var fsStat unix.Statfs_t

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

Expand Down
12 changes: 7 additions & 5 deletions pkg/types/getfstype_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
"path/filepath"
"syscall"
"unsafe"

"golang.org/x/sys/windows"
)

func GetFSType(path string) (string, error) {
kernel32, err := syscall.LoadLibrary("kernel32.dll")
kernel32, err := windows.LoadLibrary("kernel32.dll")

Check warning on line 12 in pkg/types/getfstype_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/getfstype_windows.go#L12

Added line #L12 was not covered by tests
if err != nil {
return "", err
}
defer syscall.FreeLibrary(kernel32)
defer windows.FreeLibrary(kernel32)

Check warning on line 16 in pkg/types/getfstype_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/getfstype_windows.go#L16

Added line #L16 was not covered by tests

getVolumeInformation, err := syscall.GetProcAddress(kernel32, "GetVolumeInformationW")
getVolumeInformation, err := windows.GetProcAddress(kernel32, "GetVolumeInformationW")

Check warning on line 18 in pkg/types/getfstype_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/getfstype_windows.go#L18

Added line #L18 was not covered by tests
if err != nil {
return "", err
}
Expand All @@ -27,7 +29,7 @@
// Get the root path of the volume
volumeRoot := filepath.VolumeName(absPath) + "\\"

volumeRootPtr, _ := syscall.UTF16PtrFromString(volumeRoot)
volumeRootPtr, _ := windows.UTF16PtrFromString(volumeRoot)

Check warning on line 32 in pkg/types/getfstype_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/getfstype_windows.go#L32

Added line #L32 was not covered by tests

var (
fileSystemNameBuffer = make([]uint16, 260)
Expand All @@ -49,5 +51,5 @@
return "", err
}

return syscall.UTF16ToString(fileSystemNameBuffer), nil
return windows.UTF16ToString(fileSystemNameBuffer), nil

Check warning on line 54 in pkg/types/getfstype_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/getfstype_windows.go#L54

Added line #L54 was not covered by tests
}