On Wednesday 30 April 2008 15:20:27 Roy Marples wrote:
> Sure
> Refactored against svn
> function                                             old     new   delta
> check                                                755     847     +92
> start_stop_daemon_main                              1012    1034     +22
> .rodata                                           119312  119323     +11
> ---------------------------------------------------------------------------
>--- (add/remove: 0/0 grow/shrink: 3/0 up/down: 125/0)             Total: 125
> bytes

And re-done again against the even newer svn

function                                             old     new   delta
check                                                755     847     +92
start_stop_daemon_main                               984    1006     +22
.rodata                                           119312  119323     +11
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 125/0)             Total: 125 bytes

Thanks

Roy
diff -ur a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
--- a/debianutils/start_stop_daemon.c	2008-04-30 15:52:46.000000000 +0100
+++ b/debianutils/start_stop_daemon.c	2008-04-30 15:55:28.000000000 +0100
@@ -51,7 +51,6 @@
 	char *pidfile;
 	int user_id;
 	smallint signal_nr;
-	struct stat execstat;
 };
 #define G (*(struct globals*)&bb_common_bufsiz1)
 #define found             (G.found               )
@@ -61,7 +60,6 @@
 #define pidfile           (G.pidfile             )
 #define user_id           (G.user_id             )
 #define signal_nr         (G.signal_nr           )
-#define execstat          (G.execstat            )
 #define INIT_G() \
         do { \
 		user_id = -1; \
@@ -71,15 +69,38 @@
 
 static int pid_is_exec(pid_t pid)
 {
-	struct stat st;
-	char buf[sizeof("/proc//exe") + sizeof(int)*3];
+	char cmdline[32];
+	char *p;
+	ssize_t bytes;
+	char buf[PATH_MAX];
+
+	/* Check it's the right binary */
+	snprintf(cmdline, sizeof(cmdline), "/proc/%u/exe", pid);
+	bytes = readlink(cmdline, buf, sizeof(buf));
+	if (bytes > 0) {
+		buf[bytes] = '\0';
+		if (strcmp(buf, execname) == 0)
+			return 1;
+
+		/* We should cater for deleted binaries too */
+		bytes = strlen(buf);
+		if (bytes > 10) {
+			p = buf + (bytes - 10);
+			if (strcmp(p, " (deleted)") == 0) {
+				*p = '\0';
+				if (strcmp(buf, execname) == 0)
+					return 1;
+			}
+		}
+	}
 
-	sprintf(buf, "/proc/%u/exe", pid);
-	if (stat(buf, &st) < 0)
-		return 0;
-	if (st.st_dev == execstat.st_dev
-	 && st.st_ino == execstat.st_ino)
-		return 1;
+	snprintf(cmdline, sizeof(cmdline), "/proc/%u/cmdline", pid);
+	bytes = open_read_close(cmdline, buf, sizeof(buf));
+	if (bytes > 0) {
+		buf[bytes] = '\0';
+		if (strcmp(buf, execname) == 0)
+			return 1;
+	}
 	return 0;
 }
 
@@ -257,6 +278,7 @@
 	char *signame;
 	char *startas;
 	char *chuid;
+	struct stat execstat;
 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
 //	char *retry_arg = NULL;
 //	int retries = -1;
@@ -304,9 +326,6 @@
 		if (errno)
 			user_id = xuname2uid(userspec);
 	}
-	if (execname)
-		xstat(execname, &execstat);
-
 	do_procinit(); /* Both start and stop needs to know current processes */
 
 	if (opt & CTX_STOP) {
@@ -319,6 +338,10 @@
 			printf("%s already running\n%d\n", execname, found->pid);
 		return !(opt & OPT_OKNODO);
 	}
+
+	if (execname)
+		xstat(execname, &execstat);
+
 	*--argv = startas;
 	if (opt & OPT_BACKGROUND) {
 #if BB_MMU
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to