$OpenBSD: patch-sysdeps_openbsd_procopenfiles_c,v 1.5 2014/02/03 13:50:32 ajacoutot Exp $

RM at next release.

--- sysdeps/openbsd/procopenfiles.c.orig	Sat Jul 27 14:40:23 2013
+++ sysdeps/openbsd/procopenfiles.c	Sun Feb  2 12:55:51 2014
@@ -3,6 +3,7 @@
 /* Copyright (C) 1998-99 Martin Baulig
    Copyright (C) 2004 Nicol\ufffds Lichtmaier
    Copyright (C) 2007 Joe Marcus Clarke
+   Copyright (C) 2013 Robert Nagy
    This file is part of LibGTop 1.0.
 
    Modified by Nicol\ufffds Lichtmaier to give a process open files.
@@ -33,7 +34,12 @@
 #include <sys/socket.h>
 #include <sys/param.h>
 #include <sys/sysctl.h>
+#define _KERNEL
+#include <sys/file.h>
+#undef _KERNEL
+#include <sys/mount.h>
 #include <sys/un.h>
+#include <sys/vnode.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
@@ -46,6 +52,20 @@ static const unsigned long _glibtop_sysdeps_proc_open_
 (1L << GLIBTOP_PROC_OPEN_FILES_TOTAL)|
 (1L << GLIBTOP_PROC_OPEN_FILES_SIZE);
 
+kvm_t *kd;
+
+struct filearg {
+	SLIST_ENTRY(filearg) next;
+	dev_t dev;
+	ino_t ino;
+	char *name;
+	TAILQ_HEAD(fuserhead, fuser) fusers;
+};
+
+SLIST_HEAD(fileargs, filearg);
+
+struct fileargs fileargs = SLIST_HEAD_INITIALIZER(fileargs);
+
 /* Init function. */
 
 void
@@ -54,167 +74,121 @@ _glibtop_init_proc_open_files_s (glibtop *server)
 	server->sysdeps.proc_open_files = _glibtop_sysdeps_proc_open_files;
 }
 
