Subject: kern/19989: /proc/uptime for linux emul
To: None <gnats-bugs@gnats.netbsd.org>
From: None <hamajima@nagoya.ydc.co.jp>
List: netbsd-bugs
Date: 01/22/2003 14:13:49
>Number:         19989
>Category:       kern
>Synopsis:       /proc/uptime for linux emul
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kern-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue Jan 21 21:15:01 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     HAMAJIMA Katsuomi
>Release:        NetBSD 1.6M
>Organization:
>Environment:
System: NetBSD ppc 1.6M NetBSD 1.6M (8500) #130: Tue Jan 21 19:41:19 JST 2003 hamajima@ppc:/sys/arch/macppc/compile/8500 macppc
Architecture: powerpc
Machine: macppc
>Description:
	some linux applications uses /proc/uptime.
	see fix section.
	(but this code does not care about multiprocessor.)

>How-To-Repeat:
>Fix:
Index: sys/miscfs/procfs/procfs.h
===================================================================
RCS file: /anoncvs/src/sys/miscfs/procfs/procfs.h,v
retrieving revision 1.40
diff -u -r1.40 procfs.h
--- sys/miscfs/procfs/procfs.h	2003/01/18 09:18:06	1.40
+++ sys/miscfs/procfs/procfs.h	2003/01/22 04:59:04
@@ -64,6 +64,7 @@
 	Pcpuinfo,	/* CPU info (if -o linux) */
 	Pmaps,		/* memory map, Linux style (if -o linux) */
 	Pfd,		/* a directory containing the processes open fd's */
+	Puptime,	/* elapsed time since (if -o linux) */
 #ifdef __HAVE_PROCFS_MACHDEP
 	PROCFS_MACHDEP_NODE_TYPES
 #endif
@@ -159,6 +160,8 @@
 int procfs_docpuinfo __P((struct proc *, struct proc *, struct pfsnode *,
     struct uio *));
 int procfs_dofd __P((struct proc *, struct proc *, struct pfsnode *,
+    struct uio *));
+int procfs_douptime __P((struct proc *, struct proc *, struct pfsnode *,
     struct uio *));
 
 void procfs_revoke_vnodes __P((struct proc *, void *));
Index: sys/miscfs/procfs/procfs_linux.c
===================================================================
RCS file: /anoncvs/src/sys/miscfs/procfs/procfs_linux.c,v
retrieving revision 1.4
diff -u -r1.4 procfs_linux.c
--- sys/miscfs/procfs/procfs_linux.c	2001/12/09 03:07:44	1.4
+++ sys/miscfs/procfs/procfs_linux.c	2003/01/22 04:59:04
@@ -114,6 +114,34 @@
 	len = sizeof buf;
 	if (procfs_getcpuinfstr(buf, &len) < 0)
 		return EIO;
+
+	if (len == 0)
+		return 0;
+
+	len -= uio->uio_offset;
+	cp = buf + uio->uio_offset;
+	len = imin(len, uio->uio_resid);
+	if (len <= 0)
+		error = 0;
+	else
+		error = uiomove(cp, len, uio);
+	return error;
+}
+
+int
+procfs_douptime(struct proc *curp, struct proc *p, struct pfsnode *pfs,
+		 struct uio *uio)
+{
+	char buf[512], *cp;
+	int len, error;
+	struct timeval runtime;
+	u_int64_t idle;
+
+	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
+	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
+	len = sprintf(buf, "%lu.%02lu %llu.%02llu\n",
+		      runtime.tv_sec, runtime.tv_usec / 10000,
+		      idle / hz, (((idle % hz) * 100) / hz) % 100);
 
 	if (len == 0)
 		return 0;
Index: sys/miscfs/procfs/procfs_subr.c
===================================================================
RCS file: /anoncvs/src/sys/miscfs/procfs/procfs_subr.c,v
retrieving revision 1.43
diff -u -r1.43 procfs_subr.c
--- sys/miscfs/procfs/procfs_subr.c	2003/01/18 09:18:06	1.43
+++ sys/miscfs/procfs/procfs_subr.c	2003/01/22 04:59:04
@@ -195,6 +195,7 @@
 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
 	case Pmeminfo:	/* /proc/meminfo = -r--r--r-- */
 	case Pcpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
+	case Puptime:	/* /proc/uptime = -r--r--r-- */
 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
 		vp->v_type = VREG;
 		break;
@@ -309,6 +310,9 @@
 
 	case Pfd:
 		return (procfs_dofd(curp, p, pfs, uio));
+
+	case Puptime:
+		return (procfs_douptime(curp, p, pfs, uio));
 
 #ifdef __HAVE_PROCFS_MACHDEP
 	PROCFS_MACHDEP_NODETYPE_CASES
Index: sys/miscfs/procfs/procfs_vnops.c
===================================================================
RCS file: /anoncvs/src/sys/miscfs/procfs/procfs_vnops.c,v
retrieving revision 1.93
diff -u -r1.93 procfs_vnops.c
--- sys/miscfs/procfs/procfs_vnops.c	2003/01/04 15:42:35	1.93
+++ sys/miscfs/procfs/procfs_vnops.c	2003/01/22 04:59:04
@@ -121,6 +121,7 @@
 	/*	  name		    type	    validp */
 	{ DT_REG, N("meminfo"),     Pmeminfo,        procfs_validfile_linux },
 	{ DT_REG, N("cpuinfo"),     Pcpuinfo,        procfs_validfile_linux },
+	{ DT_REG, N("uptime"),      Puptime,         procfs_validfile_linux },
 #undef N
 };
 static int nproc_root_targets =
@@ -542,6 +543,7 @@
 		break;
 	case Pmeminfo:
 	case Pcpuinfo:
+	case Puptime:
 		vap->va_nlink = 1;
 		vap->va_uid = vap->va_gid = 0;
 		break;
@@ -645,6 +647,7 @@
 	case Pcmdline:
 	case Pmeminfo:
 	case Pcpuinfo:
+	case Puptime:
 		vap->va_bytes = vap->va_size = 0;
 		break;
 	case Pmap:
>Release-Note:
>Audit-Trail:
>Unformatted: