]>
Commit | Line | Data |
---|---|---|
8e15f01d GKH |
1 | #include <stdlib.h> |
2 | #include <string.h> | |
3 | #include <stdio.h> | |
4 | #include <fcntl.h> | |
5 | #include <unistd.h> | |
6 | #include <errno.h> | |
7 | #include <ctype.h> | |
8 | ||
7ac0feeb GKH |
9 | #define DBUS_API_SUBJECT_TO_CHANGE |
10 | #include <dbus/dbus.h> | |
11 | ||
8e15f01d GKH |
12 | #include "udev.h" |
13 | #include "udev_version.h" | |
7ac0feeb | 14 | #include "udev_dbus.h" |
ff2c16f0 | 15 | #include "logging.h" |
7ac0feeb GKH |
16 | |
17 | ||
18 | /** variable for the connection the to system message bus or #NULL | |
19 | * if we cannot connect or acquire the org.kernel.udev service | |
20 | */ | |
21 | static DBusConnection* sysbus_connection; | |
22 | ||
23 | /** Disconnect from the system message bus */ | |
24 | void sysbus_disconnect(void) | |
25 | { | |
8e15f01d GKH |
26 | if (sysbus_connection == NULL) |
27 | return; | |
7ac0feeb | 28 | |
8e15f01d GKH |
29 | dbus_connection_disconnect(sysbus_connection); |
30 | sysbus_connection = NULL; | |
7ac0feeb GKH |
31 | } |
32 | ||
33 | /** Connect to the system message bus */ | |
34 | void sysbus_connect(void) | |
35 | { | |
8e15f01d GKH |
36 | DBusError error; |
37 | ||
38 | /* Connect to a well-known bus instance, the system bus */ | |
39 | dbus_error_init(&error); | |
40 | sysbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); | |
41 | if (sysbus_connection == NULL) { | |
42 | dbg("cannot connect to system message bus, error %s: %s", | |
43 | error.name, error.message); | |
44 | dbus_error_free(&error); | |
45 | return; | |
46 | } | |
47 | ||
48 | /* Acquire the org.kernel.udev service such that listeners | |
49 | * know that the message is really from us and not from a | |
50 | * random attacker. See the file udev_sysbus_policy.conf for | |
51 | * details. | |
52 | * | |
53 | * Note that a service can have multiple owners (though there | |
54 | * is a concept of a primary owner for reception of messages) | |
55 | * so no race is introduced if two copies of udev is running | |
56 | * at the same time. | |
57 | */ | |
58 | dbus_bus_acquire_service(sysbus_connection, "org.kernel.udev", 0, | |
59 | &error); | |
60 | if (dbus_error_is_set(&error)) { | |
61 | printf("cannot acquire org.kernel.udev service, error %s: %s'", | |
62 | error.name, error.message); | |
63 | sysbus_disconnect(); | |
64 | return; | |
65 | } | |
7ac0feeb GKH |
66 | } |
67 | ||
68 | ||
69 | /** Send out a signal that a device node is created | |
70 | * | |
71 | * @param dev udevice object | |
72 | * @param path Sysfs path of device | |
73 | */ | |
74 | void sysbus_send_create(struct udevice *dev, const char *path) | |
75 | { | |
8e15f01d GKH |
76 | char filename[255]; |
77 | DBusMessage* message; | |
78 | DBusMessageIter iter; | |
79 | ||
80 | if (sysbus_connection == NULL) | |
81 | return; | |
82 | ||
c472e3c8 KS |
83 | strfieldcpy(filename, udev_root); |
84 | strfieldcat(filename, dev->name); | |
8e15f01d GKH |
85 | |
86 | /* object, interface, member */ | |
87 | message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor", | |
88 | "org.kernel.udev.NodeMonitor", | |
89 | "NodeCreated"); | |
90 | ||
91 | dbus_message_iter_init(message, &iter); | |
92 | dbus_message_iter_append_string(&iter, filename); | |
93 | dbus_message_iter_append_string(&iter, path); | |
94 | ||
95 | if ( !dbus_connection_send(sysbus_connection, message, NULL) ) | |
96 | dbg("error sending d-bus signal"); | |
97 | ||
98 | dbus_message_unref(message); | |
99 | ||
100 | dbus_connection_flush(sysbus_connection); | |
7ac0feeb GKH |
101 | } |
102 | ||
103 | /** Send out a signal that a device node is deleted | |
104 | * | |
105 | * @param name Name of the device node, e.g. /udev/sda1 | |
106 | * @param path Sysfs path of device | |
107 | */ | |
108 | void sysbus_send_remove(const char* name, const char *path) | |
109 | { | |
8e15f01d GKH |
110 | char filename[255]; |
111 | DBusMessage* message; | |
112 | DBusMessageIter iter; | |
113 | ||
114 | if (sysbus_connection == NULL) | |
115 | return; | |
116 | ||
c472e3c8 KS |
117 | strfieldcpy(filename, udev_root); |
118 | strfieldcat(filename, name); | |
8e15f01d GKH |
119 | |
120 | /* object, interface, member */ | |
121 | message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor", | |
122 | "org.kernel.udev.NodeMonitor", | |
123 | "NodeDeleted"); | |
7ac0feeb | 124 | |
8e15f01d GKH |
125 | dbus_message_iter_init(message, &iter); |
126 | dbus_message_iter_append_string(&iter, filename); | |
127 | dbus_message_iter_append_string(&iter, path); | |
7ac0feeb | 128 | |
8e15f01d GKH |
129 | if ( !dbus_connection_send(sysbus_connection, message, NULL) ) |
130 | dbg("error sending d-bus signal"); | |
131 | ||
132 | dbus_message_unref(message); | |
133 | ||
134 | dbus_connection_flush(sysbus_connection); | |
135 | } |