Skip to contentSkip to conversation

compile udevd with klibc

PATCH In review Kay Sievers 2 participants 3 messages Feb 9, 2004 – Feb 11, 2004 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 compile udevd with klibc

It seems that today was just another udev-sunday for me :)

Here is a working patch to compile udevd with klibc.

It's sweet the static binary takes 6 kbytes and it runs with only 80 kbytes virtual memory.

I changed a few peaces and added a siginterrupt.c file to klibc. We may check with hpa to get the changes upstream?

thanks,
Kay

Makefile +1 -3
diff -Nru a/Makefile b/Makefile
@@ -157,7 +157,6 @@
 		-I$(LINUX_INCLUDE_DIR)
 	LIB_OBJS =
 	LDFLAGS = --static --nostdlib -nostartfiles -nodefaultlibs
-	UDEVD =
 else
 	WARNINGS += -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations
 	CRT0 =
@@ -165,12 +164,11 @@
 	CFLAGS += $(WARNINGS) -I$(GCCINCDIR)
 	LIB_OBJS = -lc
 	LDFLAGS =
-	UDEVD = $(DAEMON)
 endif
 
 CFLAGS += -I$(PWD)/libsysfs
 
-all: $(ROOT) $(SENDER) $(UDEVD) $(HELPER)
+all: $(ROOT) $(SENDER) $(DAEMON) $(HELPER)
 	@extras="$(EXTRAS)" ; for target in $$extras ; do \
 		echo $$target ; \
 		$(MAKE) prefix=$(prefix) \
klibc/klibc/Makefile +1 -1
diff -Nru a/klibc/klibc/Makefile b/klibc/klibc/Makefile
@@ -23,7 +23,7 @@
 	  fwrite.o fwrite2.o fputc.o fputs.o puts.o \
 	  sleep.o usleep.o raise.o abort.o assert.o alarm.o pause.o \
 	  __signal.o signal.o bsd_signal.o siglist.o siglongjmp.o \
-	  sigaction.o sigpending.o sigprocmask.o sigsuspend.o \
+	  sigaction.o siginterrupt.o sigpending.o sigprocmask.o sigsuspend.o \
 	  brk.o sbrk.o malloc.o realloc.o calloc.o mmap.o getpagesize.o \
 	  memcpy.o memcmp.o memset.o memccpy.o memmem.o memswap.o \
 	  memmove.o \
klibc/klibc/include/signal.h +1 -0
diff -Nru a/klibc/klibc/include/signal.h b/klibc/klibc/include/signal.h
@@ -67,6 +67,7 @@
 __extern __sighandler_t signal(int, __sighandler_t);
 __extern __sighandler_t bsd_signal(int, __sighandler_t);
 __extern int sigaction(int, const struct sigaction *, struct sigaction *);
+__extern int siginterrupt(int, int);
 __extern int sigprocmask(int, const sigset_t *, sigset_t *);
 __extern int sigpending(sigset_t *);
 __extern int sigsuspend(const sigset_t *);
klibc/klibc/siginterrupt.c new file +21 -0
diff -Nru a/klibc/klibc/siginterrupt.c b/klibc/klibc/siginterrupt.c
@@ -0,0 +1,21 @@
+/*
+ * siginterrupt.c
+ */
+
+#include <signal.h>
+
+int siginterrupt(int sig, int flag) {
+  int ret;
+  struct sigaction act;
+
+  sigaction(sig, 0, &act);
+
+  if (flag)
+    act.sa_flags &= ~SA_RESTART;
+  else
+    act.sa_flags |= SA_RESTART;
+
+  ret = sigaction(sig, &act, 0);
+
+  return ret;
+}
klibc/klibc/signal.c +1 -1
diff -Nru a/klibc/klibc/signal.c b/klibc/klibc/signal.c
@@ -7,5 +7,5 @@
 __sighandler_t signal(int signum, __sighandler_t handler)
 {
   /* Linux/SysV signal() semantics */
-  return __signal(signum, handler, SA_RESETHAND);
+  return __signal(signum, handler, SA_NODEFER);
 }
