shithub: riscv

Download patch

ref: d63734eb532c90297561782513a455b0eeae6b0d
parent: 5302e88796f0ad7fb2490e68b72ef6cd417dfb03
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Aug 3 12:02:47 EDT 2014

iostats: bring back standard filedescriptor spying

--- a/sys/src/cmd/iostats.c
+++ b/sys/src/cmd/iostats.c
@@ -263,6 +263,7 @@
 	char buf[64*1024];
 	float brpsec, bwpsec, bppsec;
 	int cpid, fspid, rspid, dbg, n, mflag;
+	char *fds[3];
 	File *fs;
 	Req *r, **rr;
 
@@ -292,11 +293,20 @@
 	if(pipe(pfd) < 0)
 		sysfatal("pipe");
 
+	/* dup std fds to be inherited to exportfs */
+	fds[0] = smprint("/fd/%d", dup(0, -1));
+	fds[1] = smprint("/fd/%d", dup(1, -1));
+	fds[2] = smprint("/fd/%d", dup(2, -1));
+
 	switch(cpid = fork()) {
 	case -1:
 		sysfatal("fork");
 	case 0:
 		close(pfd[1]);
+		close(atoi(strrchr(fds[0], '/')+1));
+		close(atoi(strrchr(fds[1], '/')+1));
+		close(atoi(strrchr(fds[2], '/')+1));
+
 		if(getwd(buf, sizeof(buf)) == 0)
 			sysfatal("no working directory");
 
@@ -305,6 +315,11 @@
 		if(mount(pfd[0], -1, "/", mflag, "") < 0)
 			sysfatal("mount /");
 
+		/* replace std fds with the exported ones */
+		close(0); open(fds[0], OREAD);
+		close(1); open(fds[1], OWRITE);
+		close(2); open(fds[2], OWRITE);
+
 		bind("#c/pid", "/dev/pid", MREPL);
 		bind("#c/ppid", "/dev/ppid", MREPL);
 		bind("#e", "/env", MREPL|MCREATE);
@@ -322,21 +337,6 @@
 
 	/* isolate us from interrupts */
 	rfork(RFNOTEG);
-	switch(fspid = fork()) {
-	default:
-		while(cpid != waitpid())
-			;
-		postnote(PNPROC, fspid, DONESTR);
-		while(fspid != waitpid())
-			;
-		exits(0);
-	case -1:
-		sysfatal("fork");
-	case 0:
-		notify(catcher);
-		break;
-	}
-
 	if(pipe(efd) < 0)
 		sysfatal("pipe");
 
@@ -344,6 +344,9 @@
 	switch(fork()) {
 	default:
 		close(efd[0]);
+		close(atoi(strrchr(fds[0], '/')+1));
+		close(atoi(strrchr(fds[1], '/')+1));
+		close(atoi(strrchr(fds[2], '/')+1));
 		break;
 	case -1:
 		sysfatal("fork");
@@ -359,6 +362,21 @@
 		exits(0);
 	}
 
+	switch(fspid = fork()) {
+	default:
+		while(cpid != waitpid())
+			;
+		postnote(PNPROC, fspid, DONESTR);
+		while(fspid != waitpid())
+			;
+		exits(0);
+	case -1:
+		sysfatal("fork");
+	case 0:
+		notify(catcher);
+		break;
+	}
+
 	fmtinstall('F', fcallfmt);
 
 	stats->rpc[Tversion].name = "version";
@@ -521,6 +539,14 @@
 	for(fs = stats->file; fs < &stats->file[Maxfile]; fs++){
 		if(fs->nopen == 0)
 			break;
+
+		if(strcmp(fs->path, fds[0]) == 0)
+			fs->path = "stdin";
+		else if(strcmp(fs->path, fds[1]) == 0)
+			fs->path = "stdout";
+		else if(strcmp(fs->path, fds[2]) == 0)
+			fs->path = "stderr";
+
 		fprint(2, "%5lud %8lud %8llud %8lud %8llud %s\n",
 			fs->nopen,
 			fs->nread, fs->bread,
--