]>
Commit | Line | Data |
---|---|---|
5cb5a6ff LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
25ac040b LP |
3 | #include <errno.h> |
4 | #include <libudev.h> | |
5 | ||
87f0e418 | 6 | #include "unit.h" |
5cb5a6ff LP |
7 | #include "device.h" |
8 | #include "strv.h" | |
25ac040b | 9 | #include "log.h" |
5cb5a6ff | 10 | |
87f0e418 LP |
11 | static void device_done(Unit *u) { |
12 | Device *d = DEVICE(u); | |
034c6ed7 LP |
13 | |
14 | assert(d); | |
25ac040b | 15 | free(d->sysfs); |
034c6ed7 LP |
16 | } |
17 | ||
87f0e418 | 18 | static void device_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff LP |
19 | |
20 | static const char* const state_table[_DEVICE_STATE_MAX] = { | |
21 | [DEVICE_DEAD] = "dead", | |
22 | [DEVICE_AVAILABLE] = "available" | |
23 | }; | |
24 | ||
25ac040b | 25 | Device *d = DEVICE(u); |
5cb5a6ff | 26 | |
25ac040b | 27 | assert(d); |
5cb5a6ff LP |
28 | |
29 | fprintf(f, | |
25ac040b LP |
30 | "%sDevice State: %s\n" |
31 | "%sSysfs Path: %s\n", | |
32 | prefix, state_table[d->state], | |
33 | prefix, d->sysfs); | |
34 | } | |
35 | ||
36 | static int device_add_escaped_name(Unit *u, const char *dn) { | |
37 | char *e; | |
38 | int r; | |
39 | ||
40 | assert(u); | |
41 | assert(dn); | |
42 | assert(dn[0] == '/'); | |
43 | ||
44 | if (!(e = unit_name_escape_path(dn+1, ".device"))) | |
45 | return -ENOMEM; | |
46 | ||
47 | r = unit_add_name(u, e); | |
48 | free(e); | |
49 | ||
50 | if (r < 0 && r != -EEXIST) | |
51 | return r; | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | static int device_process_device(Manager *m, struct udev_device *dev) { | |
57 | const char *dn, *names, *wants, *sysfs; | |
58 | Unit *u = NULL; | |
59 | int r; | |
60 | char *e, *w, *state; | |
61 | size_t l; | |
62 | bool delete; | |
63 | struct udev_list_entry *item = NULL, *first = NULL; | |
64 | ||
65 | assert(m); | |
66 | ||
67 | /* Check whether this entry is even relevant for us. */ | |
68 | dn = udev_device_get_devnode(dev); | |
69 | names = udev_device_get_property_value(dev, "SYSTEMD_NAMES"); | |
70 | wants = udev_device_get_property_value(dev, "SYSTEMD_WANTS"); | |
71 | ||
72 | if (!dn && !names && !wants) | |
73 | return 0; | |
74 | ||
75 | /* Ok, seems kinda interesting. Now, let's see if this one | |
76 | * already exists. */ | |
77 | ||
78 | if (!(sysfs = udev_device_get_syspath(dev))) | |
79 | return -ENOMEM; | |
80 | ||
81 | assert(sysfs[0] == '/'); | |
82 | if (!(e = unit_name_escape_path(sysfs+1, ".device"))) | |
83 | return -ENOMEM; | |
84 | ||
85 | if (!(u = manager_get_unit(m, e))) { | |
86 | const char *model; | |
87 | ||
88 | delete = true; | |
89 | ||
90 | if (!(u = unit_new(m))) { | |
91 | free(e); | |
92 | return -ENOMEM; | |
93 | } | |
94 | ||
95 | r = unit_add_name(u, e); | |
96 | free(e); | |
97 | ||
98 | if (r < 0) | |
99 | goto fail; | |
100 | ||
101 | if (!(DEVICE(u)->sysfs = strdup(sysfs))) { | |
102 | r = -ENOMEM; | |
103 | goto fail; | |
104 | } | |
105 | ||
106 | if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) || | |
107 | (model = udev_device_get_property_value(dev, "ID_MODEL"))) | |
108 | if (!(u->meta.description = strdup(model))) { | |
109 | r = -ENOMEM; | |
110 | goto fail; | |
111 | } | |
112 | ||
113 | } else { | |
114 | delete = false; | |
115 | free(e); | |
116 | } | |
117 | ||
118 | if (dn) | |
119 | if ((r = device_add_escaped_name(u, dn)) < 0) | |
120 | goto fail; | |
121 | ||
122 | first = udev_device_get_devlinks_list_entry(dev); | |
123 | udev_list_entry_foreach(item, first) | |
124 | if ((r = device_add_escaped_name(u, udev_list_entry_get_name(item))) < 0) | |
125 | goto fail; | |
126 | ||
127 | if (names) { | |
128 | FOREACH_WORD(w, l, names, state) { | |
129 | if (!(e = strndup(w, l))) | |
130 | goto fail; | |
131 | ||
132 | r = unit_add_name(u, e); | |
133 | free(e); | |
134 | ||
135 | if (r < 0 && r != -EEXIST) | |
136 | goto fail; | |
137 | } | |
138 | } | |
139 | ||
140 | ||
141 | if (set_isempty(u->meta.names)) { | |
142 | r = -EEXIST; | |
143 | goto fail; | |
144 | } | |
145 | ||
146 | if (wants) { | |
147 | FOREACH_WORD(w, l, wants, state) { | |
148 | if (!(e = strndup(w, l))) | |
149 | goto fail; | |
150 | ||
151 | r = unit_add_dependency_by_name(u, UNIT_WANTS, e); | |
152 | free(e); | |
153 | ||
154 | if (r < 0) | |
155 | goto fail; | |
156 | } | |
157 | } | |
158 | ||
159 | unit_add_to_load_queue(u); | |
160 | return 0; | |
161 | ||
162 | fail: | |
163 | if (delete && u) | |
164 | unit_free(u); | |
165 | return r; | |
166 | } | |
167 | ||
168 | static int device_process_path(Manager *m, const char *path) { | |
169 | int r; | |
170 | struct udev_device *dev; | |
171 | ||
172 | assert(m); | |
173 | assert(path); | |
174 | ||
175 | if (!(dev = udev_device_new_from_syspath(m->udev, path))) { | |
176 | log_warning("Failed to get udev device object from udev for path %s.", path); | |
177 | return -ENOMEM; | |
178 | } | |
179 | ||
180 | r = device_process_device(m, dev); | |
181 | udev_device_unref(dev); | |
182 | return r; | |
183 | } | |
184 | ||
185 | static void device_shutdown(Manager *m) { | |
186 | assert(m); | |
187 | ||
188 | if (m->udev) | |
189 | udev_unref(m->udev); | |
190 | } | |
191 | ||
192 | static int device_enumerate(Manager *m) { | |
193 | int r; | |
194 | struct udev_enumerate *e = NULL; | |
195 | struct udev_list_entry *item = NULL, *first = NULL; | |
196 | ||
197 | assert(m); | |
198 | ||
199 | if (!(m->udev = udev_new())) | |
200 | return -ENOMEM; | |
201 | ||
202 | if (!(e = udev_enumerate_new(m->udev))) { | |
203 | r = -ENOMEM; | |
204 | goto fail; | |
205 | } | |
206 | ||
207 | if (udev_enumerate_scan_devices(e) < 0) { | |
208 | r = -EIO; | |
209 | goto fail; | |
210 | } | |
211 | ||
212 | first = udev_enumerate_get_list_entry(e); | |
213 | udev_list_entry_foreach(item, first) | |
214 | device_process_path(m, udev_list_entry_get_name(item)); | |
215 | ||
216 | udev_enumerate_unref(e); | |
217 | ||
218 | return 0; | |
219 | ||
220 | fail: | |
221 | if (e) | |
222 | udev_enumerate_unref(e); | |
223 | ||
224 | device_shutdown(m); | |
225 | return r; | |
5cb5a6ff LP |
226 | } |
227 | ||
87f0e418 LP |
228 | static UnitActiveState device_active_state(Unit *u) { |
229 | return DEVICE(u)->state == DEVICE_DEAD ? UNIT_INACTIVE : UNIT_ACTIVE; | |
5cb5a6ff LP |
230 | } |
231 | ||
87f0e418 | 232 | const UnitVTable device_vtable = { |
5cb5a6ff LP |
233 | .suffix = ".device", |
234 | ||
87f0e418 | 235 | .init = unit_load_fragment_and_dropin, |
034c6ed7 | 236 | .done = device_done, |
5cb5a6ff LP |
237 | .dump = device_dump, |
238 | ||
25ac040b LP |
239 | .enumerate = device_enumerate, |
240 | .shutdown = device_shutdown, | |
241 | ||
034c6ed7 | 242 | .active_state = device_active_state |
5cb5a6ff | 243 | }; |