Skip to content
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
12 changes: 12 additions & 0 deletions addons/bootstrap/user-groups_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !linux

package bootstrap

import (
"fmt"
"runtime"
)

func addUserToGroup(_ *Context, userName, groupName string) error {
return fmt.Errorf("adding user %s to group %s is not supported on %s", userName, groupName, runtime.GOOS)
}
8 changes: 1 addition & 7 deletions addons/diags/file-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"io/fs"
"os"
"sync"
"syscall"
"time"

"fastcat.org/go/gdev/instance"
Expand Down Expand Up @@ -91,12 +90,7 @@ func fillTarHeader(th *tar.Header, contents io.Reader) (bool, error) {
th.Size = fi.Size()
th.ModTime = fi.ModTime()
th.Mode = int64(fi.Mode().Perm())
if fis, ok := fi.Sys().(syscall.Stat_t); ok {
// don't bother trying to look up user information
th.Uid, th.Gid = int(fis.Uid), int(fis.Gid)
th.AccessTime = time.Unix(fis.Atim.Unix())
th.ChangeTime = time.Unix(fis.Ctim.Unix())
}
fillPlatformStatFields(th, fi)
return true, nil
}

Expand Down
18 changes: 18 additions & 0 deletions addons/diags/stat-times_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package diags

import (
"archive/tar"
"io/fs"
"syscall"
"time"
)

func fillPlatformStatFields(th *tar.Header, fi fs.FileInfo) {
st, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return
}
th.Uid, th.Gid = int(st.Uid), int(st.Gid)
th.AccessTime = time.Unix(st.Atim.Unix())
th.ChangeTime = time.Unix(st.Ctim.Unix())
}
14 changes: 14 additions & 0 deletions addons/diags/stat-times_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !linux

package diags

import (
"archive/tar"
"io/fs"
)

func fillPlatformStatFields(_ *tar.Header, _ fs.FileInfo) {
// On non-Linux platforms, Uid, Gid, AccessTime, and ChangeTime
// are left at their zero values. Darwin uses different syscall.Stat_t
// field names (Atimespec/Ctimespec) and Windows has no syscall.Stat_t.
Comment on lines +11 to +13
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should at least be able to put together a reasonable implementation for Darwin? I don't know off the top of my head what the "Sys" stat structure is for Windows though

}
9 changes: 3 additions & 6 deletions addons/pm/server/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,7 @@ func (c *child) start(
for k, v := range e.Env {
cmd.Env = append(cmd.Env, k+"="+v)
}
// set pgid so we can kill process groups
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
setProcGroup(cmd)
// if logfile is not set, pass output to stdout/stderr and let journalctl
// capture it. note that this only works if we're using systemd for isolation.
if e.Logfile == "" {
Expand Down Expand Up @@ -388,16 +387,14 @@ func (c *child) start(
}

func (c *child) terminate(p *os.Process, s *api.ExecStatus) {
// signal the whole process group
if err := syscall.Kill(-p.Pid, syscall.SIGTERM); err != nil {
if err := terminateProcessGroup(p.Pid); err != nil {
log.Printf("failed to terminate %d: %v", p.Pid, err)
}
s.State = api.ExecStopping
}

func (c *child) kill(p *os.Process, s *api.ExecStatus) {
// signal the whole process group
if err := syscall.Kill(-p.Pid, syscall.SIGKILL); err != nil {
if err := killProcessGroup(p.Pid); err != nil {
log.Printf("failed to kill %d: %v", p.Pid, err)
}
if s.Group != "" {
Expand Down
22 changes: 22 additions & 0 deletions addons/pm/server/proc_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package server

import (
"os/exec"
"syscall"
)

// setProcGroup configures the command to start in a new process group,
// enabling group-wide signal delivery via negative PID.
func setProcGroup(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}

// terminateProcessGroup sends SIGTERM to the entire process group.
func terminateProcessGroup(pid int) error {
return syscall.Kill(-pid, syscall.SIGTERM)
}

// killProcessGroup sends SIGKILL to the entire process group.
func killProcessGroup(pid int) error {
return syscall.Kill(-pid, syscall.SIGKILL)
}
19 changes: 19 additions & 0 deletions addons/pm/server/proc_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build !linux

package server

import (
"fmt"
"os/exec"
"runtime"
)

func setProcGroup(_ *exec.Cmd) {}

func terminateProcessGroup(_ int) error {
return fmt.Errorf("process group termination not supported on %s", runtime.GOOS)
}

func killProcessGroup(_ int) error {
return fmt.Errorf("process group kill not supported on %s", runtime.GOOS)
}
Comment on lines +13 to +19
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the process group thing work on Darwin?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was failing when I tried cross-compiling, but I'll dig into it more to try to get a true/proper fix.

18 changes: 17 additions & 1 deletion instance/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@ import (

var info = sync.OnceValue(loadVersionInfo)

// versionOverride, if non-empty, replaces MainVersion from build info.
var versionOverride string

// SetVersion overrides the main version string reported by Version() and
// VersionInfo(). Call before any use of the version functions.
func SetVersion(v string) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the use case for this? The normal Go build process should put your VCS info into the binary, right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I need to pull that out, I pushed on the wrong branch, I was trying to fix an issue where the VCS would show in 2 different ways depending on how it was set and it was confusing because sometimes it would show mine, and sometimes it would show the gdev one. The override I tried doing didn't resolve it, but there's probably a better way to handle it regardless, it shouldn't be upstream.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main module info should definitely only ever show yours, unless you're building e.g. the "gdev" example :)

versionOverride = v
}

func Version() string {
if versionOverride != "" {
return versionOverride
}
return info().MainVersion
}

func VersionInfo() versionInfo {
return info()
vi := info()
if versionOverride != "" {
vi.MainVersion = versionOverride
}
return vi
}

type versionInfo struct {
Expand Down
6 changes: 6 additions & 0 deletions lib/sys/daemon_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"runtime"
)

// FallbackLogFileEnv is an environment variable that can be set to provide
// a fallback log file path for daemons started without systemd support.
//
// This will not be passed to the actual daemon.
const FallbackLogFileEnv = "__FALLBACK_LOG_FILE"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just go into daemon.go (new file) instead of having platform-specific copies. Probably over time other non-platform-specific bits will accumulate.


func StartDaemon(
ctx context.Context,
name string,
Expand Down