Skip to content

Commit

Permalink
Merge pull request #5614 from oasisprotocol/kostko/feature/fix-window…
Browse files Browse the repository at this point in the history
…s-build

go: Make sure packages compile on Windows
  • Loading branch information
kostko authored Mar 30, 2024
2 parents f1753d4 + 658fc23 commit 4f80a62
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 40 deletions.
9 changes: 9 additions & 0 deletions .buildkite/code.pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ steps:
plugins:
<<: *docker_plugin

- label: Test Go build for non-Linux platforms
command:
- GOOS=windows make -C go
- GOOS=darwin make -C go
retry:
<<: *retry_agent_failure
plugins:
<<: *docker_plugin

- label: Build Rust runtime loader
key: build-rust-runtime-loader
command:
Expand Down
1 change: 1 addition & 0 deletions .changelog/5614.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go: Make sure packages compile on Windows
3 changes: 3 additions & 0 deletions go/common/mkdir.go → go/common/mkdir_other.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package common

import (
Expand Down
33 changes: 33 additions & 0 deletions go/common/mkdir_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build windows
// +build windows

package common

import (
"fmt"
"os"
)

// Mkdir creates a directory iff it does not exist.
func Mkdir(d string) error {
const permDir = os.FileMode(0o700)

fi, err := os.Lstat(d)
if err != nil {
// Iff the directory does not exist, create it.
if os.IsNotExist(err) {
if err = os.MkdirAll(d, permDir); err == nil {
return nil
}
}
return err
}

// Ensure that the existing path is a directory.
fm := fi.Mode()
if !fm.IsDir() {
return fmt.Errorf("common/Mkdir: path '%s' is not a directory", d)
}

return nil
}
9 changes: 9 additions & 0 deletions go/common/syscall/syscall_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Package syscall defines OS-specific syscall parameters.
package syscall

import "syscall"

// CmdAttrs is the SysProcAttr used for spawning child processes. It is empty
// for Windows as PR_SET_PDEATH_SIG is not implemented. As a consequence, child
// processes may not be cleaned up.
var CmdAttrs = &syscall.SysProcAttr{}
27 changes: 0 additions & 27 deletions go/oasis-node/cmd/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"strings"
"syscall"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -210,32 +209,6 @@ func initDebugTCBLaxVerify() error {
return nil
}

func initRlimit() error {
// Suppress this for tooling, as it likely does not matter.
if !IsNodeCmd() {
return nil
}

var rlim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
return fmt.Errorf("failed to query RLIMIT_NOFILE: %w", err)
}

desiredLimit := config.GlobalConfig.Common.Debug.Rlimit
if flags.DebugDontBlameOasis() && desiredLimit > 0 && desiredLimit != rlim.Cur {
rlim.Cur = desiredLimit
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
return fmt.Errorf("failed setting RLIMIT_NOFILE: %w", err)
}
}

if rlim.Cur < RequiredRlimit {
return fmt.Errorf("too low RLIMIT_NOFILE, current: %d required: %d", rlim.Cur, RequiredRlimit)
}

return nil
}

// GetOutputWriter will create a file if the config string is set,
// and otherwise return os.Stdout.
func GetOutputWriter(cmd *cobra.Command, cfg string) (io.WriteCloser, bool, error) {
Expand Down
42 changes: 42 additions & 0 deletions go/oasis-node/cmd/common/common_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build !windows
// +build !windows

package common

import (
"fmt"
"syscall"

"github.com/oasisprotocol/oasis-core/go/config"
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
)

func initRlimit() error {
// Suppress this for tooling, as it likely does not matter.
if !IsNodeCmd() {
return nil
}

var rlim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
return fmt.Errorf("failed to query RLIMIT_NOFILE: %w", err)
}

desiredLimit := config.GlobalConfig.Common.Debug.Rlimit
if flags.DebugDontBlameOasis() && desiredLimit > 0 && desiredLimit != rlim.Cur {
rlim.Cur = desiredLimit
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
return fmt.Errorf("failed setting RLIMIT_NOFILE: %w", err)
}
}

if rlim.Cur < RequiredRlimit {
return fmt.Errorf("too low RLIMIT_NOFILE, current: %d required: %d", rlim.Cur, RequiredRlimit)
}

return nil
}

func Umask(mask int) int {
return syscall.Umask(mask)
}
12 changes: 12 additions & 0 deletions go/oasis-node/cmd/common/common_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build windows
// +build windows

package common

func initRlimit() error {
return nil
}

func Umask(int) int {
return 0
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package common

import (
Expand Down
9 changes: 9 additions & 0 deletions go/oasis-node/cmd/common/isatty_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build windows
// +build windows

package common

// Isatty returns true iff the provided file descriptor is a terminal.
func Isatty(fd uintptr) bool {
return false
}
3 changes: 1 addition & 2 deletions go/oasis-node/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd

import (
"os"
"syscall"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -42,7 +41,7 @@ func RootCommand() *cobra.Command {
func Execute() {
// Only the owner should have read/write/execute permissions for
// anything created by the oasis-node binary.
syscall.Umask(0o077)
cmdCommon.Umask(0o077)

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
11 changes: 0 additions & 11 deletions go/runtime/host/sandbox/process/naked.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ func (n *naked) Error() (err error) {
return
}

// Implements Process.
func (n *naked) Kill() {
_ = n.cmd.Process.Kill()
<-n.waitCh

// Some environments (lolDocker) do not have something that
// reaps zombie processes by default. Kill the process group
// as well.
_ = syscall.Kill(-n.cmd.Process.Pid, syscall.SIGKILL)
}

func (n *naked) wait() error {
err := n.cmd.Wait()
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions go/runtime/host/sandbox/process/naked_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !windows
// +build !windows

package process

import "syscall"

// Implements Process.
func (n *naked) Kill() {
_ = n.cmd.Process.Kill()
<-n.waitCh

// Some environments (lolDocker) do not have something that
// reaps zombie processes by default. Kill the process group
// as well.
_ = syscall.Kill(-n.cmd.Process.Pid, syscall.SIGKILL)
}
10 changes: 10 additions & 0 deletions go/runtime/host/sandbox/process/naked_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build windows
// +build windows

package process

// Implements Process.
func (n *naked) Kill() {
_ = n.cmd.Process.Kill()
<-n.waitCh
}

0 comments on commit 4f80a62

Please sign in to comment.