From 77cc7c4b8e78031b4f11760251442a266152299a Mon Sep 17 00:00:00 2001
From: Antonio Huete <tuxillo@quantumachine.net>
Date: Sun, 29 Nov 2009 18:03:19 +0100
Subject: [PATCH] sysctl - Make system call vector name of a process be
          available to the userland.

Process visibility is still controlled by ps_showallprocs,
so FBSD's *_cansee() functions have been ignored.

Also, while here, clear some trailing whitespaces.

Obtained-from: FreeBSD
---
 sys/kern/kern_proc.c |   63 ++++++++++++++++++++++++++++++++++---------------
 sys/sys/sysctl.h     |    1 +
 2 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c
index 797d51a..7fb0ae3 100644
--- a/sys/kern/kern_proc.c
+++ b/sys/kern/kern_proc.c
@@ -41,6 +41,7 @@
 #include <sys/sysctl.h>
 #include <sys/malloc.h>
 #include <sys/proc.h>
+#include <sys/sysent.h>
 #include <sys/jail.h>
 #include <sys/filedesc.h>
 #include <sys/tty.h>
@@ -615,7 +616,7 @@ DB_SHOW_COMMAND(pgrpdump, pgrpdump)
 				    pgrp->pg_session->s_count,
 				    (void *)LIST_FIRST(&pgrp->pg_members));
 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
-					kprintf("\t\tpid %ld addr %p pgrp %p\n", 
+					kprintf("\t\tpid %ld addr %p pgrp %p\n",
 					    (long)p->p_pid, (void *)p,
 					    (void *)p->p_pgrp);
 				}
@@ -746,7 +747,7 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
 			switch (oid) {
 			case KERN_PROC_PGRP:
 				/* could do this by traversing pgrp */
-				if (p->p_pgrp == NULL || 
+				if (p->p_pgrp == NULL ||
 				    p->p_pgrp->pg_id != (pid_t)name[0])
 					continue;
 				break;
@@ -755,19 +756,19 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
 				if ((p->p_flag & P_CONTROLT) == 0 ||
 				    p->p_session == NULL ||
 				    p->p_session->s_ttyp == NULL ||
-				    dev2udev(p->p_session->s_ttyp->t_dev) != 
+				    dev2udev(p->p_session->s_ttyp->t_dev) !=
 					(udev_t)name[0])
 					continue;
 				break;
 
 			case KERN_PROC_UID:
-				if (p->p_ucred == NULL || 
+				if (p->p_ucred == NULL ||
 				    p->p_ucred->cr_uid != (uid_t)name[0])
 					continue;
 				break;
 
 			case KERN_PROC_RUID:
-				if (p->p_ucred == NULL || 
+				if (p->p_ucred == NULL ||
 				    p->p_ucred->cr_ruid != (uid_t)name[0])
 					continue;
 				break;
@@ -826,10 +827,31 @@ post_threads:
 	return (0);
 }
 
+static int
+sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
+{
+	struct proc *p;
+	char *sv_name;
+	int *name;
+	int namelen;
+
+	namelen = arg2;
+	if (namelen != 1)
+		return (EINVAL);
+
+	name = (int *)arg1;
+	if ((p = pfind((pid_t)name[0])) == NULL)
+		return (0);
+
+	sv_name = p->p_sysent->sv_name;
+
+	return (sysctl_handle_string(oidp, sv_name, 0, req));
+}
+
 /*
  * This sysctl allows a process to retrieve the argument list or process
  * title for another process without groping around in the address space
- * of the other process.  It also allow a process to set its own "process 
+ * of the other process.  It also allow a process to set its own "process
  * title to a string of its own choice.
  */
 static int
@@ -842,7 +864,7 @@ sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
 	int error = 0;
 	struct ucred *cr1 = curproc->p_ucred;
 
-	if (namelen != 1) 
+	if (namelen != 1)
 		return (EINVAL);
 
 	p = pfind((pid_t)name[0]);
@@ -860,14 +882,14 @@ sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
 	if (req->newptr == NULL)
 		return (error);
 
-	if (p->p_args && --p->p_args->ar_ref == 0) 
+	if (p->p_args && --p->p_args->ar_ref == 0)
 		FREE(p->p_args, M_PARGS);
 	p->p_args = NULL;
 
 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
 		return (error);
 
-	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen, 
+	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
 	    M_PARGS, M_WAITOK);
 	pa->ar_ref = 1;
 	pa->ar_length = req->newlen;
@@ -884,38 +906,41 @@ SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
 
-SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
-SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD, 
+SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
 	sysctl_kern_proc, "Process table");
 
 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
 	sysctl_kern_proc_args, "Process argument list");
+
+SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
+	sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");
diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h
index f8023fb..4ce0fea 100644
--- a/sys/sys/sysctl.h
+++ b/sys/sys/sysctl.h
@@ -433,6 +433,7 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
 #define	KERN_PROC_UID		5	/* by effective uid */
 #define	KERN_PROC_RUID		6	/* by real uid */
 #define	KERN_PROC_ARGS		7	/* get/set arguments/proctitle */
+#define KERN_PROC_SV_NAME       9       /* get syscall vector name */
 
 #define KERN_PROC_FLAGMASK	0x10
 #define KERN_PROC_FLAG_LWP	0x10
-- 
1.6.4

