chiark / gitweb /
[PATCH] add NUMBER support (basically same logic as TOPOLOGY, perhaps we should merge...
[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         char temp[3];
53         int retval = 0;
54
55         char *dev;
56
57         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
58         if (dev == NULL)
59                 return -ENODEV;
60
61         dbg("dev = %s", dev);
62
63         temp[0] = dev[0];
64         temp[1] = dev[1];
65         temp[2] = 0x00;
66         *major = (int)strtol(&temp[0], NULL, 16);
67
68         temp[0] = dev[2];
69         temp[1] = dev[3];
70         temp[2] = 0x00;
71         *minor = (int)strtol(&temp[0], NULL, 16);
72
73         dbg("found major = %d, minor = %d", *major, *minor);
74
75         retval = 0;
76         return retval;
77 }
78
79 /*
80  * We also want to add some permissions here, and possibly some symlinks
81  */
82 static int create_node(char *name, char type, int major, int minor, int mode)
83 {
84         char *argv[7];
85         char mode_string[100];
86         char type_string[3];
87         char major_string[20];
88         char minor_string[20];
89         char filename[255];
90         int retval = 0;
91
92         strncpy(filename, UDEV_ROOT, sizeof(filename));
93         strncat(filename, name, sizeof(filename));
94
95         snprintf(mode_string, sizeof(mode_string), "--mode=%#o", mode);
96         snprintf(type_string, sizeof(type_string), "%c", type);
97         snprintf(major_string, sizeof(major_string), "%d", major);
98         snprintf(minor_string, sizeof(minor_string), "%d", minor);
99         
100         argv[0] = MKNOD;
101         argv[1] = mode_string;
102         argv[2] = filename;
103         argv[3] = type_string;
104         argv[4] = major_string;
105         argv[5] = minor_string;
106         argv[6] = NULL;
107         dbg ("executing %s %s %s %s %s %s",
108                 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
109         switch (fork()) {
110                 case 0:
111                         /* we are the child, so lets run the program */
112                         execv (MKNOD, argv);
113                         exit(0);
114                         break;
115                 case (-1):
116                         dbg ("fork failed.");
117                         retval = -EFAULT;
118                         break;
119                 default:
120                         break;
121         }
122         return retval;
123 }
124
125 struct sysfs_class_device *get_class_dev(char *device_name)
126 {
127         char dev_path[SYSFS_PATH_MAX];
128         struct sysfs_class_device *class_dev;
129
130         strcpy(dev_path, sysfs_path);
131         strcat(dev_path, device_name);
132
133         dbg("looking at %s", dev_path);
134
135         /* open up the sysfs class device for this thing... */
136         class_dev = sysfs_open_class_device(dev_path);
137         if (class_dev == NULL) {
138                 dbg ("sysfs_open_class_device failed");
139                 return NULL;
140         }
141         dbg("class_dev->name = %s", class_dev->name);
142
143         return class_dev;
144 }
145
146 static int udev_init(void)
147 {
148         int retval;
149
150         retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
151         dbg("sysfs_path = %s", sysfs_path);
152         return retval;
153 }
154
155 int udev_add_device(char *device, char *subsystem)
156 {
157         struct sysfs_class_device *class_dev;
158         struct device_attr attr;
159         //char *name;
160         int major;
161         int minor;
162         char type;
163         //int mode;
164         int retval = -EINVAL;
165
166         /* sleep for a second or two to give the kernel a chance to
167          * create the dev file
168          */
169         sleep(1);
170
171         udev_init();
172
173         /* for now, the block layer is the only place where block devices are */
174         if (strcmp(subsystem, "block") == 0)
175                 type = 'b';
176         else
177                 type = 'c';
178
179         class_dev = get_class_dev(device);
180         if (class_dev == NULL)
181                 goto exit;
182
183         retval = namedev_name_device(class_dev, &attr);
184         if (retval)
185                 return retval;
186
187         retval = get_major_minor(class_dev, &major, &minor);
188         if (retval) {
189                 dbg ("get_major_minor failed");
190                 goto exit;
191         }
192
193         sysfs_close_class_device(class_dev);
194
195         return create_node(attr.name, type, major, minor, attr.mode);
196
197 exit:
198         return retval;
199 }
200