Home | History | Annotate | Line # | Download | only in procfs
procfs_linux.c revision 1.39.6.1
      1  1.39.6.1     joerg /*      $NetBSD: procfs_linux.c,v 1.39.6.1 2007/10/26 15:48:55 joerg Exp $      */
      2       1.1      fvdl 
      3       1.1      fvdl /*
      4       1.1      fvdl  * Copyright (c) 2001 Wasabi Systems, Inc.
      5       1.1      fvdl  * All rights reserved.
      6       1.1      fvdl  *
      7       1.1      fvdl  * Written by Frank van der Linden for Wasabi Systems, Inc.
      8       1.1      fvdl  *
      9       1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     10       1.1      fvdl  * modification, are permitted provided that the following conditions
     11       1.1      fvdl  * are met:
     12       1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     13       1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     14       1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     16       1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     17       1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     18       1.1      fvdl  *    must display the following acknowledgement:
     19       1.1      fvdl  *      This product includes software developed for the NetBSD Project by
     20       1.1      fvdl  *      Wasabi Systems, Inc.
     21       1.1      fvdl  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22       1.1      fvdl  *    or promote products derived from this software without specific prior
     23       1.1      fvdl  *    written permission.
     24       1.1      fvdl  *
     25       1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26       1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27       1.1      fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28       1.1      fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29       1.1      fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30       1.1      fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31       1.1      fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32       1.1      fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33       1.1      fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34       1.1      fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35       1.1      fvdl  * POSSIBILITY OF SUCH DAMAGE.
     36       1.1      fvdl  */
     37       1.3     lukem 
     38       1.3     lukem #include <sys/cdefs.h>
     39  1.39.6.1     joerg __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.39.6.1 2007/10/26 15:48:55 joerg Exp $");
     40       1.1      fvdl 
     41       1.1      fvdl #include <sys/param.h>
     42       1.1      fvdl #include <sys/systm.h>
     43       1.1      fvdl #include <sys/time.h>
     44       1.1      fvdl #include <sys/kernel.h>
     45       1.1      fvdl #include <sys/proc.h>
     46       1.1      fvdl #include <sys/vnode.h>
     47      1.11  christos #include <sys/exec.h>
     48       1.7  christos #include <sys/resource.h>
     49       1.7  christos #include <sys/resourcevar.h>
     50       1.7  christos #include <sys/signal.h>
     51       1.7  christos #include <sys/signalvar.h>
     52       1.7  christos #include <sys/tty.h>
     53      1.19  jdolecek #include <sys/malloc.h>
     54      1.19  jdolecek #include <sys/mount.h>
     55      1.26      manu #include <sys/conf.h>
     56       1.1      fvdl 
     57       1.1      fvdl #include <miscfs/procfs/procfs.h>
     58      1.11  christos #include <compat/linux/common/linux_exec.h>
     59       1.1      fvdl 
     60       1.1      fvdl #include <uvm/uvm_extern.h>
     61       1.7  christos #include <uvm/uvm.h>
     62       1.1      fvdl 
     63      1.26      manu extern struct devsw_conv *devsw_conv;
     64      1.26      manu extern int max_devsw_convs;
     65      1.26      manu 
     66       1.1      fvdl #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
     67       1.1      fvdl #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
     68       1.1      fvdl 
     69      1.29  christos #define LBFSZ (8 * 1024)
     70      1.29  christos 
     71      1.35       agc static void
     72      1.35       agc get_proc_size_info(struct lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack)
     73      1.35       agc {
     74      1.35       agc 	struct proc *p = l->l_proc;
     75      1.35       agc 	struct vmspace *vm;
     76      1.35       agc 	struct vm_map *map;
     77      1.35       agc 	struct vm_map_entry *entry;
     78      1.35       agc 
     79      1.35       agc 	*stext = 0;
     80      1.35       agc 	*etext = 0;
     81      1.35       agc 	*sstack = 0;
     82      1.35       agc 
     83      1.35       agc 	proc_vmspace_getref(p, &vm);
     84      1.35       agc 	map = &vm->vm_map;
     85      1.35       agc 	vm_map_lock_read(map);
     86      1.35       agc 
     87      1.35       agc 	for (entry = map->header.next; entry != &map->header;
     88      1.35       agc 	    entry = entry->next) {
     89      1.35       agc 		if (UVM_ET_ISSUBMAP(entry))
     90      1.35       agc 			continue;
     91      1.35       agc 		/* assume text is the first entry */
     92      1.35       agc 		if (*stext == *etext) {
     93      1.35       agc 			*stext = entry->start;
     94      1.35       agc 			*etext = entry->end;
     95      1.35       agc 			break;
     96      1.35       agc 		}
     97      1.35       agc 	}
     98      1.35       agc #ifdef LINUX_USRSTACK
     99      1.35       agc 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
    100      1.35       agc 	    LINUX_USRSTACK < USRSTACK)
    101      1.35       agc 		*sstack = (unsigned long) LINUX_USRSTACK;
    102      1.35       agc 	else
    103      1.35       agc #endif
    104      1.35       agc 		*sstack = (unsigned long) USRSTACK;
    105      1.35       agc 
    106      1.35       agc 	/*
    107      1.35       agc 	 * jdk 1.6 compares low <= addr && addr < high
    108      1.35       agc 	 * if we put addr == high, then the test fails
    109      1.35       agc 	 * so eat one page.
    110      1.35       agc 	 */
    111      1.35       agc 	*sstack -= PAGE_SIZE;
    112      1.35       agc 
    113      1.35       agc 	vm_map_unlock_read(map);
    114      1.35       agc 	uvmspace_free(vm);
    115      1.35       agc }
    116      1.35       agc 
    117       1.1      fvdl /*
    118       1.1      fvdl  * Linux compatible /proc/meminfo. Only active when the -o linux
    119       1.1      fvdl  * mountflag is used.
    120       1.1      fvdl  */
    121       1.1      fvdl int
    122      1.30  christos procfs_domeminfo(struct lwp *curl, struct proc *p,
    123      1.30  christos     struct pfsnode *pfs, struct uio *uio)
    124       1.1      fvdl {
    125      1.29  christos 	char *bf;
    126      1.16    itojun 	int len;
    127      1.29  christos 	int error = 0;
    128      1.29  christos 
    129      1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    130       1.1      fvdl 
    131      1.29  christos 	len = snprintf(bf, LBFSZ,
    132       1.1      fvdl 		"        total:    used:    free:  shared: buffers: cached:\n"
    133       1.1      fvdl 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
    134       1.1      fvdl 		"Swap: %8lu %8lu %8lu\n"
    135       1.1      fvdl 		"MemTotal:  %8lu kB\n"
    136       1.1      fvdl 		"MemFree:   %8lu kB\n"
    137       1.1      fvdl 		"MemShared: %8lu kB\n"
    138       1.1      fvdl 		"Buffers:   %8lu kB\n"
    139       1.1      fvdl 		"Cached:    %8lu kB\n"
    140      1.20     perry 		"SwapTotal: %8lu kB\n"
    141       1.1      fvdl 		"SwapFree:  %8lu kB\n",
    142       1.1      fvdl 		PGTOB(uvmexp.npages),
    143       1.1      fvdl 		PGTOB(uvmexp.npages - uvmexp.free),
    144       1.1      fvdl 		PGTOB(uvmexp.free),
    145       1.1      fvdl 		0L,
    146       1.4       chs 		PGTOB(uvmexp.filepages),
    147       1.4       chs 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    148       1.1      fvdl 		PGTOB(uvmexp.swpages),
    149       1.1      fvdl 		PGTOB(uvmexp.swpginuse),
    150       1.1      fvdl 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
    151       1.1      fvdl 		PGTOKB(uvmexp.npages),
    152       1.1      fvdl 		PGTOKB(uvmexp.free),
    153       1.1      fvdl 		0L,
    154       1.4       chs 		PGTOKB(uvmexp.filepages),
    155       1.4       chs 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    156       1.1      fvdl 		PGTOKB(uvmexp.swpages),
    157       1.1      fvdl 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
    158       1.1      fvdl 
    159       1.1      fvdl 	if (len == 0)
    160      1.29  christos 		goto out;
    161       1.1      fvdl 
    162      1.29  christos 	error = uiomove_frombuf(bf, len, uio);
    163      1.29  christos out:
    164      1.29  christos 	free(bf, M_TEMP);
    165      1.29  christos 	return error;
    166       1.1      fvdl }
    167       1.1      fvdl 
    168       1.7  christos /*
    169      1.26      manu  * Linux compatible /proc/devices. Only active when the -o linux
    170      1.26      manu  * mountflag is used.
    171      1.26      manu  */
    172      1.26      manu int
    173      1.30  christos procfs_dodevices(struct lwp *curl, struct proc *p,
    174      1.30  christos     struct pfsnode *pfs, struct uio *uio)
    175      1.26      manu {
    176      1.29  christos 	char *bf;
    177      1.26      manu 	int offset = 0;
    178      1.29  christos 	int i, error = ENAMETOOLONG;
    179      1.29  christos 
    180      1.31      elad 	/* XXX elad - may need filtering. */
    181      1.31      elad 
    182      1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    183      1.26      manu 
    184      1.29  christos 	offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
    185      1.29  christos 	if (offset >= LBFSZ)
    186      1.29  christos 		goto out;
    187      1.26      manu 
    188      1.26      manu 	for (i = 0; i < max_devsw_convs; i++) {
    189      1.26      manu 		if ((devsw_conv[i].d_name == NULL) ||
    190      1.26      manu 		    (devsw_conv[i].d_cmajor == -1))
    191      1.26      manu 			continue;
    192      1.26      manu 
    193      1.29  christos 		offset += snprintf(&bf[offset], LBFSZ - offset,
    194      1.26      manu 		    "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
    195      1.29  christos 		if (offset >= LBFSZ)
    196      1.29  christos 			goto out;
    197      1.26      manu 	}
    198      1.26      manu 
    199      1.29  christos 	offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
    200      1.29  christos 	if (offset >= LBFSZ)
    201      1.29  christos 		goto out;
    202      1.26      manu 
    203      1.26      manu 	for (i = 0; i < max_devsw_convs; i++) {
    204      1.26      manu 		if ((devsw_conv[i].d_name == NULL) ||
    205      1.26      manu 		    (devsw_conv[i].d_bmajor == -1))
    206      1.26      manu 			continue;
    207      1.26      manu 
    208      1.29  christos 		offset += snprintf(&bf[offset], LBFSZ - offset,
    209      1.26      manu 		    "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
    210      1.29  christos 		if (offset >= LBFSZ)
    211      1.29  christos 			goto out;
    212      1.26      manu 	}
    213      1.26      manu 
    214      1.29  christos 	error = uiomove_frombuf(bf, offset, uio);
    215      1.29  christos out:
    216      1.29  christos 	free(bf, M_TEMP);
    217      1.29  christos 	return error;
    218      1.26      manu }
    219      1.26      manu 
    220      1.26      manu /*
    221      1.35       agc  * Linux compatible /proc/stat. Only active when the -o linux
    222      1.35       agc  * mountflag is used.
    223      1.35       agc  */
    224      1.35       agc int
    225      1.35       agc procfs_docpustat(struct lwp *curl, struct proc *p,
    226      1.35       agc     struct pfsnode *pfs, struct uio *uio)
    227      1.35       agc {
    228      1.35       agc 	struct timeval	 runtime;
    229      1.35       agc 	char		*bf;
    230      1.35       agc 	int	 	 error;
    231      1.35       agc 	int	 	 len;
    232      1.37       agc #if defined(MULTIPROCESSOR)
    233      1.37       agc         struct cpu_info *ci;
    234      1.37       agc         CPU_INFO_ITERATOR cii;
    235      1.38       agc #endif
    236      1.35       agc 	int	 	 i;
    237      1.35       agc 
    238      1.35       agc 	error = ENAMETOOLONG;
    239      1.35       agc 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    240      1.35       agc 
    241      1.35       agc 	len = snprintf(bf, LBFSZ,
    242      1.36    dogcow 		"cpu %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
    243      1.35       agc 		curcpu()->ci_schedstate.spc_cp_time[CP_USER],
    244      1.35       agc 		curcpu()->ci_schedstate.spc_cp_time[CP_NICE],
    245      1.35       agc 		curcpu()->ci_schedstate.spc_cp_time[CP_SYS] /*+ [CP_INTR]*/,
    246      1.35       agc 		curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]);
    247      1.35       agc 	if (len == 0)
    248      1.35       agc 		goto out;
    249      1.35       agc 
    250      1.37       agc #if defined(MULTIPROCESSOR)
    251      1.38       agc #define ALLCPUS	CPU_INFO_FOREACH(cii, ci)
    252      1.38       agc #define CPUNAME	ci
    253      1.38       agc #else
    254      1.38       agc #define ALLCPUS	; i < 1 ;
    255      1.38       agc #define CPUNAME	curcpu()
    256      1.38       agc #endif
    257      1.38       agc 
    258      1.35       agc 	i = 0;
    259      1.38       agc 	for (ALLCPUS) {
    260      1.35       agc 		len += snprintf(&bf[len], LBFSZ - len,
    261      1.36    dogcow 			"cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
    262      1.36    dogcow 			"\n", i,
    263      1.38       agc 			CPUNAME->ci_schedstate.spc_cp_time[CP_USER],
    264      1.38       agc 			CPUNAME->ci_schedstate.spc_cp_time[CP_NICE],
    265      1.38       agc 			CPUNAME->ci_schedstate.spc_cp_time[CP_SYS],
    266      1.38       agc 			CPUNAME->ci_schedstate.spc_cp_time[CP_IDLE]);
    267      1.35       agc 		if (len >= LBFSZ)
    268      1.35       agc 			goto out;
    269      1.35       agc 		i += 1;
    270      1.35       agc 	}
    271      1.35       agc 
    272  1.39.6.1     joerg 	timersub(&curlwp->l_stime, &boottime, &runtime);
    273      1.35       agc 	len += snprintf(&bf[len], LBFSZ - len,
    274      1.35       agc 			"disk 0 0 0 0\n"
    275      1.35       agc 			"page %u %u\n"
    276      1.35       agc 			"swap %u %u\n"
    277      1.35       agc 			"intr %u\n"
    278      1.35       agc 			"ctxt %u\n"
    279      1.35       agc 			"btime %lld\n",
    280      1.35       agc 			uvmexp.pageins, uvmexp.pdpageouts,
    281      1.35       agc 			uvmexp.pgswapin, uvmexp.pgswapout,
    282      1.35       agc 			uvmexp.intrs,
    283      1.35       agc 			uvmexp.swtch,
    284      1.35       agc 			(long long)boottime.tv_sec);
    285      1.35       agc 	if (len >= LBFSZ)
    286      1.35       agc 		goto out;
    287      1.35       agc 
    288      1.35       agc 	error = uiomove_frombuf(bf, len, uio);
    289      1.35       agc out:
    290      1.35       agc 	free(bf, M_TEMP);
    291      1.35       agc 	return error;
    292      1.35       agc }
    293      1.35       agc 
    294      1.35       agc /*
    295      1.35       agc  * Linux compatible /proc/loadavg. Only active when the -o linux
    296      1.35       agc  * mountflag is used.
    297      1.35       agc  */
    298      1.35       agc int
    299      1.35       agc procfs_doloadavg(struct lwp *curl, struct proc *p,
    300      1.35       agc     struct pfsnode *pfs, struct uio *uio)
    301      1.35       agc {
    302      1.35       agc 	char	*bf;
    303      1.35       agc 	int 	 error;
    304      1.35       agc 	int 	 len;
    305      1.35       agc 
    306      1.35       agc 	error = ENAMETOOLONG;
    307      1.35       agc 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    308      1.35       agc 
    309      1.35       agc 	averunnable.fscale = FSCALE;
    310      1.35       agc 	len = snprintf(bf, LBFSZ,
    311      1.35       agc 	        "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
    312      1.35       agc 		(int)(averunnable.ldavg[0] / averunnable.fscale),
    313      1.35       agc 		(int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
    314      1.35       agc 		(int)(averunnable.ldavg[1] / averunnable.fscale),
    315      1.35       agc 		(int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
    316      1.35       agc 		(int)(averunnable.ldavg[2] / averunnable.fscale),
    317      1.35       agc 		(int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
    318      1.35       agc 		1,		/* number of ONPROC processes */
    319      1.35       agc 		nprocs,
    320      1.35       agc 		30000);		/* last pid */
    321      1.35       agc 	if (len == 0)
    322      1.35       agc 		goto out;
    323      1.35       agc 
    324      1.35       agc 	error = uiomove_frombuf(bf, len, uio);
    325      1.35       agc out:
    326      1.35       agc 	free(bf, M_TEMP);
    327      1.35       agc 	return error;
    328      1.35       agc }
    329      1.35       agc 
    330      1.35       agc /*
    331      1.35       agc  * Linux compatible /proc/<pid>/statm. Only active when the -o linux
    332      1.35       agc  * mountflag is used.
    333      1.35       agc  */
    334      1.35       agc int
    335      1.35       agc procfs_do_pid_statm(struct lwp *curl, struct lwp *l,
    336      1.35       agc     struct pfsnode *pfs, struct uio *uio)
    337      1.35       agc {
    338      1.37       agc 	struct vmspace	*vm;
    339      1.35       agc 	struct proc	*p = l->l_proc;
    340      1.35       agc 	struct rusage	*ru = &p->p_stats->p_ru;
    341      1.35       agc 	char		*bf;
    342      1.35       agc 	int	 	 error;
    343      1.35       agc 	int	 	 len;
    344      1.35       agc 
    345      1.35       agc 	error = ENAMETOOLONG;
    346      1.35       agc 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    347      1.35       agc 
    348      1.37       agc 	/* XXX - we use values from vmspace, since dsl says that ru figures
    349      1.37       agc 	   are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */
    350      1.37       agc 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
    351      1.39       agc 		goto out;
    352      1.37       agc 	}
    353      1.35       agc 
    354      1.35       agc 	len = snprintf(bf, LBFSZ,
    355      1.35       agc 	        "%lu %lu %lu %lu %lu %lu %lu\n",
    356      1.37       agc 		(unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */
    357      1.37       agc 		(unsigned long)(vm->vm_rssize),	/* resident */
    358      1.37       agc 		(unsigned long)(ru->ru_ixrss),	/* shared */
    359      1.37       agc 		(unsigned long)(vm->vm_tsize),	/* text size in pages */
    360      1.37       agc 		(unsigned long)(vm->vm_dsize),	/* data size in pages */
    361      1.37       agc 		(unsigned long)(vm->vm_ssize),	/* stack size in pages */
    362      1.35       agc 		(unsigned long) 0);
    363      1.37       agc 
    364      1.35       agc 	if (len == 0)
    365      1.35       agc 		goto out;
    366      1.35       agc 
    367      1.35       agc 	error = uiomove_frombuf(bf, len, uio);
    368      1.35       agc out:
    369      1.35       agc 	free(bf, M_TEMP);
    370      1.35       agc 	return error;
    371      1.35       agc }
    372      1.35       agc 
    373      1.37       agc #define USEC_2_TICKS(x)		((x) / 10000)
    374      1.37       agc 
    375      1.35       agc /*
    376       1.7  christos  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
    377       1.7  christos  * mountflag is used.
    378       1.7  christos  */
    379       1.7  christos int
    380      1.30  christos procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
    381      1.30  christos     struct pfsnode *pfs, struct uio *uio)
    382       1.7  christos {
    383      1.29  christos 	char *bf;
    384      1.25  christos 	struct proc *p = l->l_proc;
    385      1.16    itojun 	int len;
    386       1.7  christos 	struct tty *tty = p->p_session->s_ttyp;
    387       1.7  christos 	struct rusage *ru = &p->p_stats->p_ru;
    388       1.7  christos 	struct rusage *cru = &p->p_stats->p_cru;
    389       1.7  christos 	unsigned long stext = 0, etext = 0, sstack = 0;
    390      1.32        ad 	struct timeval rt;
    391      1.39       agc 	struct vmspace	*vm;
    392      1.29  christos 	int error = 0;
    393      1.29  christos 
    394      1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    395       1.7  christos 
    396      1.39       agc 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
    397      1.39       agc 		goto out;
    398      1.39       agc 	}
    399      1.39       agc 
    400      1.35       agc 	get_proc_size_info(l, &stext, &etext, &sstack);
    401      1.32        ad 
    402      1.33        ad 	mutex_enter(&proclist_lock);
    403      1.32        ad 	mutex_enter(&p->p_mutex);
    404      1.32        ad 	mutex_enter(&p->p_smutex);
    405      1.32        ad 
    406      1.32        ad 	calcru(p, NULL, NULL, NULL, &rt);
    407       1.7  christos 
    408      1.29  christos 	len = snprintf(bf, LBFSZ,
    409       1.7  christos 	    "%d (%s) %c %d %d %d %d %d "
    410       1.7  christos 	    "%u "
    411       1.7  christos 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
    412       1.7  christos 	    "%d %d %d "
    413       1.8   hannken 	    "%lu %lu %lu %lu %" PRIu64 " "
    414       1.7  christos 	    "%lu %lu %lu "
    415       1.7  christos 	    "%u %u "
    416       1.7  christos 	    "%u %u %u %u "
    417       1.7  christos 	    "%lu %lu %lu %d %d\n",
    418       1.7  christos 
    419       1.7  christos 	    p->p_pid,
    420       1.7  christos 	    p->p_comm,
    421       1.7  christos 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
    422      1.28      elad 	    (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
    423       1.7  christos 
    424       1.7  christos 	    p->p_pgid,
    425       1.7  christos 	    p->p_session->s_sid,
    426       1.7  christos 	    tty ? tty->t_dev : 0,
    427      1.15  christos 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
    428       1.7  christos 
    429       1.7  christos 	    p->p_flag,
    430       1.7  christos 
    431       1.7  christos 	    ru->ru_minflt,
    432       1.7  christos 	    cru->ru_minflt,
    433       1.7  christos 	    ru->ru_majflt,
    434       1.7  christos 	    cru->ru_majflt,
    435      1.37       agc 	    USEC_2_TICKS(ru->ru_utime.tv_usec),
    436      1.37       agc 	    USEC_2_TICKS(ru->ru_stime.tv_usec),
    437      1.37       agc 	    USEC_2_TICKS(cru->ru_utime.tv_usec),
    438      1.37       agc 	    USEC_2_TICKS(cru->ru_stime.tv_usec),
    439       1.7  christos 
    440      1.38       agc 	    l->l_priority,				/* XXX: priority */
    441      1.39       agc 	    p->p_nice - 20,
    442       1.7  christos 	    0,
    443       1.7  christos 
    444      1.32        ad 	    rt.tv_sec,
    445       1.7  christos 	    p->p_stats->p_start.tv_sec,
    446      1.39       agc 	    (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */
    447      1.39       agc 	    (unsigned long)(vm->vm_rssize),	/* resident */
    448       1.7  christos 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
    449       1.7  christos 
    450       1.7  christos 	    stext,					/* start code */
    451       1.7  christos 	    etext,					/* end code */
    452       1.7  christos 	    sstack,					/* mm start stack */
    453       1.7  christos 	    0,						/* XXX: pc */
    454       1.7  christos 	    0,						/* XXX: sp */
    455      1.32        ad 	    p->p_sigpend.sp_set.__bits[0],		/* XXX: pending */
    456      1.32        ad 	    0,						/* XXX: held */
    457       1.7  christos 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
    458       1.7  christos 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
    459       1.7  christos 
    460       1.7  christos 	    (unsigned long)(intptr_t)l->l_wchan,
    461       1.7  christos 	    ru->ru_nvcsw,
    462       1.7  christos 	    ru->ru_nivcsw,
    463       1.7  christos 	    p->p_exitsig,
    464       1.7  christos 	    0);						/* XXX: processor */
    465       1.7  christos 
    466      1.32        ad 	mutex_exit(&p->p_smutex);
    467      1.32        ad 	mutex_exit(&p->p_mutex);
    468      1.33        ad 	mutex_exit(&proclist_lock);
    469      1.32        ad 
    470       1.7  christos 	if (len == 0)
    471      1.29  christos 		goto out;
    472       1.7  christos 
    473      1.29  christos 	error = uiomove_frombuf(bf, len, uio);
    474      1.29  christos out:
    475      1.29  christos 	free(bf, M_TEMP);
    476      1.29  christos 	return error;
    477       1.7  christos }
    478       1.7  christos 
    479       1.1      fvdl int
    480      1.30  christos procfs_docpuinfo(struct lwp *curl, struct proc *p,
    481      1.30  christos     struct pfsnode *pfs, struct uio *uio)
    482       1.1      fvdl {
    483      1.29  christos 	int len = LBFSZ;
    484      1.23  christos 	char *bf = malloc(len, M_TEMP, M_WAITOK);
    485      1.21  christos 	int error;
    486       1.1      fvdl 
    487      1.23  christos 	if (procfs_getcpuinfstr(bf, &len) < 0) {
    488      1.21  christos 		error = ENOSPC;
    489      1.21  christos 		goto done;
    490      1.21  christos 	}
    491       1.5       jrf 
    492      1.21  christos 	if (len == 0) {
    493      1.21  christos 		error = 0;
    494      1.21  christos 		goto done;
    495      1.21  christos 	}
    496       1.5       jrf 
    497      1.23  christos 	error = uiomove_frombuf(bf, len, uio);
    498      1.21  christos done:
    499      1.23  christos 	free(bf, M_TEMP);
    500      1.21  christos 	return error;
    501       1.5       jrf }
    502       1.5       jrf 
    503       1.5       jrf int
    504      1.30  christos procfs_douptime(struct lwp *curl, struct proc *p,
    505      1.30  christos     struct pfsnode *pfs, struct uio *uio)
    506       1.5       jrf {
    507      1.29  christos 	char *bf;
    508      1.16    itojun 	int len;
    509       1.5       jrf 	struct timeval runtime;
    510       1.5       jrf 	u_int64_t idle;
    511      1.29  christos 	int error = 0;
    512      1.29  christos 
    513      1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    514       1.5       jrf 
    515  1.39.6.1     joerg 	timersub(&curlwp->l_stime, &boottime, &runtime);
    516       1.5       jrf 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
    517      1.29  christos 	len = snprintf(bf, LBFSZ,
    518      1.16    itojun 	    "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
    519      1.16    itojun 	    runtime.tv_sec, runtime.tv_usec / 10000,
    520      1.16    itojun 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
    521       1.1      fvdl 
    522       1.1      fvdl 	if (len == 0)
    523      1.29  christos 		goto out;
    524       1.1      fvdl 
    525      1.29  christos 	error = uiomove_frombuf(bf, len, uio);
    526      1.29  christos out:
    527      1.29  christos 	free(bf, M_TEMP);
    528      1.29  christos 	return error;
    529       1.1      fvdl }
    530      1.19  jdolecek 
    531      1.19  jdolecek int
    532      1.30  christos procfs_domounts(struct lwp *curl, struct proc *p,
    533      1.30  christos     struct pfsnode *pfs, struct uio *uio)
    534      1.19  jdolecek {
    535      1.29  christos 	char *bf, *mtab = NULL;
    536      1.19  jdolecek 	const char *fsname;
    537      1.19  jdolecek 	size_t len, mtabsz = 0;
    538      1.19  jdolecek 	struct mount *mp, *nmp;
    539      1.19  jdolecek 	struct statvfs *sfs;
    540      1.19  jdolecek 	int error = 0;
    541      1.19  jdolecek 
    542      1.31      elad 	/* XXX elad - may need filtering. */
    543      1.31      elad 
    544      1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    545  1.39.6.1     joerg 	mutex_enter(&mountlist_lock);
    546      1.19  jdolecek 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    547      1.19  jdolecek 	     mp = nmp) {
    548  1.39.6.1     joerg 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) {
    549      1.19  jdolecek 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    550      1.19  jdolecek 			continue;
    551      1.19  jdolecek 		}
    552      1.19  jdolecek 
    553      1.19  jdolecek 		sfs = &mp->mnt_stat;
    554      1.19  jdolecek 
    555      1.19  jdolecek 		/* Linux uses different names for some filesystems */
    556      1.19  jdolecek 		fsname = sfs->f_fstypename;
    557      1.19  jdolecek 		if (strcmp(fsname, "procfs") == 0)
    558      1.19  jdolecek 			fsname = "proc";
    559      1.19  jdolecek 		else if (strcmp(fsname, "ext2fs") == 0)
    560      1.19  jdolecek 			fsname = "ext2";
    561      1.20     perry 
    562      1.29  christos 		len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n",
    563      1.19  jdolecek 			sfs->f_mntfromname,
    564      1.19  jdolecek 			sfs->f_mntonname,
    565      1.19  jdolecek 			fsname,
    566      1.19  jdolecek 			(mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
    567      1.19  jdolecek 			(mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
    568      1.19  jdolecek 			(mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
    569      1.19  jdolecek 			(mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
    570      1.19  jdolecek 			(mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
    571      1.19  jdolecek 			(mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
    572      1.19  jdolecek 			);
    573      1.19  jdolecek 
    574      1.19  jdolecek 		mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
    575      1.23  christos 		memcpy(mtab + mtabsz, bf, len);
    576      1.19  jdolecek 		mtabsz += len;
    577      1.19  jdolecek 
    578  1.39.6.1     joerg 		mutex_enter(&mountlist_lock);
    579      1.19  jdolecek 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
    580      1.19  jdolecek 		vfs_unbusy(mp);
    581      1.19  jdolecek 	}
    582  1.39.6.1     joerg 	mutex_exit(&mountlist_lock);
    583      1.29  christos 	free(bf, M_TEMP);
    584      1.19  jdolecek 
    585      1.19  jdolecek 	if (mtabsz > 0) {
    586      1.19  jdolecek 		error = uiomove_frombuf(mtab, mtabsz, uio);
    587      1.19  jdolecek 		free(mtab, M_TEMP);
    588      1.19  jdolecek 	}
    589      1.19  jdolecek 
    590      1.19  jdolecek 	return error;
    591      1.19  jdolecek }
    592