Skip to content

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

Open
@jimbobmcgee

Description

@jimbobmcgee

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()...

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions