Commit Graph

49 Commits

Author SHA1 Message Date
Bryan C. Mills
1911637744 windows/svc: use separate (and more descriptive) service names in tests
Notably, the DisplayName field was set to the same thing in both
sys.TestExample and mrg.TestMyService, which may explain the collision
reported in golang/go#59298.

Moreover, the adjective ”my” conveys no information whatsoever — we
shouldn't use it in tests or examples.

Also skip the tests that install services if GO_BUILDER_NAME is not
set, to reduce the likelihood of 'go test all' in a user's working
directory being mistaken for a malicious or compromised program.

Fixes golang/go#59298.

Change-Id: Ib00bf7400bfaa34e1a1d49125c43b97019b53c82
Reviewed-on: https://go-review.googlesource.com/c/sys/+/481015
Reviewed-by: Carlos Amedee <carlos@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
2023-05-04 17:52:42 +00:00
Craig Davison
9524d496ef windows/svc/mgr: Service.Control: populate Status when returning certain errors
Fixes golang/go#59015

Change-Id: I45f22049f3a05f807f78d20c9ed67c6c79e3d3c1
GitHub-Last-Rev: 929aeb4acb
GitHub-Pull-Request: golang/sys#156
Reviewed-on: https://go-review.googlesource.com/c/sys/+/484895
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-04-25 08:02:03 +00:00
Sebastian Soto
dbd8f99a5e windows: add Service.ListDependentServices
This method allows a user to list all Windows services which are
dependent upon a given service.

This commit makes use of the EnumDependentServices Windows API call.
Without this, a user would have to iterate through each service on the
system, and check if the given service is listed in each service's
dependencies list.

The implementation of ListDependentServices is mostly the same as
Mgr.ListServices, as the API calls behave in the same way.

Fixes golang/go#56766

Change-Id: I9ec18c97afd02f48deef691ccdd5c26d6501add1
Reviewed-on: https://go-review.googlesource.com/c/sys/+/451363
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2023-04-14 09:16:30 +00:00
Bryan C. Mills
68f9dcb1f9 windows/debug/svc: buffer channel passed to signal.Notify
The os/signal package drops signal notifications if the channel is not
ready to receive immediately. To avoid dropping signals (for example,
while processing the other branch of a 'select'), the channel must be
buffered.

This fixes a mistake flagged by 'go vet ./...'.

Change-Id: I2a20dbe2aa27ae8ec009fff5e7be47e4409fdddd
Reviewed-on: https://go-review.googlesource.com/c/sys/+/465595
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-02-06 18:23:23 +00:00
Russ Cox
b5fbb4746d all: gofmt
Gofmt to update doc comments to the new formatting.

For golang/go#51082.

Change-Id: I9a1c4b671c06820a1c383ee515f7884965fefa54
Reviewed-on: https://go-review.googlesource.com/c/sys/+/399602
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2022-04-29 23:34:32 +00:00
Jason A. Donenfeld
1d35b9e2eb windows/svc: use NtQuerySystemInformation in IsWindowsService
This brings the algorithm more exactly in line with what .NET does for
the identically named function. Specifically, instead of using
OpenProcess, which requires rights that restricted services might not
have, we use NtQuerySystemInformation(SYSTEM_PROCESS_INFORMATION) to
find the parent process image name and session ID.

Fixes golang/go#44921.

