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