Home | History | Annotate | Line # | Download | only in procfs
procfs_linux.c revision 1.72
      1  1.72   mlelstv /*      $NetBSD: procfs_linux.c,v 1.72 2016/03/28 17:23:47 mlelstv 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.72   mlelstv __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.72 2016/03/28 17:23:47 mlelstv 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.70      matt #include <sys/cpu.h>
     45   1.1      fvdl #include <sys/kernel.h>
     46   1.1      fvdl #include <sys/proc.h>
     47   1.1      fvdl #include <sys/vnode.h>
     48  1.11  christos #include <sys/exec.h>
     49   1.7  christos #include <sys/resource.h>
     50   1.7  christos #include <sys/resourcevar.h>
     51   1.7  christos #include <sys/signal.h>
     52   1.7  christos #include <sys/signalvar.h>
     53   1.7  christos #include <sys/tty.h>
     54  1.19  jdolecek #include <sys/malloc.h>
     55  1.19  jdolecek #include <sys/mount.h>
     56  1.26      manu #include <sys/conf.h>
     57  1.61  jmcneill #include <sys/sysctl.h>
     58  1.62  christos #include <sys/kauth.h>
     59  1.63  christos #include <sys/filedesc.h>
     60   1.1      fvdl 
     61   1.1      fvdl #include <miscfs/procfs/procfs.h>
     62  1.54        ad 
     63  1.11  christos #include <compat/linux/common/linux_exec.h>
     64  1.61  jmcneill #include <compat/linux32/common/linux32_sysctl.h>
     65   1.1      fvdl 
     66   1.1      fvdl #include <uvm/uvm_extern.h>
     67   1.7  christos #include <uvm/uvm.h>
     68   1.1      fvdl 
     69  1.26      manu extern struct devsw_conv *devsw_conv;
     70  1.26      manu extern int max_devsw_convs;
     71  1.26      manu 
     72   1.1      fvdl #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
     73   1.1      fvdl #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
     74   1.1      fvdl 
     75  1.29  christos #define LBFSZ (8 * 1024)
     76  1.29  christos 
     77  1.35       agc static void
     78  1.35       agc get_proc_size_info(struct lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack)
     79  1.35       agc {
     80  1.35       agc 	struct proc *p = l->l_proc;
     81  1.35       agc 	struct vmspace *vm;
     82  1.35       agc 	struct vm_map *map;
     83  1.35       agc 	struct vm_map_entry *entry;
     84  1.35       agc 
     85  1.35       agc 	*stext = 0;
     86  1.35       agc 	*etext = 0;
     87  1.35       agc 	*sstack = 0;
     88  1.35       agc 
     89  1.35       agc 	proc_vmspace_getref(p, &vm);
     90  1.35       agc 	map = &vm->vm_map;
     91  1.35       agc 	vm_map_lock_read(map);
     92  1.35       agc 
     93  1.35       agc 	for (entry = map->header.next; entry != &map->header;
     94  1.35       agc 	    entry = entry->next) {
     95  1.35       agc 		if (UVM_ET_ISSUBMAP(entry))
     96  1.35       agc 			continue;
     97  1.35       agc 		/* assume text is the first entry */
     98  1.35       agc 		if (*stext == *etext) {
     99  1.35       agc 			*stext = entry->start;
    100  1.35       agc 			*etext = entry->end;
    101  1.35       agc 			break;
    102  1.35       agc 		}
    103  1.35       agc 	}
    104  1.60  jmcneill #if defined(LINUX_USRSTACK32) && defined(USRSTACK32)
    105  1.44  christos 	if (strcmp(p->p_emul->e_name, "linux32") == 0 &&
    106  1.44  christos 	    LINUX_USRSTACK32 < USRSTACK32)
    107  1.44  christos 		*sstack = (unsigned long)LINUX_USRSTACK32;
    108  1.44  christos 	else
    109  1.44  christos #endif
    110  1.35       agc #ifdef LINUX_USRSTACK
    111  1.35       agc 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
    112  1.35       agc 	    LINUX_USRSTACK < USRSTACK)
    113  1.44  christos 		*sstack = (unsigned long)LINUX_USRSTACK;
    114  1.44  christos 	else
    115  1.44  christos #endif
    116  1.44  christos #ifdef	USRSTACK32
    117  1.44  christos 	if (strstr(p->p_emul->e_name, "32") != NULL)
    118  1.44  christos 		*sstack = (unsigned long)USRSTACK32;
    119  1.35       agc 	else
    120  1.35       agc #endif
    121  1.44  christos 		*sstack = (unsigned long)USRSTACK;
    122  1.35       agc 
    123  1.35       agc 	/*
    124  1.35       agc 	 * jdk 1.6 compares low <= addr && addr < high
    125  1.35       agc 	 * if we put addr == high, then the test fails
    126  1.35       agc 	 * so eat one page.
    127  1.35       agc 	 */
    128  1.35       agc 	*sstack -= PAGE_SIZE;
    129  1.35       agc 
    130  1.35       agc 	vm_map_unlock_read(map);
    131  1.35       agc 	uvmspace_free(vm);
    132  1.35       agc }
    133  1.35       agc 
    134   1.1      fvdl /*
    135   1.1      fvdl  * Linux compatible /proc/meminfo. Only active when the -o linux
    136   1.1      fvdl  * mountflag is used.
    137   1.1      fvdl  */
    138   1.1      fvdl int
    139  1.30  christos procfs_domeminfo(struct lwp *curl, struct proc *p,
    140  1.30  christos     struct pfsnode *pfs, struct uio *uio)
    141   1.1      fvdl {
    142  1.29  christos 	char *bf;
    143  1.16    itojun 	int len;
    144  1.29  christos 	int error = 0;
    145  1.29  christos 
    146  1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    147   1.1      fvdl 
    148  1.29  christos 	len = snprintf(bf, LBFSZ,
    149   1.1      fvdl 		"        total:    used:    free:  shared: buffers: cached:\n"
    150   1.1      fvdl 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
    151   1.1      fvdl 		"Swap: %8lu %8lu %8lu\n"
    152   1.1      fvdl 		"MemTotal:  %8lu kB\n"
    153   1.1      fvdl 		"MemFree:   %8lu kB\n"
    154   1.1      fvdl 		"MemShared: %8lu kB\n"
    155   1.1      fvdl 		"Buffers:   %8lu kB\n"
    156   1.1      fvdl 		"Cached:    %8lu kB\n"
    157  1.20     perry 		"SwapTotal: %8lu kB\n"
    158   1.1      fvdl 		"SwapFree:  %8lu kB\n",
    159   1.1      fvdl 		PGTOB(uvmexp.npages),
    160   1.1      fvdl 		PGTOB(uvmexp.npages - uvmexp.free),
    161   1.1      fvdl 		PGTOB(uvmexp.free),
    162   1.1      fvdl 		0L,
    163   1.4       chs 		PGTOB(uvmexp.filepages),
    164   1.4       chs 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    165   1.1      fvdl 		PGTOB(uvmexp.swpages),
    166   1.1      fvdl 		PGTOB(uvmexp.swpginuse),
    167   1.1      fvdl 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
    168   1.1      fvdl 		PGTOKB(uvmexp.npages),
    169   1.1      fvdl 		PGTOKB(uvmexp.free),
    170   1.1      fvdl 		0L,
    171   1.4       chs 		PGTOKB(uvmexp.filepages),
    172   1.4       chs 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    173   1.1      fvdl 		PGTOKB(uvmexp.swpages),
    174   1.1      fvdl 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
    175   1.1      fvdl 
    176   1.1      fvdl 	if (len == 0)
    177  1.29  christos 		goto out;
    178   1.1      fvdl 
    179  1.29  christos 	error = uiomove_frombuf(bf, len, uio);
    180  1.29  christos out:
    181  1.29  christos 	free(bf, M_TEMP);
    182  1.29  christos 	return error;
    183   1.1      fvdl }
    184   1.1      fvdl 
    185   1.7  christos /*
    186  1.26      manu  * Linux compatible /proc/devices. Only active when the -o linux
    187  1.26      manu  * mountflag is used.
    188  1.26      manu  */
    189  1.26      manu int
    190  1.30  christos procfs_dodevices(struct lwp *curl, struct proc *p,
    191  1.30  christos     struct pfsnode *pfs, struct uio *uio)
    192  1.26      manu {
    193  1.29  christos 	char *bf;
    194  1.26      manu 	int offset = 0;
    195  1.29  christos 	int i, error = ENAMETOOLONG;
    196  1.29  christos 
    197  1.31      elad 	/* XXX elad - may need filtering. */
    198  1.31      elad 
    199  1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    200  1.26      manu 
    201  1.29  christos 	offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
    202  1.29  christos 	if (offset >= LBFSZ)
    203  1.29  christos 		goto out;
    204  1.26      manu 
    205  1.55     pooka 	mutex_enter(&device_lock);
    206  1.26      manu 	for (i = 0; i < max_devsw_convs; i++) {
    207  1.26      manu 		if ((devsw_conv[i].d_name == NULL) ||
    208  1.26      manu 		    (devsw_conv[i].d_cmajor == -1))
    209  1.26      manu 			continue;
    210  1.26      manu 
    211  1.29  christos 		offset += snprintf(&bf[offset], LBFSZ - offset,
    212  1.26      manu 		    "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
    213  1.43        ad 		if (offset >= LBFSZ) {
    214  1.55     pooka 			mutex_exit(&device_lock);
    215  1.29  christos 			goto out;
    216  1.43        ad 		}
    217  1.26      manu 	}
    218  1.26      manu 
    219  1.29  christos 	offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
    220  1.43        ad 	if (offset >= LBFSZ) {
    221  1.55     pooka 		mutex_exit(&device_lock);
    222  1.29  christos 		goto out;
    223  1.43        ad 	}
    224  1.26      manu 
    225  1.26      manu 	for (i = 0; i < max_devsw_convs; i++) {
    226  1.26      manu 		if ((devsw_conv[i].d_name == NULL) ||
    227  1.26      manu 		    (devsw_conv[i].d_bmajor == -1))
    228  1.26      manu 			continue;
    229  1.26      manu 
    230  1.29  christos 		offset += snprintf(&bf[offset], LBFSZ - offset,
    231  1.26      manu 		    "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
    232  1.43        ad 		if (offset >= LBFSZ) {
    233  1.55     pooka 			mutex_exit(&device_lock);
    234  1.29  christos 			goto out;
    235  1.43        ad 		}
    236  1.26      manu 	}
    237  1.55     pooka 	mutex_exit(&device_lock);
    238  1.26      manu 
    239  1.29  christos 	error = uiomove_frombuf(bf, offset, uio);
    240  1.29  christos out:
    241  1.29  christos 	free(bf, M_TEMP);
    242  1.29  christos 	return error;
    243  1.26      manu }
    244  1.26      manu 
    245  1.26      manu /*
    246  1.35       agc  * Linux compatible /proc/stat. Only active when the -o linux
    247  1.35       agc  * mountflag is used.
    248  1.35       agc  */
    249  1.35       agc int
    250  1.35       agc procfs_docpustat(struct lwp *curl, struct proc *p,
    251  1.35       agc     struct pfsnode *pfs, struct uio *uio)
    252  1.35       agc {
    253  1.35       agc 	char		*bf;
    254  1.35       agc 	int	 	 error;
    255  1.35       agc 	int	 	 len;
    256  1.37       agc #if defined(MULTIPROCESSOR)
    257  1.37       agc         struct cpu_info *ci;
    258  1.37       agc         CPU_INFO_ITERATOR cii;
    259  1.38       agc #endif
    260  1.35       agc 	int	 	 i;
    261  1.59      matt 	uint64_t	nintr;
    262  1.59      matt 	uint64_t	nswtch;
    263  1.35       agc 
    264  1.35       agc 	error = ENAMETOOLONG;
    265  1.35       agc 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    266  1.35       agc 
    267  1.35       agc 	len = snprintf(bf, LBFSZ,
    268  1.36    dogcow 		"cpu %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
    269  1.35       agc 		curcpu()->ci_schedstate.spc_cp_time[CP_USER],
    270  1.35       agc 		curcpu()->ci_schedstate.spc_cp_time[CP_NICE],
    271  1.35       agc 		curcpu()->ci_schedstate.spc_cp_time[CP_SYS] /*+ [CP_INTR]*/,
    272  1.35       agc 		curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]);
    273  1.35       agc 	if (len == 0)
    274  1.35       agc 		goto out;
    275  1.35       agc 
    276  1.37       agc #if defined(MULTIPROCESSOR)
    277  1.38       agc #define ALLCPUS	CPU_INFO_FOREACH(cii, ci)
    278  1.38       agc #define CPUNAME	ci
    279  1.38       agc #else
    280  1.38       agc #define ALLCPUS	; i < 1 ;
    281  1.38       agc #define CPUNAME	curcpu()
    282  1.38       agc #endif
    283  1.38       agc 
    284  1.35       agc 	i = 0;
    285  1.59      matt 	nintr = 0;
    286  1.59      matt 	nswtch = 0;
    287  1.38       agc 	for (ALLCPUS) {
    288  1.35       agc 		len += snprintf(&bf[len], LBFSZ - len,
    289  1.36    dogcow 			"cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
    290  1.36    dogcow 			"\n", i,
    291  1.38       agc 			CPUNAME->ci_schedstate.spc_cp_time[CP_USER],
    292  1.38       agc 			CPUNAME->ci_schedstate.spc_cp_time[CP_NICE],
    293  1.38       agc 			CPUNAME->ci_schedstate.spc_cp_time[CP_SYS],
    294  1.38       agc 			CPUNAME->ci_schedstate.spc_cp_time[CP_IDLE]);
    295  1.35       agc 		if (len >= LBFSZ)
    296  1.35       agc 			goto out;
    297  1.35       agc 		i += 1;
    298  1.59      matt 		nintr += CPUNAME->ci_data.cpu_nintr;
    299  1.59      matt 		nswtch += CPUNAME->ci_data.cpu_nswtch;
    300  1.35       agc 	}
    301  1.35       agc 
    302  1.35       agc 	len += snprintf(&bf[len], LBFSZ - len,
    303  1.35       agc 			"disk 0 0 0 0\n"
    304  1.35       agc 			"page %u %u\n"
    305  1.35       agc 			"swap %u %u\n"
    306  1.59      matt 			"intr %"PRIu64"\n"
    307  1.59      matt 			"ctxt %"PRIu64"\n"
    308  1.59      matt 			"btime %"PRId64"\n",
    309  1.35       agc 			uvmexp.pageins, uvmexp.pdpageouts,
    310  1.35       agc 			uvmexp.pgswapin, uvmexp.pgswapout,
    311  1.59      matt 			nintr,
    312  1.59      matt 			nswtch,
    313  1.59      matt 			boottime.tv_sec);
    314  1.35       agc 	if (len >= LBFSZ)
    315  1.35       agc 		goto out;
    316  1.35       agc 
    317  1.35       agc 	error = uiomove_frombuf(bf, len, uio);
    318  1.35       agc out:
    319  1.35       agc 	free(bf, M_TEMP);
    320  1.35       agc 	return error;
    321  1.35       agc }
    322  1.35       agc 
    323  1.35       agc /*
    324  1.35       agc  * Linux compatible /proc/loadavg. Only active when the -o linux
    325  1.35       agc  * mountflag is used.
    326  1.35       agc  */
    327  1.35       agc int
    328  1.35       agc procfs_doloadavg(struct lwp *curl, struct proc *p,
    329  1.35       agc     struct pfsnode *pfs, struct uio *uio)
    330  1.35       agc {
    331  1.35       agc 	char	*bf;
    332  1.35       agc 	int 	 error;
    333  1.35       agc 	int 	 len;
    334  1.35       agc 
    335  1.35       agc 	error = ENAMETOOLONG;
    336  1.35       agc 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    337  1.35       agc 
    338  1.35       agc 	averunnable.fscale = FSCALE;
    339  1.35       agc 	len = snprintf(bf, LBFSZ,
    340  1.35       agc 	        "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
    341  1.35       agc 		(int)(averunnable.ldavg[0] / averunnable.fscale),
    342  1.35       agc 		(int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
    343  1.35       agc 		(int)(averunnable.ldavg[1] / averunnable.fscale),
    344  1.35       agc 		(int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
    345  1.35       agc 		(int)(averunnable.ldavg[2] / averunnable.fscale),
    346  1.35       agc 		(int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
    347  1.35       agc 		1,		/* number of ONPROC processes */
    348  1.35       agc 		nprocs,
    349  1.35       agc 		30000);		/* last pid */
    350  1.35       agc 	if (len == 0)
    351  1.35       agc 		goto out;
    352  1.35       agc 
    353  1.35       agc 	error = uiomove_frombuf(bf, len, uio);
    354  1.35       agc out:
    355  1.35       agc 	free(bf, M_TEMP);
    356  1.35       agc 	return error;
    357  1.35       agc }
    358  1.35       agc 
    359  1.35       agc /*
    360  1.35       agc  * Linux compatible /proc/<pid>/statm. Only active when the -o linux
    361  1.35       agc  * mountflag is used.
    362  1.35       agc  */
    363  1.35       agc int
    364  1.35       agc procfs_do_pid_statm(struct lwp *curl, struct lwp *l,
    365  1.35       agc     struct pfsnode *pfs, struct uio *uio)
    366  1.35       agc {
    367  1.37       agc 	struct vmspace	*vm;
    368  1.35       agc 	struct proc	*p = l->l_proc;
    369  1.35       agc 	char		*bf;
    370  1.35       agc 	int	 	 error;
    371  1.35       agc 	int	 	 len;
    372  1.72   mlelstv 	struct kinfo_proc2 ki;
    373  1.35       agc 
    374  1.35       agc 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    375  1.35       agc 
    376  1.37       agc 	/* XXX - we use values from vmspace, since dsl says that ru figures
    377  1.37       agc 	   are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */
    378  1.37       agc 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
    379  1.39       agc 		goto out;
    380  1.37       agc 	}
    381  1.35       agc 
    382  1.72   mlelstv 	mutex_enter(proc_lock);
    383  1.72   mlelstv 	mutex_enter(p->p_lock);
    384  1.72   mlelstv 
    385  1.72   mlelstv 	/* retrieve RSS size */
    386  1.72   mlelstv 	fill_kproc2(p, &ki, false);
    387  1.72   mlelstv 
    388  1.72   mlelstv 	mutex_exit(p->p_lock);
    389  1.72   mlelstv 	mutex_exit(proc_lock);
    390  1.72   mlelstv 
    391  1.72   mlelstv 	uvmspace_free(vm);
    392  1.72   mlelstv 
    393  1.35       agc 	len = snprintf(bf, LBFSZ,
    394  1.35       agc 	        "%lu %lu %lu %lu %lu %lu %lu\n",
    395  1.72   mlelstv 		(unsigned long)(ki.p_vm_msize),	/* size */
    396  1.72   mlelstv 		(unsigned long)(ki.p_vm_rssize),/* resident */
    397  1.72   mlelstv 		(unsigned long)(ki.p_uru_ixrss),/* shared */
    398  1.72   mlelstv 		(unsigned long)(ki.p_vm_tsize),	/* text */
    399  1.72   mlelstv 		(unsigned long) 0,		/* library (unused) */
    400  1.72   mlelstv 		(unsigned long)(ki.p_vm_dsize + ki.p_vm_ssize),	/* data+stack */
    401  1.72   mlelstv 		(unsigned long) 0);		/* dirty */
    402  1.58  dholland 
    403  1.35       agc 	if (len == 0)
    404  1.35       agc 		goto out;
    405  1.35       agc 
    406  1.35       agc 	error = uiomove_frombuf(bf, len, uio);
    407  1.35       agc out:
    408  1.35       agc 	free(bf, M_TEMP);
    409  1.35       agc 	return error;
    410  1.35       agc }
    411  1.35       agc 
    412  1.69     njoly #define UTIME2TICKS(s,u)	(((uint64_t)(s) * 1000000 + (u)) / 10000)
    413  1.37       agc 
    414  1.35       agc /*
    415   1.7  christos  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
    416   1.7  christos  * mountflag is used.
    417   1.7  christos  */
    418   1.7  christos int
    419  1.30  christos procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
    420  1.30  christos     struct pfsnode *pfs, struct uio *uio)
    421   1.7  christos {
    422  1.29  christos 	char *bf;
    423  1.25  christos 	struct proc *p = l->l_proc;
    424  1.16    itojun 	int len;
    425   1.7  christos 	struct rusage *cru = &p->p_stats->p_cru;
    426   1.7  christos 	unsigned long stext = 0, etext = 0, sstack = 0;
    427  1.32        ad 	struct timeval rt;
    428  1.39       agc 	struct vmspace	*vm;
    429  1.69     njoly 	struct kinfo_proc2 ki;
    430  1.71      maxv 	int error;
    431  1.29  christos 
    432  1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    433   1.7  christos 
    434  1.39       agc 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
    435  1.39       agc 		goto out;
    436  1.39       agc 	}
    437  1.39       agc 
    438  1.35       agc 	get_proc_size_info(l, &stext, &etext, &sstack);
    439  1.32        ad 
    440  1.49        ad 	mutex_enter(proc_lock);
    441  1.50        ad 	mutex_enter(p->p_lock);
    442  1.32        ad 
    443  1.69     njoly 	fill_kproc2(p, &ki, false);
    444  1.32        ad 	calcru(p, NULL, NULL, NULL, &rt);
    445   1.7  christos 
    446  1.29  christos 	len = snprintf(bf, LBFSZ,
    447  1.69     njoly 	    "%d (%s) %c %d %d %d %u %d "
    448   1.7  christos 	    "%u "
    449  1.69     njoly 	    "%"PRIu64" %lu %"PRIu64" %lu %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" "
    450  1.69     njoly 	    "%d %d %"PRIu64" "
    451  1.69     njoly 	    "%lld %"PRIu64" %"PRId64" %lu %"PRIu64" "
    452   1.7  christos 	    "%lu %lu %lu "
    453   1.7  christos 	    "%u %u "
    454   1.7  christos 	    "%u %u %u %u "
    455  1.69     njoly 	    "%"PRIu64" %"PRIu64" %"PRIu64" %d %"PRIu64"\n",
    456   1.7  christos 
    457  1.69     njoly 	    ki.p_pid,						/* 1 pid */
    458  1.69     njoly 	    ki.p_comm,						/* 2 tcomm */
    459  1.69     njoly 	    "0RRSTZXR8"[(ki.p_stat > 8) ? 0 : (int)ki.p_stat],	/* 3 state */
    460  1.69     njoly 	    ki.p_ppid,						/* 4 ppid */
    461  1.69     njoly 	    ki.p__pgid,						/* 5 pgrp */
    462  1.69     njoly 	    ki.p_sid,						/* 6 sid */
    463  1.69     njoly 	    (ki.p_tdev != (uint32_t)NODEV) ? ki.p_tdev : 0,	/* 7 tty_nr */
    464  1.69     njoly 	    ki.p_tpgid,						/* 8 tty_pgrp */
    465   1.7  christos 
    466  1.69     njoly 	    ki.p_flag,						/* 9 flags */
    467   1.7  christos 
    468  1.69     njoly 	    ki.p_uru_minflt,					/* 10 min_flt */
    469   1.7  christos 	    cru->ru_minflt,
    470  1.69     njoly 	    ki.p_uru_majflt,					/* 12 maj_flt */
    471   1.7  christos 	    cru->ru_majflt,
    472  1.69     njoly 	    UTIME2TICKS(ki.p_uutime_sec, ki.p_uutime_usec),	/* 14 utime */
    473  1.69     njoly 	    UTIME2TICKS(ki.p_ustime_sec, ki.p_ustime_usec),	/* 15 stime */
    474  1.69     njoly 	    UTIME2TICKS(cru->ru_utime.tv_sec, cru->ru_utime.tv_usec), /* 16 cutime */
    475  1.69     njoly 	    UTIME2TICKS(cru->ru_stime.tv_sec, cru->ru_stime.tv_usec), /* 17 cstime */
    476  1.69     njoly 
    477  1.69     njoly 	    ki.p_priority,				/* XXX: 18 priority */
    478  1.69     njoly 	    ki.p_nice - NZERO,				/* 19 nice */
    479  1.69     njoly 	    ki.p_nlwps,					/* 20 num_threads */
    480   1.7  christos 
    481  1.56  christos 	    (long long)rt.tv_sec,
    482  1.69     njoly 	    UTIME2TICKS(ki.p_ustart_sec, ki.p_ustart_usec), /* 22 start_time */
    483  1.69     njoly 	    ki.p_vm_msize,				/* 23 vsize */
    484  1.69     njoly 	    PGTOKB(ki.p_vm_rssize),			/* 24 rss */
    485  1.69     njoly 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,		/* 25 rsslim */
    486  1.69     njoly 
    487  1.69     njoly 	    stext,					/* 26 start_code */
    488  1.69     njoly 	    etext,					/* 27 end_code */
    489  1.69     njoly 	    sstack,					/* 28 start_stack */
    490  1.69     njoly 
    491  1.69     njoly 	    0,						/* XXX: 29 esp */
    492  1.69     njoly 	    0,						/* XXX: 30 eip */
    493  1.69     njoly 
    494  1.69     njoly 	    ki.p_siglist.__bits[0],			/* XXX: 31 pending */
    495  1.69     njoly 	    0,						/* XXX: 32 blocked */
    496  1.69     njoly 	    ki.p_sigignore.__bits[0],		/* 33 sigign */
    497  1.69     njoly 	    ki.p_sigcatch.__bits[0],		/* 34 sigcatch */
    498  1.69     njoly 
    499  1.69     njoly 	    ki.p_wchan,					/* 35 wchan */
    500  1.69     njoly 	    ki.p_uru_nvcsw,
    501  1.69     njoly 	    ki.p_uru_nivcsw,
    502  1.69     njoly 	    ki.p_exitsig,				/* 38 exit_signal */
    503  1.69     njoly 	    ki.p_cpuid);				/* 39 task_cpu */
    504   1.7  christos 
    505  1.50        ad 	mutex_exit(p->p_lock);
    506  1.49        ad 	mutex_exit(proc_lock);
    507  1.32        ad 
    508  1.58  dholland 	uvmspace_free(vm);
    509  1.58  dholland 
    510   1.7  christos 	if (len == 0)
    511  1.29  christos 		goto out;
    512   1.7  christos 
    513  1.29  christos 	error = uiomove_frombuf(bf, len, uio);
    514  1.29  christos out:
    515  1.29  christos 	free(bf, M_TEMP);
    516  1.29  christos 	return error;
    517   1.7  christos }
    518   1.7  christos 
    519   1.1      fvdl int
    520  1.30  christos procfs_docpuinfo(struct lwp *curl, struct proc *p,
    521  1.30  christos     struct pfsnode *pfs, struct uio *uio)
    522   1.1      fvdl {
    523  1.67  christos 	size_t len = LBFSZ;
    524  1.67  christos 	char *bf = NULL;
    525  1.21  christos 	int error;
    526   1.1      fvdl 
    527  1.67  christos 	do {
    528  1.67  christos 		if (bf)
    529  1.67  christos 			free(bf, M_TEMP);
    530  1.67  christos 		bf = malloc(len, M_TEMP, M_WAITOK);
    531  1.67  christos 	} while (procfs_getcpuinfstr(bf, &len) < 0);
    532   1.5       jrf 
    533  1.21  christos 	if (len == 0) {
    534  1.21  christos 		error = 0;
    535  1.21  christos 		goto done;
    536  1.21  christos 	}
    537   1.5       jrf 
    538  1.23  christos 	error = uiomove_frombuf(bf, len, uio);
    539  1.21  christos done:
    540  1.23  christos 	free(bf, M_TEMP);
    541  1.21  christos 	return error;
    542   1.5       jrf }
    543   1.5       jrf 
    544   1.5       jrf int
    545  1.30  christos procfs_douptime(struct lwp *curl, struct proc *p,
    546  1.30  christos     struct pfsnode *pfs, struct uio *uio)
    547   1.5       jrf {
    548  1.29  christos 	char *bf;
    549  1.16    itojun 	int len;
    550   1.5       jrf 	struct timeval runtime;
    551   1.5       jrf 	u_int64_t idle;
    552  1.29  christos 	int error = 0;
    553  1.29  christos 
    554  1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    555   1.5       jrf 
    556  1.47      yamt 	microuptime(&runtime);
    557   1.5       jrf 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
    558  1.29  christos 	len = snprintf(bf, LBFSZ,
    559  1.56  christos 	    "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n",
    560  1.56  christos 	    (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000,
    561  1.16    itojun 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
    562   1.1      fvdl 
    563   1.1      fvdl 	if (len == 0)
    564  1.29  christos 		goto out;
    565   1.1      fvdl 
    566  1.29  christos 	error = uiomove_frombuf(bf, len, uio);
    567  1.29  christos out:
    568  1.29  christos 	free(bf, M_TEMP);
    569  1.29  christos 	return error;
    570   1.1      fvdl }
    571  1.19  jdolecek 
    572  1.63  christos static int
    573  1.63  christos procfs_format_sfs(char **mtab, size_t *mlen, char *buf, size_t blen,
    574  1.63  christos     const struct statvfs *sfs, struct lwp *curl, int suser)
    575  1.63  christos {
    576  1.63  christos 	const char *fsname;
    577  1.63  christos 
    578  1.63  christos 	/* Linux uses different names for some filesystems */
    579  1.63  christos 	fsname = sfs->f_fstypename;
    580  1.63  christos 	if (strcmp(fsname, "procfs") == 0)
    581  1.63  christos 		fsname = "proc";
    582  1.63  christos 	else if (strcmp(fsname, "ext2fs") == 0)
    583  1.63  christos 		fsname = "ext2";
    584  1.63  christos 
    585  1.63  christos 	blen = snprintf(buf, blen, "%s %s %s %s%s%s%s%s%s 0 0\n",
    586  1.63  christos 	    sfs->f_mntfromname, sfs->f_mntonname, fsname,
    587  1.63  christos 	    (sfs->f_flag & ST_RDONLY) ? "ro" : "rw",
    588  1.63  christos 	    (sfs->f_flag & ST_NOSUID) ? ",nosuid" : "",
    589  1.63  christos 	    (sfs->f_flag & ST_NOEXEC) ? ",noexec" : "",
    590  1.63  christos 	    (sfs->f_flag & ST_NODEV) ? ",nodev" : "",
    591  1.63  christos 	    (sfs->f_flag & ST_SYNCHRONOUS) ? ",sync" : "",
    592  1.63  christos 	    (sfs->f_flag & ST_NOATIME) ? ",noatime" : "");
    593  1.63  christos 
    594  1.63  christos 	*mtab = realloc(*mtab, *mlen + blen, M_TEMP, M_WAITOK);
    595  1.63  christos 	memcpy(*mtab + *mlen, buf, blen);
    596  1.63  christos 	*mlen += blen;
    597  1.63  christos 	return sfs->f_mntonname[0] == '/' && sfs->f_mntonname[1] == '\0';
    598  1.63  christos }
    599  1.63  christos 
    600  1.19  jdolecek int
    601  1.30  christos procfs_domounts(struct lwp *curl, struct proc *p,
    602  1.30  christos     struct pfsnode *pfs, struct uio *uio)
    603  1.19  jdolecek {
    604  1.29  christos 	char *bf, *mtab = NULL;
    605  1.63  christos 	size_t mtabsz = 0;
    606  1.19  jdolecek 	struct mount *mp, *nmp;
    607  1.64  christos 	int error = 0, root = 0;
    608  1.63  christos 	struct cwdinfo *cwdi = curl->l_proc->p_cwdi;
    609  1.62  christos 
    610  1.29  christos 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    611  1.63  christos 
    612  1.41        ad 	mutex_enter(&mountlist_lock);
    613  1.66  christos 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
    614  1.63  christos 		struct statvfs sfs;
    615  1.63  christos 
    616  1.62  christos 		if (vfs_busy(mp, &nmp))
    617  1.19  jdolecek 			continue;
    618  1.19  jdolecek 
    619  1.64  christos 		if ((error = dostatvfs(mp, &sfs, curl, MNT_WAIT, 0)) == 0)
    620  1.63  christos 			root |= procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
    621  1.64  christos 			    &sfs, curl, 0);
    622  1.19  jdolecek 
    623  1.52        ad 		vfs_unbusy(mp, false, &nmp);
    624  1.19  jdolecek 	}
    625  1.41        ad 	mutex_exit(&mountlist_lock);
    626  1.63  christos 
    627  1.63  christos 	/*
    628  1.63  christos 	 * If we are inside a chroot that is not itself a mount point,
    629  1.63  christos 	 * fake a root entry.
    630  1.63  christos 	 */
    631  1.63  christos 	if (!root && cwdi->cwdi_rdir)
    632  1.63  christos 		(void)procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
    633  1.63  christos 		    &cwdi->cwdi_rdir->v_mount->mnt_stat, curl, 1);
    634  1.63  christos 
    635  1.29  christos 	free(bf, M_TEMP);
    636  1.19  jdolecek 
    637  1.19  jdolecek 	if (mtabsz > 0) {
    638  1.19  jdolecek 		error = uiomove_frombuf(mtab, mtabsz, uio);
    639  1.19  jdolecek 		free(mtab, M_TEMP);
    640  1.19  jdolecek 	}
    641  1.19  jdolecek 
    642  1.19  jdolecek 	return error;
    643  1.19  jdolecek }
    644  1.61  jmcneill 
    645  1.61  jmcneill /*
    646  1.61  jmcneill  * Linux compatible /proc/version. Only active when the -o linux
    647  1.61  jmcneill  * mountflag is used.
    648  1.61  jmcneill  */
    649  1.61  jmcneill int
    650  1.61  jmcneill procfs_doversion(struct lwp *curl, struct proc *p,
    651  1.61  jmcneill     struct pfsnode *pfs, struct uio *uio)
    652  1.61  jmcneill {
    653  1.61  jmcneill 	char *bf;
    654  1.61  jmcneill 	char lostype[20], losrelease[20], lversion[80];
    655  1.61  jmcneill 	const char *postype, *posrelease, *pversion;
    656  1.61  jmcneill 	const char *emulname = curlwp->l_proc->p_emul->e_name;
    657  1.61  jmcneill 	int len;
    658  1.61  jmcneill 	int error = 0;
    659  1.61  jmcneill 	int nm[4];
    660  1.61  jmcneill 	size_t buflen;
    661  1.61  jmcneill 
    662  1.61  jmcneill 	CTASSERT(EMUL_LINUX_KERN_OSTYPE == EMUL_LINUX32_KERN_OSTYPE);
    663  1.61  jmcneill 	CTASSERT(EMUL_LINUX_KERN_OSRELEASE == EMUL_LINUX32_KERN_OSRELEASE);
    664  1.61  jmcneill 	CTASSERT(EMUL_LINUX_KERN_VERSION == EMUL_LINUX32_KERN_VERSION);
    665  1.61  jmcneill 
    666  1.61  jmcneill 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    667  1.61  jmcneill 
    668  1.61  jmcneill 	sysctl_lock(false);
    669  1.61  jmcneill 
    670  1.61  jmcneill 	if (strncmp(emulname, "linux", 5) == 0) {
    671  1.61  jmcneill 		/*
    672  1.61  jmcneill 		 * Lookup the emulation ostype, osrelease, and version.
    673  1.61  jmcneill 		 * Since compat_linux and compat_linux32 can be built as
    674  1.61  jmcneill 		 * modules, we use sysctl to obtain the values instead of
    675  1.61  jmcneill 		 * using the symbols directly.
    676  1.61  jmcneill 		 */
    677  1.61  jmcneill 
    678  1.61  jmcneill 		if (strcmp(emulname, "linux32") == 0) {
    679  1.61  jmcneill 			nm[0] = CTL_EMUL;
    680  1.61  jmcneill 			nm[1] = EMUL_LINUX32;
    681  1.61  jmcneill 			nm[2] = EMUL_LINUX32_KERN;
    682  1.61  jmcneill 		} else {
    683  1.61  jmcneill 			nm[0] = CTL_EMUL;
    684  1.61  jmcneill 			nm[1] = EMUL_LINUX;
    685  1.61  jmcneill 			nm[2] = EMUL_LINUX_KERN;
    686  1.61  jmcneill 		}
    687  1.61  jmcneill 
    688  1.61  jmcneill 		nm[3] = EMUL_LINUX_KERN_OSTYPE;
    689  1.61  jmcneill 		buflen = sizeof(lostype);
    690  1.61  jmcneill 		error = sysctl_dispatch(nm, __arraycount(nm),
    691  1.61  jmcneill 		    lostype, &buflen,
    692  1.61  jmcneill 		    NULL, 0, NULL, NULL, NULL);
    693  1.61  jmcneill 		if (error)
    694  1.61  jmcneill 			goto out;
    695  1.61  jmcneill 
    696  1.61  jmcneill 		nm[3] = EMUL_LINUX_KERN_OSRELEASE;
    697  1.61  jmcneill 		buflen = sizeof(losrelease);
    698  1.61  jmcneill 		error = sysctl_dispatch(nm, __arraycount(nm),
    699  1.61  jmcneill 		    losrelease, &buflen,
    700  1.61  jmcneill 		    NULL, 0, NULL, NULL, NULL);
    701  1.61  jmcneill 		if (error)
    702  1.61  jmcneill 			goto out;
    703  1.61  jmcneill 
    704  1.61  jmcneill 		nm[3] = EMUL_LINUX_KERN_VERSION;
    705  1.61  jmcneill 		buflen = sizeof(lversion);
    706  1.61  jmcneill 		error = sysctl_dispatch(nm, __arraycount(nm),
    707  1.61  jmcneill 		    lversion, &buflen,
    708  1.61  jmcneill 		    NULL, 0, NULL, NULL, NULL);
    709  1.61  jmcneill 		if (error)
    710  1.61  jmcneill 			goto out;
    711  1.61  jmcneill 
    712  1.61  jmcneill 		postype = lostype;
    713  1.61  jmcneill 		posrelease = losrelease;
    714  1.61  jmcneill 		pversion = lversion;
    715  1.61  jmcneill 	} else {
    716  1.61  jmcneill 		postype = ostype;
    717  1.61  jmcneill 		posrelease = osrelease;
    718  1.61  jmcneill 		strlcpy(lversion, version, sizeof(lversion));
    719  1.61  jmcneill 		if (strchr(lversion, '\n'))
    720  1.61  jmcneill 			*strchr(lversion, '\n') = '\0';
    721  1.61  jmcneill 		pversion = lversion;
    722  1.61  jmcneill 	}
    723  1.61  jmcneill 
    724  1.61  jmcneill 	len = snprintf(bf, LBFSZ,
    725  1.61  jmcneill 		"%s version %s (%s@localhost) (gcc version %s) %s\n",
    726  1.61  jmcneill 		postype, posrelease, emulname,
    727  1.61  jmcneill #ifdef __VERSION__
    728  1.61  jmcneill 		__VERSION__,
    729  1.61  jmcneill #else
    730  1.61  jmcneill 		"unknown",
    731  1.61  jmcneill #endif
    732  1.61  jmcneill 		pversion);
    733  1.61  jmcneill 
    734  1.61  jmcneill 	if (len == 0)
    735  1.61  jmcneill 		goto out;
    736  1.61  jmcneill 
    737  1.61  jmcneill 	error = uiomove_frombuf(bf, len, uio);
    738  1.61  jmcneill out:
    739  1.61  jmcneill 	free(bf, M_TEMP);
    740  1.61  jmcneill 	sysctl_unlock();
    741  1.61  jmcneill 	return error;
    742  1.61  jmcneill }
    743