tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Linux procfs



christos%astron.com@localhost (Christos Zoulas) wrote:
>In article <x7efu7wmk2.fsf%ren.fdy2.co.uk@localhost>,
>Robert Swindells  <rjs%fdy2.co.uk@localhost> wrote:
>>
>>matthew green <mrg%eterna.com.au@localhost> wrote:
>>>Robert Swindells writes:
>>>> 
>>>> Do we care about keeping all the Linux procfs code in
>>>> sys/miscfs/proc/procfs_linux.c ?
>>>> 
>>>> Some Linux applications have started grovelling in /proc/self/status to
>>>> read various values.
>>>> 
>>>> I have added some Linux emulation specific code to procfs_status.c but
>>>> can move it if people object.
>>>
>>>what are the extensions?  maybe we want them for netbsd anyway?
>>
>>It is mostly just changing the format of what is printed.
>>
>>I haven't replicated everything that is in the procfs node under Linux,
>>just enough to get one application working (Xilinx Vivado). One of the
>>lines is used by the HTML browser in JavaFX though so it will probably
>>be needed elsewhere.
>>
>>>can the code be structured such that most of it is in procfs_linux.c
>>>and called to from procfs_status.c?
>>
>>I think it would be more a case of moving the whole contents of
>>procfs_status.c to procfs_linux.c
>>
>>The diff is below.
>
>The same diff ignoring whitespace becomes:

[snip] 

>Which means that it would be better to refactor this code into separate
>routines (linux specific and netbsd-specific). The function is already
>too long and complicated.

Split up into two functions:

Index: procfs_status.c
===================================================================
RCS file: /cvsroot/src/sys/miscfs/procfs/procfs_status.c,v
retrieving revision 1.37
diff -u -r1.37 procfs_status.c
--- procfs_status.c	14 Nov 2016 08:55:51 -0000	1.37
+++ procfs_status.c	28 Sep 2017 12:20:29 -0000
@@ -88,13 +88,8 @@
 
 #include <miscfs/procfs/procfs.h>
 
-int
-procfs_dostatus(
-    struct lwp *curl,
-    struct lwp *l,
-    struct pfsnode *pfs,
-    struct uio *uio
-)
+static int
+procfs_status_netbsd(struct lwp *l, struct uio *uio)
 {
 	struct session *sess;
 	struct tty *tp;
@@ -107,8 +102,6 @@
 	char psbuf[256+MAXHOSTNAMELEN];		/* XXX - conservative */
 	uint16_t ngroups;
 
-	if (uio->uio_rw != UIO_READ)
-		return (EOPNOTSUPP);
 
 	mutex_enter(proc_lock);
 	mutex_enter(p->p_lock);
@@ -183,3 +176,63 @@
 
 	return (uiomove_frombuf(psbuf, ps - psbuf, uio));
 }
+
+static int
+procfs_status_linux(struct lwp *l, struct uio *uio)
+{
+	kauth_cred_t cr;
+	struct proc *p = l->l_proc;
+	char *ps;
+	int pid, ppid;
+	u_int i;
+	char psbuf[256+MAXHOSTNAMELEN];		/* XXX - conservative */
+	uint16_t ngroups;
+
+	mutex_enter(proc_lock);
+	mutex_enter(p->p_lock);
+
+	pid = p->p_pid;
+	ppid = p->p_ppid;
+
+	ps = psbuf;
+	ps += snprintf(ps, sizeof(psbuf), "Name:\t%s\n", p->p_comm);
+	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Pid:\t%d\n", pid);
+	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "PPid:\t%d\n", ppid);
+	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "TracerPid:\t%d\n", 0);
+
+	cr = p->p_cred;
+	ngroups = kauth_cred_ngroups(cr);
+	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Groups:\t");
+	for (i = 0; i < ngroups; i++)
+		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d ",
+		    kauth_cred_group(cr, i));
+	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
+
+	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
+	    "VmPeak:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
+	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
+	    "VmSize:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
+	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
+	    "VmRSS:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_RSS].rlim_cur / 1024);
+
+	mutex_exit(p->p_lock);
+	mutex_exit(proc_lock);
+
+	return (uiomove_frombuf(psbuf, ps - psbuf, uio));
+}
+
+int
+procfs_dostatus(struct lwp *curl, struct lwp *l, struct pfsnode *pfs,
+    struct uio *uio)
+{
+	const char *emulname = curlwp->l_proc->p_emul->e_name;
+
+	if (uio->uio_rw != UIO_READ)
+		return (EOPNOTSUPP);
+
+	if (strncmp(emulname, "linux", 5) == 0) {
+		return procfs_status_linux(l, uio);
+	} else {
+		return procfs_status_netbsd(l, uio);
+	}
+}


Home | Main Index | Thread Index | Old Index