From: Frank Sorenson <frank@tuxrocks.com>
To: Rob Landley <rob@landley.net>
Cc: Daniele Orlandi <daniele@orlandi.com>, linux-kernel@vger.kernel.org
Subject: Re: An idea on devfs vs. udev
Date: Mon, 31 Oct 2005 02:21:26 -0700 [thread overview]
Message-ID: <4365E216.5000403@tuxrocks.com> (raw)
In-Reply-To: <200510301557.29024.rob@landley.net>
[-- Attachment #1: Type: text/plain, Size: 1204 bytes --]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Rob Landley wrote:
> I'm pondering adding a micro-udev to busybox (it's fairly far down on the todo
> list).
snip
> function makedev
> {
> j=`echo "$1" | sed 's .*/\(.*\)/dev \1 '`
> minor=`cat "$1" | sed 's .*: '`
> major=`cat "$1" | sed 's :.* '`
> mknod tmpdir/"$j" $2 $major $minor
> }
>
> for i in `find /sys/block -name "dev"`
> do
> makedev -m 700 $i b $major $minor
> echo -n b
> done
>
> for i in `find /sys/class -name "dev"`
> do
> makedev -m 700 $i c $major $minor
> echo -n c
> done
>
> You think implementing that in C would take more than 4k?
I'm certain that someone can do better than this, but here's a C version
that does it in 4684 bytes (stripped). It's simple, and runs fast. I'm
sure that someone could optimize this much further.
Frank
- --
Frank Sorenson - KD7TZK
Systems Manager, Computer Science Department
Brigham Young University
frank@tuxrocks.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iD8DBQFDZeIWaI0dwg4A47wRAiWgAJ9LzBcNhfrW5KHP9f8qg+spfGUSKwCdFTlw
grK0kedpIQ/JRDN2yMqLG4U=
=K4vE
-----END PGP SIGNATURE-----
[-- Attachment #2: miniudev.c --]
[-- Type: text/x-csrc, Size: 2566 bytes --]
/*
* miniudev - implement a small udev that can run at bootup
* Copyright Frank Sorenson <frank@tuxrocks.com> 2005
*
* Permission is hereby granted to copy, modify and redistribute this code
* in terms of the GNU Library General Public License, Version 2 or later,
* at your option.
*
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#define DEV_PATH "/dev"
#define DEV_MODE 0700
int get_device_info(const char *path, int *major, int *minor)
{
char buf[512];
int fd;
int ret;
char *chr;
char got_colon = 0;
int tempval = 0;
sprintf(buf, "%s/dev", path);
fd = open(buf, O_RDONLY);
ret = read(fd, buf, 1024);
buf[ret - 1] = '\0';
if (ret == 0)
return -1;
close(fd);
chr = buf;
while (*chr != '\0') {
if (*chr == ':') {
*major = tempval;
tempval = 0;
got_colon ++;
} else {
tempval *= 10;
tempval += (*chr - '0');
}
chr ++;
}
*minor = tempval;
return 0;
}
int make_device(const char *path, int type)
{
char buf[512];
const char *device_name;
int major;
int minor;
int ret;
int local_errno;
device_name = path;
while (*device_name != '\0')
device_name ++;
while ((*(device_name - 1) != '/') && (device_name > path)) {
device_name --;
}
get_device_info(path, &major, &minor);
printf("%s: %d %d\n", device_name, major, minor);
sprintf(buf, "%s/%s", DEV_PATH, device_name);
ret = mknod(buf, DEV_MODE | type, makedev(major, minor));
if (ret != 0) {
local_errno = errno;
printf("Could not create device node %s: error %d\n", buf, local_errno);
}
return 0;
}
char cmp(const char *s1, const char *s2)
{
int n = 0;
while (s1[n] == s2[n]) {
if (s1[n] == '\0')
return 1;
n++;
}
return 0;
}
int find_dev(const char *path, int type)
{
DIR *dir;
struct dirent *entry;
char temp_path[512];
int local_errno;
dir = opendir(path);
if (dir == NULL) {
printf("Could not open path %s: error %d\n", path, local_errno);
exit(-1);
}
do {
entry = readdir(dir);
if (entry == NULL)
break;
if (cmp(entry->d_name, "."))
continue;
if (cmp(entry->d_name, ".."))
continue;
if (entry->d_type == DT_DIR) {
sprintf(temp_path, "%s/%s", path, entry->d_name);
find_dev(temp_path, type);
}
if (cmp(entry->d_name, "dev")) {
make_device(path, type);
}
} while (entry != NULL);
closedir(dir);
return 0;
}
int main(int argc, char *argv[])
{
find_dev("/sys/block", S_IFBLK);
find_dev("/sys/class", S_IFCHR);
return 0;
}
next prev parent reply other threads:[~2005-10-31 9:21 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-10-30 18:07 An idea on devfs vs. udev Daniele Orlandi
2005-10-30 21:18 ` Neil Brown
2005-10-30 22:23 ` Greg KH
2005-10-30 21:32 ` Neil Brown
2005-11-08 18:41 ` Matt Mackall
2005-11-08 18:51 ` Greg KH
2005-11-08 19:17 ` Matt Mackall
2005-11-08 22:13 ` Bill Davidsen
2005-10-30 21:57 ` Rob Landley
2005-10-31 9:21 ` Frank Sorenson [this message]
2005-10-30 22:18 ` Greg KH
2005-10-30 21:35 ` Neil Brown
2005-10-30 21:48 ` Daniele Orlandi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4365E216.5000403@tuxrocks.com \
--to=frank@tuxrocks.com \
--cc=daniele@orlandi.com \
--cc=linux-kernel@vger.kernel.org \
--cc=rob@landley.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox