Home | History | Annotate | Line # | Download | only in procfs
procfs_linux.c revision 1.25.4.1
      1 /*      $NetBSD: procfs_linux.c,v 1.25.4.1 2006/10/21 14:37:18 ad Exp $      */
      2 
      3 /*
      4  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Frank van der Linden for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.25.4.1 2006/10/21 14:37:18 ad Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/time.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/vnode.h>
     47 #include <sys/exec.h>
     48 #include <sys/resource.h>
     49 #include <sys/resourcevar.h>
     50 #include <sys/signal.h>
     51 #include <sys/signalvar.h>
     52 #include <sys/tty.h>
     53 #include <sys/malloc.h>
     54 #include <sys/mount.h>
     55 
     56 #include <miscfs/procfs/procfs.h>
     57 #include <compat/linux/common/linux_exec.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 #include <uvm/uvm.h>
     61 
     62 #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
     63 #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
     64 
     65 /*
     66  * Linux compatible /proc/meminfo. Only active when the -o linux
     67  * mountflag is used.
     68  */
     69 int
     70 procfs_domeminfo(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
     71 		 struct uio *uio)
     72 {
     73 	char bf[512];
     74 	int len;
     75 
     76 	len = snprintf(bf, sizeof bf,
     77 		"        total:    used:    free:  shared: buffers: cached:\n"
     78 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
     79 		"Swap: %8lu %8lu %8lu\n"
     80 		"MemTotal:  %8lu kB\n"
     81 		"MemFree:   %8lu kB\n"
     82 		"MemShared: %8lu kB\n"
     83 		"Buffers:   %8lu kB\n"
     84 		"Cached:    %8lu kB\n"
     85 		"SwapTotal: %8lu kB\n"
     86 		"SwapFree:  %8lu kB\n",
     87 		PGTOB(uvmexp.npages),
     88 		PGTOB(uvmexp.npages - uvmexp.free),
     89 		PGTOB(uvmexp.free),
     90 		0L,
     91 		PGTOB(uvmexp.filepages),
     92 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
     93 		PGTOB(uvmexp.swpages),
     94 		PGTOB(uvmexp.swpginuse),
     95 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
     96 		PGTOKB(uvmexp.npages),
     97 		PGTOKB(uvmexp.free),
     98 		0L,
     99 		PGTOKB(uvmexp.filepages),
    100 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    101 		PGTOKB(uvmexp.swpages),
    102 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
    103 
    104 	if (len == 0)
    105 		return 0;
    106 
    107 	return (uiomove_frombuf(bf, len, uio));
    108 }
    109 
    110 /*
    111  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
    112  * mountflag is used.
    113  */
    114 int
    115 procfs_do_pid_stat(struct lwp *curl, struct lwp *l, struct pfsnode *pfs,
    116 		 struct uio *uio)
    117 {
    118 	char bf[512];
    119 	struct proc *p = l->l_proc;
    120 	int len;
    121 	struct tty *tty = p->p_session->s_ttyp;
    122 	struct rusage *ru = &p->p_stats->p_ru;
    123 	struct rusage *cru = &p->p_stats->p_cru;
    124 	struct vm_map *map = &p->p_vmspace->vm_map;
    125 	struct vm_map_entry *entry;
    126 	unsigned long stext = 0, etext = 0, sstack = 0;
    127 	struct timeval rt;
    128 
    129 	if (map != &curproc->p_vmspace->vm_map)
    130 		vm_map_lock_read(map);
    131 	for (entry = map->header.next; entry != &map->header;
    132 	    entry = entry->next) {
    133 		if (UVM_ET_ISSUBMAP(entry))
    134 			continue;
    135 		/* assume text is the first entry */
    136 		if (stext == etext) {
    137 			stext = entry->start;
    138 			etext = entry->end;
    139 			break;
    140 		}
    141 	}
    142 #ifdef LINUX_USRSTACK
    143 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
    144 	    LINUX_USRSTACK < USRSTACK)
    145 		sstack = (unsigned long) LINUX_USRSTACK;
    146 	else
    147 #endif
    148 		sstack = (unsigned long) USRSTACK;
    149 
    150 	if (map != &curproc->p_vmspace->vm_map)
    151 		vm_map_unlock_read(map);
    152 
    153 	calcru(p, NULL, NULL, NULL, &rt);
    154 
    155 	len = snprintf(bf, sizeof(bf),
    156 	    "%d (%s) %c %d %d %d %d %d "
    157 	    "%u "
    158 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
    159 	    "%d %d %d "
    160 	    "%lu %lu %lu %lu %" PRIu64 " "
    161 	    "%lu %lu %lu "
    162 	    "%u %u "
    163 	    "%u %u %u %u "
    164 	    "%lu %lu %lu %d %d\n",
    165 
    166 	    p->p_pid,
    167 	    p->p_comm,
    168 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
    169 	    p->p_pptr->p_pid,
    170 
    171 	    p->p_pgid,
    172 	    p->p_session->s_sid,
    173 	    tty ? tty->t_dev : 0,
    174 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
    175 
    176 	    p->p_flag,
    177 
    178 	    ru->ru_minflt,
    179 	    cru->ru_minflt,
    180 	    ru->ru_majflt,
    181 	    cru->ru_majflt,
    182 	    ru->ru_utime.tv_sec,
    183 	    ru->ru_stime.tv_sec,
    184 	    cru->ru_utime.tv_sec,
    185 	    cru->ru_stime.tv_sec,
    186 
    187 	    p->p_nice,					/* XXX: priority */
    188 	    p->p_nice,
    189 	    0,
    190 
    191 	    rt.tv_sec,
    192 	    p->p_stats->p_start.tv_sec,
    193 	    ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
    194 	    ru->ru_maxrss,
    195 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
    196 
    197 	    stext,					/* start code */
    198 	    etext,					/* end code */
    199 	    sstack,					/* mm start stack */
    200 	    0,						/* XXX: pc */
    201 	    0,						/* XXX: sp */
    202 	    p->p_sigpend.sp_set.__bits[0],		/* XXX: pending */
    203 	    0,						/* XXX: held */
    204 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
    205 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
    206 
    207 	    (unsigned long)(intptr_t)l->l_wchan,
    208 	    ru->ru_nvcsw,
    209 	    ru->ru_nivcsw,
    210 	    p->p_exitsig,
    211 	    0);						/* XXX: processor */
    212 
    213 	if (len == 0)
    214 		return 0;
    215 
    216 	return (uiomove_frombuf(bf, len, uio));
    217 }
    218 
    219 int
    220 procfs_docpuinfo(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
    221 		 struct uio *uio)
    222 {
    223 	int len = 4096;
    224 	char *bf = malloc(len, M_TEMP, M_WAITOK);
    225 	int error;
    226 
    227 	if (procfs_getcpuinfstr(bf, &len) < 0) {
    228 		error = ENOSPC;
    229 		goto done;
    230 	}
    231 
    232 	if (len == 0) {
    233 		error = 0;
    234 		goto done;
    235 	}
    236 
    237 	error = uiomove_frombuf(bf, len, uio);
    238 done:
    239 	free(bf, M_TEMP);
    240 	return error;
    241 }
    242 
    243 int
    244 procfs_douptime(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
    245 		 struct uio *uio)
    246 {
    247 	char bf[512];
    248 	int len;
    249 	struct timeval runtime;
    250 	u_int64_t idle;
    251 
    252 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
    253 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
    254 	len = snprintf(bf, sizeof(bf),
    255 	    "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
    256 	    runtime.tv_sec, runtime.tv_usec / 10000,
    257 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
    258 
    259 	if (len == 0)
    260 		return 0;
    261 
    262 	return (uiomove_frombuf(bf, len, uio));
    263 }
    264 
    265 int
    266 procfs_domounts(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
    267 		 struct uio *uio)
    268 {
    269 	char bf[512], *mtab = NULL;
    270 	const char *fsname;
    271 	size_t len, mtabsz = 0;
    272 	struct mount *mp, *nmp;
    273 	struct statvfs *sfs;
    274 	int error = 0;
    275 
    276 	simple_lock(&mountlist_slock);
    277 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    278 	     mp = nmp) {
    279 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    280 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    281 			continue;
    282 		}
    283 
    284 		sfs = &mp->mnt_stat;
    285 
    286 		/* Linux uses different names for some filesystems */
    287 		fsname = sfs->f_fstypename;
    288 		if (strcmp(fsname, "procfs") == 0)
    289 			fsname = "proc";
    290 		else if (strcmp(fsname, "ext2fs") == 0)
    291 			fsname = "ext2";
    292 
    293 		len = snprintf(bf, sizeof(bf), "%s %s %s %s%s%s%s%s%s 0 0\n",
    294 			sfs->f_mntfromname,
    295 			sfs->f_mntonname,
    296 			fsname,
    297 			(mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
    298 			(mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
    299 			(mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
    300 			(mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
    301 			(mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
    302 			(mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
    303 			);
    304 
    305 		mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
    306 		memcpy(mtab + mtabsz, bf, len);
    307 		mtabsz += len;
    308 
    309 		simple_lock(&mountlist_slock);
    310 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
    311 		vfs_unbusy(mp);
    312 	}
    313 	simple_unlock(&mountlist_slock);
    314 
    315 	if (mtabsz > 0) {
    316 		error = uiomove_frombuf(mtab, mtabsz, uio);
    317 		free(mtab, M_TEMP);
    318 	}
    319 
    320 	return error;
    321 }
    322