-static GArray *
-parse_output(const char *output) {
-	GArray *entries;
-	char **lines;
-	char  *ftype = NULL;
-	char  *fname = NULL;
-	guint  i;
-	guint  len;
-	int    fd = -1;
+static char *
+addr_to_string(struct kinfo_file *kf)
+{
+	char *buffer = NULL;
+	struct in_addr faddr;
 
-	entries = g_array_new(FALSE, FALSE, sizeof(glibtop_open_files_entry));
+	memcpy(&faddr, kf->inp_faddru, sizeof(faddr));
 
-	lines = g_strsplit(output, "\n", 0);
-	len = g_strv_length(lines);
+	if ((kf->so_protocol == IPPROTO_TCP) ||
+	    (kf->so_protocol == IPPROTO_UDP)) {
+		if (kf->inp_fport) {
+			buffer = g_strdup(faddr.s_addr == INADDR_ANY ? "*" :
+ 			    inet_ntoa(faddr));
+		}
+	}
 
-	for (i = 0; i < len && lines[i]; i++) {
-		glibtop_open_files_entry entry = {0};
+	return buffer;
+}
 
-		if (strlen(lines[i]) < 2)
-			continue;
+glibtop_open_files_entry *
+glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf,	pid_t pid)
+{
+	struct kinfo_file *kf, *kflast;
+	int cnt;
+	char kbuf[_POSIX2_LINE_MAX];
+	GArray *entries;
 
-		if (!g_str_has_prefix(lines[i], "f") &&
-		    !g_str_has_prefix(lines[i], "t") &&
-		    !g_str_has_prefix(lines[i], "n"))
-			continue;
+	memset(buf, 0, sizeof (glibtop_proc_open_files));
 
-		if (g_str_has_prefix(lines[i], "f")) {
-			if (!g_ascii_isdigit(*(lines[i] + 1)))
-				i += 2;
-			else
-				fd = atoi(lines[i] + 1);
-			continue;
-		}
+	if ((kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, kbuf)) == NULL)
+		g_warning("%s", kbuf);
 
-		if (g_str_has_prefix(lines[i], "t")) {
-			ftype = lines[i];
-			ftype++;
-			continue;
-		} else {
-			fname = lines[i];
-			fname++;
-		}
+	if ((kf = kvm_getfiles(kd, KERN_FILE_BYPID, pid, sizeof(*kf), &cnt)) == NULL)
+		g_warning("%s", kvm_geterr(kd));
 
-		if (ftype == NULL || fname == NULL)
+	entries = g_array_new(FALSE, FALSE, sizeof(glibtop_open_files_entry));
+
+	for (kflast = &kf[cnt]; kf < kflast; ++kf) {
+		glibtop_open_files_entry entry = {0};
+
+		if (kf->fd_fd < 0)
 			continue;
 
-		if (!strcmp(ftype, "unix")) {
-			entry.type = GLIBTOP_FILE_TYPE_LOCALSOCKET;
-			g_strlcpy(entry.info.localsock.name, fname,
-			  	  sizeof(entry.info.localsock.name));
-		} else if (!strcmp(ftype, "PIPE")) {
-			entry.type = GLIBTOP_FILE_TYPE_PIPE;
-		} else if (!strcmp(ftype, "VREG") ||
-			   !strcmp(ftype, "GDIR") ||
-			   !strcmp(ftype, "GREG") ||
-			   !strcmp(ftype, "VCHR") ||
-			   !strcmp(ftype, "VBLK") ||
-			   !strcmp(ftype, "DIR")  ||
-			   !strcmp(ftype, "LINK") ||
-			   !strcmp(ftype, "REG")  ||
-			   !strcmp(ftype, "VDIR")) {
-			entry.type = GLIBTOP_FILE_TYPE_FILE;
-			g_strlcpy(entry.info.file.name, fname,
-				  sizeof(entry.info.file.name));
-		} else if (!strcmp(ftype, "IPv4")) {
-			char **hosts;
-			char **remote_host;
+		if (kf->f_type == DTYPE_SOCKET) {
+			if (kf->so_family == AF_LOCAL) {
+				entry.type = GLIBTOP_FILE_TYPE_LOCALSOCKET;
+				static char *stypename[] = {
+					"unused",	/* 0 */
+					"stream",
+					"dgram",
+					"raw",
+					"rdm",
+					"seqpak"
+				};
+#define STYPEMAX 5
+				char *stype, stypebuf[24];
 
-			if (!strstr(fname, "->")) {
-				remote_host = g_strsplit(fname, ":", 0);
-			} else {
-				hosts = g_strsplit(fname, "->", 0);
-				if (g_strv_length(hosts) < 2) {
-					g_strfreev(hosts);
-					continue;
+				if (kf->so_type > STYPEMAX) {
+					snprintf(stypebuf, sizeof(stypebuf), "?%d", kf->so_type);
+					stype = stypebuf;
+				} else {
+					stype = stypename[kf->so_type];
 				}
+				g_strlcpy(entry.info.localsock.name, stype,
+				    sizeof(entry.info.localsock.name));
+			} else if (kf->so_family == AF_INET ||
+				   kf->so_family == AF_INET6) {
+				char *addrstr;
 
-				remote_host = g_strsplit(hosts[1], ":", 0);
-				g_strfreev(hosts);
-			}
+				if (kf->so_family == AF_INET)
+					entry.type = GLIBTOP_FILE_TYPE_INETSOCKET;
+				else
+					entry.type = GLIBTOP_FILE_TYPE_INET6SOCKET;
 
-			if (g_strv_length(remote_host) < 2) {
-				g_strfreev(remote_host);
-				continue;
-			}
-
-			entry.type = GLIBTOP_FILE_TYPE_INETSOCKET;
-			if (!strcmp(remote_host[0], "*"))
-				g_strlcpy(entry.info.sock.dest_host, "0.0.0.0",
-					  sizeof(entry.info.sock.dest_host));
-			else
+				addrstr = addr_to_string(kf);
 				g_strlcpy(entry.info.sock.dest_host,
-					  remote_host[0],
+					  addrstr,
 					  sizeof(entry.info.sock.dest_host));
-			entry.info.sock.dest_port = atoi(remote_host[1]);
+				g_free(addrstr);
+				entry.info.sock.dest_port = kf->inp_fport ? ntohs(kf->inp_fport) : 0;
+			}
+		} else if (kf->f_type == DTYPE_PIPE) {
+			entry.type = GLIBTOP_FILE_TYPE_PIPE;
+		} else if (kf->f_type == DTYPE_VNODE) {
+			int badtype = 0;
+			char nbuf[MAXPATHLEN];
 
-			g_strfreev(remote_host);
-		} else if (!strcmp(ftype, "IPv6")) {
-			char **hosts;
-			char **remote_host;
+			entry.type = GLIBTOP_FILE_TYPE_FILE;
 
-			if (!strstr(fname, "->")) {
-				remote_host = g_strsplit(fname, ":", 0);
-			} else {
-				hosts = g_strsplit(fname, "->", 0);
-				if (g_strv_length(hosts) < 2) {
-					g_strfreev(hosts);
-					continue;
-				}
-
-				remote_host = g_strsplit(hosts[1], "]", 0);
-				g_strfreev(hosts);
+			if ((kf->v_type == VNON) ||
+			    (kf->v_type == VBAD) ||
+			    (kf->v_tag == VT_NON && !(kf->v_flag & VCLONE))) {
+				badtype = 1;
 			}
 
-			if (g_strv_length(remote_host) < 2) {
-				g_strfreev(remote_host);
+			if (badtype)
 				continue;
-			}
 
-			entry.type = GLIBTOP_FILE_TYPE_INET6SOCKET;
-			if (!strcmp(remote_host[0], "*"))
-				g_strlcpy(entry.info.sock.dest_host, "0.0.0.0",
-					  sizeof(entry.info.sock.dest_host));
-			else
-				g_strlcpy(entry.info.sock.dest_host,
-					  remote_host[0] + 1,
-					  sizeof(entry.info.sock.dest_host));
-			entry.info.sock.dest_port = atoi(remote_host[1] + 1);
-
-			g_strfreev(remote_host);
+			if ((kf->v_type == VBLK) ||
+			    (kf->v_type == VCHR)) {
+				snprintf(nbuf, sizeof(nbuf), "/dev/%s",
+				    devname(kf->va_rdev, kf->v_type == VCHR ?  S_IFCHR : S_IFBLK));
+			} else {
+				snprintf(nbuf, sizeof(nbuf), "inode %llu on %s", kf->va_fileid,
+				    kf->f_mntonname);
+			}
+			g_strlcpy(entry.info.file.name, nbuf,
+			    sizeof(entry.info.file.name));
 		} else
 			continue;
 
-		entry.fd = fd;
+		entry.fd = kf->fd_fd;
 
-		fd = -1;
-		ftype = NULL;
-		fname = NULL;
-
 		g_array_append_val(entries, entry);
 	}
-
-	g_strfreev(lines);
-
-	return entries;
-}
-
-glibtop_open_files_entry *
-glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf,	pid_t pid)
-{
-	char *output;
-	GArray *entries;
-
-	memset(buf, 0, sizeof (glibtop_proc_open_files));
-
-	output = execute_lsof(pid);
-	if (output == NULL) return NULL;
-
-	entries = parse_output(output);
-
-	g_free(output);
 
 	buf->flags = _glibtop_sysdeps_proc_open_files;
 	buf->number = entries->len;
