chiark / gitweb /
[PATCH] add klibc linux symlink info to the README
[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 #include <sys/stat.h>
31
32 #include "udev.h"
33 #include "udev_version.h"
34 #include "namedev.h"
35 #include "udevdb.h"
36 #include "libsysfs/libsysfs.h"
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  *      MM:mm
43  *              MM is the major
44  *              mm is the minor
45  *              The value is in decimal.
46  */
47 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
48 {
49         int retval = -ENODEV;
50
51         char *dev;
52
53         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
54         if (dev == NULL)
55                 goto exit;
56
57         dbg("dev = %s", dev);
58
59         if (sscanf(dev, "%u:%u", major, minor) != 2)
60                 goto exit;
61
62         dbg("found major = %d, minor = %d", *major, *minor);
63
64         retval = 0;
65 exit:
66         return retval;
67 }
68
69 /*
70  * We also want to add some permissions here, and possibly some symlinks
71  */
72 static int create_node(struct udevice *dev)
73 {
74         char filename[255];
75         int retval = 0;
76         dev_t res;
77
78         strncpy(filename, udev_root, sizeof(filename));
79         strncat(filename, dev->name, sizeof(filename));
80
81 #ifdef __KLIBC__
82         res = (dev->major << 8) | (dev->minor);
83 #else
84         res = makedev(dev->major, dev->minor);
85 #endif
86
87         switch (dev->type) {
88         case 'b':
89                 dev->mode |= S_IFBLK;
90                 break;
91         case 'c':
92         case 'u':
93                 dev->mode |= S_IFCHR;
94                 break;
95         case 'p':
96                 dev->mode |= S_IFIFO;
97                 break;
98         default:
99                 dbg("unknown node type %c\n", dev->type);
100                 return -EINVAL;
101         }
102
103         dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
104         retval = mknod(filename, dev->mode, res);
105         if (retval)
106                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
107                     filename, dev->mode, dev->major, dev->minor, strerror(errno));
108
109         // FIXME set the ownership of the node
110         return retval;
111 }
112
113 static struct sysfs_class_device *get_class_dev(char *device_name)
114 {
115         char dev_path[SYSFS_PATH_MAX];
116         struct sysfs_class_device *class_dev = NULL;
117
118         strcpy(dev_path, sysfs_path);
119         strcat(dev_path, device_name);
120
121         dbg("looking at %s", dev_path);
122
123         /* open up the sysfs class device for this thing... */
124         class_dev = sysfs_open_class_device(dev_path);
125         if (class_dev == NULL) {
126                 dbg ("sysfs_open_class_device failed");
127                 goto exit;
128         }
129         dbg("class_dev->name = %s", class_dev->name);
130
131 exit:
132         return class_dev;
133 }
134
135 /* wait for the "dev" file to show up in the directory in sysfs.
136  * If it doesn't happen in about 10 seconds, give up.
137  */
138 #define SECONDS_TO_WAIT_FOR_DEV         10
139 static int sleep_for_dev(char *path)
140 {
141         char filename[SYSFS_PATH_MAX + 6];
142         int loop = SECONDS_TO_WAIT_FOR_DEV;
143         int retval;
144
145         strcpy(filename, sysfs_path);
146         strcat(filename, path);
147         strcat(filename, "/dev");
148
149         while (loop--) {
150                 struct stat buf;
151
152                 dbg("looking for %s", filename);
153                 retval = stat(filename, &buf);
154                 if (!retval)
155                         goto exit;
156
157                 /* sleep for a second or two to give the kernel a chance to
158                  * create the dev file */
159                 sleep(1);
160         }
161         retval = -ENODEV;
162 exit:
163         return retval;
164 }
165
166 int udev_add_device(char *path, char *subsystem)
167 {
168         struct sysfs_class_device *class_dev;
169         struct udevice dev;
170         int retval = -EINVAL;
171
172         /* for now, the block layer is the only place where block devices are */
173         if (strcmp(subsystem, "block") == 0)
174                 dev.type = 'b';
175         else
176                 dev.type = 'c';
177
178         retval = sleep_for_dev(path);
179         if (retval)
180                 goto exit;
181
182         class_dev = get_class_dev(path);
183         if (class_dev == NULL)
184                 goto exit;
185
186         retval = namedev_name_device(class_dev, &dev);
187         if (retval)
188                 return retval;
189
190         retval = get_major_minor(class_dev, &dev.major, &dev.minor);
191         if (retval) {
192                 dbg("get_major_minor failed");
193                 goto exit;
194         }
195
196 //      strcpy(dev.name, attr.name);
197 //      strcpy(dev.owner, attr.owner);
198 //      strcpy(dev.group, attr.group);
199 //      dev.mode = attr.mode;
200         
201         retval = udevdb_add_dev(path, &dev);
202         if (retval != 0)
203                 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
204                     "But remove might not work properly for this device.");
205
206         sysfs_close_class_device(class_dev);
207
208         dbg("name = %s", dev.name);
209         retval = create_node(&dev);
210
211 exit:
212         return retval;
213 }
214