Просмотр исходного кода

s-s-d: Swap pid_is_cmd() and pid_is_exec() libkvm implementations

pid_is_exec() was checking for the process name while pid_is_cmd() was
checking for the (supposed) executable pathname, so they were performing
each other's task.
Guillem Jover лет назад: 14
Родитель
Сommit
e07f75e6fd
2 измененных файлов с 35 добавлено и 33 удалено
  1. 2 0
      debian/changelog
  2. 33 33
      utils/start-stop-daemon.c

+ 2 - 0
debian/changelog

@@ -50,6 +50,8 @@ dpkg (1.16.2) UNRELEASED; urgency=low
   * Use a different temporary file per process on libcompat's vsnprintf()
     function to avoid race conditions from childs after fork(3).
     Reported by Daniel Ruoso <daniel@ruoso.com>. Closes: #655411
+  * Fix start-stop-daemon --exec and --name options on FreeBSD, NetBSD and
+    OpenBSD by swapping the process matching implementations.
 
   [ Raphaël Hertzog ]
   * Update Dpkg::Shlibs to look into multiarch paths when cross-building

+ 33 - 33
utils/start-stop-daemon.c

@@ -1018,9 +1018,11 @@ static bool
 pid_is_exec(pid_t pid, const char *name)
 {
 	kvm_t *kd;
-	int nentries;
+	int nentries, argv_len = 0;
 	struct kinfo_proc *kp;
-	char errbuf[_POSIX2_LINE_MAX], *pidexec;
+	char errbuf[_POSIX2_LINE_MAX], buf[_POSIX2_LINE_MAX];
+	char **pid_argv_p;
+	char *start_argv_0_p, *end_argv_0_p;
 
 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
 	if (kd == NULL)
@@ -1028,10 +1030,31 @@ pid_is_exec(pid_t pid, const char *name)
 	kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
 	if (kp == NULL)
 		errx(1, "%s", kvm_geterr(kd));
-	pidexec = (&kp->kp_proc)->p_comm;
-	if (strlen(name) != strlen(pidexec))
+	pid_argv_p = kvm_getargv(kd, kp, argv_len);
+	if (pid_argv_p == NULL)
+		errx(1, "%s", kvm_geterr(kd));
+
+	/* Find and compare string. */
+	start_argv_0_p = *pid_argv_p;
+
+	/* Find end of argv[0] then copy and cut of str there. */
+	end_argv_0_p = strchr(*pid_argv_p, ' ');
+	if (end_argv_0_p == NULL)
+		/* There seems to be no space, so we have the command
+		 * already in its desired form. */
+		start_argv_0_p = *pid_argv_p;
+	else {
+		/* Tests indicate that this never happens, since
+		 * kvm_getargv itself cuts of tailing stuff. This is
+		 * not what the manpage says, however. */
+		strncpy(buf, *pid_argv_p, (end_argv_0_p - start_argv_0_p));
+		buf[(end_argv_0_p - start_argv_0_p) + 1] = '\0';
+		start_argv_0_p = buf;
+	}
+
+	if (strlen(name) != strlen(start_argv_0_p))
 		return false;
-	return (strcmp(name, pidexec) == 0) ? 1 : 0;
+	return (strcmp(name, start_argv_0_p) == 0) ? 1 : 0;
 }
 #endif
 
@@ -1139,11 +1162,9 @@ static bool
 pid_is_cmd(pid_t pid, const char *name)
 {
 	kvm_t *kd;
-	int nentries, argv_len = 0;
+	int nentries;
 	struct kinfo_proc *kp;
-	char errbuf[_POSIX2_LINE_MAX], buf[_POSIX2_LINE_MAX];
-	char **pid_argv_p;
-	char *start_argv_0_p, *end_argv_0_p;
+	char errbuf[_POSIX2_LINE_MAX], *pidexec;
 
 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
 	if (kd == NULL)
@@ -1151,31 +1172,10 @@ pid_is_cmd(pid_t pid, const char *name)
 	kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
 	if (kp == NULL)
 		errx(1, "%s", kvm_geterr(kd));
-	pid_argv_p = kvm_getargv(kd, kp, argv_len);
-	if (pid_argv_p == NULL)
-		errx(1, "%s", kvm_geterr(kd));
-
-	/* Find and compare string. */
-	start_argv_0_p = *pid_argv_p;
-
-	/* Find end of argv[0] then copy and cut of str there. */
-	end_argv_0_p = strchr(*pid_argv_p, ' ');
-	if (end_argv_0_p == NULL)
-		/* There seems to be no space, so we have the command
-		 * already in its desired form. */
-		start_argv_0_p = *pid_argv_p;
-	else {
-		/* Tests indicate that this never happens, since
-		 * kvm_getargv itself cuts of tailing stuff. This is
-		 * not what the manpage says, however. */
-		strncpy(buf, *pid_argv_p, (end_argv_0_p - start_argv_0_p));
-		buf[(end_argv_0_p - start_argv_0_p) + 1] = '\0';
-		start_argv_0_p = buf;
-	}
-
-	if (strlen(name) != strlen(start_argv_0_p))
+	pidexec = (&kp->kp_proc)->p_comm;
+	if (strlen(name) != strlen(pidexec))
 		return false;
-	return (strcmp(name, start_argv_0_p) == 0) ? 1 : 0;
+	return (strcmp(name, pidexec) == 0) ? 1 : 0;
 }
 #endif