Skip to content

x/sys/windows: mgr.Service.UpdateConfig(...) cannot set empty dependency list #73494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jimbobmcgee opened this issue Apr 24, 2025 · 2 comments
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@jimbobmcgee
Copy link

Go version

1.22.4 / x/sys@v0.20.0 (but the relevant code hasn't changed

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=D:\Users\...\AppData\Local\go-build
set GOENV=D:\Users\...\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\Users\...\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\Users\...\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\Projects\Internal\...\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=D:\Users\...\AppData\Local\Temp\go-build2673762109=/tmp/go-build -gno-record-gcc-switches

What did you do?

I was trying to update the configuration of a developed Windows service to remove all dependencies on other services, using the (*mgr.Service).UpdateConfig(mgr.Config) function. The mgr.Config struct exposes dependencies as mgr.Config.Dependencies []string, to which I was supplying an empty string slice.

Minimal example; error checking/cleanup elided:

m, _ := mgr.Connect()
s, _ := m.OpenService("mysvc")
c, _ := s.Config()
c.Dependencies = []string{}
s.UpdateConfig(c)

What did you see happen?

The service dependencies are unchanged, as observed in the Windows GUI. Subsequent calls to mgr.OpenService(...).Config() return the original value for Dependencies.

What did you expect to see?

The service configuration should have been updated to have zero dependencies.

Naiive workaround is to call windows.ChangeServiceConfig(...) (i.e. the Win32 function wrapper) directly, passing a pointer to an explicitly-crafted []uint16

empty := []uint16{0}
m, _ := mgr.Connect()
s, _ := m.OpenService("mysvc")
windows.ChangeServiceConfig(s.Handle,
    windows.SERVICE_NO_CHANGE, windows.SERVICE_NO_CHANGE, windows.SERVICE_NO_CHANGE,
    nil, nil, nil,
    &empty[0],
    nil, nil, nil)

Problem appears to relate to unexposed method toStringBlock([]string) *uint16, and how it handles empty slices (i.e. it returns nil).

Guidance for the Win32 ChangeServiceConfigW method suggests:

Specify NULL if you are not changing the existing dependencies. Specify an empty string if the service has no dependencies.

Since changing the behaviour of toStringBlock or the exposure of Dependencies may have backward-compatibility concerns, I'm not sure of the best way to expose the intent to clear this list. Perhaps a new method on mgr.Config which empties the slice and sets an unexported flag, which UpdateConfig would then handle?

func (c mgr.Config) ClearDependencies() {
    c.Depenencies = []string{}
    c.clearedDependencies = true
}

func (s *mgr.Service) UpdateConfig(c mgr.Config) error {
    //...
    var depends *uint16
    switch {
    case c.clearedDependencies && len(c.Dependencies) == 0:
        empty := []uint16{0}
        depends = &empty[0]
    default:
        depends = toStringBlock(c.Dependencies)
    }
    //...
    err = windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
        c.ErrorControl, binaryPathNamePointer, loadOrderGroupPointer,
        nil, depends, serviceStartNamePointer,
        passwordPointer, displayNamePointer)
    //...
}

Or maybe it is sufficient to document the lmitation and alternative in UpdateConfig()...

@gopherbot gopherbot added this to the Unreleased milestone Apr 24, 2025
@gabyhelp
Copy link

@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Apr 24, 2025
@JunyangShao JunyangShao changed the title x/sys/windows/svc/mgr: mgr.Service.UpdateConfig(...) cannot set empty dependency list x/sys/windows: mgr.Service.UpdateConfig(...) cannot set empty dependency list Apr 25, 2025
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Apr 25, 2025
@JunyangShao
Copy link
Contributor

@golang/runtime

@JunyangShao JunyangShao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed compiler/runtime Issues related to the Go compiler and/or runtime. labels Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants