common: FreeBSD+kFreeBSD: Implement get_process_name (same as in Linux)
authorMathieu Parent <math.parent@gmail.com>
Mon, 14 Jan 2013 11:13:24 +0000 (12:13 +0100)
committerAmitay Isaacs <amitay@gmail.com>
Tue, 22 Jan 2013 07:03:44 +0000 (18:03 +1100)
Signed-off-by: Mathieu Parent <math.parent@gmail.com>
(This used to be ctdb commit 258092aaf6b7a9bdc14f0fb35e8bd7f7dc742b3f)

ctdb/common/system_freebsd.c
ctdb/common/system_kfreebsd.c

index 2fb3f45ebd09841928278b6b0e4354ae821b2d21..1ce9d3d16dad4d9b3b489c7085c78e451c102624 100644 (file)
@@ -372,7 +372,21 @@ int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
 
 char *ctdb_get_process_name(pid_t pid)
 {
-       /* FIXME FreeBSD: get_process_name not implemented */
+       char path[32];
+       char buf[PATH_MAX];
+       char *ptr;
+       int n;
+
+       snprintf(path, sizeof(path), "/proc/%d/exe", pid);
+       n = readlink(path, buf, sizeof(buf));
+       if (n < 0) {
+               return NULL;
+       }
+
+       /* Remove any extra fields */
+       buf[n] = '\0';
+       ptr = strtok(buf, " ");
+       return strdup(ptr);
        return NULL;
 }
 
index 85442ed28d58cacd83e27a7635960fb52ad8c7ae..103c1e0692682c90f99f3c77200c13b342c7da25 100644 (file)
@@ -365,8 +365,21 @@ int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
 
 char *ctdb_get_process_name(pid_t pid)
 {
-       /* FIXME kFreeBSD: get_process_name not implemented */
-       return NULL;
+       char path[32];
+       char buf[PATH_MAX];
+       char *ptr;
+       int n;
+
+       snprintf(path, sizeof(path), "/proc/%d/exe", pid);
+       n = readlink(path, buf, sizeof(buf));
+       if (n < 0) {
+               return NULL;
+       }
+
+       /* Remove any extra fields */
+       buf[n] = '\0';
+       ptr = strtok(buf, " ");
+       return strdup(ptr);
 }
 
 bool ctdb_get_lock_info(pid_t req_pid, struct ctdb_lock_info *lock_info)