summaryrefslogtreecommitdiff
path: root/udev/udev_device.c
blob: 5ae66743f6db359c3c21b39d4a5307fa383a38f7 (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
/*
 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 * 
 *	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, write to the Free Software Foundation, Inc.,
 *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/sockios.h>

#include "udev.h"
#include "udev_rules.h"


struct udevice *udev_device_init(struct udev *udev)
{
	struct udevice *udevice;

	udevice = malloc(sizeof(struct udevice));
	if (udevice == NULL)
		return NULL;
	memset(udevice, 0x00, sizeof(struct udevice));

	udevice->udev = udev;

	INIT_LIST_HEAD(&udevice->symlink_list);
	INIT_LIST_HEAD(&udevice->run_list);
	INIT_LIST_HEAD(&udevice->env_list);

	/* set sysfs device to local storage, can be overridden if needed */
	udevice->dev = &udevice->dev_local;

	/* default node permissions */
	udevice->mode = 0660;
	strcpy(udevice->owner, "root");
	strcpy(udevice->group, "root");

	udevice->event_timeout = -1;
	return udevice;
}

void udev_device_cleanup(struct udevice *udevice)
{
	if (udevice == NULL)
		return;
	name_list_cleanup(udevice->udev, &udevice->symlink_list);
	name_list_cleanup(udevice->udev, &udevice->run_list);
	name_list_cleanup(udevice->udev, &udevice->env_list);
	free(udevice);
}

dev_t udev_device_get_devt(struct udevice *udevice)
{
	const char *attr;
	unsigned int maj, min;

	/* read it from sysfs  */
	attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "dev");
	if (attr != NULL) {
		if (sscanf(attr, "%u:%u", &maj, &min) == 2)
			return makedev(maj, min);
	}
	return makedev(0, 0);
}