Skip to contentSkip to conversation

udevstart.c -> kscan.c

PATCH Lindsay Haisley 2 participants 4 messages Oct 25, 2004

lore
Share & tools
lore
#0 lore raw

Here's a rather more polished, less buggy version of kscan.c, as a diff against udevstart.c.

Signature · 5 lines
Lindsay Haisley       | "Fighting against human |     PGP public key
FMP Computer Services |    creativity is like   |      available at
512-259-1190          |    trying to eradicate  | <http://pubkeys.fmp.com>
http://www.fmp.com    |        dandelions"      |
                      |      (Pamela Jones)     |
kscan.c renamed from udevstart.c +44 -55
@@ -1,5 +1,5 @@
 /*
- * udevstart.c
+ * kscan.c, based on udevstart.c
  *
  * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
  * 
@@ -20,6 +20,7 @@
  *	with this program; if not, write to the Free Software Foundation, Inc.,
  *	675 Mass Ave, Cambridge, MA 02139, USA.
  *
+ *      Mods to create kscan added by Lindsay Haisley <[email protected]>
  */
 
 #include <stdlib.h>
@@ -33,28 +34,15 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "logging.h"
 #include "udev_lib.h"
 #include "list.h"
 
 
-#ifdef LOG
-unsigned char logname[LOGNAME_SIZE];
-void log_message(int level, const char *format, ...)
-{
-	va_list args;
-
-	va_start(args, format);
-	vsyslog(level, format, args);
-	va_end(args);
-}
-#endif
-
-
 #define MAX_PATHLEN		1024
 #define SYSBLOCK		"/sys/block"
 #define SYSCLASS		"/sys/class"
-#define UDEV_BIN		"/sbin/udev"
+#define OUTPUT_FORMAT		"%-15s KERNEL=\"%s\"\n"
+
 
 struct device {
 	struct list_head list;
@@ -62,6 +50,37 @@
 	char subsys[MAX_PATHLEN];
 };
 
+
+/* Display native kernel devices */
+static void print_dev(char *psubsys, char *path)
+{
+	char *strbuf, *tok, *utok, *cstr;
+	static char *last_subsys = NULL;
+
+	if (last_subsys == NULL) last_subsys = malloc(MAX_PATHLEN);
+	strbuf = malloc(MAX_PATHLEN);
+	strcpy(strbuf, path);
+	tok = strtok(strbuf, "/");
+	utok = tok;
+	while (1) {
+		if (!tok) break;
+		else utok = tok;
+		tok = strtok(NULL, "/");
+	}
+	if (strcmp(psubsys, last_subsys)) {
+		strcpy(last_subsys, psubsys);
+		cstr = malloc(strlen(psubsys) + 1);
+		strcpy(cstr, psubsys);
+		strcat(cstr, ":");
+		printf("\n" OUTPUT_FORMAT, cstr, utok);
+		free(cstr);
+	} else {
+		printf(OUTPUT_FORMAT, "", utok);
+	}
+	free(strbuf);
+}	
+
+
 /* sort files in lexical order */
 static int device_list_insert(char *path, char *subsystem, struct list_head *device_list)
 {
@@ -76,7 +95,6 @@
 
 	new_device = malloc(sizeof(struct device));
 	if (new_device == NULL) {
-		dbg("error malloc");
 		return -ENOMEM;
 	}
 
@@ -86,32 +104,6 @@
 	return 0;
 }
 
-static void udev_exec(const char *path, const char* subsystem)
-{
-	pid_t pid;
-	char action[] = "ACTION=add";
-	char devpath[MAX_PATHLEN];
-	char nosleep[] = "UDEV_NO_SLEEP=1";
-	char *env[] = { action, devpath, nosleep, NULL };
-
-	strcpy(devpath, "DEVPATH=");
-	strfieldcat(devpath, path);
-
-	pid = fork();
-	switch (pid) {
-	case 0:
-		/* child */
-		execle(UDEV_BIN, "udev", subsystem, NULL, env);
-		dbg("exec of child failed");
-		exit(1);
-		break;
-	case -1:
-		dbg("fork of child failed");
-		break;
-	default:
-		wait(NULL);
-	}
-}
 
 /* list of devices that we should run last due to any one of a number of reasons */
 static char *last_list[] = {
@@ -119,7 +111,7 @@
 	NULL,
 };
 
-static void exec_list(struct list_head *device_list)
+static void print_list(struct list_head *device_list)
 {
 	struct device *loop_device;
 	struct device *tmp_device;
@@ -137,20 +129,20 @@
 		if (found)
 			continue;
 
-		udev_exec(loop_device->path, loop_device->subsys);
+		print_dev(loop_device->subsys, loop_device->path);
 		list_del(&loop_device->list);
 		free(loop_device);
 	}
 
 	/* handle the rest of the devices left over, if any */
 	list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
-		udev_exec(loop_device->path, loop_device->subsys);
+		print_dev(loop_device->subsys, loop_device->path);
 		list_del(&loop_device->list);
 		free(loop_device);
 	}
 }
 
-static void udev_scan_block(void)
+static void p_udev_scan_block(void)
 {
 	DIR *dir;
 	struct dirent *dent;
@@ -207,10 +199,10 @@
 		closedir(dir);
 	}
 
