chiark / gitweb /
[PATCH] Added tdb code from latest cvs version in the samba tree
[elogind.git] / udev-add.c
1 /*
2  * udev-add.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "udev.h"
32 #include "udev_version.h"
33 #include "namedev.h"
34 #include "libsysfs/libsysfs.h"
35
36
37 /* 
38  * Right now the major/minor of a device is stored in a file called
39  * "dev" in sysfs.
40  * The number is stored as:
41  *      MMmm
42  *              MM is the major
43  *              mm is the minor
44  *              The value is in hex.
45  * Yes, this will probably change when we go to a bigger major/minor
46  * range, and will have to be changed at that time.
47  */
48 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
49 {
50         int retval = -ENODEV;
51
52         char *dev;
53
54         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
55         if (dev == NULL)
56                 goto exit;
57
58         dbg("dev = %s", dev);
59
60         if (sscanf(dev, "%u:%u", major, minor) != 2)
61                 goto exit;
62
63         dbg("found major = %d, minor = %d", *major, *minor);
64
65         retval = 0;
66 exit:
67         return retval;
68 }
69
70 /*
71  * We also want to add some permissions here, and possibly some symlinks
72  */
73 static int create_node(char *name, char type, int major, int minor, int mode)
74 {
75         char filename[255];
76         int retval = 0;
77         strncpy(filename, UDEV_ROOT, sizeof(filename));
78         strncat(filename, name, sizeof(filename));
79         switch (type) {
80         case 'b':
81                 mode |= S_IFBLK;
82                 break;
83         case 'c':
84         case 'u':
85                 mode |= S_IFCHR;
86                 break;
87         case 'p':
88                 mode |= S_IFIFO;
89                 break;
90         default:
91                 dbg("unknown node type %c\n", type);
92                 return -EINVAL;
93         }
94
95         retval = mknod(filename,mode,makedev(major,minor));
96         if (retval)
97                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
98                     filename, mode, major, minor, strerror(errno));
99         return retval;
100 }
101
102 struct sysfs_class_device *get_class_dev(char *device_name)
103 {
104         char sysfs_path[SYSFS_PATH_MAX];
105         char dev_path[SYSFS_PATH_MAX];
106         int retval;
107         struct sysfs_class_device *class_dev = NULL;
108
109
110         retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
111         dbg("sysfs_path = %s", sysfs_path);
112         if (retval) {
113                 dbg("sysfs_get_mnt_path failed");
114                 goto exit;
115         }
116
117         strcpy(dev_path, sysfs_path);
118         strcat(dev_path, device_name);
119
120         dbg("looking at %s", dev_path);
121
122         /* open up the sysfs class device for this thing... */
123         class_dev = sysfs_open_class_device(dev_path);
124         if (class_dev == NULL) {
125                 dbg ("sysfs_open_class_device failed");
126                 goto exit;
127         }
128         dbg("class_dev->name = %s", class_dev->name);
129
130 exit:
131         return class_dev;
132 }
133
134 int udev_add_device(char *device, char *subsystem)
135 {
136         struct sysfs_class_device *class_dev;
137         struct device_attr attr;
138         int major;
139         int minor;
140         char type;
141         int retval = -EINVAL;
142
143         /* for now, the block layer is the only place where block devices are */
144         if (strcmp(subsystem, "block") == 0)
145                 type = 'b';
146         else
147                 type = 'c';
148
149         /* sleep for a second or two to give the kernel a chance to
150          * create the dev file
151          */
152         sleep(1);
153
154         class_dev = get_class_dev(device);
155         if (class_dev == NULL)
156                 goto exit;
157
158         retval = namedev_name_device(class_dev, &attr);
159         if (retval)
160                 return retval;
161
162         retval = get_major_minor(class_dev, &major, &minor);
163         if (retval) {
164                 dbg ("get_major_minor failed");
165                 goto exit;
166         }
167
168         sysfs_close_class_device(class_dev);
169
170         return create_node(attr.name, type, major, minor, attr.mode);
171
172 exit:
173         return retval;
174 }
175