[PATCH] start-stop-daemon: make --exec follow symlinks.

Joakim Tjernlund joakim.tjernlund at transmode.se
Fri Apr 18 09:32:48 UTC 2008


Currently start-stop-daemon --stop --exec <file> does not follow
symlinks and this is wrong.

This patch uses stat(2) to get the file's st_dev and st_ino and searches
for a matching pair. A positive side effect is the right file is always
found. The code is also smaller.

The stat(2) method is also used by debian.
This patch is also against 1.8.2 gentoo

--- a/debianutils/start_stop_daemon.c.org	2008-04-18 11:01:39.000000000 +0200
+++ b/debianutils/start_stop_daemon.c	2008-04-18 11:02:43.000000000 +0200
@@ -23,6 +23,7 @@
 static char *userspec;
 static char *cmdname;
 static char *execname;
+static struct stat execstat;
 static char *pidfile;
 static smallint quiet;
 
@@ -36,19 +37,15 @@
 static int pid_is_exec(pid_t pid, const char *name)
 {
 	char buf[sizeof("/proc//exe") + sizeof(int)*3];
-	char *execbuf;
-	int n;
+	struct stat st;
 
 	sprintf(buf, "/proc/%u/exe", pid);
-	n = strlen(name) + 1;
-	execbuf = xzalloc(n + 1);
-	readlink(buf, execbuf, n);
-
-	/* if readlink fails, execbuf still contains "" */
-	n = strcmp(execbuf, name);
-	if (ENABLE_FEATURE_CLEAN_UP)
-		free(execbuf);
-	return !n; /* nonzero (true) if execbuf == name */
+	if (stat(buf, &st) <0)
+		return 0;
+	if (st.st_dev == execstat.st_dev &&
+	    st.st_ino == execstat.st_ino)
+		return 1;
+	return 0;
 }
 
 static int pid_is_user(int pid, int uid)
@@ -288,6 +285,8 @@
 		if (errno)
 			user_id = xuname2uid(userspec);
 	}
+	if (execname)
+		stat(execname, &execstat);
 
 	if (opt & CTX_STOP) {
 		int i = do_stop();




More information about the busybox mailing list