Change-Id: Ie2ad7521cf4c530037d086e61dbc2413e4e7777c
Reviewed-on: https://go-review.googlesource.com/c/sys/+/372554
Trust: Jason Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
Trust: Patrik Nyblom <pnyb@google.com>
Run-TryBot: Patrik Nyblom <pnyb@google.com>
2021-12-16 02:10:12 +00:00
Bryan C. Mills
99a53858aa all: add missing //go:build comments
These were apparently overlooked in CL 357329, CL 294490, CL 296889,
and other various updates to this module. (I noticed them via gopls
while investigating golang/go#49466.)

Updates golang/go#41184

Change-Id: Id042bb6fe5282e6d528e9315acf2ad29d0df58ba
Reviewed-on: https://go-review.googlesource.com/c/sys/+/362577
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-11-10 15:43:04 +00:00
Jason A. Donenfeld
6e7872819d windows/svc: allow querying service start reason
The QueryServiceDynamicInformation API makes it possible to determine
why a service started. This is declared as an optional function because
the API is Win8+.

Reference:
https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-queryservicedynamicinformation

Change-Id: I18e9a95b35f8c37d94c9900626c1785f425701dc
Reviewed-on: https://go-review.googlesource.com/c/sys/+/358394
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-10-29 16:52:21 +00:00
Jason A. Donenfeld
25fac919d9 windows/svc: do not close service handle
The documentation [1] says "The service status handle does not have to
be closed." That makes it seem like it's optional. But actually, closing
it raises a user exception, because it's not a legitimate handle to
close.

This commit also adds the right go build comments with a `go fmt` pass.

[1] https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-registerservicectrlhandlerexa

Change-Id: Ibf67c0e8d94c87068720472009b24cecdfda6519
Reviewed-on: https://go-review.googlesource.com/c/sys/+/357250
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-10-20 15:30:01 +00:00
Jason A. Donenfeld
0ec99a608a windows/svc: rewrite in Go
The old service management code was written in assembly and communicated
over Windows events, which resulted in non-obvious control flow.
NewCallback makes it possible to rewrite all of this in vanilla Go. This
also enables the service test on the Go builders, as modifying system
services shouldn't be an issue there.

Change-Id: I8003b57d11d4469f762058c648a4b7733530eeb8
Reviewed-on: https://go-review.googlesource.com/c/sys/+/330010
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-10-20 06:40:51 +00:00
Song Lim
8803ae5d13 windows/svc/mgr: fix misspelled word in service.go
fix misspelled word "service" in line 62

Change-Id: I646f073d5604c36775d27c478a7b4b007f70ff62
GitHub-Last-Rev: 79901a6ca3
GitHub-Pull-Request: golang/sys#107
Reviewed-on: https://go-review.googlesource.com/c/sys/+/314573
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-05-03 08:07:04 +00:00
Alex Brainman
d6cf4ab887 windows/svc: use ioutil.WriteFile instead of os.WriteFile
os.WriteFile is not available on Go 1.15. But we still need to
support Go 1.15.

Fixes broken Go 1.15 build

https://build.golang.org/log/07b89b007d0aca2e97bc01cdf4c6a1d90e6732f9

Change-Id: I18433985ec82477160058ef96c160345304ba1ad
Reviewed-on: https://go-review.googlesource.com/c/sys/+/299309
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-05 21:05:45 +00:00
Jason A. Donenfeld
7844c3c200 windows/svc: rewrite IsWindowsService
Alex copied this from a temporary thing I had written for the runtime
package. In runtime, you can't really access other packages like
syscall, so everything has to be very manual. But in x/sys, we can do
things properly. So this reimplements the function in a more straight
forward way.

Change-Id: I1634904bb1e10f33252954ce02d4b17ae56592e5
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298830
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-05 03:40:16 +00:00
Alex Brainman
b0d1d43c01 windows/svc: make IsWindowsService handle parent exit
IsWindowsService fails when parent process has exited.

This change makes IsWindowsService return false instead.

Added a test to verify the change.

Change-Id: Ibb2aa9478e8afd9e35011bbdc0985bdf8f0af9cc
Reviewed-on: https://go-review.googlesource.com/c/sys/+/294729
TryBot-Result: Go Bot <gobot@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
2021-02-27 04:07:30 +00:00
Jason A. Donenfeld
beda7e5e15 windows: port to ARM64
This also fixes up some spaces->tabs issues and moves build tags from
the file contents to the file name.

Updates golang/go#36439.

Change-Id: Iafa03e26575603a802832e587bb18ebd16fd176b
Reviewed-on: https://go-review.googlesource.com/c/sys/+/289449
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Russ Cox <rsc@golang.org>
2021-02-18 14:52:45 +00:00
Meng Zhuo
8ad439b19e windows: remove ErrorControl value check in CreateService
ErrorIgnore is 0 which means we shouldn't rely on zero value checking.

Fixes golang/go#43094

Change-Id: I7af3132cfe58d73c7abab92203fb02a0ada15218
Reviewed-on: https://go-review.googlesource.com/c/sys/+/276572
Trust: Meng Zhuo <mzh@golangcn.org>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Meng Zhuo <mzh@golangcn.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2020-12-11 09:08:39 +00:00
Jason A. Donenfeld
ed752295db windows/svc/mgr: report exit status when querying services
One thing the newer notification API does not do is automatically
provide the service exit code in its notifier response. It requires
querying manually. Unfortunately, we weren't propagating this
information up from the lower level struct, so this commit copys that
information over.

Change-Id: I70c683007ce34ffab6196329acefc8443f921ebe
Reviewed-on: https://go-review.googlesource.com/c/sys/+/274577
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-04 22:54:14 +00:00
Jason A. Donenfeld
ba5294a509 windows/svc/mgr: fetch service sid type in Config()
Currently, doing s.UpdateConfig(s.Config()) will destroy the service,
because s.Config() fails to populate the SidType member, but
UpdateConfig will set the SidType, so that expression effectively zeros
out the SidType. Fix this by having Config() fetch the SidType in the
same way that it fetches the other additional fields there, such as
DelayedStartUp.

Change-Id: Idb917ef1e942020499b411b7777b995c29f0e7d2
Reviewed-on: https://go-review.googlesource.com/c/sys/+/270897
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2020-11-17 22:26:35 +00:00
Alex Brainman
db71ae6646 windows/svc/mgr: rename service in TestMyService
TestMyService uses the same service name as service in
golang.org/x/sys/windows/svc.TestExample.

https://build.golang.org sometimes schedules TestMyService and
TestExample to run at the same time. If that happens, tests fail,
because they were not designed to run in parallel.

This CL renames service in TestMyService, so it does not clash with
TestExample.

Fixes golang/go#42211

Change-Id: I928c2ebbae77de3540f5ce16948fb96eeeed836d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/267604
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-11-06 08:11:18 +00:00
Alex Brainman
1dbcb44a4e windows/svc: stop service before deletion in TestExample
TestExample normally stops and deletes test service at the end of the
test. But, if TestExample does not complete for some reason, test
service might remain running and installed.

There is some code that deletes "left over" test service before starting
the test. But that code fails, if service is running. Deletion only
works, if service is not running. This CL adds code to stop the "left
over" service so it can be deleted.

Update golang/go#42211

Change-Id: I826dd587063265c5b96076668c3704c0a7eaa3d8
Reviewed-on: https://go-review.googlesource.com/c/sys/+/267603
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-11-06 07:48:44 +00:00
Alex Brainman
280f808b4a windows/svc: add IsWindowsService function
CL 244958 includes isWindowsService function that determines if a
process is running as a service. The code of the function is based on
public .Net implementation.

IsAnInteractiveSession function implements similar functionality, but
is based on an old Stackoverflow post., which is not as authoritative
as code written by Microsoft for their official product.

This change copies CL 244958 isWindowsService function into svc package
and makes it public. The intention is that future users will prefer 
IsWindowsService to IsAnInteractiveSession.

Also this change adds "Deprecated" comment to IsAnInteractiveSession to
point future users to IsWindowsService.

Call to IsAnInteractiveSession is also replaced with IsWindowsService
in golang.org/x/sys/windows/svc/example package.

Change-Id: I4a33b7f590ee8161d1134d8e83668e9da4e6b434
Reviewed-on: https://go-review.googlesource.com/c/sys/+/259397
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Trust: Alex Brainman <alex.brainman@gmail.com>
2020-10-08 06:31:27 +00:00
Lewis Waddicor
288bc346aa windows: support service PreShutdown service control signal
This adds support for the PreShutdown signal indicating the system will
be shutting down. This allows services which need time to shutdown
gracefully to use SERVICE_ACCEPT_PRESHUTDOWN to register for
SERVICE_CONTROL_PRESHUTDOWN events.

https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nc-winsvc-lphandler_function_ex?redirectedfrom=MSDN

Change-Id: I380d366d49ff380972c1efe83f43610b30432f35
GitHub-Last-Rev: db8ea88b25
GitHub-Pull-Request: golang/sys#85
Reviewed-on: https://go-review.googlesource.com/c/sys/+/253477
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Daniel Martí <mvdan@mvdan.cc>
2020-09-15 08:46:02 +00:00
Alex Brainman
4b90ce9b60 windows/svc: align stack in servicemain before calling syscall
I noticed that we call syscall in servicemain without aligning stack.
That is against Windows rules, so align the stack as required.

I tried running this code with specifically non-aligned stack (I aligned
stack, and then subtracted 1 from SP) on my Windows 10 to test this
change. But it makes no difference on my Windows 10 PC - I built and run
golang.org/x/sys/windows/svc/example, and it runs successfully
regardless of stack alignment. But alignment might make difference on
other computers.

Maybe fixes golang/go#40160

Change-Id: I351f7f730fba4aa6dc409a79de4ad737b4a0a7d4
Reviewed-on: https://go-review.googlesource.com/c/sys/+/246317
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-08-02 09:19:54 +00:00
Yaroslav Vorobiov
fe76b779f2 windows: fix -d=checkptr slice failures
This CL fixes unsafe casts to slices that are missing length or capacity.

Running tests with -d=checkptr enabled may panic on casting unsafe.Pointer to a static array of large predefined length, that is most likely much bigger than the size of the actual array in memory. Checkptr check is not satisfied if slicing operator misses length and capacity arguments `(*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:]`, or when there is no slicing at all `(*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))`.

To find all potential cases I used `grep -nr ")(unsafe.Pointer(" ./windows`, then filtered out safe casts when object size is always static and known at compile time.

To reproduce the issue run tests with checkptr enabled `go test -a -gcflags=all=-d=checkptr ./windows/...`.

Updates golang/go#34972
Fixes golang/go#38355

Change-Id: I9dd2084b4f9fb7618cdb140fb2f38b56b6d6cc04
GitHub-Last-Rev: 73288ad18a
GitHub-Pull-Request: golang/sys#65
Reviewed-on: https://go-review.googlesource.com/c/sys/+/225418
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2020-05-19 10:57:57 +00:00
Jason A. Donenfeld
b397fe3ad8 windows/svc: use CloseServiceHandle for service handles
From MSDN on CreateServiceW:

  The returned handle is only valid for the process that called
  CreateService. It can be closed by calling the CloseServiceHandle
  function.

This isn't an actual kernel object to be closed with CloseHandle.

Change-Id: Iee225a666576d57a6c4864abef20206d54cbbce2
Reviewed-on: https://go-review.googlesource.com/c/sys/+/198298
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-10-02 09:15:54 +00:00
utkarsh-extc
51ab0e2dea windows: add support for automatic delayed start in windows service
Change-Id: Iad33ea0f6627ac98c89dbaab0b41b3dd724c3163
GitHub-Last-Rev: 8764fdbd32
GitHub-Pull-Request: golang/sys#36
Reviewed-on: https://go-review.googlesource.com/c/sys/+/187198
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-04 05:38:45 +00:00
Jason A. Donenfeld
301114b31c windows: allow determining if manager is locked
The SCM can be locked by NT. While traditionally any process could lock
the SCM using "LockServiceDatabase", Microsoft removed this
functionality because it created so many bugs, and that function now
does nothing. However, the system itself, via the "NT Service Control
Manager", is still allowed to lock the SCM.

For example, at boot time on Windows 8.1, the SCM is locked after a
service is started until that service reports itself in a running state.
This poses a bit of a problem: it's useful to install device drivers
from inside services as part of their initialization, and mark the
service as having started only after the device has installed. But
device installation might potentially load new drivers, and drivers
themselves exist as a special type of service. This means that if a
driver is installed before marking the service as started, the entire
SCM will deadlock, and the OS will be partially unresponsive for a
minute or two.

Fortunately Microsoft supplies an API for exactly this purpose. The
solution is to mark the service as started before installing device
drivers, only under the circumstance that the SCM is locked. So, this
commit adds the proper API for determining this. It can be used like
this:

    if m, err := mgr.Connect(); err == nil {
        if lockStatus, err := m.LockStatus(); err == nil && lockStatus.IsLocked {
            log.Printf("SCM locked for %v by %s, marking service as started", lockStatus.Age, lockStatus.Owner)
            changes <- svc.Status{State: svc.Running}
        }
        m.Disconnect()
    }
    deviceDriver.Install()

This creates messages like the following, indicating that this API
works:

    SCM locked for 1s by .\NT Service Control Manager, marking service as started

Change-Id: Ic2f5b387e23efc3a287b2ab96ff84b357b712e36
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180977
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-06-09 08:25:36 +00:00
Jason A. Donenfeld
83eebf7133 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>
2019-06-08 04:32:40 +00:00
Jason A. Donenfeld
2cc0cad0ac windows: document new ProcessId field
Change-Id: I761f962f30eb48471a0f033cf30ea6c428d3ce49
Reviewed-on: https://go-review.googlesource.com/c/sys/+/174237
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-04-29 09:44:11 +00:00
Jason A. Donenfeld
1607263960 windows: allow querying service ProcessId
The two functions have the same levels of compatibility, but this latter
one gives us access to the process ID, which is important for things
like WFP whitelisting. The change required is fairly trivial too.

Change-Id: Ifb6b3ee3e897202b9cffa1388c53c25cbcfede61
Reviewed-on: https://go-review.googlesource.com/c/sys/+/173666
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-04-25 14:56:19 +00:00
Jason A. Donenfeld
9773273309 windows: add missing error constants
While the types_windows file previously had a small handful of types,
this forced application code to have an awkward mixture of artisanal
error constants and factory-ready ones. This commit adds the missing
ones and separates them into a new file, since they are quite numerous.

These also preserve the order of winerr.h, which should make it somewhat
easier to import new ones in the future.

Fixes golang/go#31360

Change-Id: If2abc507a8884ec1641f0b17fe0c612a950d3644
Reviewed-on: https://go-review.googlesource.com/c/sys/+/170918
Reviewed-by: Jason Donenfeld <Jason@zx2c4.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-04-12 21:31:03 +00:00
Jason A. Donenfeld
70f5298506 windows/svc: safely load system DLLs
These DLLs, well advapi32.dll in particular, are vulnerable to classic
DLL directory injection attacks. The rest of x/sys/windows moved over to
the safe system loader, but apparently the svc package was forgotten.
This tidies up that oversight.

Change-Id: I330fa752cf2d49ccc5cf1bd60fb4bd612bd2b6b0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/165758
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-03-06 17:15:55 +00:00
Alex Brainman
b6889370fb windows/svc: align ctlHandler parameters
ctlHandler is passed to RegisterServiceCtrlHandlerEx as a callback
function. But all ctlHandler parameters have to be uintptr aligned
regardless of their type.

Adjust ctlHandler parameters, so they are aligned accordingly.

Also uncomment part of TestExample that verifies passing of
ChangeRequest.Context.

Fixes golang/go#25660

Change-Id: Ie96fa5c78b911ad9df6775f5c49e7f6d56464f6b
Reviewed-on: https://go-review.googlesource.com/c/158698
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-03-02 02:57:03 +00:00
Alex Brainman
12036c158a windows/svc: add Context to ChangeRequest
New Context field will be used in the following CL to test
ctlHandler parameter alignments.

Also adjust TestExample to pass hard coded Context value of 123456
to test service, and verify that correct value is logged. Final
part of the test is commented out, and will be adjusted in the next
CL.

Updates golang/go#25660

Change-Id: Iad2896ae497ee1edc0d62655eaf08671ec2651c5
Reviewed-on: https://go-review.googlesource.com/c/158697
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-03-02 02:54:30 +00:00
Jordan Rhee
074acd46bc windows/svc: use wevtutil.exe instead of powershell for windows/arm
Use wevtutil.exe to query event log instead of powershell's Get-EventLog
command, which is not available on Windows IoT Core.

Updates golang/go#26148

Change-Id: Idc16d0b18d2826730d2b4062388b2a764c4886c6
Reviewed-on: https://go-review.googlesource.com/c/154817
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-12-18 19:26:12 +00:00
Jordan Rhee
90868a75fe windows: add support for windows/arm
Updates golang/go#26148

Change-Id: I0425244bfb0c8f600a6f02d9d7b228e175a720a4
Reviewed-on: https://go-review.googlesource.com/127665
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-09-24 17:59:46 +00:00
madiganz
ac767d655b windows/svc/mgr: add ability to set a reboot message and command when a service fails
Added configuration options for a windows service recovery settings.
New configurations include modifying the reboot message, or command
to be run when a service fails, and getting the current reboot message
or command.

Fixes golang/go#23239

Change-Id: I3e501d66e97745b7536fd654aee2bba488083e6d
Reviewed-on: https://go-review.googlesource.com/122579
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2018-07-15 08:55:29 +00:00
Zachary Madigan
151529c776 windows, windows/svc/mgr: add ability to modify service recovery settings.
Added configuration options for a windows service recovery settings.
Current configurations include modifying actions taken when a service
fails, setting the reset period, and getting the current recovery
settings.

Updates golang/go#23239

Change-Id: I4e91b2068122731e6eba3332afb0fe300b298c97
Reviewed-on: https://go-review.googlesource.com/104635
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2018-07-04 09:49:41 +00:00
Hana (Hyang-Ah) Kim
7dfd1290c7 windows: address vet reports
Found in https://storage.googleapis.com/go-build-log/300ed876/misc-vet-vetall_dd96a388.log

cmd/vendor/golang.org/x/sys/windows/asm_windows_386.s:9: [386] getprocaddress: wrong argument size 8; expected $...-16
cmd/vendor/golang.org/x/sys/windows/asm_windows_386.s:12: [386] loadlibrary: wrong argument size 4; expected $...-12
cmd/vendor/golang.org/x/sys/windows/svc/service.go:337: cmd/vendor/golang.org/x/sys/windows.SERVICE_TABLE_ENTRY composite literal uses unkeyed fields
cmd/vendor/golang.org/x/sys/windows/svc/service.go:338: cmd/vendor/golang.org/x/sys/windows.SERVICE_TABLE_ENTRY composite literal uses unkeyed fields
cmd/vendor/golang.org/x/sys/windows/svc/debug/service.go:34: cmd/vendor/golang.org/x/sys/windows/svc.ChangeRequest composite literal uses unkeyed fields
cmd/vendor/golang.org/x/sys/windows/svc/mgr/config.go:124: cmd/vendor/golang.org/x/sys/windows.SERVICE_DESCRIPTION composite literal uses unkeyed fields

Change-Id: Ic08cfcc802aac89cfa1eb98661ec556c844e8013
Reviewed-on: https://go-review.googlesource.com/112557
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-05-10 03:28:50 +00:00
Alex Brainman
78d5f264b4 windows/svc: correct MOVL instruction in sys_amd64.s
Second argument of servicemain is a pointer. See

https://msdn.microsoft.com/en-us/library/windows/desktop/ms685138(v=vs.85).aspx

So amd64 assembler code should use MOVQ to read that value.
I probably copied 386 assembler code into amd64 and did not
adjust the code.

Broken code was used to pass parameters to the service, so
add some tests to verify that parameter passing works.

Fixes golang/go#24575

Change-Id: I89f8cad026ea13f8a5d78ff3e24b7236e27fc91f
Reviewed-on: https://go-review.googlesource.com/110160
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-05-01 09:27:40 +00:00
Tobias Klauser
9e4fff1f4d all: single space after period
Follow CL 20022 and consistently use single space after a period in
documentation.

Generated with:

$ perl -i -npe 's,^(\s*// .+[a-z]\.)  +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.)  +([A-Z])')

