Skip to content

Commit

Permalink
Cleanup a bit
Browse files Browse the repository at this point in the history
Use UTF16PtrFromString instead of deprecated StringToUTF16, Use buffered chanel for signals, use t.TempDir
  • Loading branch information
a.karpov committed May 21, 2024
1 parent 7703882 commit 5a1fb4d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
11 changes: 1 addition & 10 deletions service_su_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func TestMain(m *testing.M) {

func TestInstallRunRestartStopRemove(t *testing.T) {
p := &program{}
reportDir := mustTempDir(t)
defer os.RemoveAll(reportDir)
reportDir := t.TempDir()

s := mustNewRunAsService(t, p, reportDir)
_ = s.Uninstall()
Expand Down Expand Up @@ -88,14 +87,6 @@ func runService() {
}
}

func mustTempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "servicetest")
if err != nil {
t.Fatal(err)
}
return dir
}

func writeReport(reportDir string, action string) {
b := []byte("go_test_service_report")
timeStamp := time.Now().UnixNano()
Expand Down
10 changes: 6 additions & 4 deletions service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,12 @@ func lowPrivMgr() (*mgr.Mgr, error) {
}

func lowPrivSvc(m *mgr.Mgr, name string) (*mgr.Service, error) {
name16p, err := syscall.UTF16PtrFromString(name)
if err != nil {
return nil, fmt.Errorf("failed to convert '%s' to utf16 string: %w", name, err)
}
h, err := windows.OpenService(
m.Handle, syscall.StringToUTF16Ptr(name),
m.Handle, name16p,
windows.SERVICE_QUERY_CONFIG|windows.SERVICE_QUERY_STATUS|windows.SERVICE_START|windows.SERVICE_STOP)
if err != nil {
return nil, err
Expand Down Expand Up @@ -469,10 +473,8 @@ func (ws *windowsService) Run() error {
return err
}

sigChan := make(chan os.Signal)

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)

<-sigChan

return ws.i.Stop(ws)
Expand Down

0 comments on commit 5a1fb4d

Please sign in to comment.