tech-kern archive

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

Re: Linux procfs



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:

Index: procfs_status.c
===================================================================
RCS file: /cvsroot/src/sys/miscfs/procfs/procfs_status.c,v
retrieving revision 1.37
diff -u -b -w -u -r1.37 procfs_status.c
--- procfs_status.c	14 Nov 2016 08:55:51 -0000	1.37
+++ procfs_status.c	26 Jun 2017 19:32:47 -0000
@@ -102,6 +102,7 @@
 	struct proc *p = l->l_proc;
 	char *ps;
 	const char *sep;
+	const char *emulname = curlwp->l_proc->p_emul->e_name;
 	int pid, ppid, pgid, sid;
 	u_int i;
 	char psbuf[256+MAXHOSTNAMELEN];		/* XXX - conservative */
@@ -119,9 +120,31 @@
 	sess = p->p_pgrp->pg_session;
 	sid = sess->s_sid;
 
+	ps = psbuf;
+	if (strncmp(emulname, "linux", 5) == 0) {
+		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%8lu kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
+		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
+		    "VmSize:\t%8lu kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
+		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
+		    "VmRSS:\t%8lu kB\n", p->p_rlimit[RLIMIT_RSS].rlim_cur / 1024);
+
+	} else {
 /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg uid gid groups ... */
 
-	ps = psbuf;
 	memcpy(ps, p->p_comm, MAXCOMLEN);
 	ps[MAXCOMLEN] = '\0';
 	ps += strlen(ps);
@@ -177,7 +200,7 @@
 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), ",%d",
 		    kauth_cred_group(cr, i));
 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
-
+	}
 	mutex_exit(p->p_lock);
 	mutex_exit(proc_lock);
 

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.

christos



Home | Main Index | Thread Index | Old Index