chiark / gitweb /
[PATCH] man page update
[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  * 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  *      MM:mm
42  *              MM is the major
43  *              mm is the minor
44  *              The value is in decimal.
45  */
46 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
47 {
48         int retval = -ENODEV;
49
50         char *dev;
51
52         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
53         if (dev == NULL)
54                 goto exit;
55
56         dbg("dev = %s", dev);
57
58         if (sscanf(dev, "%u:%u", major, minor) != 2)
59                 goto exit;
60
61         dbg("found major = %d, minor = %d", *major, *minor);
62
63         retval = 0;
64 exit:
65         return retval;
66 }
67
68 /*
69  * We also want to add some permissions here, and possibly some symlinks
70  */
71 static int create_node(struct udevice *dev)
72 {
73         char filename[255];
74         int retval = 0;
75
76         strncpy(filename, udev_root, sizeof(filename));
77         strncat(filename, dev->name, sizeof(filename));
78
79         switch (dev->type) {
80         case 'b':
81                 dev->mode |= S_IFBLK;
82                 break;
83         case 'c':
84         case 'u':
85                 dev->mode |= S_IFCHR;
86                 break;
87         case 'p':
88                 dev->mode |= S_IFIFO;
89                 break;
90         default:
91                 dbg("unknown node type %c\n", dev->type);
92                 return -EINVAL;
93         }
94
95         dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
96         retval = mknod(filename, dev->mode, makedev(dev->major, dev->minor));
97         if (retval)
98                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
99                     filename, dev->mode, dev->major, dev->minor, strerror(errno));
100
101         // FIXME set the ownership of the node
102         return retval;
103 }
104
105 static struct sysfs_class_device *get_class_dev(char *device_name)
106 {
107         char dev_path[SYSFS_PATH_MAX];
108         struct sysfs_class_device *class_dev = NULL;
109
110         strcpy(dev_path, sysfs_path);
111         strcat(dev_path, device_name);
112
113         dbg("looking at %s", dev_path);
114
115         /* open up the sysfs class device for this thing... */
116         class_dev = sysfs_open_class_device(dev_path);
117         if (class_dev == NULL) {
118                 dbg ("sysfs_open_class_device failed");
119                 goto exit;
120         }
121         dbg("class_dev->name = %s", class_dev->name);
122
123 exit:
124         return class_dev;
125 }
126
127 /* wait for the "dev" file to show up in the directory in sysfs.
128  * If it doesn't happen in about 10 seconds, give up.
129  */
130 #define SECONDS_TO_WAIT_FOR_DEV         10
131 static int sleep_for_dev(char *path)
132 {
133         char filename[SYSFS_PATH_MAX + 6];
134         struct stat buf;
135         int loop = 0;
136         int retval = -ENODEV;
137
138         strcpy(filename, sysfs_path);
139         strcat(filename, path);
140         strcat(filename, "/dev");
141
142         while (loop < SECONDS_TO_WAIT_FOR_DEV) {
143                 dbg("looking for %s", filename);
144                 retval = stat(filename, &buf);
145                 if (retval == 0) {
146                         retval = 0;
147                         goto exit;
148                 }
149
150                 /* sleep for a second or two to give the kernel a chance to
151                  * create the dev file */
152                 sleep(1);
153                 ++loop;
154         }
155         retval = -ENODEV;
156 exit:
157         return retval;
158 }
159
160 int udev_add_device(char *path, char *subsystem)
161 {
162         struct sysfs_class_device *class_dev;
163         struct udevice dev;
164         struct device_attr attr;
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, &attr);
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