chiark / gitweb /
[PATCH] libsysfs does not need mntent.h in it's header file.
[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
77         strncpy(filename, udev_root, sizeof(filename));
78         strncat(filename, dev->name, sizeof(filename));
79
80         switch (dev->type) {
81         case 'b':
82                 dev->mode |= S_IFBLK;
83                 break;
84         case 'c':
85         case 'u':
86                 dev->mode |= S_IFCHR;
87                 break;
88         case 'p':
89                 dev->mode |= S_IFIFO;
90                 break;
91         default:
92                 dbg("unknown node type %c\n", dev->type);
93                 return -EINVAL;
94         }
95
96         dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
97         retval = mknod(filename, dev->mode, makedev(dev->major, dev->minor));
98         if (retval)
99                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
100                     filename, dev->mode, dev->major, dev->minor, strerror(errno));
101
102         // FIXME set the ownership of the node
103         return retval;
104 }
105
106 static struct sysfs_class_device *get_class_dev(char *device_name)
107 {
108         char dev_path[SYSFS_PATH_MAX];
109         struct sysfs_class_device *class_dev = NULL;
110
111         strcpy(dev_path, sysfs_path);
112         strcat(dev_path, device_name);
113
114         dbg("looking at %s", dev_path);
115
116         /* open up the sysfs class device for this thing... */
117         class_dev = sysfs_open_class_device(dev_path);
118         if (class_dev == NULL) {
119                 dbg ("sysfs_open_class_device failed");
120                 goto exit;
121         }
122         dbg("class_dev->name = %s", class_dev->name);
123
124 exit:
125         return class_dev;
126 }
127
128 /* wait for the "dev" file to show up in the directory in sysfs.
129  * If it doesn't happen in about 10 seconds, give up.
130  */
131 #define SECONDS_TO_WAIT_FOR_DEV         10
132 static int sleep_for_dev(char *path)
133 {
134         char filename[SYSFS_PATH_MAX + 6];
135         struct stat buf;
136         int loop = 0;
137         int retval = -ENODEV;
138
139         strcpy(filename, sysfs_path);
140         strcat(filename, path);
141         strcat(filename, "/dev");
142
143         while (loop < SECONDS_TO_WAIT_FOR_DEV) {
144                 dbg("looking for %s", filename);
145                 retval = stat(filename, &buf);
146                 if (retval == 0) {
147                         retval = 0;
148                         goto exit;
149                 }
150
151                 /* sleep for a second or two to give the kernel a chance to
152                  * create the dev file */
153                 sleep(1);
154                 ++loop;
155         }
156         retval = -ENODEV;
157 exit:
158         return retval;
159 }
160
161 int udev_add_device(char *path, char *subsystem)
162 {
163         struct sysfs_class_device *class_dev;
164         struct udevice dev;
165         int retval = -EINVAL;
166
167         /* for now, the block layer is the only place where block devices are */
168         if (strcmp(subsystem, "block") == 0)
169                 dev.type = 'b';
170         else
171                 dev.type = 'c';
172
173         retval = sleep_for_dev(path);
174         if (retval)
175                 goto exit;
176
177         class_dev = get_class_dev(path);
178         if (class_dev == NULL)
179                 goto exit;
180
181         retval = namedev_name_device(class_dev, &dev);
182         if (retval)
183                 return retval;
184
185         retval = get_major_minor(class_dev, &dev.major, &dev.minor);
186         if (retval) {
187                 dbg("get_major_minor failed");
188                 goto exit;
189         }
190
191 //      strcpy(dev.name, attr.name);
192 //      strcpy(dev.owner, attr.owner);
193 //      strcpy(dev.group, attr.group);
194 //      dev.mode = attr.mode;
195         
196         retval = udevdb_add_dev(path, &dev);
197         if (retval != 0)
198                 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
199                     "But remove might not work properly for this device.");
200
201         sysfs_close_class_device(class_dev);
202
203         dbg("name = %s", dev.name);
204         retval = create_node(&dev);
205
206 exit:
207         return retval;
208 }
209