Skip to content

wadey/systemconfiguration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

systemconfig

systemconfig is a cgo-free Go interface to macOS/Darwin system frameworks (CoreFoundation and SystemConfiguration.framework).

It directly invokes macOS system symbols using Go's internal linker directives (//go:cgo_import_dynamic) and assembly trampolines, enabling compilation and execution even when CGO is disabled (CGO_ENABLED=0).

Features

  • Dynamic Store Client: Establish sessions to the macOS System Configuration registry to query and manage configuration keys.
  • Key List Matching: Copy and filter keys using regex patterns.
  • Typed Operations: Set, update, read, and delete keys using standard Go types (string, []string, error).
  • Fully Cgo-Free: No C compilers or headers are needed to build, and it can compile natively under default Darwin targets.

Usage

Connecting to the Dynamic Store & Managing Keys

package main

import (
	"fmt"
	"log"

	"github.com/wadey/systemconfig"
)

func main() {
	// Create a new dynamic store session
	store, err := systemconfig.NewDynamicStore("MyApp")
	if err != nil {
		log.Fatalf("Failed to connect: %v", err)
	}
	defer store.Close()

	// List all global IPv4 configuration keys
	keys, err := store.CopyKeyList("Setup:/Network/Global/.*")
	if err != nil {
		log.Fatalf("Failed to copy keys: %v", err)
	}
	fmt.Println("Found configuration keys:", keys)

	// Read a configuration string
	val, err := store.GetStringValue("Setup:/Network/Global/IPv4")
	if err != nil {
		log.Fatalf("Failed to get value: %v", err)
	}
	fmt.Printf("Global IPv4 config ID: %q\n", val)
}

How It Works

The primary motivation for this library is to allow code that interacts with the macOS System Configuration registry to compile successfully in pure-Go environments where CGO is disabled (CGO_ENABLED=0).

Normally, any code referencing macOS framework APIs requires a C compiler and standard CGO, which prevents cross-compilation and makes it impossible to build clean static binaries. By using Go's internal dynamic import mechanism and assembly trampolines, this library allows the compiler to resolve dynamic system symbols at link-time without needing CGO enabled.

This technique is adapted from the Go standard library (crypto/x509/internal/macos), where Go uses the exact same dynamic loading approach to verify certificates against the native macOS trust store in cgo-disabled configurations.

This library avoids cgo entirely by:

  1. Declaring runtime imports to native symbols in CoreFoundation and SystemConfiguration:
    //go:cgo_import_dynamic sc_SCDynamicStoreCopyValue SCDynamicStoreCopyValue "/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration"
  2. Bridging dynamic symbols to Go package symbols via custom assembly trampolines:
    TEXT ·sc_SCDynamicStoreCopyValue_trampoline(SB),NOSPLIT,$0-0
        JMP sc_SCDynamicStoreCopyValue(SB)
  3. Mapping the local execution helper to the Go runtime's internal dynamic syscall handler using //go:linkname.

License

Licensed under the BSD-style License. See the LICENSE details in the standard Go template.

About

cgo-less Go library to talk to SystemConfiguration.framework

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors