Home | History | Annotate | Line # | Download | only in procfs
procfs_linux.c revision 1.25.4.4
      1 /*      $NetBSD: procfs_linux.c,v 1.25.4.4 2007/01/12 01:04:11 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.4 2007/01/12 01:04:11 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 #include <sys/conf.h>
     56 
     57 #include <miscfs/procfs/procfs.h>
     58 #include <compat/linux/common/linux_exec.h>
     59 
     60 #include <uvm/uvm_extern.h>
     61 #include <uvm/uvm.h>
     62 
     63 extern struct devsw_conv *devsw_conv;
     64 extern int max_devsw_convs;
     65 
     66 #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
     67 #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
     68 
     69 #define LBFSZ (8 * 1024)
     70 
     71 /*
     72  * Linux compatible /proc/meminfo. Only active when the -o linux
     73  * mountflag is used.
     74  */
     75 int
     76 procfs_domeminfo(struct lwp *curl, struct proc *p,
     77     struct pfsnode *pfs, struct uio *uio)
     78 {
     79 	char *bf;
     80 	int len;
     81 	int error = 0;
     82 
     83 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
     84 
     85 	len = snprintf(bf, LBFSZ,
     86 		"        total:    used:    free:  shared: buffers: cached:\n"
     87 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
     88 		"Swap: %8lu %8lu %8lu\n"
     89 		"MemTotal:  %8lu kB\n"
     90 		"MemFree:   %8lu kB\n"
     91 		"MemShared: %8lu kB\n"
     92 		"Buffers:   %8lu kB\n"
     93 		"Cached:    %8lu kB\n"
     94 		"SwapTotal: %8lu kB\n"
     95 		"SwapFree:  %8lu kB\n",
     96 		PGTOB(uvmexp.npages),
     97 		PGTOB(uvmexp.npages - uvmexp.free),
     98 		PGTOB(uvmexp.free),
     99 		0L,
    100 		PGTOB(uvmexp.filepages),
    101 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    102 		PGTOB(uvmexp.swpages),
    103 		PGTOB(uvmexp.swpginuse),
    104 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
    105 		PGTOKB(uvmexp.npages),
    106 		PGTOKB(uvmexp.free),
    107 		0L,
    108 		PGTOKB(uvmexp.filepages),
    109 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    110 		PGTOKB(uvmexp.swpages),
    111 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
    112 
    113 	if (len == 0)
    114 		goto out;
    115 
    116 	error = uiomove_frombuf(bf, len, uio);
    117 out:
    118 	free(bf, M_TEMP);
    119 	return error;
    120 }
    121 
    122 /*
    123  * Linux compatible /proc/devices. Only active when the -o linux
    124  * mountflag is used.
    125  */
    126 int
    127 procfs_dodevices(struct lwp *curl, struct proc *p,
    128     struct pfsnode *pfs, struct uio *uio)
    129 {
    130 	char *bf;
    131 	int offset = 0;
    132 	int i, error = ENAMETOOLONG;
    133 
    134 	/* XXX elad - may need filtering. */
    135 
    136 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    137 
    138 	offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
    139 	if (offset >= LBFSZ)
    140 		goto out;
    141 
    142 	for (i = 0; i < max_devsw_convs; i++) {
    143 		if ((devsw_conv[i].d_name == NULL) ||
    144 		    (devsw_conv[i].d_cmajor == -1))
    145 			continue;
    146 
    147 		offset += snprintf(&bf[offset], LBFSZ - offset,
    148 		    "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
    149 		if (offset >= LBFSZ)
    150 			goto out;
    151 	}
    152 
    153 	offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
    154 	if (offset >= LBFSZ)
    155 		goto out;
    156 
    157 	for (i = 0; i < max_devsw_convs; i++) {
    158 		if ((devsw_conv[i].d_name == NULL) ||
    159 		    (devsw_conv[i].d_bmajor == -1))
    160 			continue;
    161 
    162 		offset += snprintf(&bf[offset], LBFSZ - offset,
    163 		    "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
    164 		if (offset >= LBFSZ)
    165 			goto out;
    166 	}
    167 
    168 	error = uiomove_frombuf(bf, offset, uio);
    169 out:
    170 	free(bf, M_TEMP);
    171 	return error;
    172 }
    173 
    174 /*
    175  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
    176  * mountflag is used.
    177  */
    178 int
    179 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
    180     struct pfsnode *pfs, struct uio *uio)
    181 {
    182 	char *bf;
    183 	struct proc *p = l->l_proc;
    184 	int len;
    185 	struct tty *tty = p->p_session->s_ttyp;
    186 	struct rusage *ru = &p->p_stats->p_ru;
    187 	struct rusage *cru = &p->p_stats->p_cru;
    188 	struct vmspace *vm;
    189 	struct vm_map *map;
    190 	struct vm_map_entry *entry;
    191 	unsigned long stext = 0, etext = 0, sstack = 0;
    192 	struct timeval rt;
    193 	int error = 0;
    194 
    195 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    196 
    197 	proc_vmspace_getref(p, &vm);
    198 	map = &vm->vm_map;
    199 	vm_map_lock_read(map);
    200 
    201 	for (entry = map->header.next; entry != &map->header;
    202 	    entry = entry->next) {
    203 		if (UVM_ET_ISSUBMAP(entry))
    204 			continue;
    205 		/* assume text is the first entry */
    206 		if (stext == etext) {
    207 			stext = entry->start;
    208 			etext = entry->end;
    209 			break;
    210 		}
    211 	}
    212 #ifdef LINUX_USRSTACK
    213 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
    214 	    LINUX_USRSTACK < USRSTACK)
    215 		sstack = (unsigned long) LINUX_USRSTACK;
    216 	else
    217 #endif
    218 		sstack = (unsigned long) USRSTACK;
    219 
    220 	vm_map_unlock_read(map);
    221 	uvmspace_free(vm);
    222 
    223 	rw_enter(&proclist_lock, RW_READER);
    224 	mutex_enter(&p->p_mutex);
    225 	mutex_enter(&p->p_smutex);
    226 
    227 	calcru(p, NULL, NULL, NULL, &rt);
    228 
    229 	len = snprintf(bf, LBFSZ,
    230 	    "%d (%s) %c %d %d %d %d %d "
    231 	    "%u "
    232 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
    233 	    "%d %d %d "
    234 	    "%lu %lu %lu %lu %" PRIu64 " "
    235 	    "%lu %lu %lu "
    236 	    "%u %u "
    237 	    "%u %u %u %u "
    238 	    "%lu %lu %lu %d %d\n",
    239 
    240 	    p->p_pid,
    241 	    p->p_comm,
    242 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
    243 	    (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
    244 
    245 	    p->p_pgid,
    246 	    p->p_session->s_sid,
    247 	    tty ? tty->t_dev : 0,
    248 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
    249 
    250 	    p->p_flag,
    251 
    252 	    ru->ru_minflt,
    253 	    cru->ru_minflt,
    254 	    ru->ru_majflt,
    255 	    cru->ru_majflt,
    256 	    ru->ru_utime.tv_sec,
    257 	    ru->ru_stime.tv_sec,
    258 	    cru->ru_utime.tv_sec,
    259 	    cru->ru_stime.tv_sec,
    260 
    261 	    p->p_nice,					/* XXX: priority */
    262 	    p->p_nice,
    263 	    0,
    264 
    265 	    rt.tv_sec,
    266 	    p->p_stats->p_start.tv_sec,
    267 	    ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
    268 	    ru->ru_maxrss,
    269 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
    270 
    271 	    stext,					/* start code */
    272 	    etext,					/* end code */
    273 	    sstack,					/* mm start stack */
    274 	    0,						/* XXX: pc */
    275 	    0,						/* XXX: sp */
    276 	    p->p_sigpend.sp_set.__bits[0],		/* XXX: pending */
    277 	    0,						/* XXX: held */
    278 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
    279 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
    280 
    281 	    (unsigned long)(intptr_t)l->l_wchan,
    282 	    ru->ru_nvcsw,
    283 	    ru->ru_nivcsw,
    284 	    p->p_exitsig,
    285 	    0);						/* XXX: processor */
    286 
    287 	mutex_exit(&p->p_smutex);
    288 	mutex_exit(&p->p_mutex);
    289 	rw_exit(&proclist_lock);
    290 
    291 	if (len == 0)
    292 		goto out;
    293 
    294 	error = uiomove_frombuf(bf, len, uio);
    295 out:
    296 	free(bf, M_TEMP);
    297 	return error;
    298 }
    299 
    300 int
    301 procfs_docpuinfo(struct lwp *curl, struct proc *p,
    302     struct pfsnode *pfs, struct uio *uio)
    303 {
    304 	int len = LBFSZ;
    305 	char *bf = malloc(len, M_TEMP, M_WAITOK);
    306 	int error;
    307 
    308 	if (procfs_getcpuinfstr(bf, &len) < 0) {
    309 		error = ENOSPC;
    310 		goto done;
    311 	}
    312 
    313 	if (len == 0) {
    314 		error = 0;
    315 		goto done;
    316 	}
    317 
    318 	error = uiomove_frombuf(bf, len, uio);
    319 done:
    320 	free(bf, M_TEMP);
    321 	return error;
    322 }
    323 
    324 int
    325 procfs_douptime(struct lwp *curl, struct proc *p,
    326     struct pfsnode *pfs, struct uio *uio)
    327 {
    328 	char *bf;
    329 	int len;
    330 	struct timeval runtime;
    331 	u_int64_t idle;
    332 	int error = 0;
    333 
    334 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    335 
    336 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
    337 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
    338 	len = snprintf(bf, LBFSZ,
    339 	    "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
    340 	    runtime.tv_sec, runtime.tv_usec / 10000,
    341 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
    342 
    343 	if (len == 0)
    344 		goto out;
    345 
    346 	error = uiomove_frombuf(bf, len, uio);
    347 out:
    348 	free(bf, M_TEMP);
    349 	return error;
    350 }
    351 
    352 int
    353 procfs_domounts(struct lwp *curl, struct proc *p,
    354     struct pfsnode *pfs, struct uio *uio)
    355 {
    356 	char *bf, *mtab = NULL;
    357 	const char *fsname;
    358 	size_t len, mtabsz = 0;
    359 	struct mount *mp, *nmp;
    360 	struct statvfs *sfs;
    361 	int error = 0;
    362 
    363 	/* XXX elad - may need filtering. */
    364 
    365 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    366 	simple_lock(&mountlist_slock);
    367 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    368 	     mp = nmp) {
    369 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    370 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    371 			continue;
    372 		}
    373 
    374 		sfs = &mp->mnt_stat;
    375 
    376 		/* Linux uses different names for some filesystems */
    377 		fsname = sfs->f_fstypename;
    378 		if (strcmp(fsname, "procfs") == 0)
    379 			fsname = "proc";
    380 		else if (strcmp(fsname, "ext2fs") == 0)
    381 			fsname = "ext2";
    382 
    383 		len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n",
    384 			sfs->f_mntfromname,
    385 			sfs->f_mntonname,
    386 			fsname,
    387 			(mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
    388 			(mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
    389 			(mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
    390 			(mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
    391 			(mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
    392 			(mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
    393 			);
    394 
    395 		mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
    396 		memcpy(mtab + mtabsz, bf, len);
    397 		mtabsz += len;
    398 
    399 		simple_lock(&mountlist_slock);
    400 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
    401 		vfs_unbusy(mp);
    402 	}
    403 	simple_unlock(&mountlist_slock);
    404 	free(bf, M_TEMP);
    405 
    406 	if (mtabsz > 0) {
    407 		error = uiomove_frombuf(mtab, mtabsz, uio);
    408 		free(mtab, M_TEMP);
    409 	}
    410 
    411 	return error;
    412 }
    413