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

s-s-d: Port process handling to Mac OS X

Based-on-a-patch-by: Mo McRoberts <mo@nevali.net>
Signed-off-by: Guillem Jover <guillem@debian.org>
Guillem Jover лет назад: 10
Родитель
Сommit
47f9afbd7b
2 измененных файлов с 89 добавлено и 0 удалено
  1. 2 0
      debian/changelog
  2. 87 0
      utils/start-stop-daemon.c

+ 2 - 0
debian/changelog

@@ -40,6 +40,8 @@ dpkg (1.18.11) UNRELEASED; urgency=medium
       start-stop-daemon. This affects Mac OS X.
       start-stop-daemon. This affects Mac OS X.
     - On FreeBSD return STATUS_UNKNOWN instead of false in start-stop-daemon
     - On FreeBSD return STATUS_UNKNOWN instead of false in start-stop-daemon
       do_procinit().
       do_procinit().
+    - Port start-stop-daemon process handling to Mac OS X.
+      Based on a patch by Mo McRoberts <mo@nevali.net>.
   * Perl modules:
   * Perl modules:
     - Obsolete Source-Version substvar in Dpkg::Substvars by emitting errors.
     - Obsolete Source-Version substvar in Dpkg::Substvars by emitting errors.
     - Rework keyring hooks in Dpkg::Vendor. Deprecate the keyrings hook, and
     - Rework keyring hooks in Dpkg::Vendor. Deprecate the keyrings hook, and

+ 87 - 0
utils/start-stop-daemon.c

@@ -41,6 +41,8 @@
 #  define OSNetBSD
 #  define OSNetBSD
 #elif defined(__DragonFly__)
 #elif defined(__DragonFly__)
 #  define OSDragonFlyBSD
 #  define OSDragonFlyBSD
+#elif defined(__APPLE__) && defined(__MACH__)
+#  define OSDarwin
 #else
 #else
 #  error Unknown architecture - cannot build start-stop-daemon
 #  error Unknown architecture - cannot build start-stop-daemon
 #endif
 #endif
@@ -103,6 +105,10 @@
 #include <ps.h>
 #include <ps.h>
 #endif
 #endif
 
 
+#if defined(OSDarwin)
+#include <libproc.h>
+#endif
+
 #ifdef HAVE_KVM_H
 #ifdef HAVE_KVM_H
 #include <kvm.h>
 #include <kvm.h>
 #if defined(OSFreeBSD)
 #if defined(OSFreeBSD)
@@ -1385,6 +1391,21 @@ pid_is_exec(pid_t pid, const struct stat *esb)
 
 
 	return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
 	return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
 }
 }
+#elif defined(OSDarwin)
+static bool
+pid_is_exec(pid_t pid, const struct stat *esb)
+{
+	struct stat sb;
+	char pathname[_POSIX_PATH_MAX];
+
+	if (proc_pidpath(pid, pathname, sizeof(pathname)) < 0)
+		return false;
+
+	if (stat(pathname, &sb) != 0)
+		return false;
+
+	return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
+}
 #elif defined(OShpux)
 #elif defined(OShpux)
 static bool
 static bool
 pid_is_exec(pid_t pid, const struct stat *esb)
 pid_is_exec(pid_t pid, const struct stat *esb)
@@ -1507,6 +1528,17 @@ pid_is_child(pid_t pid, pid_t ppid)
 
 
 	return pi->ppid == ppid;
 	return pi->ppid == ppid;
 }
 }
+#elif defined(OSDarwin)
+static bool
+pid_is_child(pid_t pid, pid_t ppid)
+{
+	struct proc_bsdinfo info;
+
+	if (proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, sizeof(info)) < 0)
+		return false;
+
+	return (pid_t)info.pbi_ppid == ppid;
+}
 #elif defined(OShpux)
 #elif defined(OShpux)
 static bool
 static bool
 pid_is_child(pid_t pid, pid_t ppid)
 pid_is_child(pid_t pid, pid_t ppid)
@@ -1594,6 +1626,17 @@ pid_is_user(pid_t pid, uid_t uid)
 	ps = get_proc_stat(pid, PSTAT_OWNER_UID);
 	ps = get_proc_stat(pid, PSTAT_OWNER_UID);
 	return ps && (uid_t)proc_stat_owner_uid(ps) == uid;
 	return ps && (uid_t)proc_stat_owner_uid(ps) == uid;
 }
 }
+#elif defined(OSDarwin)
+static bool
+pid_is_user(pid_t pid, uid_t uid)
+{
+	struct proc_bsdinfo info;
+
+	if (proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, sizeof(info)) < 0)
+		return false;
+
+	return info.pbi_ruid == uid;
+}
 #elif defined(OShpux)
 #elif defined(OShpux)
 static bool
 static bool
 pid_is_user(pid_t pid, uid_t uid)
 pid_is_user(pid_t pid, uid_t uid)
@@ -1716,6 +1759,17 @@ pid_is_cmd(pid_t pid, const char *name)
 		return false;
 		return false;
 	return (strcmp(pst.pst_ucomm, name) == 0);
 	return (strcmp(pst.pst_ucomm, name) == 0);
 }
 }
+#elif defined(OSDarwin)
+static bool
+pid_is_cmd(pid_t pid, const char *name)
+{
+	char pathname[_POSIX_PATH_MAX];
+
+	if (proc_pidpath(pid, pathname, sizeof(pathname)) < 0)
+		return false;
+
+	return strcmp(pathname, name) == 0;
+}
 #elif defined(OSFreeBSD)
 #elif defined(OSFreeBSD)
 static bool
 static bool
 pid_is_cmd(pid_t pid, const char *name)
 pid_is_cmd(pid_t pid, const char *name)
@@ -1891,6 +1945,39 @@ do_procinit(void)
 	else
 	else
 		return STATUS_DEAD;
 		return STATUS_DEAD;
 }
 }
+#elif defined(OSDarwin)
+static enum status_code
+do_procinit(void)
+{
+	pid_t *pid_buf;
+	int i, npids, pid_bufsize;
+	enum status_code prog_status = STATUS_DEAD;
+
+	npids = proc_listallpids(NULL, 0);
+	if (npids == 0)
+		return STATUS_UNKNOWN;
+
+	/* Try to avoid sudden changes in number of PIDs. */
+	npids += 4096;
+	pid_bufsize = sizeof(pid_t) * npids;
+	pid_buf = xmalloc(pid_bufsize);
+
+	npids = proc_listallpids(pid_buf, pid_bufsize);
+	if (npids == 0)
+		return STATUS_UNKNOWN;
+
+	for (i = 0; i < npids; i++) {
+		enum status_code pid_status;
+
+		pid_status = pid_check(pid_buf[i]);
+		if (pid_status < prog_status)
+			prog_status = pid_status;
+	}
+
+	free(pid_buf);
+
+	return prog_status;
+}
 #elif defined(OShpux)
 #elif defined(OShpux)
 static enum status_code
 static enum status_code
 do_procinit(void)
 do_procinit(void)