Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
26add8e6
G
Gopsutil
项目概览
Canread
/
Gopsutil
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gopsutil
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
26add8e6
编写于
5月 21, 2018
作者:
P
pytimer
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[windows]service: add windows service feature
上级
eeb1d38d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
215 addition
and
0 deletion
+215
-0
winservices/manager.go
winservices/manager.go
+32
-0
winservices/winservices.go
winservices/winservices.go
+126
-0
winservices/zsyscall_windows_ex.go
winservices/zsyscall_windows_ex.go
+57
-0
未找到文件。
winservices/manager.go
0 → 100644
浏览文件 @
26add8e6
// +build windows
package
winservices
import
(
"golang.org/x/sys/windows/svc/mgr"
)
type
scmanager
struct
{
mgr
*
mgr
.
Mgr
}
func
openSCManager
()
(
*
scmanager
,
error
)
{
m
,
err
:=
mgr
.
Connect
()
if
err
!=
nil
{
return
nil
,
err
}
return
&
scmanager
{
m
},
nil
}
func
(
sc
*
scmanager
)
close
()
error
{
return
sc
.
mgr
.
Disconnect
()
}
func
getService
(
serviceName
string
)
(
*
mgr
.
Service
,
error
)
{
m
,
err
:=
openSCManager
()
if
err
!=
nil
{
return
nil
,
err
}
defer
m
.
close
()
return
m
.
mgr
.
OpenService
(
serviceName
)
}
winservices/winservices.go
0 → 100644
浏览文件 @
26add8e6
// +build windows
package
winservices
import
(
"context"
"unsafe"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)
// Service represent a windows service.
type
Service
struct
{
Name
string
Config
mgr
.
Config
Status
ServiceStatus
srv
*
mgr
.
Service
}
// ServiceStatus combines State and Accepted commands to fully describe running service.
type
ServiceStatus
struct
{
State
svc
.
State
Accepts
svc
.
Accepted
Pid
uint32
Win32ExitCode
uint32
}
// NewService create and return a windows Service
func
NewService
(
name
string
)
(
*
Service
,
error
)
{
// call windows service function need to OpenService handler,
// so first call func OpenService to get the specified service handler.
service
,
err
:=
getService
(
name
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
Service
{
Name
:
name
,
srv
:
service
,
},
nil
}
// GetServiceDetail get a windows service by name
func
(
s
*
Service
)
GetServiceDetail
()
error
{
return
s
.
GetServiceDetailWithContext
(
context
.
Background
())
}
// GetServiceDetailWithContext get a windows service by name
func
(
s
*
Service
)
GetServiceDetailWithContext
(
ctx
context
.
Context
)
error
{
config
,
err
:=
s
.
QueryServiceConfigWithContext
(
ctx
)
if
err
!=
nil
{
return
err
}
s
.
Config
=
config
status
,
err
:=
s
.
QueryStatusWithContext
(
ctx
)
if
err
!=
nil
{
return
err
}
s
.
Status
=
status
return
nil
}
// QueryServiceConfig return the specified service config
func
(
s
*
Service
)
QueryServiceConfig
()
(
mgr
.
Config
,
error
)
{
return
s
.
QueryServiceConfigWithContext
(
context
.
Background
())
}
// QueryServiceConfigWithContext call QueryServiceConfig() and QueryServiceConfig2()
// implement windows https://msdn.microsoft.com/en-us/library/windows/desktop/ms684932(v=vs.85).aspx
func
(
s
*
Service
)
QueryServiceConfigWithContext
(
ctx
context
.
Context
)
(
mgr
.
Config
,
error
)
{
return
s
.
srv
.
Config
()
}
// QueryStatus return the specified name service currentState and ControlsAccepted
func
(
s
*
Service
)
QueryStatus
()
(
ServiceStatus
,
error
)
{
return
s
.
QueryStatusWithContext
(
context
.
Background
())
}
// QueryStatusWithContext return the specified name service currentState and ControlsAccepted
func
(
s
*
Service
)
QueryStatusWithContext
(
ctx
context
.
Context
)
(
ServiceStatus
,
error
)
{
var
p
*
windows
.
SERVICE_STATUS_PROCESS
var
bytesNeeded
uint32
var
buf
[]
byte
if
err
:=
QueryServiceStatusEx
(
s
.
srv
.
Handle
,
SC_STATUS_PROCESS_INFO
,
nil
,
0
,
&
bytesNeeded
);
err
!=
windows
.
ERROR_INSUFFICIENT_BUFFER
{
return
ServiceStatus
{},
err
}
buf
=
make
([]
byte
,
bytesNeeded
)
p
=
(
*
windows
.
SERVICE_STATUS_PROCESS
)(
unsafe
.
Pointer
(
&
buf
[
0
]))
if
err
:=
QueryServiceStatusEx
(
s
.
srv
.
Handle
,
SC_STATUS_PROCESS_INFO
,
&
buf
[
0
],
uint32
(
len
(
buf
)),
&
bytesNeeded
);
err
!=
nil
{
return
ServiceStatus
{},
err
}
return
ServiceStatus
{
State
:
svc
.
State
(
p
.
CurrentState
),
Accepts
:
svc
.
Accepted
(
p
.
ControlsAccepted
),
Pid
:
p
.
ProcessId
,
Win32ExitCode
:
p
.
Win32ExitCode
,
},
nil
}
// ListServices return all windows service
// reference to golang.org/x/sys/windows/svc/mgr#ListServices()
func
ListServices
()
([]
Service
,
error
)
{
m
,
err
:=
openSCManager
()
if
err
!=
nil
{
return
nil
,
err
}
defer
m
.
close
()
names
,
err
:=
m
.
mgr
.
ListServices
()
if
err
!=
nil
{
return
nil
,
err
}
services
:=
make
([]
Service
,
0
)
for
_
,
name
:=
range
names
{
services
=
append
(
services
,
Service
{
Name
:
name
})
}
return
services
,
nil
}
winservices/zsyscall_windows_ex.go
0 → 100644
浏览文件 @
26add8e6
// +build windows
package
winservices
import
(
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
var
(
modadvapi32
=
windows
.
NewLazySystemDLL
(
"advapi32.dll"
)
procQueryServiceStatusEx
=
modadvapi32
.
NewProc
(
"QueryServiceStatusEx"
)
)
// Do the interface allocations only once for common
// Errno values.
const
(
errnoERROR_IO_PENDING
=
997
)
var
(
errERROR_IO_PENDING
error
=
syscall
.
Errno
(
errnoERROR_IO_PENDING
)
)
// errnoErr returns common boxed Errno values, to prevent
// allocations at runtime.
func
errnoErr
(
e
syscall
.
Errno
)
error
{
switch
e
{
case
0
:
return
nil
case
errnoERROR_IO_PENDING
:
return
errERROR_IO_PENDING
}
// TODO: add more here, after collecting data on the common
// error values see on Windows. (perhaps when running
// all.bat?)
return
e
}
// SC_STATUS_PROCESS_INFO reference to https://msdn.microsoft.com/en-us/library/windows/desktop/ms684941(v=vs.85).aspx,
// Use SC_STATUS_PROCESS_INFO to retrieve the service status information.
const
SC_STATUS_PROCESS_INFO
int
=
0
// QueryServiceStatusEx golang/sys/windows standard library can not implement QueryServiceStatusEx.
func
QueryServiceStatusEx
(
service
windows
.
Handle
,
infoLevel
int
,
buff
*
byte
,
buffSize
uint32
,
bytesNeeded
*
uint32
)
(
err
error
)
{
r1
,
_
,
e1
:=
syscall
.
Syscall6
(
procQueryServiceStatusEx
.
Addr
(),
5
,
uintptr
(
service
),
uintptr
(
infoLevel
),
uintptr
(
unsafe
.
Pointer
(
buff
)),
uintptr
(
buffSize
),
uintptr
(
unsafe
.
Pointer
(
bytesNeeded
)),
0
)
if
r1
==
0
{
if
e1
!=
0
{
err
=
errnoErr
(
e1
)
}
else
{
err
=
syscall
.
EINVAL
}
}
return
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录