chiark / gitweb /
test: replace ancient sysfs tree with recent one
[elogind.git] / udev / udev_device.c
1 /*
2  * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <net/if.h>
29 #include <linux/sockios.h>
30
31 #include "udev.h"
32 #include "udev_rules.h"
33
34
35 struct udevice *udev_device_init(struct udev *udev)
36 {
37         struct udevice *udevice;
38
39         udevice = malloc(sizeof(struct udevice));
40         if (udevice == NULL)
41                 return NULL;
42         memset(udevice, 0x00, sizeof(struct udevice));
43
44         udevice->udev = udev;
45
46         INIT_LIST_HEAD(&udevice->symlink_list);
47         INIT_LIST_HEAD(&udevice->run_list);
48         INIT_LIST_HEAD(&udevice->env_list);
49
50         /* set sysfs device to local storage, can be overridden if needed */
51         udevice->dev = &udevice->dev_local;
52
53         /* default node permissions */
54         udevice->mode = 0660;
55         strcpy(udevice->owner, "root");
56         strcpy(udevice->group, "root");
57
58         udevice->event_timeout = -1;
59         return udevice;
60 }
61
62 void udev_device_cleanup(struct udevice *udevice)
63 {
64         if (udevice == NULL)
65                 return;
66         name_list_cleanup(udevice->udev, &udevice->symlink_list);
67         name_list_cleanup(udevice->udev, &udevice->run_list);
68         name_list_cleanup(udevice->udev, &udevice->env_list);
69         free(udevice);
70 }
71
72 dev_t udev_device_get_devt(struct udevice *udevice)
73 {
74         const char *attr;
75         unsigned int maj, min;
76
77         /* read it from sysfs  */
78         attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "dev");
79         if (attr != NULL) {
80                 if (sscanf(attr, "%u:%u", &maj, &min) == 2)
81                         return makedev(maj, min);
82         }
83         return makedev(0, 0);
84 }