chiark / gitweb /
[PATCH] cleanup the mknod code a bit.
[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 static char sysfs_path[SYSFS_PATH_MAX];
38
39 /* 
40  * Right now the major/minor of a device is stored in a file called
41  * "dev" in sysfs.
42  * The number is stored as:
43  *      MMmm
44  *              MM is the major
45  *              mm is the minor
46  *              The value is in hex.
47  * Yes, this will probably change when we go to a bigger major/minor
48  * range, and will have to be changed at that time.
49  */
50 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
51 {
52         int retval = -ENODEV;
53
54         char *dev;
55
56         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
57         if (dev == NULL)
58                 goto exit;
59
60         dbg("dev = %s", dev);
61
62         if (sscanf(dev, "%u:%u", major, minor) != 2)
63                 goto exit;
64
65         dbg("found major = %d, minor = %d", *major, *minor);
66
67         retval = 0;
68 exit:
69         return retval;
70 }
71
72 /*
73  * We also want to add some permissions here, and possibly some symlinks
74  */
75 static int create_node(char *name, char type, int major, int minor, int mode)
76 {
77         char filename[255];
78         int retval = 0;
79         strncpy(filename, UDEV_ROOT, sizeof(filename));
80         strncat(filename, name, sizeof(filename));
81         switch (type) {
82         case 'b':
83                 mode |= S_IFBLK;
84                 break;
85         case 'c':
86         case 'u':
87                 mode |= S_IFCHR;
88                 break;
89         case 'p':
90                 mode |= S_IFIFO;
91                 break;
92         default:
93                 dbg("unknown node type %c\n", type);
94                 return -EINVAL;
95         }
96
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 dev_path[SYSFS_PATH_MAX];
107         struct sysfs_class_device *class_dev;
108
109         strcpy(dev_path, sysfs_path);
110         strcat(dev_path, device_name);
111
112         dbg("looking at %s", dev_path);
113
114         /* open up the sysfs class device for this thing... */
115         class_dev = sysfs_open_class_device(dev_path);
116         if (class_dev == NULL) {
117                 dbg ("sysfs_open_class_device failed");
118                 return NULL;
119         }
120         dbg("class_dev->name = %s", class_dev->name);
121
122         return class_dev;
123 }
124
125 static int udev_init(void)
126 {
127         int retval;
128
129         retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
130         dbg("sysfs_path = %s", sysfs_path);
131         return retval;
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         //char *name;
139         int major;
140         int minor;
141         char type;
142         //int mode;
143         int retval = -EINVAL;
144
145         /* sleep for a second or two to give the kernel a chance to
146          * create the dev file
147          */
148         sleep(1);
149
150         udev_init();
151
152         /* for now, the block layer is the only place where block devices are */
153         if (strcmp(subsystem, "block") == 0)
154                 type = 'b';
155         else
156                 type = 'c';
157
158         class_dev = get_class_dev(device);
159         if (class_dev == NULL)
160                 goto exit;
161
162         retval = namedev_name_device(class_dev, &attr);
163         if (retval)
164                 return retval;
165
166         retval = get_major_minor(class_dev, &major, &minor);
167         if (retval) {
168                 dbg ("get_major_minor failed");
169                 goto exit;
170         }
171
172         sysfs_close_class_device(class_dev);
173
174         return create_node(attr.name, type, major, minor, attr.mode);
175
176 exit:
177         return retval;
178 }
179