udev

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 15 Imported by: 0

README

Userspace Devices (udev)

This package serves as a substitute for Linux's udev when you're operating a desktop environment within a container.

Note, most of the code for this package was modified and copied from: pilebones' udev.

Overview

In traditional host operating systems, udev is responsible for device management and permission control through a userspace process. However, containers usually do not run their own udev instances.

Another key challenge is that kernel events are global and not restricted to a specific namespace. When utilizing udev to emulate virtual peripherals like gamepads, mice, or keyboards, those devices become visible on the host system. Running individual udev instances for each container would result in each of these instances detecting events across all containers, thereby creating devices indiscriminately. This package aims to confine device creation to the specific container in which it operates.

Using a standalone udev instance within the container is not a foolproof solution either. Certain applications are designed to look for devices under specific names (e.g., /dev/input/js0-4). Employing udev to merely filter the devices established by the host system does not guarantee device name consistency. This is because another container might already have initialized the js0-4 devices, causing devices within our container to be assigned different names and, consequently, remain undetected.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToStringUEvent

func ToStringUEvent(evt UEvent) string

Types

type ConnectionMode

type ConnectionMode int
const (
	KERNEL ConnectionMode = 1
	UDEV   ConnectionMode = 2
)

type Device

type Device struct {
	OriginalId int
	Id         int
	KObj       string
	Env        map[string]string
	Major      int16
	Minor      int16
	DevPath    string
	DeviceType DeviceType
	// contains filtered or unexported fields
}

func (*Device) CleanupUDevDatabaseData

func (d *Device) CleanupUDevDatabaseData() error

func (*Device) Close

func (d *Device) Close() error

func (*Device) EmitUDevEvent

func (d *Device) EmitUDevEvent(action KObjAction) error

func (*Device) GetSysPath

func (d *Device) GetSysPath() string

func (*Device) GetUDevDBDevicePath

func (d *Device) GetUDevDBDevicePath() string

func (*Device) GetUDevDBInputPath

func (d *Device) GetUDevDBInputPath() string

func (*Device) Initialize

func (d *Device) Initialize(udev *UDev)

* This function should be called after all of the public properties of the device have been set.

func (*Device) MakeDeviceNode

func (d *Device) MakeDeviceNode()

func (*Device) WriteUDevDatabaseData

func (d *Device) WriteUDevDatabaseData()

type DeviceType

type DeviceType int
const (
	KEYBOARD DeviceType = iota
	MOUSE
	TOUCHSCREEN
	GAMEPAD
)

type KObjAction

type KObjAction string
const (
	ADD     KObjAction = "add"
	REMOVE  KObjAction = "remove"
	CHANGE  KObjAction = "change"
	MOVE    KObjAction = "move"
	ONLINE  KObjAction = "online"
	OFFLINE KObjAction = "offline"
	BIND    KObjAction = "bind"
	UNBIND  KObjAction = "unbind"
)

func ParseKObjAction

func ParseKObjAction(raw string) (a KObjAction, err error)

func (KObjAction) String

func (a KObjAction) String() string

type NetlinkConnection

type NetlinkConnection struct {
	Fd   int
	Addr syscall.SockaddrNetlink
	Mode ConnectionMode
	// contains filtered or unexported fields
}

func NewNetlinkConnection

func NewNetlinkConnection(ctx context.Context, mode ConnectionMode) *NetlinkConnection

func (*NetlinkConnection) Close

func (c *NetlinkConnection) Close() error

Close allow to close file descriptor and socket bound

func (*NetlinkConnection) Connect

func (c *NetlinkConnection) Connect() error

func (*NetlinkConnection) ReadMsg

func (c *NetlinkConnection) ReadMsg() (msg []byte, err error)

ReadMsg allow to read an entire uevent msg

func (*NetlinkConnection) ReadUEvent

func (c *NetlinkConnection) ReadUEvent() (*UEvent, error)

ReadMsg allow to read an entire uevent msg

func (*NetlinkConnection) WriteUEvent

func (c *NetlinkConnection) WriteUEvent(event UEvent) (err error)

type UDev

type UDev struct {

	// Used to listen to events from udev
	UDevEvents *eventemitter.Emitter
	// Used to listen to events from the Kernel
	KernelEvents *eventemitter.Emitter
	// contains filtered or unexported fields
}

func NewUDev

func NewUDev(ctx context.Context) *UDev

func (*UDev) Close

func (u *UDev) Close() error

func (*UDev) Open

func (udev *UDev) Open() error

func (*UDev) WriteUDevEvent

func (u *UDev) WriteUDevEvent(evt UEvent) error

type UEvent

type UEvent struct {
	Action KObjAction
	KObj   string
	Env    map[string]string
}

func ParseUEvent

func ParseUEvent(raw []byte) (e *UEvent, err error)

func (UEvent) Bytes

func (e UEvent) Bytes() []byte

func (UEvent) Equal

func (e UEvent) Equal(e2 UEvent) (bool, error)

func (UEvent) String

func (e UEvent) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL