Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke committed Nov 2, 2024
1 parent 36ee09d commit a7534d4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
10 changes: 1 addition & 9 deletions internal/mi/mi_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,9 @@ func Benchmark_MI_Query_Unmarshal(b *testing.B) {
var processes []win32Process

for i := 0; i < b.N; i++ {
operation, err := session.QueryInstances(mi.OperationFlagsStandardRTTI, nil, mi.NamespaceRootCIMv2, mi.QueryDialectWQL, "SELECT Name FROM Win32_Process WHERE Handle = 0 OR Handle = 4")

require.NoError(b, err)
require.NotEmpty(b, operation)

err = operation.Unmarshal(&processes)
err := session.QueryUnmarshal(&processes, mi.OperationFlagsStandardRTTI, nil, mi.NamespaceRootCIMv2, mi.QueryDialectWQL, "SELECT Name FROM Win32_Process WHERE Handle = 0 OR Handle = 4")
require.NoError(b, err)
require.Equal(b, []win32Process{{Name: "System Idle Process"}, {Name: "System"}}, processes)

err = operation.Close()
require.NoError(b, err)
}

b.StopTimer()
Expand Down
3 changes: 1 addition & 2 deletions internal/mi/mi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ func Test_MI_QueryUnmarshal(t *testing.T) {

var processes []win32Process

err = session.QueryUnmarshal(mi.OperationFlagsStandardRTTI, nil, mi.NamespaceRootCIMv2, mi.QueryDialectWQL,
"select Name from win32_process where handle = 0", &processes)
err = session.QueryUnmarshal(&processes, mi.OperationFlagsStandardRTTI, nil, mi.NamespaceRootCIMv2, mi.QueryDialectWQL, "select Name from win32_process where handle = 0")

require.NoError(t, err)
require.Equal(t, []win32Process{{Name: "System Idle Process"}}, processes)
Expand Down
23 changes: 9 additions & 14 deletions internal/mi/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"reflect"
"syscall"
"time"
"unsafe"

"golang.org/x/sys/windows"
Expand Down Expand Up @@ -172,8 +171,9 @@ func (s *Session) QueryInstances(flags OperationFlags, operationOptions *Operati
// QueryUnmarshal queries for a set of instances based on a query expression.
//
// https://learn.microsoft.com/en-us/windows/win32/api/mi/nf-mi-mi_session_queryinstances
func (s *Session) QueryUnmarshal(flags OperationFlags, operationOptions *OperationOptions, namespaceName Namespace,
queryDialect QueryDialect, queryExpression string, dst any,
func (s *Session) QueryUnmarshal(dst any,
flags OperationFlags, operationOptions *OperationOptions,
namespaceName Namespace, queryDialect QueryDialect, queryExpression string,
) error {
if s == nil || s.ft == nil {
return ErrNotInitialized
Expand Down Expand Up @@ -315,23 +315,18 @@ func (s *Session) QueryUnmarshal(flags OperationFlags, operationOptions *Operati

var errs []error

for {
select {
case err := <-errCh:
if err != nil {
errs = append(errs, err)
}

return errors.Join(errs...)
case <-time.After(10 * time.Second):
return errors.New("timeout")
for err := range errCh {
if err != nil {
errs = append(errs, err)
}
}

return errors.Join(errs...)
}

// Query queries for a set of instances based on a query expression.
func (s *Session) Query(dst any, namespaceName Namespace, queryExpression string) error {
err := s.QueryUnmarshal(OperationFlagsStandardRTTI, nil, namespaceName, QueryDialectWQL, queryExpression, dst)
err := s.QueryUnmarshal(dst, OperationFlagsStandardRTTI, nil, namespaceName, QueryDialectWQL, queryExpression)
if err != nil {
return fmt.Errorf("WMI query failed: %w", err)
}
Expand Down

0 comments on commit a7534d4

Please sign in to comment.