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