Exemplo n.º 1
0
// Returns nil on device error, missing entry on sysattr error
func (self Device) SysAttrs(sysAttrs ...string) map[string]string {
	udev := C.udev_new()
	defer C.udev_unref(udev)

	cSysPath := C.CString(self.SysPath)
	defer C.free(unsafe.Pointer(cSysPath))

	udev_device := C.udev_device_new_from_syspath(udev, cSysPath)
	if udev_device == nil {
		return nil
	}
	defer C.udev_device_unref(udev_device)

	// get
	out := make(map[string]string)

	for _, sysAttr := range sysAttrs {
		cSysAttr := C.CString(sysAttr)
		defer C.free(unsafe.Pointer(cSysAttr))

		cValue := C.udev_device_get_sysattr_value(udev_device, cSysAttr)
		if cValue == nil {
			continue
		}

		out[sysAttr] = C.GoString(cValue)
	}

	return out
}
Exemplo n.º 2
0
func (self *Monitor) Close() {
	C.udev_monitor_unref(self.udev_monitor)
	C.udev_unref(self.udev)

	self.udev_monitor = nil
	self.udev = nil
}
Exemplo n.º 3
0
func MonitorUdev(subsystem string) (*Monitor, error) {
	cName := C.CString("udev")
	defer C.free(unsafe.Pointer(cName))

	udev := C.udev_new()
	udev_monitor := C.udev_monitor_new_from_netlink(udev, cName)

	if udev_monitor == nil {
		C.udev_unref(udev)
		return nil, Error{"udev_monitor_new_from_netlink"}
	}

	if subsystem != "" {
		cSubsystem := C.CString(subsystem)
		defer C.free(unsafe.Pointer(cSubsystem))

		if C.udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, cSubsystem, nil) < 0 {
			C.udev_monitor_unref(udev_monitor)
			C.udev_unref(udev)
			return nil, Error{"udev_monitor_filter_add_match_subsystem_devtype"}
		}
	}

	if C.udev_monitor_enable_receiving(udev_monitor) < 0 {
		C.udev_monitor_unref(udev_monitor)
		C.udev_unref(udev)
		return nil, Error{"udev_monitor_enable_receiving"}
	}

	// set blocking
	if fd := C.udev_monitor_get_fd(udev_monitor); fd < 0 {
		C.udev_monitor_unref(udev_monitor)
		C.udev_unref(udev)
		return nil, Error{"udev_monitor_get_fd"}
	} else if err := syscall.SetNonblock(int(fd), false); err != nil {
		C.udev_monitor_unref(udev_monitor)
		C.udev_unref(udev)
		return nil, err
	}

	return &Monitor{udev, udev_monitor}, nil
}
Exemplo n.º 4
0
/*
 * Filter by match fields:
 *  * Subsystem
 */
func Enumerate(match Device) ([]Device, error) {
	udev := C.udev_new()
	defer C.udev_unref(udev)

	udev_enumerate := C.udev_enumerate_new(udev)
	defer C.udev_enumerate_unref(udev_enumerate)

	if match.Subsystem != "" {
		// libudev strdup's the string
		cSubsystem := C.CString(match.Subsystem)
		defer C.free(unsafe.Pointer(cSubsystem))

		if C.udev_enumerate_add_match_subsystem(udev_enumerate, cSubsystem) < 0 {
			return nil, Error{"udev_enumerate_add_match_subsystem"}
		}
	}

	if C.udev_enumerate_scan_devices(udev_enumerate) < 0 {
		return nil, Error{"udev_enumerate_scan_devices"}
	}

	// list
	var devices []Device

	udev_list_entry := C.udev_enumerate_get_list_entry(udev_enumerate)
	for ; udev_list_entry != nil; udev_list_entry = C.udev_list_entry_get_next(udev_list_entry) {
		udev_device := C.udev_device_new_from_syspath(udev, C.udev_list_entry_get_name(udev_list_entry))
		if udev_device == nil {
			return nil, Error{"udev_device_new_from_syspath"}
		}
		defer C.udev_device_unref(udev_device)

		var device Device

		if err := device.fromUdev(udev_device); err != nil {
			return nil, err
		} else {
			devices = append(devices, device)
		}
	}

	return devices, nil
}
Exemplo n.º 5
0
func (self Device) ParentWithSubsystemDevType(subsystem string, devtype string) (device Device, err error) {
	udev := C.udev_new()
	defer C.udev_unref(udev)

	cSysPath := C.CString(self.SysPath)
	defer C.free(unsafe.Pointer(cSysPath))

	udev_device := C.udev_device_new_from_syspath(udev, cSysPath)
	if udev_device == nil {
		return device, Error{"udev_device_new_from_syspath"}
	}
	defer C.udev_device_unref(udev_device)

	// parent
	cSubsystem := C.CString(subsystem)
	defer C.free(unsafe.Pointer(cSubsystem))

	var cDevType *C.char
	if devtype != "" {
		cDevType = C.CString(devtype)
		defer C.free(unsafe.Pointer(cDevType))
	}

	udev_parent := C.udev_device_get_parent_with_subsystem_devtype(udev_device, cSubsystem, cDevType)
	if udev_parent == nil {
		return device, Error{"udev_device_get_parent"}
	}
	// lifetime tied to udev_device

	// return
	if err := device.fromUdev(udev_parent); err != nil {
		return device, err
	}

	return device, nil
}
Exemplo n.º 6
0
Arquivo: udev.go Projeto: jessta/udev
func (u Udev) Unref() {
	C.udev_unref(u.ptr)
}
Exemplo n.º 7
0
func udevUnref(u *Udev) {
	C.udev_unref(u.ptr)
}