Explorar o código

windows: properly plumb sidtype into service creation

Many service attributes are settable using the ordinary CreateService
function, but ones added later in Windows need to be set using the
ChangeServiceConfig2 function. One of these is the Description field,
which is nicely plumbed behind the scenes, so that users of the API can
set it, and the mgr package will just figure out what to do with it.
Another one that works exactly the same way is SidType. Support for its
constants was added in 30999d6 ("windows: add missing service
constants"), but it wasn't actually built into the mgr package's
configuration struct, creating inconstancies in interface.

This commit rectifies that by adding proper support to mgr's config
struct.

Change-Id: I4f148f2d2477a03a65e8a571a8401392f6fe9f28
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180978
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Jason A. Donenfeld %!s(int64=6) %!d(string=hai) anos
pai
achega
83eebf7133
Modificáronse 2 ficheiros con 19 adicións e 0 borrados
  1. 9 0
      windows/svc/mgr/config.go
  2. 10 0
      windows/svc/mgr/mgr.go

+ 9 - 0
windows/svc/mgr/config.go

@@ -42,6 +42,7 @@ type Config struct {
 	DisplayName      string
 	Password         string
 	Description      string
+	SidType          uint32 // one of SERVICE_SID_TYPE, the type of sid to use for the service
 }
 
 func toString(p *uint16) string {
@@ -114,6 +115,10 @@ func updateDescription(handle windows.Handle, desc string) error {
 		windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d)))
 }
 
+func updateSidType(handle windows.Handle, sidType uint32) error {
+	return windows.ChangeServiceConfig2(handle, windows.SERVICE_CONFIG_SERVICE_SID_INFO, (*byte)(unsafe.Pointer(&sidType)))
+}
+
 // UpdateConfig updates service s configuration parameters.
 func (s *Service) UpdateConfig(c Config) error {
 	err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
@@ -123,6 +128,10 @@ func (s *Service) UpdateConfig(c Config) error {
 	if err != nil {
 		return err
 	}
+	err = updateSidType(s.Handle, c.SidType)
+	if err != nil {
+		return err
+	}
 	return updateDescription(s.Handle, c.Description)
 }
 

+ 10 - 0
windows/svc/mgr/mgr.go

@@ -102,9 +102,19 @@ func (m *Mgr) CreateService(name, exepath string, c Config, args ...string) (*Se
 	if err != nil {
 		return nil, err
 	}
+	if c.SidType != windows.SERVICE_SID_TYPE_NONE {
+		err = updateSidType(h, c.SidType)
+		if err != nil {
+			windows.DeleteService(h)
+			windows.CloseHandle(h)
+			return nil, err
+		}
+	}
 	if c.Description != "" {
 		err = updateDescription(h, c.Description)
 		if err != nil {
+			windows.DeleteService(h)
+			windows.CloseHandle(h)
 			return nil, err
 		}
 	}