udevd.c +9 -4
diff -Nru a/udevd.c b/udevd.c
@@ -108,14 +108,18 @@
 static void udev_run(struct hotplug_msg *msg)
 {
 	pid_t pid;
-	setenv("ACTION", msg->action, 1);
-	setenv("DEVPATH", msg->devpath, 1);
+	char action[32];
+	char devpath[256];
+	char *env[] = { action, devpath, NULL };
+
+	snprintf(action, sizeof(action), "ACTION=%s", msg->action);
+	snprintf(devpath, sizeof(devpath), "DEVPATH=%s", msg->devpath);
 
 	pid = fork();
 	switch (pid) {
 	case 0:
 		/* child */
-		execl(UDEV_BIN, "udev", msg->subsystem, NULL);
+		execle(UDEV_BIN, "udev", msg->subsystem, NULL, env);
 		dbg("exec of child failed");
 		exit(1);
 		break;
@@ -282,6 +286,7 @@
 
 	init_logging("udevd");
 
+	/* set signal handler */
 	signal(SIGINT, sig_handler);
 	signal(SIGTERM, sig_handler);
 	signal(SIGALRM, sig_handler);
@@ -304,7 +309,7 @@
 	}
 
 	/* the bind takes care of ensuring only one copy running */
-	retval = bind(ssock, &saddr, addrlen);
+	retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
 	if (retval < 0) {
 		dbg("bind failed\n");
 		goto exit;
udevsend.c +1 -1
diff -Nru a/udevsend.c b/udevsend.c
@@ -174,7 +174,7 @@
 	/* If we can't send, try to start daemon and resend message */
 	loop = UDEVSEND_CONNECT_RETRY;
 	while (loop--) {
-		retval = sendto(sock, &message, size, 0, (struct sockaddr*)&saddr, addrlen);
+		retval = sendto(sock, &message, size, 0, (struct sockaddr *)&saddr, addrlen);
 		if (retval != -1) {
 			retval = 0;
 			goto close_and_exit;
#1 lore raw
Quoted 9 lines · Kay Sievers

It seems that today was just another udev-sunday for me :)

Here is a working patch to compile udevd with klibc.

It's sweet the static binary takes 6 kbytes and it runs with only 80 kbytes virtual memory.

I changed a few peaces and added a siginterrupt.c file to klibc. We may check with hpa to get the changes upstream?

So here is the next try :)
hpa, for good reason, didn't like my changes to klibc.
He will dump signal() completely from klibc instead, so here we switch to sigaction() and keep udevd working with klibc.

thanks,
Kay

Makefile +1 -3
diff -Nru a/Makefile b/Makefile
@@ -157,7 +157,6 @@
 		-I$(LINUX_INCLUDE_DIR)
 	LIB_OBJS =
 	LDFLAGS = --static --nostdlib -nostartfiles -nodefaultlibs
-	UDEVD =
 else
 	WARNINGS += -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations
 	CRT0 =
@@ -165,12 +164,11 @@
 	CFLAGS += $(WARNINGS) -I$(GCCINCDIR)
 	LIB_OBJS = -lc
 	LDFLAGS =
-	UDEVD = $(DAEMON)
 endif
 
 CFLAGS += -I$(PWD)/libsysfs
 
-all: $(ROOT) $(SENDER) $(UDEVD) $(HELPER)
+all: $(ROOT) $(SENDER) $(DAEMON) $(HELPER)
 	@extras="$(EXTRAS)" ; for target in $$extras ; do \
 		echo $$target ; \
 		$(MAKE) prefix=$(prefix) \
udev.c +6 -4
diff -Nru a/udev.c b/udev.c
@@ -42,14 +42,12 @@
 
 static void sig_handler(int signum)
 {
-	dbg("caught signal %d", signum);
 	switch (signum) {
 		case SIGINT:
 		case SIGTERM:
 			sysbus_disconnect();
 			udevdb_exit();
 			exit(20 + signum);
-			break;
 		default:
 			dbg("unhandled signal");
 	}
@@ -95,6 +93,7 @@
 	char *subsystem;
 	int retval = -EINVAL;
 	int i;
+	struct sigaction act;
 
 	action = get_action();
 	if (!action) {
@@ -141,8 +140,11 @@
 	}
 
 	/* set up a default signal handler for now */
-	signal(SIGINT, sig_handler);
-	signal(SIGTERM, sig_handler);
+	act.sa_handler = sig_handler;
+	sigemptyset (&act.sa_mask);
+	act.sa_flags = SA_RESTART;
+	sigaction(SIGINT, &act, NULL);
+	sigaction(SIGTERM, &act, NULL);
 
 	/* initialize the naming deamon */
 	namedev_init();
udevd.c +18 -10
diff -Nru a/udevd.c b/udevd.c
@@ -108,14 +108,18 @@
 static void udev_run(struct hotplug_msg *msg)
 {
 	pid_t pid;
-	setenv("ACTION", msg->action, 1);
-	setenv("DEVPATH", msg->devpath, 1);
+	char action[32];
+	char devpath[256];
+	char *env[] = { action, devpath, NULL };
+
+	snprintf(action, sizeof(action), "ACTION=%s", msg->action);
+	snprintf(devpath, sizeof(devpath), "DEVPATH=%s", msg->devpath);
 
 	pid = fork();
 	switch (pid) {
 	case 0:
 		/* child */
-		execl(UDEV_BIN, "udev", msg->subsystem, NULL);
+		execle(UDEV_BIN, "udev", msg->subsystem, NULL, env);
 		dbg("exec of child failed");
 		exit(1);
 		break;
@@ -279,17 +283,21 @@
 	struct sockaddr_un saddr;
 	socklen_t addrlen;
 	int retval;
+	struct sigaction act;
 
 	init_logging("udevd");
 
-	signal(SIGINT, sig_handler);
-	signal(SIGTERM, sig_handler);
-	signal(SIGALRM, sig_handler);
-	signal(SIGCHLD, sig_handler);
+	/* set signal handler */
+	act.sa_handler = sig_handler;
+	sigemptyset (&act.sa_mask);
+	act.sa_flags = SA_RESTART;
+	sigaction(SIGINT, &act, NULL);
+	sigaction(SIGTERM, &act, NULL);
 
 	/* we want these two to interrupt system calls */
-	siginterrupt(SIGALRM, 1);
-	siginterrupt(SIGCHLD, 1);
+	act.sa_flags = 0;
+	sigaction(SIGALRM, &act, NULL);
+	sigaction(SIGCHLD, &act, NULL);
 
 	memset(&saddr, 0x00, sizeof(saddr));
 	saddr.sun_family = AF_LOCAL;
@@ -304,7 +312,7 @@
 	}
 
 	/* the bind takes care of ensuring only one copy running */
-	retval = bind(ssock, &saddr, addrlen);
+	retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
 	if (retval < 0) {
 		dbg("bind failed\n");
 		goto exit;
udevsend.c +1 -1
diff -Nru a/udevsend.c b/udevsend.c
@@ -174,7 +174,7 @@
 	/* If we can't send, try to start daemon and resend message */
 	loop = UDEVSEND_CONNECT_RETRY;
 	while (loop--) {
-		retval = sendto(sock, &message, size, 0, (struct sockaddr*)&saddr, addrlen);
+		retval = sendto(sock, &message, size, 0, (struct sockaddr *)&saddr, addrlen);
 		if (retval != -1) {
 			retval = 0;
 			goto close_and_exit;
#2 lore raw
In reply to Kay Sievers
Quoted 8 lines · Kay Sievers
Quoted 7 lines · Kay Sievers

It seems that today was just another udev-sunday for me :)

Here is a working patch to compile udevd with klibc.

It's sweet the static binary takes 6 kbytes and it runs with only 80 kbytes virtual memory.

Quoted 7 lines
Quoted 2 lines

I changed a few peaces and added a siginterrupt.c file to klibc. We may check with hpa to get the changes upstream?

So here is the next try :)
hpa, for good reason, didn't like my changes to klibc.
He will dump signal() completely from klibc instead, so here we switch to sigaction() and keep udevd working with klibc.

Very nice, I've applied this.

thanks,

greg k-h

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel