【问题标题】:Add and generate custom system calls in golang在 golang 中添加和生成自定义系统调用
【发布时间】:2019-05-10 12:01:05
【问题描述】:

我想将系统调用 GetDiskFreeSpaceExW 添加到 go 系统调用并扩展 hcsshim 当前可用的系统调用:https://github.com/microsoft/hcsshim/blob/master/zsyscall_windows.go

我看到有一种生成它们的方法,但仍然不清楚添加另一个 DLL 的语法是什么以及必须使用哪些结构类型。

当前 hcs.go 用于生成 hcsshim 的系统调用:

// Shim for the Host Compute Service (HCS) to manage Windows Server
// containers and Hyper-V containers.

package hcs

import (
    "syscall"
)

//go:generate go run ../../mksyscall_windows.go -output zsyscall_windows.go hcs.go

//sys hcsEnumerateComputeSystems(query string, computeSystems **uint16, result **uint16) (hr error) = vmcompute.HcsEnumerateComputeSystems?
//sys hcsCreateComputeSystem(id string, configuration string, identity syscall.Handle, computeSystem *hcsSystem, result **uint16) (hr error) = vmcompute.HcsCreateComputeSystem?
//sys hcsOpenComputeSystem(id string, computeSystem *hcsSystem, result **uint16) (hr error) = vmcompute.HcsOpenComputeSystem?
//sys hcsCloseComputeSystem(computeSystem hcsSystem) (hr error) = vmcompute.HcsCloseComputeSystem?
//sys hcsStartComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsStartComputeSystem?
//sys hcsShutdownComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsShutdownComputeSystem?
//sys hcsTerminateComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsTerminateComputeSystem?
//sys hcsPauseComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsPauseComputeSystem?
//sys hcsResumeComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsResumeComputeSystem?
//sys hcsGetComputeSystemProperties(computeSystem hcsSystem, propertyQuery string, properties **uint16, result **uint16) (hr error) = vmcompute.HcsGetComputeSystemProperties?
//sys hcsModifyComputeSystem(computeSystem hcsSystem, configuration string, result **uint16) (hr error) = vmcompute.HcsModifyComputeSystem?
//sys hcsRegisterComputeSystemCallback(computeSystem hcsSystem, callback uintptr, context uintptr, callbackHandle *hcsCallback) (hr error) = vmcompute.HcsRegisterComputeSystemCallback?
//sys hcsUnregisterComputeSystemCallback(callbackHandle hcsCallback) (hr error) = vmcompute.HcsUnregisterComputeSystemCallback?

//sys hcsCreateProcess(computeSystem hcsSystem, processParameters string, processInformation *hcsProcessInformation, process *hcsProcess, result **uint16) (hr error) = vmcompute.HcsCreateProcess?
//sys hcsOpenProcess(computeSystem hcsSystem, pid uint32, process *hcsProcess, result **uint16) (hr error) = vmcompute.HcsOpenProcess?
//sys hcsCloseProcess(process hcsProcess) (hr error) = vmcompute.HcsCloseProcess?
//sys hcsTerminateProcess(process hcsProcess, result **uint16) (hr error) = vmcompute.HcsTerminateProcess?
//sys hcsSignalProcess(process hcsProcess, options string, result **uint16) (hr error) = vmcompute.HcsSignalProcess?
//sys hcsGetProcessInfo(process hcsProcess, processInformation *hcsProcessInformation, result **uint16) (hr error) = vmcompute.HcsGetProcessInfo?
//sys hcsGetProcessProperties(process hcsProcess, processProperties **uint16, result **uint16) (hr error) = vmcompute.HcsGetProcessProperties?
//sys hcsModifyProcess(process hcsProcess, settings string, result **uint16) (hr error) = vmcompute.HcsModifyProcess?
//sys hcsGetServiceProperties(propertyQuery string, properties **uint16, result **uint16) (hr error) = vmcompute.HcsGetServiceProperties?
//sys hcsRegisterProcessCallback(process hcsProcess, callback uintptr, context uintptr, callbackHandle *hcsCallback) (hr error) = vmcompute.HcsRegisterProcessCallback?
//sys hcsUnregisterProcessCallback(callbackHandle hcsCallback) (hr error) = vmcompute.HcsUnregisterProcessCallback?

//sys hcsGetDiskFreeSpaceExW(string lpDirectoryName, **uint16 lpFreeBytesAvailableToCaller, **uint16 lpTotalNumberOfBytes, **uint16 lpTotalNumberOfFreeBytes) = 

type hcsSystem syscall.Handle
type hcsProcess syscall.Handle
type hcsCallback syscall.Handle

type hcsProcessInformation struct {
    ProcessId uint32
    Reserved  uint32
    StdInput  syscall.Handle
    StdOutput syscall.Handle
    StdError  syscall.Handle
}

因此,我希望有一个有效的系统调用来获取磁盘使用情况和磁盘空间。

【问题讨论】:

  • 你的链接很好,到目前为止找到了类似的代码,谢谢。澄清我的问题。我想获取系统调用以获取要生成的磁盘使用情况,就像上面的函数原型一样,将其集成到现有的代码库中。

标签: windows go wmi monitoring system-calls


【解决方案1】:

像这样创建一个文件:

//go:generate mkwinsyscall -output zfree.go free.go
//sys getDiskFreeSpace(directoryName string, availableToCaller *int, numberOfBytes *int, numberOfFreeBytes *int) (err error) = GetDiskFreeSpaceExW
package main

func main() {
   var availableToCaller, numberOfBytes, numberOfFreeBytes int
   getDiskFreeSpace(
      `C:\`, &availableToCaller, &numberOfBytes, &numberOfFreeBytes,
   )
   println("availableToCaller", availableToCaller)
   println("numberOfBytes", numberOfBytes)
   println("numberOfFreeBytes", numberOfFreeBytes)
}

然后构建[1]:

go mod init free
go generate
go mod tidy
go build

或者,您可以使用预制包装器 [2],但我更喜欢我的,因为您不必担心字符串转换。

  1. https://github.com/golang/sys/tree/master/windows/mkwinsyscall
  2. https://pkg.go.dev/golang.org/x/sys/windows#GetDiskFreeSpaceEx

【讨论】:

    猜你喜欢
    • 2017-10-29
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2012-10-04
    • 2018-08-01
    • 1970-01-01
    相关资源
    最近更新 更多