Home | History | Annotate | Line # | Download | only in procfs
procfs_linux.c revision 1.15.2.2.4.1
      1      1.15.2.1      tron /*      $NetBSD: procfs_linux.c,v 1.15.2.2.4.1 2006/10/29 13:00:44 tron 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.15.2.1      tron __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.15.2.2.4.1 2006/10/29 13:00:44 tron 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.1      fvdl 
     54           1.1      fvdl #include <miscfs/procfs/procfs.h>
     55          1.11  christos #include <compat/linux/common/linux_exec.h>
     56           1.1      fvdl 
     57           1.1      fvdl #include <uvm/uvm_extern.h>
     58           1.7  christos #include <uvm/uvm.h>
     59           1.1      fvdl 
     60           1.1      fvdl #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
     61           1.1      fvdl #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
     62           1.1      fvdl 
     63           1.1      fvdl /*
     64           1.1      fvdl  * Linux compatible /proc/meminfo. Only active when the -o linux
     65           1.1      fvdl  * mountflag is used.
     66           1.1      fvdl  */
     67           1.1      fvdl int
     68          1.10      fvdl procfs_domeminfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
     69           1.1      fvdl 		 struct uio *uio)
     70           1.1      fvdl {
     71           1.1      fvdl 	char buf[512], *cp;
     72           1.1      fvdl 	int len, error;
     73           1.1      fvdl 
     74           1.1      fvdl 	len = snprintf(buf, sizeof buf,
     75           1.1      fvdl 		"        total:    used:    free:  shared: buffers: cached:\n"
     76           1.1      fvdl 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
     77           1.1      fvdl 		"Swap: %8lu %8lu %8lu\n"
     78           1.1      fvdl 		"MemTotal:  %8lu kB\n"
     79           1.1      fvdl 		"MemFree:   %8lu kB\n"
     80           1.1      fvdl 		"MemShared: %8lu kB\n"
     81           1.1      fvdl 		"Buffers:   %8lu kB\n"
     82           1.1      fvdl 		"Cached:    %8lu kB\n"
     83           1.1      fvdl 		"SwapTotal: %8lu kB\n"
     84           1.1      fvdl 		"SwapFree:  %8lu kB\n",
     85           1.1      fvdl 		PGTOB(uvmexp.npages),
     86           1.1      fvdl 		PGTOB(uvmexp.npages - uvmexp.free),
     87           1.1      fvdl 		PGTOB(uvmexp.free),
     88           1.1      fvdl 		0L,
     89           1.4       chs 		PGTOB(uvmexp.filepages),
     90           1.4       chs 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
     91           1.1      fvdl 		PGTOB(uvmexp.swpages),
     92           1.1      fvdl 		PGTOB(uvmexp.swpginuse),
     93           1.1      fvdl 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
     94           1.1      fvdl 		PGTOKB(uvmexp.npages),
     95           1.1      fvdl 		PGTOKB(uvmexp.free),
     96           1.1      fvdl 		0L,
     97           1.4       chs 		PGTOKB(uvmexp.filepages),
     98           1.4       chs 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
     99           1.1      fvdl 		PGTOKB(uvmexp.swpages),
    100           1.1      fvdl 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
    101           1.1      fvdl 
    102           1.1      fvdl 	if (len == 0)
    103           1.1      fvdl 		return 0;
    104           1.1      fvdl 
    105           1.1      fvdl 	len -= uio->uio_offset;
    106           1.1      fvdl 	cp = buf + uio->uio_offset;
    107           1.1      fvdl 	len = imin(len, uio->uio_resid);
    108           1.1      fvdl 	if (len <= 0)
    109           1.1      fvdl 		error = 0;
    110           1.1      fvdl 	else
    111           1.1      fvdl 		error = uiomove(cp, len, uio);
    112           1.1      fvdl 	return error;
    113           1.1      fvdl }
    114           1.1      fvdl 
    115           1.7  christos /*
    116           1.7  christos  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
    117           1.7  christos  * mountflag is used.
    118           1.7  christos  */
    119           1.7  christos int
    120      1.15.2.2      tron procfs_do_pid_stat(struct proc *curp, struct lwp *l, struct pfsnode *pfs,
    121           1.7  christos 		 struct uio *uio)
    122           1.7  christos {
    123           1.7  christos 	char buf[512], *cp;
    124           1.7  christos 	int len, error;
    125      1.15.2.2      tron 	struct proc *p = l->l_proc;
    126           1.7  christos 	struct tty *tty = p->p_session->s_ttyp;
    127           1.7  christos 	struct rusage *ru = &p->p_stats->p_ru;
    128           1.7  christos 	struct rusage *cru = &p->p_stats->p_cru;
    129           1.7  christos 	struct vm_map *map = &p->p_vmspace->vm_map;
    130          1.11  christos 	struct vm_map_entry *entry;
    131           1.7  christos 	unsigned long stext = 0, etext = 0, sstack = 0;
    132           1.7  christos 
    133           1.7  christos 	if (map != &curproc->p_vmspace->vm_map)
    134           1.7  christos 		vm_map_lock_read(map);
    135           1.7  christos 	for (entry = map->header.next; entry != &map->header;
    136           1.7  christos 	    entry = entry->next) {
    137           1.7  christos 		if (UVM_ET_ISSUBMAP(entry))
    138           1.7  christos 			continue;
    139           1.7  christos 		/* assume text is the first entry */
    140           1.7  christos 		if (stext == etext) {
    141           1.7  christos 			stext = entry->start;
    142           1.7  christos 			etext = entry->end;
    143          1.11  christos 			break;
    144           1.7  christos 		}
    145           1.7  christos 	}
    146          1.13  christos #ifdef LINUX_USRSTACK
    147          1.12  christos 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
    148          1.12  christos 	    LINUX_USRSTACK < USRSTACK)
    149          1.14        he 		sstack = (unsigned long) LINUX_USRSTACK;
    150          1.11  christos 	else
    151          1.13  christos #endif
    152          1.14        he 		sstack = (unsigned long) USRSTACK;
    153           1.7  christos 
    154           1.7  christos 	if (map != &curproc->p_vmspace->vm_map)
    155           1.7  christos 		vm_map_unlock_read(map);
    156           1.7  christos 
    157           1.7  christos 	len = snprintf(buf, sizeof(buf),
    158           1.7  christos 	    "%d (%s) %c %d %d %d %d %d "
    159           1.7  christos 	    "%u "
    160           1.7  christos 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
    161           1.7  christos 	    "%d %d %d "
    162           1.8   hannken 	    "%lu %lu %lu %lu %" PRIu64 " "
    163           1.7  christos 	    "%lu %lu %lu "
    164           1.7  christos 	    "%u %u "
    165           1.7  christos 	    "%u %u %u %u "
    166           1.7  christos 	    "%lu %lu %lu %d %d\n",
    167           1.7  christos 
    168           1.7  christos 	    p->p_pid,
    169           1.7  christos 	    p->p_comm,
    170           1.7  christos 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
    171  1.15.2.2.4.1      tron 	    (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
    172           1.7  christos 
    173           1.7  christos 	    p->p_pgid,
    174           1.7  christos 	    p->p_session->s_sid,
    175           1.7  christos 	    tty ? tty->t_dev : 0,
    176          1.15  christos 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
    177           1.7  christos 
    178           1.7  christos 	    p->p_flag,
    179           1.7  christos 
    180           1.7  christos 	    ru->ru_minflt,
    181           1.7  christos 	    cru->ru_minflt,
    182           1.7  christos 	    ru->ru_majflt,
    183           1.7  christos 	    cru->ru_majflt,
    184           1.7  christos 	    ru->ru_utime.tv_sec,
    185           1.7  christos 	    ru->ru_stime.tv_sec,
    186           1.7  christos 	    cru->ru_utime.tv_sec,
    187           1.7  christos 	    cru->ru_stime.tv_sec,
    188           1.7  christos 
    189           1.7  christos 	    p->p_nice,					/* XXX: priority */
    190           1.7  christos 	    p->p_nice,
    191           1.7  christos 	    0,
    192           1.7  christos 
    193           1.7  christos 	    p->p_rtime.tv_sec,
    194           1.7  christos 	    p->p_stats->p_start.tv_sec,
    195           1.7  christos 	    ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
    196           1.7  christos 	    ru->ru_maxrss,
    197           1.7  christos 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
    198           1.7  christos 
    199           1.7  christos 	    stext,					/* start code */
    200           1.7  christos 	    etext,					/* end code */
    201           1.7  christos 	    sstack,					/* mm start stack */
    202           1.7  christos 	    0,						/* XXX: pc */
    203           1.7  christos 	    0,						/* XXX: sp */
    204           1.7  christos 	    p->p_sigctx.ps_siglist.__bits[0],		/* pending */
    205           1.7  christos 	    p->p_sigctx.ps_sigmask.__bits[0],		/* blocked */
    206           1.7  christos 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
    207           1.7  christos 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
    208           1.7  christos 
    209           1.7  christos 	    (unsigned long)(intptr_t)l->l_wchan,
    210           1.7  christos 	    ru->ru_nvcsw,
    211           1.7  christos 	    ru->ru_nivcsw,
    212           1.7  christos 	    p->p_exitsig,
    213           1.7  christos 	    0);						/* XXX: processor */
    214           1.7  christos 
    215           1.7  christos 	if (len == 0)
    216           1.7  christos 		return 0;
    217           1.7  christos 
    218           1.7  christos 	len -= uio->uio_offset;
    219           1.7  christos 	cp = buf + uio->uio_offset;
    220           1.7  christos 	len = imin(len, uio->uio_resid);
    221           1.7  christos 	if (len <= 0)
    222           1.7  christos 		error = 0;
    223           1.7  christos 	else
    224           1.7  christos 		error = uiomove(cp, len, uio);
    225           1.7  christos 	return error;
    226           1.7  christos }
    227           1.7  christos 
    228           1.1      fvdl int
    229          1.10      fvdl procfs_docpuinfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
    230           1.1      fvdl 		 struct uio *uio)
    231           1.1      fvdl {
    232           1.1      fvdl 	char buf[512], *cp;
    233           1.1      fvdl 	int len, error;
    234           1.1      fvdl 
    235           1.1      fvdl 	len = sizeof buf;
    236           1.1      fvdl 	if (procfs_getcpuinfstr(buf, &len) < 0)
    237           1.1      fvdl 		return EIO;
    238           1.5       jrf 
    239           1.5       jrf 	if (len == 0)
    240           1.5       jrf 		return 0;
    241           1.5       jrf 
    242           1.5       jrf 	len -= uio->uio_offset;
    243           1.5       jrf 	cp = buf + uio->uio_offset;
    244           1.5       jrf 	len = imin(len, uio->uio_resid);
    245           1.5       jrf 	if (len <= 0)
    246           1.5       jrf 		error = 0;
    247           1.5       jrf 	else
    248           1.5       jrf 		error = uiomove(cp, len, uio);
    249           1.5       jrf 	return error;
    250           1.5       jrf }
    251           1.5       jrf 
    252           1.5       jrf int
    253          1.10      fvdl procfs_douptime(struct proc *curp, struct proc *p, struct pfsnode *pfs,
    254           1.5       jrf 		 struct uio *uio)
    255           1.5       jrf {
    256           1.5       jrf 	char buf[512], *cp;
    257           1.5       jrf 	int len, error;
    258           1.5       jrf 	struct timeval runtime;
    259           1.5       jrf 	u_int64_t idle;
    260           1.5       jrf 
    261           1.5       jrf 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
    262           1.5       jrf 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
    263           1.6   hannken 	len = sprintf(buf, "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
    264           1.5       jrf 		      runtime.tv_sec, runtime.tv_usec / 10000,
    265           1.5       jrf 		      idle / hz, (((idle % hz) * 100) / hz) % 100);
    266           1.1      fvdl 
    267           1.1      fvdl 	if (len == 0)
    268           1.1      fvdl 		return 0;
    269           1.1      fvdl 
    270           1.1      fvdl 	len -= uio->uio_offset;
    271           1.1      fvdl 	cp = buf + uio->uio_offset;
    272           1.1      fvdl 	len = imin(len, uio->uio_resid);
    273           1.1      fvdl 	if (len <= 0)
    274           1.1      fvdl 		error = 0;
    275           1.1      fvdl 	else
    276           1.1      fvdl 		error = uiomove(cp, len, uio);
    277           1.1      fvdl 	return error;
    278           1.1      fvdl }
    279