-	exec_list(&device_list);
+	print_list(&device_list);
 }
 
-static void udev_scan_class(void)
+static void p_udev_scan_class(void)
 {
 	DIR *dir;
 	struct dirent *dent;
@@ -264,15 +256,12 @@
 		closedir(dir);
 	}
 
-	exec_list(&device_list);
+	print_list(&device_list);
 }
 
 int main(int argc, char *argv[], char *envp[])
 {
-	init_logging("udevstart");
-
-	udev_scan_class();
-	udev_scan_block();
-
+	p_udev_scan_block();
+	p_udev_scan_class();
 	return 0;
 }
#1 lore raw
Quoted 2 lines · Lindsay Haisley

Here's a rather more polished, less buggy version of kscan.c, as a diff against udevstart.c.

You do realize that the current version of udev is 042, not 030, right? udevstart is radically different in the current version.

Also, what you are looking for (a list of all kernel class devices) can be done with the following short bit of shell code. Feel free to add the 'KERNEL=' portion to the beginning of every line if you think it's necessary.

thanks,

greg k-h

------------------------

#! /bin/bash
#
# Directory where sysfs is mounted
SYSFS_DIR=/sys

# handle block devices and their partitions
for i in ${SYSFS_DIR}/block/*; do
	# each drive
	echo ${i#${SYSFS_DIR}/block/}
	# each partition, on each device
	for j in $i/*; do
		if [ -f $j/dev ]; then
			echo ${j#${SYSFS_DIR}} | cut --delimiter='/' --fields=4-
		fi
	done
done
# all other device classes
for i in ${SYSFS_DIR}/class/*; do
	for j in $i/*; do
		if [ -f $j/dev ]; then
			echo ${j#${SYSFS_DIR}} | cut --delimiter='/' --fields=4-
		fi
	done
done

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw
In reply to Lindsay Haisley

Thus spake Greg KH on Mon, Oct 25, 2004 at 01:45:10AM CDT

Quoted 6 lines · Lindsay Haisley
Quoted 2 lines

Here's a rather more polished, less buggy version of kscan.c, as a diff against udevstart.c.

You do realize that the current version of udev is 042, not 030, right? udevstart is radically different in the current version.

Yes, I'm running gentoo stable, which is at 030. Unstable is at 042. If I'm going to mess with this stuff and try to talk intelligently (?) about it I should get on the same plane with everyone else :/

Quoted 4 lines

Also, what you are looking for (a list of all kernel class devices) can be done with the following short bit of shell code. Feel free to add the 'KERNEL=' portion to the beginning of every line if you think it's necessary.

Not necessary, only convenient, as is the SYSFS{blah}="foo" output from udevinfo.

This script works fine, and gives the required information. The C patch I tossed out is slightly more informative in that it breaks devices down by category and makes them easier to find in the output, but the end result is the same.

If it's not already there, this script certainly belongs in the distribution.

Quoted 3 lines

thanks,

greg k-h

And thank you for all your good work on this!

Signature · 16 lines
Lindsay Haisley       | "Fighting against human |     PGP public key
FMP Computer Services |    creativity is like   |      available at
512-259-1190          |    trying to eradicate  | <http://pubkeys.fmp.com>
http://www.fmp.com    |        dandelions"      |
                      |      (Pamela Jones)     |


-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#3 lore raw
In reply to Lindsay Haisley

Thus spake Lindsay Haisley on Mon, Oct 25, 2004 at 02:22:28AM CDT

Quoted 5 lines
Quoted 5 lines

Also, what you are looking for (a list of all kernel class devices) can be done with the following short bit of shell code. Feel free to add the 'KERNEL=' portion to the beginning of every line if you think it's necessary.

For what it's worth, this prettys it up a bit and adds copy 'n paste convenience.

-------------------------------------

#! /bin/bash
#
# Directory where sysfs is mounted
SYSFS_DIR=/sys

# handle block devices and their partitions                                                         
echo "Block Devices:"
for i in ${SYSFS_DIR}/block/*; do
        # each drive
        echo -n "    KERNEL=\""                                                                   
        echo -n ${i#${SYSFS_DIR}/block/}
        echo \"
        # each partition, on each device                                                            
        for j in $i/*; do
                if [ -f $j/dev ]; then
                        echo -n ${j#${SYSFS_DIR}} | awk -F/ '{printf("    KERNEL=\"%s\"\n", $4)}'
                fi
        done
done
echo; echo "Other Devices:"
# all other device classes                                                                          
for i in ${SYSFS_DIR}/class/*; do
        for j in $i/*; do
                if [ -f $j/dev ]; then
                        echo -n ${j#${SYSFS_DIR}} | awk -F/ '{printf("    KERNEL=\"%s\"\n", $4)}'

fi
done
done

Signature · 16 lines
Lindsay Haisley       | "Fighting against human |     PGP public key
FMP Computer Services |    creativity is like   |      available at
512-259-1190          |    trying to eradicate  | <http://pubkeys.fmp.com>
http://www.fmp.com    |        dandelions"      |
                      |      (Pamela Jones)     |


-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel