summaryrefslogtreecommitdiffstats
path: root/discover/udev.c
blob: 317b4bc320d7e824cf15abb02e632c2bc33fb359 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <assert.h>
#include <errno.h>
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include <log/log.h>
#include <types/types.h>
#include <talloc/talloc.h>
#include <waiter/waiter.h>
#include <system/system.h>

#include "event.h"
#include "udev.h"
#include "pb-discover.h"
#include "device-handler.h"
#include "cdrom.h"

/* We set a default monitor buffer size, as we may not process monitor
 * events while performing device discvoery. systemd uses a 128M buffer, so
 * we'll do the same here */
static const int monitor_bufsize = 128 * 1024 * 1024;

struct pb_udev {
	struct udev *udev;
	struct udev_monitor *monitor;
	struct device_handler *handler;
};

static int udev_destructor(void *p)
{
	struct pb_udev *udev = p;

	udev_monitor_unref(udev->monitor);
	udev->monitor = NULL;

	udev_unref(udev->udev);
	udev->udev = NULL;

	return 0;
}

static void udev_setup_device_params(struct udev_device *udev,
		struct discover_device *dev)
{
	struct udev_list_entry *list, *entry;

	list = udev_device_get_properties_list_entry(udev);
	if (!list)
		return;

	udev_list_entry_foreach(entry, list)
		discover_device_set_param(dev,
				udev_list_entry_get_name(entry),
				udev_list_entry_get_value(entry));
}

static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
		const char *name)
{
	struct discover_device *ddev;
	const char *typestr;
	const char *uuid;
	const char *path;
	const char *node;
	const char *prop;
	const char *type;
	bool cdrom;

	typestr = udev_device_get_devtype(dev);
	if (!typestr) {
		pb_debug("udev_device_get_devtype failed\n");
		return -1;
	}

	if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
		pb_log("SKIP %s: invalid type %s\n", name, typestr);
		return 0;
	}

	node = udev_device_get_devnode(dev);
	path = udev_device_get_devpath(dev);
	if (path && (strstr(path, "virtual/block/loop")
			|| strstr(path, "virtual/block/ram"))) {
		pb_log("SKIP: %s: ignored (path=%s)\n", name, path);
		return 0;
	}

	cdrom = node && !!udev_device_get_property_value(dev, "ID_CDROM");
	if (cdrom) {
		/* CDROMs require a little initialisation, to get
		 * petitboot-compatible tray behaviour */
		cdrom_init(node);
		if (!cdrom_media_present(node)) {
			pb_log("SKIP: %s: no media present\n", name);
			return 0;
		}
	}

	type = udev_device_get_property_value(dev, "ID_FS_TYPE");
	if (!type) {
		pb_log("SKIP: %s: no ID_FS_TYPE property\n", name);
		return 0;
	}

	/* We may see multipath devices; they'll have the same uuid as an
	 * existing device, so only parse the first. */
	uuid = udev_device_get_property_value(dev, "ID_FS_UUID");
	if (uuid) {
		ddev = device_lookup_by_uuid(udev->handler, uuid);
		if (ddev) {
			pb_log("SKIP: %s UUID [%s] already present (as %s)\n",
					name, uuid, ddev->device->id);
			return -1;
		}
	}

	ddev = discover_device_create(udev->handler, name);

	ddev->device_path = talloc_strdup(ddev, node);

	if (uuid)
		ddev->uuid = talloc_strdup(ddev, uuid);
	prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
	if (prop)
		ddev->label = talloc_strdup(ddev, prop);
	ddev->device->type = cdrom ? DEVICE_TYPE_OPTICAL : DEVICE_TYPE_DISK;

	udev_setup_device_params(dev, ddev);

	device_handler_discover(udev->handler, ddev);

	return 0;
}

static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
{
	const char *subsys;
	const char *name;

	name = udev_device_get_sysname(dev);
	if (!name) {
		pb_debug("udev_device_get_sysname failed\n");
		return -1;
	}

	subsys = udev_device_get_subsystem(dev);
	if (!subsys) {
		pb_debug("udev_device_get_subsystem failed\n");
		return -1;
	}

	if (device_lookup_by_id(udev->handler, name)) {
		pb_debug("device %s is already present?\n", name);
		return -1;
	}

	if (!strcmp(subsys, "block")) {
		return udev_handle_block_add(udev, dev, name);
	}

	pb_log("SKIP %s: unknown subsystem %s\n", name, subsys);
	return -1;
}


static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
{
	struct discover_device *ddev;
	const char *name;

	name = udev_device_get_sysname(dev);
	if (!name) {
		pb_debug("udev_device_get_sysname failed\n");
		return -1;
	}

	ddev = device_lookup_by_id(udev->handler, name);
	if (!ddev)
		return 0;

	device_handler_remove(udev->handler, ddev);

	return 0;
}

/* returns true if further event processing should stop (eg., we've
 * ejected the cdrom)
 */
static bool udev_handle_cdrom_events(struct pb_udev *udev,
		struct udev_device *dev, struct discover_device *ddev)
{
	const char *node;

	node = udev_device_get_devnode(dev);

	/* handle CDROM eject requests */
	if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
		bool eject = false;

		pb_debug("udev: eject request\n");

		/* If the device is mounted, cdrom_id's own eject request may
		 * have failed. So, we'll need to do our own here.
		 */
		if (ddev) {
			eject = ddev->mounted;
			udev_handle_dev_remove(udev, dev);
			return false;
		}

		if (eject)
			cdrom_eject(node);

		return true;
	}

	if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
		if (cdrom_media_present(node))
			udev_handle_dev_add(udev, dev);
		else
			udev_handle_dev_remove(udev, dev);
		return true;
	}

	return false;
}

static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
{
	struct discover_device *ddev;
	const char *name;
	int rc = 0;

	name = udev_device_get_sysname(dev);

	ddev = device_lookup_by_id(udev->handler, name);

	/* if this is a CDROM device, process eject & media change requests;
	 * these may stop further processing */
	if (!udev_device_get_property_value(dev, "ID_CDROM")) {
		if (udev_handle_cdrom_events(udev, dev, ddev))
			return 0;
	}

	/* if this is a new device, treat it as an add */
	if (!ddev)
		rc = udev_handle_dev_add(udev, dev);

	return rc;
}

static int udev_handle_dev_action(struct udev_device *dev, const char *action)
{
	struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));

#ifdef DEBUG
	{
		struct udev_list_entry *list;
		const char *name;

		list = udev_device_get_properties_list_entry(dev);
		name = udev_device_get_sysname(dev);

		pb_debug("%s: action %s, device %s\n", __func__, action, name);
		pb_debug("%s properties:\n", __func__);

		for (; list; list = udev_list_entry_get_next(list))
			pb_log("\t%-20s: %s\n", udev_list_entry_get_name(list),
					udev_list_entry_get_value(list));
	} while (0);
#endif

	if (!strcmp(action, "add"))
		return udev_handle_dev_add(udev, dev);

	else if (!strcmp(action, "remove"))
		return udev_handle_dev_remove(udev, dev);

	else if (!strcmp(action, "change"))
		return udev_handle_dev_change(udev, dev);

	return 0;
}

static int udev_enumerate(struct udev *udev)
{
	int result;
	struct udev_list_entry *list, *entry;
	struct udev_enumerate *enumerate;

	enumerate = udev_enumerate_new(udev);

	if (!enumerate) {
		pb_log("udev_enumerate_new failed\n");
		return -1;
	}

	result = udev_enumerate_add_match_subsystem(enumerate, "block");
	if (result) {
		pb_log("udev_enumerate_add_match_subsystem failed\n");
		goto fail;
	}

	result = udev_enumerate_add_match_is_initialized(enumerate);
	if (result) {
		pb_log("udev_enumerate_add_match_is_initialised failed\n");
		goto fail;
	}

	udev_enumerate_scan_devices(enumerate);

	list = udev_enumerate_get_list_entry(enumerate);

	if (!list) {
		pb_log("udev_enumerate_get_list_entry failed\n");
		goto fail;
	}

	udev_list_entry_foreach(entry, list) {
		const char *syspath;
		struct udev_device *dev;

		syspath = udev_list_entry_get_name(entry);
		dev = udev_device_new_from_syspath(udev, syspath);

		udev_handle_dev_action(dev, "add");

		udev_device_unref(dev);
	}

	udev_enumerate_unref(enumerate);
	return 0;

fail:
	udev_enumerate_unref(enumerate);
	return -1;
}

static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
{
	int result;
	struct udev_monitor *m;

	*monitor = NULL;
	m = udev_monitor_new_from_netlink(udev, "udev");

	if (!m) {
		pb_log("udev_monitor_new_from_netlink failed\n");
		goto out_err;
	}

	result = udev_monitor_set_receive_buffer_size(m, monitor_bufsize);
	if (result) {
		pb_log("udev_monitor_set_rx_bufsize(%d) failed\n",
			monitor_bufsize);
	}

	result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
		NULL);

	if (result) {
		pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
		goto out_err;
	}

	result = udev_monitor_enable_receiving(m);

	if (result) {
		pb_log("udev_monitor_enable_receiving failed\n");
		goto out_err;
	}

	*monitor = m;
	return 0;

out_err:
	udev_monitor_unref(m);
	return -1;
}

/*
 * udev_process - waiter callback for monitor netlink.
 */

static int udev_process(void *arg)
{
	struct udev_monitor *monitor = arg;
	struct udev_device *dev;
	const char *action;

	dev = udev_monitor_receive_device(monitor);
	if (!dev) {
		pb_log("udev_monitor_receive_device failed\n");
		return -1;
	}

	action = udev_device_get_action(dev);

	if (!action) {
		pb_log("udev_device_get_action failed\n");
	} else {
		udev_handle_dev_action(dev, action);
	}

	udev_device_unref(dev);
	return 0;
}

static void udev_log_fn(struct udev __attribute__((unused)) *udev,
	int __attribute__((unused)) priority, const char *file, int line,
	const char *fn, const char *format, va_list args)
{
      pb_log("libudev: %s %s:%d: ", fn, file, line);
      vfprintf(pb_log_get_stream(), format, args);
}

struct pb_udev *udev_init(struct waitset *waitset,
	struct device_handler *handler)
{
	int result;
	struct pb_udev *udev = talloc(NULL, struct pb_udev);

	talloc_set_destructor(udev, udev_destructor);
	udev->handler = handler;

	udev->udev = udev_new();

	if (!udev->udev) {
		pb_log("udev_new failed\n");
		goto fail_new;
	}

	udev_set_userdata(udev->udev, udev);

	udev_set_log_fn(udev->udev, udev_log_fn);

	result = udev_setup_monitor(udev->udev, &udev->monitor);
	if (result)
		goto fail_monitor;

	result = udev_enumerate(udev->udev);
	if (result)
		goto fail_enumerate;

	waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
		udev_process, udev->monitor);

	pb_debug("%s: waiting on udev\n", __func__);

	return udev;

fail_monitor:
	udev_monitor_unref(udev->monitor);
fail_enumerate:
	udev_unref(udev->udev);
fail_new:
	talloc_free(udev);
	return NULL;
}

void udev_destroy(struct pb_udev *udev)
{
	talloc_free(udev);
}
OpenPOWER on IntegriCloud