Change-Id: Ia29ad823668f060e81293e848a79fc4b4857d94b
Reviewed-on: https://go-review.googlesource.com/73530
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-10-25 20:39:07 +00:00
Tyler Treat
f7928cfef4 windows/svc: add missing Accept commands
Add the remaining AcceptX commands (AcceptParamChange,
AcceptNetBindChange, AcceptHardwareProfileChange, AcceptPowerEvent, and
AcceptSessionChange) to the windows/svc package. Also add support for
these to updateStatus such that a service can actually accept these
commands.

Change-Id: I11191e7798489b3222a3e76ba55129f10fd202ab
Reviewed-on: https://go-review.googlesource.com/46722
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-06-27 01:25:38 +00:00
Alex Brainman
fb4cac33e3 windows/svc/mgr: add Mgr.ListServices
Add API to list services installed on the system.

Fixes golang/go#20596

Change-Id: Ifa2f20ef15ccb962bd21d03788ce931dd45f2630
Reviewed-on: https://go-review.googlesource.com/45711
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-06-15 05:32:24 +00:00
helloPiers
dbc2be9168 windows/svc/mgr: correct documentation for args parameter of Mgr.CreateService
The args passed to Mgr.CreateService end up as the command-line
arguments (available via os.Args), not as the args passed to
Svc.Handler.Execute.

