]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev-add.c
[PATCH] man page beauty
[thirdparty/systemd.git] / udev-add.c
CommitLineData
ea733a2f
GKH
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>
32ff5bca 30#include <sys/stat.h>
10950dfe
GKH
31#include <sys/types.h>
32#include <grp.h>
33#ifndef __KLIBC__
34#include <pwd.h>
35#endif
ea733a2f
GKH
36
37#include "udev.h"
38#include "udev_version.h"
39#include "namedev.h"
8e41d35d 40#include "udevdb.h"
ea733a2f 41#include "libsysfs/libsysfs.h"
10950dfe 42#include "klibc_fixups.h"
ea733a2f 43
ea733a2f
GKH
44/*
45 * Right now the major/minor of a device is stored in a file called
46 * "dev" in sysfs.
47 * The number is stored as:
727d1ba5 48 * MM:mm
ea733a2f
GKH
49 * MM is the major
50 * mm is the minor
727d1ba5 51 * The value is in decimal.
ea733a2f 52 */
30defadd 53static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
ea733a2f 54{
ea256f90 55 int retval = -ENODEV;
ea733a2f
GKH
56
57 char *dev;
58
59 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
60 if (dev == NULL)
ea256f90 61 goto exit;
ea733a2f 62
f7b4eca4 63 dbg("dev='%s'", dev);
ea733a2f 64
30defadd 65 if (sscanf(dev, "%u:%u", &udev->major, &udev->minor) != 2)
ea256f90 66 goto exit;
ea733a2f 67
f7b4eca4 68 dbg("found major=%d, minor=%d", udev->major, udev->minor);
ea733a2f
GKH
69
70 retval = 0;
ea256f90 71exit:
ea733a2f
GKH
72 return retval;
73}
74
75/*
c19a6b30
KS
76 * we possibly want to add some symlinks here
77 * only numeric owner/group id's are supported
ea733a2f 78 */
5840bc63 79static int create_node(struct udevice *dev)
ea733a2f 80{
ea733a2f
GKH
81 char filename[255];
82 int retval = 0;
e308ebb7
GKH
83 uid_t uid = 0;
84 gid_t gid = 0;
1e959a4b 85 dev_t res;
5840bc63 86
c056c514 87 strncpy(filename, udev_root, sizeof(filename));
5840bc63
GKH
88 strncat(filename, dev->name, sizeof(filename));
89
1e959a4b
GKH
90#ifdef __KLIBC__
91 res = (dev->major << 8) | (dev->minor);
92#else
93 res = makedev(dev->major, dev->minor);
94#endif
95
5840bc63 96 switch (dev->type) {
1331c889 97 case 'b':
5840bc63 98 dev->mode |= S_IFBLK;
1331c889
GKH
99 break;
100 case 'c':
101 case 'u':
5840bc63 102 dev->mode |= S_IFCHR;
1331c889
GKH
103 break;
104 case 'p':
5840bc63 105 dev->mode |= S_IFIFO;
1331c889
GKH
106 break;
107 default:
5840bc63 108 dbg("unknown node type %c\n", dev->type);
1331c889
GKH
109 return -EINVAL;
110 }
0abf54fc 111
218eae87
KS
112 /* create subdirectories if requested */
113 if (strchr(dev->name, '/')) {
114 char path[255];
115 char *pos;
116 struct stat stats;
117
118 strncpy(path, filename, sizeof(path));
119 pos = strchr(path+1, '/');
120 while (1) {
121 pos = strchr(pos+1, '/');
122 if (pos == NULL)
123 break;
124 *pos = 0x00;
125 if (stat(path, &stats)) {
126 retval = mkdir(path, 0755);
127 if (retval) {
128 dbg("mkdir(%s) failed with error '%s'",
129 path, strerror(errno));
130 return retval;
131 }
f7b4eca4 132 dbg("created '%s'", path);
218eae87
KS
133 }
134 *pos = '/';
135 }
136 }
137
5840bc63 138 dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
1e959a4b 139 retval = mknod(filename, dev->mode, res);
1331c889
GKH
140 if (retval)
141 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
5840bc63
GKH
142 filename, dev->mode, dev->major, dev->minor, strerror(errno));
143
5bd1061c
GKH
144 dbg("chmod(%s, %#o)", filename, dev->mode);
145 retval = chmod(filename, dev->mode);
146 if (retval)
147 dbg("chmod(%s, %#o) failed with error '%s'",
148 filename, dev->mode, strerror(errno));
149
c19a6b30
KS
150 if (*dev->owner) {
151 char *endptr;
152 unsigned long id = strtoul(dev->owner, &endptr, 10);
153 if (*endptr == 0x00)
154 uid = (uid_t) id;
10950dfe
GKH
155 else {
156 struct passwd *pw = getpwnam(dev->owner);
157 if (!pw)
f7b4eca4 158 dbg("user unknown '%s'", dev->owner);
10950dfe
GKH
159 else
160 uid = pw->pw_uid;
161 }
c19a6b30
KS
162 }
163
164 if (*dev->group) {
165 char *endptr;
166 unsigned long id = strtoul(dev->group, &endptr, 10);
167 if (*endptr == 0x00)
168 gid = (gid_t) id;
10950dfe
GKH
169 else {
170 struct group *gr = getgrnam(dev->group);
171 if (!gr)
f7b4eca4 172 dbg("group unknown '%s'", dev->group);
10950dfe
GKH
173 else
174 gid = gr->gr_gid;
175 }
c19a6b30
KS
176 }
177
178 if (uid || gid) {
179 dbg("chown(%s, %u, %u)", filename, uid, gid);
180 retval = chown(filename, uid, gid);
181 if (retval)
182 dbg("chown(%s, %u, %u) failed with error '%s'", filename,
183 uid, gid, strerror(errno));
184 }
185
ea733a2f
GKH
186 return retval;
187}
188
d7e954a4 189static struct sysfs_class_device *get_class_dev(char *device_name)
ea733a2f
GKH
190{
191 char dev_path[SYSFS_PATH_MAX];
63dde9f8
GKH
192 struct sysfs_class_device *class_dev = NULL;
193
ea733a2f
GKH
194 strcpy(dev_path, sysfs_path);
195 strcat(dev_path, device_name);
196
f7b4eca4 197 dbg("looking at '%s'", dev_path);
ea733a2f
GKH
198
199 /* open up the sysfs class device for this thing... */
200 class_dev = sysfs_open_class_device(dev_path);
201 if (class_dev == NULL) {
202 dbg ("sysfs_open_class_device failed");
63dde9f8 203 goto exit;
ea733a2f 204 }
f7b4eca4 205 dbg("class_dev->name='%s'", class_dev->name);
ea733a2f 206
63dde9f8 207exit:
ea733a2f
GKH
208 return class_dev;
209}
210
29fd7e67
GKH
211/* wait for the "dev" file to show up in the directory in sysfs.
212 * If it doesn't happen in about 10 seconds, give up.
213 */
214#define SECONDS_TO_WAIT_FOR_DEV 10
6089318c 215static int sleep_for_dev(char *path)
29fd7e67
GKH
216{
217 char filename[SYSFS_PATH_MAX + 6];
c332cfc7
RL
218 int loop = SECONDS_TO_WAIT_FOR_DEV;
219 int retval;
29fd7e67
GKH
220
221 strcpy(filename, sysfs_path);
5840bc63 222 strcat(filename, path);
29fd7e67
GKH
223 strcat(filename, "/dev");
224
c332cfc7
RL
225 while (loop--) {
226 struct stat buf;
227
f7b4eca4 228 dbg("looking for '%s'", filename);
29fd7e67 229 retval = stat(filename, &buf);
c332cfc7 230 if (!retval)
29fd7e67 231 goto exit;
29fd7e67 232
f7b4eca4 233 /* sleep to give the kernel a chance to create the dev file */
29fd7e67
GKH
234 sleep(1);
235 }
236 retval = -ENODEV;
237exit:
238 return retval;
239}
240
5840bc63 241int udev_add_device(char *path, char *subsystem)
ea733a2f 242{
30defadd 243 struct sysfs_class_device *class_dev = NULL;
5840bc63 244 struct udevice dev;
ea733a2f
GKH
245 int retval = -EINVAL;
246
f3b04a2e
GKH
247 memset(&dev, 0x00, sizeof(dev));
248
ea733a2f
GKH
249 /* for now, the block layer is the only place where block devices are */
250 if (strcmp(subsystem, "block") == 0)
5840bc63 251 dev.type = 'b';
ea733a2f 252 else
5840bc63 253 dev.type = 'c';
ea733a2f 254
5840bc63 255 retval = sleep_for_dev(path);
29fd7e67
GKH
256 if (retval)
257 goto exit;
63dde9f8 258
5840bc63 259 class_dev = get_class_dev(path);
ea733a2f
GKH
260 if (class_dev == NULL)
261 goto exit;
262
30defadd 263 retval = get_major_minor(class_dev, &dev);
ea733a2f 264 if (retval) {
ca999860 265 dbg("get_major_minor failed");
ea733a2f
GKH
266 goto exit;
267 }
268
30defadd
GKH
269 retval = namedev_name_device(class_dev, &dev);
270 if (retval)
271 goto exit;
272
5840bc63 273 retval = udevdb_add_dev(path, &dev);
8e41d35d 274 if (retval != 0)
5840bc63 275 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
ca999860
GKH
276 "But remove might not work properly for this device.");
277
f7b4eca4 278 dbg("name='%s'", dev.name);
5840bc63 279 retval = create_node(&dev);
ea733a2f
GKH
280
281exit:
30defadd
GKH
282 if (class_dev)
283 sysfs_close_class_device(class_dev);
284
ea733a2f
GKH
285 return retval;
286}