Skip to content

Commit

Permalink
Swap out all usages of the syscall package (sourcegraph#513)
Browse files Browse the repository at this point in the history
with the `golang.org/x/sys/unix` package.

`syscall` has been frozen since Go 1.3 and deprecated
(https://go.dev/doc/go1.4#major_library_changes).

Using the `golang.org/x/sys/unix` package will bring in bug fixes
and enhancements since `syscall` was frozen in 1.3,
and will pave the way for multi-platform builds
(which will affect only the single-program local install, most likely).
  • Loading branch information
peterguy authored Jan 13, 2023
1 parent e0cf62d commit a303c6c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
7 changes: 4 additions & 3 deletions build/builder_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package build

import (
"os"
"syscall"

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

func init() {
umask = os.FileMode(syscall.Umask(0))
syscall.Umask(int(umask))
umask = os.FileMode(unix.Umask(0))
unix.Umask(int(umask))
}
11 changes: 6 additions & 5 deletions cmd/zoekt-sourcegraph-indexserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"text/tabwriter"
"time"

"golang.org/x/sys/unix"

"github.com/keegancsmith/tmpfriend"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -247,7 +248,7 @@ func (s *Server) loggedRun(tr trace.Trace, cmd *exec.Cmd) (err error) {
} else {
// Send quit (C-\) first so we get a stack dump.
log.Printf("no output for %s, quitting %s", noOutputTimeout, cmd.Args)
if err := cmd.Process.Signal(syscall.SIGQUIT); err != nil {
if err := cmd.Process.Signal(unix.SIGQUIT); err != nil {
log.Println("quit failed:", err)
}

Expand Down Expand Up @@ -312,7 +313,7 @@ func (s *Server) Run() {
// testing we also listen for SIGUSR1 to trigger updates.
//
// "pkill -SIGUSR1 zoekt-sourcegra"
for range jitterTicker(s.Interval, syscall.SIGUSR1) {
for range jitterTicker(s.Interval, unix.SIGUSR1) {
if b, err := os.ReadFile(filepath.Join(s.IndexDir, pauseFileName)); err == nil {
log.Printf("indexserver manually paused via PAUSE file: %s", string(bytes.TrimSpace(b)))
continue
Expand Down Expand Up @@ -359,15 +360,15 @@ func (s *Server) Run() {
}()

go func() {
for range jitterTicker(s.VacuumInterval, syscall.SIGUSR1) {
for range jitterTicker(s.VacuumInterval, unix.SIGUSR1) {
if s.shardMerging {
s.vacuum()
}
}
}()

go func() {
for range jitterTicker(s.MergeInterval, syscall.SIGUSR1) {
for range jitterTicker(s.MergeInterval, unix.SIGUSR1) {
if s.shardMerging {
s.doMerge()
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/zoekt-sourcegraph-indexserver/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
"os"
"path/filepath"
"syscall"

"golang.org/x/sys/unix"

"github.com/sourcegraph/zoekt"
"github.com/sourcegraph/zoekt/build"
Expand Down Expand Up @@ -113,6 +114,6 @@ func jsonMarshalTmpFile(v interface{}, p string) (_ string, err error) {
var umask os.FileMode

func init() {
umask = os.FileMode(syscall.Umask(0))
syscall.Umask(int(umask))
umask = os.FileMode(unix.Umask(0))
unix.Umask(int(umask))
}
13 changes: 7 additions & 6 deletions cmd/zoekt-webserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ import (
"runtime"
"strconv"
"strings"
"syscall"
"time"

"golang.org/x/sys/unix"

"github.com/sourcegraph/mountinfo"

"github.com/sourcegraph/zoekt"
Expand Down Expand Up @@ -337,7 +338,7 @@ func addProxyHandler(mux *http.ServeMux, socket string) {
func shutdownSignalChan(maxReads int) <-chan os.Signal {
c := make(chan os.Signal, maxReads)
signal.Notify(c, os.Interrupt) // terminal C-c and goreman
signal.Notify(c, syscall.SIGTERM) // Kubernetes
signal.Notify(c, unix.SIGTERM) // Kubernetes
return c
}

Expand Down Expand Up @@ -442,8 +443,8 @@ func mustRegisterDiskMonitor(path string) {
Help: "Amount of free space disk space.",
ConstLabels: prometheus.Labels{"path": path},
}, func() float64 {
var stat syscall.Statfs_t
_ = syscall.Statfs(path, &stat)
var stat unix.Statfs_t
_ = unix.Statfs(path, &stat)
return float64(stat.Bavail * uint64(stat.Bsize))
}))

Expand All @@ -452,8 +453,8 @@ func mustRegisterDiskMonitor(path string) {
Help: "Amount of total disk space.",
ConstLabels: prometheus.Labels{"path": path},
}, func() float64 {
var stat syscall.Statfs_t
_ = syscall.Statfs(path, &stat)
var stat unix.Statfs_t
_ = unix.Statfs(path, &stat)
return float64(stat.Blocks * uint64(stat.Bsize))
}))
}
Expand Down
7 changes: 4 additions & 3 deletions indexfile_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"fmt"
"log"
"os"
"syscall"

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

type mmapedIndexFile struct {
Expand All @@ -45,7 +46,7 @@ func (f *mmapedIndexFile) Size() (uint32, error) {
}

func (f *mmapedIndexFile) Close() {
if err := syscall.Munmap(f.data); err != nil {
if err := unix.Munmap(f.data); err != nil {
log.Printf("WARN failed to Munmap %s: %v", f.name, err)
}
}
Expand All @@ -70,7 +71,7 @@ func NewIndexFile(f *os.File) (IndexFile, error) {
}

rounded := (r.size + 4095) &^ 4095
r.data, err = syscall.Mmap(int(f.Fd()), 0, int(rounded), syscall.PROT_READ, syscall.MAP_SHARED)
r.data, err = unix.Mmap(int(f.Fd()), 0, int(rounded), unix.PROT_READ, unix.MAP_SHARED)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions tombstones.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"os"
"path/filepath"
"strconv"
"syscall"

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

// ShardMergingEnabled returns true if SRC_ENABLE_SHARD_MERGING is set to true.
Expand Down Expand Up @@ -104,6 +105,6 @@ func JsonMarshalRepoMetaTemp(shardPath string, repositoryMetadata interface{}) (
var umask os.FileMode

func init() {
umask = os.FileMode(syscall.Umask(0))
syscall.Umask(int(umask))
umask = os.FileMode(unix.Umask(0))
unix.Umask(int(umask))
}

0 comments on commit a303c6c

Please sign in to comment.