Skip to content

Commit b8426f7

Browse files
ale7714Otterverse
andauthored
Update make lint and fix issues (#85)
Co-authored-by: James Otting <[email protected]>
1 parent 3579cc6 commit b8426f7

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ clean:
4646
rm -rf bin/
4747

4848
bin/golangci-lint: Makefile
49-
GOBIN=`pwd`/bin go install github.com/golangci/golangci-lint/cmd/[email protected]
49+
GOOS='' GOBIN=`pwd`/bin go install github.com/golangci/golangci-lint/cmd/[email protected]
5050

5151
.PHONY: lint
5252
lint: bin/golangci-lint
5353
go mod tidy
54-
bin/golangci-lint run -v --fix
54+
GOOS='linux' bin/golangci-lint run -v --fix
55+
GOOS='windows' bin/golangci-lint run -v --fix
5556

5657
.PHONY: test
5758
test:

cmd/viam-agent/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929

3030
// only changed/set at startup, so no mutex.
3131
globalLogger = logging.NewLogger("viam-agent")
32-
globalCancel func() //nolint:unused
32+
globalCancel context.CancelFunc
3333
)
3434

3535
//nolint:lll
@@ -46,11 +46,11 @@ type agentOpts struct {
4646

4747
//nolint:gocognit
4848
func commonMain() {
49-
ctx, cancel := setupExitSignalHandling()
50-
globalCancel = cancel
49+
var ctx context.Context
50+
ctx, globalCancel = setupExitSignalHandling()
5151

5252
defer func() {
53-
cancel()
53+
globalCancel()
5454
activeBackgroundWorkers.Wait()
5555
}()
5656

cmd/viam-agent/main_windows.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
"github.com/viamrobotics/agent"
99
"github.com/viamrobotics/agent/utils"
1010
"go.viam.com/rdk/logging"
11+
goutils "go.viam.com/utils"
1112
"golang.org/x/sys/windows/svc"
1213
"golang.org/x/sys/windows/svc/debug"
1314
"golang.org/x/sys/windows/svc/eventlog"
1415
)
1516

16-
var elog debug.Log
17-
var _ svc.Handler = (*agentService)(nil)
17+
var (
18+
elog debug.Log
19+
_ svc.Handler = (*agentService)(nil)
20+
)
1821

1922
const serviceName = "viam-agent"
2023

@@ -27,10 +30,10 @@ func (*agentService) Execute(args []string, r <-chan svc.ChangeRequest, changes
2730
for {
2831
c := <-r
2932
if c.Cmd == svc.Stop || c.Cmd == svc.Shutdown {
30-
elog.Info(1, fmt.Sprintf("%s service stopping", serviceName))
33+
goutils.UncheckedError(elog.Info(1, fmt.Sprintf("%s service stopping", serviceName)))
3134
break
3235
} else {
33-
elog.Error(1, fmt.Sprintf("unexpected control request #%d", c))
36+
goutils.UncheckedError(elog.Error(1, fmt.Sprintf("unexpected control request #%d", c)))
3437
}
3538
}
3639
changes <- svc.Status{State: svc.StopPending}
@@ -41,7 +44,7 @@ func main() {
4144
if inService, err := svc.IsWindowsService(); err != nil {
4245
panic(err)
4346
} else if !inService {
44-
println("no service detected -- running as normal process")
47+
globalLogger.Info("no service detected -- running as normal process")
4548
commonMain()
4649
return
4750
}
@@ -51,29 +54,31 @@ func main() {
5154
if err != nil {
5255
return
5356
}
54-
defer elog.Close()
57+
defer func() {
58+
goutils.UncheckedError(elog.Close())
59+
}()
5560

56-
elog.Info(1, fmt.Sprintf("starting %s service", serviceName))
61+
goutils.UncheckedError(elog.Info(1, fmt.Sprintf("starting %s service", serviceName)))
5762
go commonMain()
5863
// note: svc.Run hangs until windows terminates the service. Then we manually call
5964
// globalCancel, which stops the go commonMain goroutine, then we wait for the waitgroup.
6065
err = svc.Run(serviceName, &agentService{})
6166
if err != nil {
62-
elog.Error(1, fmt.Sprintf("%s service failed: %v", serviceName, err))
67+
goutils.UncheckedError(elog.Error(1, fmt.Sprintf("%s service failed: %v", serviceName, err)))
6368
return
6469
}
6570
if globalCancel == nil {
66-
elog.Error(1, "globalCancel is nil, shutdown will be unclean")
71+
goutils.UncheckedError(elog.Error(1, "globalCancel is nil, shutdown will be unclean"))
6772
} else {
6873
globalCancel()
6974
}
7075
// wait first so viam-server doesn't try to restart
7176
activeBackgroundWorkers.Wait()
7277
// KillTree to catch any stragglers
7378
if err := utils.KillTree(-1); err != nil {
74-
elog.Error(1, fmt.Sprintf("error killing subtree %s", err))
79+
goutils.UncheckedError(elog.Error(1, fmt.Sprintf("error killing subtree %s", err)))
7580
}
76-
elog.Info(1, fmt.Sprintf("%s service stopped", serviceName))
81+
goutils.UncheckedError(elog.Info(1, fmt.Sprintf("%s service stopped", serviceName)))
7782
}
7883

7984
func ignoredSignal(_ os.Signal) bool {

utils/utils_windows.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"syscall"
1111

1212
"go.viam.com/rdk/logging"
13+
goutils "go.viam.com/utils"
1314
"golang.org/x/sys/windows/svc/eventlog"
1415
)
1516

@@ -28,7 +29,9 @@ func SyncFS(syncPath string) error {
2829
if err != nil {
2930
return err
3031
}
31-
defer syscall.CloseHandle(handle)
32+
defer func() {
33+
goutils.UncheckedError(syscall.CloseHandle(handle))
34+
}()
3235
err = syscall.Fsync(handle)
3336
if err != nil {
3437
return err
@@ -38,21 +41,26 @@ func SyncFS(syncPath string) error {
3841

3942
// KillTree kills the process tree on windows (because other signaling doesn't work).
4043
func KillTree(pid int) error {
41-
elog, _ := eventlog.Open("viam-agent")
42-
// note: we're ignoring the log error because we want this to work
44+
elog, err := eventlog.Open("viam-agent")
45+
if err != nil {
46+
// Check error but continue since we want this to work
47+
elog = nil
48+
}
49+
4350
if pid == -1 {
4451
pid = os.Getpid()
4552
}
53+
54+
// Use a fixed command string to prevent injection
55+
//nolint:gosec // WMIC.exe is a fixed command
4656
cmd := exec.Command("WMIC.exe", "process", "where", fmt.Sprintf("ParentProcessId=%d", pid), "get", "ProcessId")
4757
output, err := cmd.Output()
4858
if err != nil {
4959
return err
50-
// elog.Error(1, fmt.Sprintf("error executing %s %s", cmd.Path, cmd.Args))
51-
// elog.Error(1, fmt.Sprintf("error getting child process for #%d, #%s", pid, err))
5260
}
5361
lines := strings.Split(string(output), "\r\n")
5462
if elog != nil {
55-
elog.Info(1, fmt.Sprintf("KillTree stopping %d children of pid %d", len(lines), pid))
63+
goutils.UncheckedError(elog.Info(1, fmt.Sprintf("KillTree stopping %d children of pid %d", len(lines), pid)))
5664
}
5765
for _, line := range lines[1:] {
5866
if line == "" {
@@ -62,22 +70,24 @@ func KillTree(pid int) error {
6270
_, err := fmt.Sscan(line, &childPID)
6371
if err != nil {
6472
if elog != nil {
65-
elog.Error(1, fmt.Sprintf("not a valid childProcess line %q, #%s", line, err))
73+
goutils.UncheckedError(elog.Error(1, fmt.Sprintf("not a valid childProcess line %q, #%s", line, err)))
6674
}
6775
continue
6876
}
77+
78+
//nolint:gosec // taskkill is a fixed command
6979
cmd = exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(childPID))
7080
err = cmd.Run()
7181
if elog != nil {
7282
if err != nil {
73-
elog.Error(1, fmt.Sprintf("error running taskkill pid %d: #%s", childPID, err))
83+
goutils.UncheckedError(elog.Error(1, fmt.Sprintf("error running taskkill pid %d: #%s", childPID, err)))
7484
} else {
75-
elog.Info(1, fmt.Sprintf("killed pid %d", childPID))
85+
goutils.UncheckedError(elog.Info(1, fmt.Sprintf("killed pid %d", childPID)))
7686
}
7787
}
7888
}
7989
if elog != nil {
80-
elog.Info(1, "KillTree finished")
90+
goutils.UncheckedError(elog.Info(1, "KillTree finished"))
8191
}
8292
return nil
8393
}

0 commit comments

Comments
 (0)