Skip to content

p-x9/swift-sysctl

Repository files navigation

swift-sysctl

A Swift wrapper for sysctl.

Github issues Github forks Github stars Github top language

Usage

The basic functions are defined in the Sysctl namespace.

Access

You can access specific values in the same way as for an oid name, in a chain of name, as follows:

// "kern.osversion"
let osVersion = Sysctl.sysctl(kern.osversion)

// "machdep.cpu.vendor"
let cpuVendorName = Sysctl.sysctl(machdep.cpu.vendor)

Each field also holds the type information of the value associated with its OID. Therefore, in the above example, the value is automatically obtained as a String.

(See Node directory)

Note

Some values cannot be read without root permission.

Note

OIDs that exist vary greatly depending on the CPU architecture. Some OIDs may not exist except on macOS.

Implementation

The following is a brief description of the implementation.

OID

In the OID directory, the name, id, and value type information of the OID for each node are defined.

Node

The OIDs provided to sysctl are structured as a tree. Each node in the tree is defined in the Node directory.

If a node has children, the aggregate type of the child nodes is retained as Node<Child> type.

public let ipc = Node<Ipc>(
    oid: OID.Kern.ipc
)

If it is a terminal node, it has type information for the value associated with the OID.

public let ostype = LeafNode<String>(
    oid: OID.Kern.ostype
)

Field

Field is the path traced from the root node to the terminal node. (The root node is defined in TopNodes.swift.)

From the root node to the terminal node, it is possible to access the nodes by dot-connecting.

let field: Field<String> = kern.ostype
let field2: Field<CInt> = kern.argmax

The value can be retrieved by giving this Field to the Sysctl.sysctl function.

let ostype: String = Sysctl.sysctl(field)
let argmax: CInt = Sysctl.sysctl(field)

License

swift-sysctl is released under the MIT License. See LICENSE