File: udev.go

package info (click to toggle)
snapd 2.49-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 36,432 kB
  • sloc: ansic: 12,125; sh: 8,453; python: 2,163; makefile: 1,284; exp: 173; xml: 22
file content (111 lines) | stat: -rw-r--r-- 4,441 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2016-2018 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package udev

import (
	"fmt"
	"os/exec"
)

// reloadRules runs three commands that reload udev rule database.
//
// The commands are: udevadm control --reload-rules
//                   udevadm trigger --subsystem-nomatch=input
//                   udevadm settle --timeout=3
// and optionally trigger other subsystems as defined in the interfaces. Eg:
//                   udevadm trigger --subsystem-match=input
//                   udevadm trigger --property-match=ID_INPUT_JOYSTICK=1
func (b *Backend) reloadRules(subsystemTriggers []string) error {
	if b.preseed {
		return nil
	}

	output, err := exec.Command("udevadm", "control", "--reload-rules").CombinedOutput()
	if err != nil {
		return fmt.Errorf("cannot reload udev rules: %s\nudev output:\n%s", err, string(output))
	}

	// By default, trigger for all events except the input subsystem since
	// it can cause noticeable blocked input on, for example, classic
	// desktop.
	output, err = exec.Command("udevadm", "trigger", "--subsystem-nomatch=input").CombinedOutput()
	if err != nil {
		return fmt.Errorf("cannot run udev triggers: %s\nudev output:\n%s", err, string(output))
	}

	// FIXME: track if also should trigger the joystick property if it
	// wasn't already since we are not able to detect interfaces that are
	// removed and set subsystemTriggers correctly. When we can, remove
	// this. Allows joysticks to be removed from the device cgroup on
	// interface disconnect.
	inputJoystickTriggered := false

	for _, subsystem := range subsystemTriggers {
		if subsystem == "input/joystick" {
			// If one of the interfaces said it uses the input
			// subsystem for joysticks, then trigger the joystick
			// events in a way that is specific to joysticks to not
			// block other inputs.
			output, err = exec.Command("udevadm", "trigger", "--property-match=ID_INPUT_JOYSTICK=1").CombinedOutput()
			if err != nil {
				return fmt.Errorf("cannot run udev triggers for joysticks: %s\nudev output:\n%s", err, string(output))
			}
			inputJoystickTriggered = true
		} else if subsystem == "input/key" {
			// If one of the interfaces said it uses the input
			// subsystem for input keys, then trigger the keys
			// events in a way that is specific to input keys
			// to not block other inputs.
			output, err = exec.Command("udevadm", "trigger", "--property-match=ID_INPUT_KEY=1", "--property-match=ID_INPUT_KEYBOARD!=1").CombinedOutput()
			if err != nil {
				return fmt.Errorf("cannot run udev triggers for keys: %s\nudev output:\n%s", err, string(output))
			}
		} else if subsystem != "" {
			// If one of the interfaces said it uses a subsystem,
			// then do it too.
			output, err = exec.Command("udevadm", "trigger", "--subsystem-match="+subsystem).CombinedOutput()
			if err != nil {
				return fmt.Errorf("cannot run udev triggers for %s subsystem: %s\nudev output:\n%s", subsystem, err, string(output))
			}

			if subsystem == "input" {
				inputJoystickTriggered = true
			}
		}
	}

	// FIXME: if not already triggered, trigger the joystick property if it
	// wasn't already since we are not able to detect interfaces that are
	// removed and set subsystemTriggers correctly. When we can, remove
	// this. Allows joysticks to be removed from the device cgroup on
	// interface disconnect.
	if !inputJoystickTriggered {
		output, err = exec.Command("udevadm", "trigger", "--property-match=ID_INPUT_JOYSTICK=1").CombinedOutput()
		if err != nil {
			return fmt.Errorf("cannot run udev triggers for joysticks: %s\nudev output:\n%s", err, string(output))
		}
	}

	// give our triggered events a chance to be handled before exiting.
	// Ignore errors since we don't want to error on still pending events.
	_ = exec.Command("udevadm", "settle", "--timeout=10").Run()

	return nil
}