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