chiark / gitweb /
[PATCH] fix segfaults when dealing with partitions.
[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 "udevdb.h"
35 #include "libsysfs/libsysfs.h"
36
37
38 /* 
39  * Right now the major/minor of a device is stored in a file called
40  * "dev" in sysfs.
41  * The number is stored as:
42  *      MMmm
43  *              MM is the major
44  *              mm is the minor
45  *              The value is in hex.
46  * Yes, this will probably change when we go to a bigger major/minor
47  * range, and will have to be changed at that time.
48  */
49 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
50 {
51         int retval = -ENODEV;
52
53         char *dev;
54
55         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
56         if (dev == NULL)
57                 goto exit;
58
59         dbg("dev = %s", dev);
60
61         if (sscanf(dev, "%u:%u", major, minor) != 2)
62                 goto exit;
63
64         dbg("found major = %d, minor = %d", *major, *minor);
65
66         retval = 0;
67 exit:
68         return retval;
69 }
70
71 /*
72  * We also want to add some permissions here, and possibly some symlinks
73  */
74 static int create_node(char *name, char type, int major, int minor, int mode)
75 {
76         char filename[255];
77         int retval = 0;
78         strncpy(filename, UDEV_ROOT, sizeof(filename));
79         strncat(filename, name, sizeof(filename));
80         switch (type) {
81         case 'b':
82                 mode |= S_IFBLK;
83                 break;
84         case 'c':
85         case 'u':
86                 mode |= S_IFCHR;
87                 break;
88         case 'p':
89                 mode |= S_IFIFO;
90                 break;
91         default:
92                 dbg("unknown node type %c\n", type);
93                 return -EINVAL;
94         }
95
96         dbg("mknod(%s, %#o, %u, %u)", filename, mode, major, minor);
97         retval = mknod(filename,mode,makedev(major,minor));
98         if (retval)
99                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
100                     filename, mode, major, minor, strerror(errno));
101         return retval;
102 }
103
104 struct sysfs_class_device *get_class_dev(char *device_name)
105 {
106         char sysfs_path[SYSFS_PATH_MAX];
107         char dev_path[SYSFS_PATH_MAX];
108         int retval;
109         struct sysfs_class_device *class_dev = NULL;
110
111
112         retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
113         dbg("sysfs_path = %s", sysfs_path);
114         if (retval) {
115                 dbg("sysfs_get_mnt_path failed");
116                 goto exit;
117         }
118
119         strcpy(dev_path, sysfs_path);
120         strcat(dev_path, device_name);
121
122         dbg("looking at %s", dev_path);
123
124         /* open up the sysfs class device for this thing... */
125         class_dev = sysfs_open_class_device(dev_path);
126         if (class_dev == NULL) {
127                 dbg ("sysfs_open_class_device failed");
128                 goto exit;
129         }
130         dbg("class_dev->name = %s", class_dev->name);
131
132 exit:
133         return class_dev;
134 }
135
136 int udev_add_device(char *device, char *subsystem)
137 {
138         struct sysfs_class_device *class_dev;
139         struct device_attr attr;
140         struct udevice dbdev;
141         int major;
142         int minor;
143         char type;
144         int retval = -EINVAL;
145
146         /* for now, the block layer is the only place where block devices are */
147         if (strcmp(subsystem, "block") == 0)
148                 type = 'b';
149         else
150                 type = 'c';
151
152         /* sleep for a second or two to give the kernel a chance to
153          * create the dev file
154          */
155         sleep(1);
156
157         class_dev = get_class_dev(device);
158         if (class_dev == NULL)
159                 goto exit;
160
161         retval = namedev_name_device(class_dev, &attr);
162         if (retval)
163                 return retval;
164
165         retval = get_major_minor(class_dev, &major, &minor);
166         if (retval) {
167                 dbg ("get_major_minor failed");
168                 goto exit;
169         }
170         memset(&dbdev, 0, sizeof(dbdev));
171         strncpy(dbdev.name, attr.name, NAME_SIZE);
172         if (class_dev->sysdevice) {
173                 strncpy(dbdev.sysfs_path, class_dev->sysdevice->directory->path, PATH_SIZE);
174                 strncpy(dbdev.bus_id, class_dev->sysdevice->bus_id, ID_SIZE);
175         }
176         strncpy(dbdev.class_dev_name, class_dev->name, NAME_SIZE);
177         if ((sysfs_get_name_from_path(subsystem, dbdev.class_name, NAME_SIZE)) != 0)
178                 strcpy(dbdev.class_name, "unkown");
179         strcpy(dbdev.bus_name, "unknown");
180         if (class_dev->driver != NULL)
181                 strncpy(dbdev.driver, class_dev->driver->name, NAME_SIZE);
182         else
183                 strcpy(dbdev.driver, "unkown");
184         dbdev.type = type;
185         dbdev.major = major;
186         dbdev.minor = minor;
187         dbdev.mode = attr.mode;
188
189         sysfs_close_class_device(class_dev);
190
191         retval = udevdb_add_udevice(&dbdev);
192         if (retval != 0)
193                 goto exit;
194
195         return create_node(attr.name, type, major, minor, attr.mode);
196
197 exit:
198         return retval;
199 }
200