Fixes golang/go#20440

Change-Id: I55a1ec3432b377082c018fe960114c96350e8978
Reviewed-on: https://go-review.googlesource.com/43714
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-05-21 16:21:43 +00:00
Julian Pastarmov
e62c3de784 windows: use RegisterServiceCtrlHandlerEx to allow for advanced notifications
This allows services written with this library to register for more
advanced notifications like SERVICE_CONTROL_DEVICEEVENT for example.

Also the code now exposes the service status handle through a new
api call, because the handle is needed to register for such
notifications and can not be obtained by any other means.

Change-Id: I80796e1dd9d94ec9d6c236d0413b17288c67fe1f
Reviewed-on: https://go-review.googlesource.com/42812
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-05-18 05:52:01 +00:00
Mikio Hara
98b5b1e7e8 windows/svc/mgr: gofmt -w -s
Change-Id: I4ef179aacaf0694cd7661b4d9972ce85abeee8df
Reviewed-on: https://go-review.googlesource.com/43470
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-05-17 08:38:02 +00:00
Nick Petroni
20457ee8ea x/sys/windows/svc/mgr: allow other ServiceType values in CreateService
Preserves SERVICE_WIN32_OWN_PROCESS as the default.

Fixes golang/go#14019

Change-Id: I764016c1b5ec5e1fe88ed39b9803754af966b2f3
Reviewed-on: https://go-review.googlesource.com/18760
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
2016-01-21 06:23:56 +00:00
Alex Brainman
b4e2899615 x/sys/windows/svc/mgr: skip tests when we are not authorised to manage services
It seems some Windows versions requires you to be
an Administrator to manage services. Just google for
"openscmanager access denied windows 2008" or similar.

Fixes golang/go#11156.

Change-Id: I4b09d244a61179ece7a1319234e5c3199423cbe9
Reviewed-on: https://go-review.googlesource.com/10933
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-06-12 01:44:55 +00:00
Alex Brainman
65705ef9e9 windows/svc: add new package to help create and manage Windows services
Change-Id: I58bb446aaa387b31d8a9ff4217793a170b96a7e2
Reviewed-on: https://go-review.googlesource.com/9104
Reviewed-by: Rob Pike <r@golang.org>
2015-05-01 05:26:56 +00:00