1  | 
     | 
     | 
    /*	$OpenBSD: nlist.c,v 1.19 2015/01/16 06:39:32 deraadt Exp $	*/  | 
    
    
    2  | 
     | 
     | 
    /*	$NetBSD: nlist.c,v 1.11 1995/03/21 09:08:03 cgd Exp $	*/  | 
    
    
    3  | 
     | 
     | 
     | 
    
    
    4  | 
     | 
     | 
    /*-  | 
    
    
    5  | 
     | 
     | 
     * Copyright (c) 1990, 1993, 1994  | 
    
    
    6  | 
     | 
     | 
     *	The Regents of the University of California.  All rights reserved.  | 
    
    
    7  | 
     | 
     | 
     *  | 
    
    
    8  | 
     | 
     | 
     * Redistribution and use in source and binary forms, with or without  | 
    
    
    9  | 
     | 
     | 
     * modification, are permitted provided that the following conditions  | 
    
    
    10  | 
     | 
     | 
     * are met:  | 
    
    
    11  | 
     | 
     | 
     * 1. Redistributions of source code must retain the above copyright  | 
    
    
    12  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer.  | 
    
    
    13  | 
     | 
     | 
     * 2. Redistributions in binary form must reproduce the above copyright  | 
    
    
    14  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer in the  | 
    
    
    15  | 
     | 
     | 
     *    documentation and/or other materials provided with the distribution.  | 
    
    
    16  | 
     | 
     | 
     * 3. Neither the name of the University nor the names of its contributors  | 
    
    
    17  | 
     | 
     | 
     *    may be used to endorse or promote products derived from this software  | 
    
    
    18  | 
     | 
     | 
     *    without specific prior written permission.  | 
    
    
    19  | 
     | 
     | 
     *  | 
    
    
    20  | 
     | 
     | 
     * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND  | 
    
    
    21  | 
     | 
     | 
     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  | 
    
    
    22  | 
     | 
     | 
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  | 
    
    
    23  | 
     | 
     | 
     * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE  | 
    
    
    24  | 
     | 
     | 
     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  | 
    
    
    25  | 
     | 
     | 
     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  | 
    
    
    26  | 
     | 
     | 
     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  | 
    
    
    27  | 
     | 
     | 
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  | 
    
    
    28  | 
     | 
     | 
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  | 
    
    
    29  | 
     | 
     | 
     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  | 
    
    
    30  | 
     | 
     | 
     * SUCH DAMAGE.  | 
    
    
    31  | 
     | 
     | 
     */  | 
    
    
    32  | 
     | 
     | 
     | 
    
    
    33  | 
     | 
     | 
    #include <sys/param.h>	/* MAXCOMLEN */  | 
    
    
    34  | 
     | 
     | 
    #include <sys/time.h>  | 
    
    
    35  | 
     | 
     | 
    #include <sys/signal.h>  | 
    
    
    36  | 
     | 
     | 
    #include <sys/proc.h>  | 
    
    
    37  | 
     | 
     | 
    #include <sys/resource.h>  | 
    
    
    38  | 
     | 
     | 
    #include <sys/sysctl.h>  | 
    
    
    39  | 
     | 
     | 
     | 
    
    
    40  | 
     | 
     | 
    #include <err.h>  | 
    
    
    41  | 
     | 
     | 
    #include <errno.h>  | 
    
    
    42  | 
     | 
     | 
    #include <kvm.h>  | 
    
    
    43  | 
     | 
     | 
    #include <nlist.h>  | 
    
    
    44  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    45  | 
     | 
     | 
    #include <string.h>  | 
    
    
    46  | 
     | 
     | 
    #include <unistd.h>  | 
    
    
    47  | 
     | 
     | 
     | 
    
    
    48  | 
     | 
     | 
    #include "ps.h"  | 
    
    
    49  | 
     | 
     | 
     | 
    
    
    50  | 
     | 
     | 
    struct	nlist psnl[] = { | 
    
    
    51  | 
     | 
     | 
    	{"_fscale"}, | 
    
    
    52  | 
     | 
     | 
    #define	X_FSCALE	0  | 
    
    
    53  | 
     | 
     | 
    	{"_ccpu"}, | 
    
    
    54  | 
     | 
     | 
    #define	X_CCPU		1  | 
    
    
    55  | 
     | 
     | 
    	{"_physmem"}, | 
    
    
    56  | 
     | 
     | 
    #define	X_PHYSMEM	2  | 
    
    
    57  | 
     | 
     | 
    	{"_maxslp"}, | 
    
    
    58  | 
     | 
     | 
    #define X_MAXSLP	3  | 
    
    
    59  | 
     | 
     | 
    	{NULL} | 
    
    
    60  | 
     | 
     | 
    };  | 
    
    
    61  | 
     | 
     | 
     | 
    
    
    62  | 
     | 
     | 
    fixpt_t	ccpu;				/* kernel _ccpu variable */  | 
    
    
    63  | 
     | 
     | 
    int	nlistread;			/* if nlist already read. */  | 
    
    
    64  | 
     | 
     | 
    u_int	mempages;			/* number of pages of phys. memory */  | 
    
    
    65  | 
     | 
     | 
    int	fscale;				/* kernel _fscale variable */  | 
    
    
    66  | 
     | 
     | 
    int	maxslp;  | 
    
    
    67  | 
     | 
     | 
     | 
    
    
    68  | 
     | 
     | 
    extern kvm_t *kd;  | 
    
    
    69  | 
     | 
     | 
     | 
    
    
    70  | 
     | 
     | 
    #define kread(x, v) \  | 
    
    
    71  | 
     | 
     | 
    	kvm_read(kd, psnl[x].n_value, &v, sizeof v) != sizeof(v)  | 
    
    
    72  | 
     | 
     | 
     | 
    
    
    73  | 
     | 
     | 
    int  | 
    
    
    74  | 
     | 
     | 
    donlist(void)  | 
    
    
    75  | 
     | 
    2  | 
    { | 
    
    
    76  | 
     | 
     | 
    	int64_t physmem;  | 
    
    
    77  | 
     | 
     | 
    	int rval, mib[2];  | 
    
    
    78  | 
     | 
     | 
    	size_t siz;  | 
    
    
    79  | 
     | 
     | 
     | 
    
    
    80  | 
     | 
    2  | 
    	rval = 0;  | 
    
    
    81  | 
     | 
    2  | 
    	nlistread = 1;  | 
    
    
    82  | 
     | 
     | 
     | 
    
    
    83  | 
    ✓✗✗✓
  | 
    2  | 
    	if (kd != NULL && !kvm_sysctl_only) { | 
    
    
    84  | 
     | 
     | 
    		if (kvm_nlist(kd, psnl)) { | 
    
    
    85  | 
     | 
     | 
    			nlisterr(psnl);  | 
    
    
    86  | 
     | 
     | 
    			eval = 1;  | 
    
    
    87  | 
     | 
     | 
    			return (1);  | 
    
    
    88  | 
     | 
     | 
    		}  | 
    
    
    89  | 
     | 
     | 
    		if (kread(X_FSCALE, fscale)) { | 
    
    
    90  | 
     | 
     | 
    			warnx("fscale: %s", kvm_geterr(kd)); | 
    
    
    91  | 
     | 
     | 
    			eval = rval = 1;  | 
    
    
    92  | 
     | 
     | 
    		}  | 
    
    
    93  | 
     | 
     | 
    		if (kread(X_PHYSMEM, mempages)) { | 
    
    
    94  | 
     | 
     | 
    			warnx("physmem: %s", kvm_geterr(kd)); | 
    
    
    95  | 
     | 
     | 
    			eval = rval = 1;  | 
    
    
    96  | 
     | 
     | 
    		}  | 
    
    
    97  | 
     | 
     | 
    		if (kread(X_CCPU, ccpu)) { | 
    
    
    98  | 
     | 
     | 
    			warnx("ccpu: %s", kvm_geterr(kd)); | 
    
    
    99  | 
     | 
     | 
    			eval = rval = 1;  | 
    
    
    100  | 
     | 
     | 
    		}  | 
    
    
    101  | 
     | 
     | 
    		if (kread(X_MAXSLP, maxslp)) { | 
    
    
    102  | 
     | 
     | 
    			warnx("maxslp: %s", kvm_geterr(kd)); | 
    
    
    103  | 
     | 
     | 
    			eval = rval = 1;  | 
    
    
    104  | 
     | 
     | 
    		}  | 
    
    
    105  | 
     | 
     | 
    	} else { | 
    
    
    106  | 
     | 
    2  | 
    		siz = sizeof (fscale);  | 
    
    
    107  | 
     | 
    2  | 
    		mib[0] = CTL_KERN;  | 
    
    
    108  | 
     | 
    2  | 
    		mib[1] = KERN_FSCALE;  | 
    
    
    109  | 
    ✗✓ | 
    2  | 
    		if (sysctl(mib, 2, &fscale, &siz, NULL, 0) < 0) { | 
    
    
    110  | 
     | 
     | 
    			warnx("fscale: failed to get kern.fscale"); | 
    
    
    111  | 
     | 
     | 
    			eval = rval = 1;  | 
    
    
    112  | 
     | 
     | 
    		}  | 
    
    
    113  | 
     | 
    2  | 
    		siz = sizeof (physmem);  | 
    
    
    114  | 
     | 
    2  | 
    		mib[0] = CTL_HW;  | 
    
    
    115  | 
     | 
    2  | 
    		mib[1] = HW_PHYSMEM64;  | 
    
    
    116  | 
    ✗✓ | 
    2  | 
    		if (sysctl(mib, 2, &physmem, &siz, NULL, 0) < 0) { | 
    
    
    117  | 
     | 
     | 
    			warnx("physmem: failed to get hw.physmem"); | 
    
    
    118  | 
     | 
     | 
    			eval = rval = 1;  | 
    
    
    119  | 
     | 
     | 
    		}  | 
    
    
    120  | 
     | 
     | 
    		/* translate bytes into page count */  | 
    
    
    121  | 
     | 
    2  | 
    		mempages = physmem / getpagesize();  | 
    
    
    122  | 
     | 
    2  | 
    		siz = sizeof (ccpu);  | 
    
    
    123  | 
     | 
    2  | 
    		mib[0] = CTL_KERN;  | 
    
    
    124  | 
     | 
    2  | 
    		mib[1] = KERN_CCPU;  | 
    
    
    125  | 
    ✗✓ | 
    2  | 
    		if (sysctl(mib, 2, &ccpu, &siz, NULL, 0) < 0) { | 
    
    
    126  | 
     | 
     | 
    			warnx("ccpu: failed to get kern.ccpu"); | 
    
    
    127  | 
     | 
     | 
    			eval = rval = 1;  | 
    
    
    128  | 
     | 
     | 
    		}  | 
    
    
    129  | 
     | 
    2  | 
    		siz = sizeof (maxslp);  | 
    
    
    130  | 
     | 
    2  | 
    		mib[0] = CTL_VM;  | 
    
    
    131  | 
     | 
    2  | 
    		mib[1] = VM_MAXSLP;  | 
    
    
    132  | 
    ✗✓ | 
    2  | 
    		if (sysctl(mib, 2, &maxslp, &siz, NULL, 0) < 0) { | 
    
    
    133  | 
     | 
     | 
    			warnx("maxslp: failed to get vm.maxslp"); | 
    
    
    134  | 
     | 
     | 
    			eval = rval = 1;  | 
    
    
    135  | 
     | 
     | 
    		}  | 
    
    
    136  | 
     | 
     | 
    	}  | 
    
    
    137  | 
     | 
    2  | 
    	return (rval);  | 
    
    
    138  | 
     | 
     | 
    }  | 
    
    
    139  | 
     | 
     | 
     | 
    
    
    140  | 
     | 
     | 
    void  | 
    
    
    141  | 
     | 
     | 
    nlisterr(struct nlist nl[])  | 
    
    
    142  | 
     | 
     | 
    { | 
    
    
    143  | 
     | 
     | 
    	int i;  | 
    
    
    144  | 
     | 
     | 
     | 
    
    
    145  | 
     | 
     | 
    	(void)fprintf(stderr, "ps: nlist: can't find following symbols:");  | 
    
    
    146  | 
     | 
     | 
    	for (i = 0; nl[i].n_name != NULL; i++)  | 
    
    
    147  | 
     | 
     | 
    		if (nl[i].n_value == 0)  | 
    
    
    148  | 
     | 
     | 
    			(void)fprintf(stderr, " %s", nl[i].n_name);  | 
    
    
    149  | 
     | 
     | 
    	(void)fprintf(stderr, "\n");  | 
    
    
    150  | 
     | 
     | 
    }  |