Exemple #1
0
static void
fstat_kvm(int what, int arg)
{
	struct kinfo_proc *p, *plast;
	char buf[_POSIX2_LINE_MAX];
	int cnt;

	ALLOC_OFILES(256);	/* reserve space for file pointers */

	/*
	 * Discard setgid privileges if not the running kernel so that bad
	 * guys can't print interesting stuff from kernel memory.
	 */
	if (nlistf != NULL || memf != NULL)
		setgid(getgid());

	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
		errx(1, "%s", buf);
	setgid(getgid());
#ifdef notdef
	if (kvm_nlist(kd, nl) != 0)
		errx(1, "no namelist: %s", kvm_geterr(kd));
#endif
	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
		errx(1, "%s", kvm_geterr(kd));
	print_header();
	for (plast = &p[cnt]; p < plast; ++p) {
		if (p->ki_stat == SZOMB)
			continue;
		dofiles(p);
		if (mflg)
			dommap(p);
	}
}
Exemple #2
0
int
main(int argc, char **argv)
{
    struct passwd *passwd;
    struct kinfo_proc *p, *plast;
    struct proc proc;
    int arg, ch, what;
    char *memf, *nlistf;
    char buf[_POSIX2_LINE_MAX];
    int cnt;

    arg = 0;
    what = KERN_PROC_ALL;
    nlistf = memf = NULL;
    while ((ch = getopt(argc, argv, "fmnp:u:vwN:M:")) != -1)
        switch((char)ch) {
        case 'f':
            fsflg = 1;
            break;
        case 'M':
            memf = optarg;
            break;
        case 'N':
            nlistf = optarg;
            break;
        case 'm':
            mflg = 1;
            break;
        case 'n':
            nflg = 1;
            break;
        case 'p':
            if (pflg++)
                usage();
            if (!isdigit(*optarg)) {
                warnx("-p requires a process id");
                usage();
            }
            what = KERN_PROC_PID;
            arg = atoi(optarg);
            break;
        case 'u':
            if (uflg++)
                usage();
            if (!(passwd = getpwnam(optarg)))
                errx(1, "%s: unknown uid", optarg);
            what = KERN_PROC_UID;
            arg = passwd->pw_uid;
            break;
        case 'v':
            vflg = 1;
            break;
        case 'w':
            wflg_mnt = 40;
            wflg_cmd = 16;
            break;
        case '?':
        default:
            usage();
        }

    if (*(argv += optind)) {
        for (; *argv; ++argv) {
            if (getfname(*argv))
                checkfile = 1;
        }
        if (!checkfile)	/* file(s) specified, but none accessable */
            exit(1);
    }

    ALLOC_OFILES(256);	/* reserve space for file pointers */

    if (fsflg && !checkfile) {
        /* -f with no files means use wd */
        if (getfname(".") == 0)
            exit(1);
        checkfile = 1;
    }

    /*
     * Discard setgid privileges if not the running kernel so that bad
     * guys can't print interesting stuff from kernel memory.
     */
    if (nlistf != NULL || memf != NULL)
        setgid(getgid());

    if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
        errx(1, "%s", buf);
#ifdef notdef
    if (kvm_nlist(kd, nl) != 0)
        errx(1, "no namelist: %s", kvm_geterr(kd));
#endif
    if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
        errx(1, "%s", kvm_geterr(kd));
    if (nflg)
        printf("USER     %-*.*s %*.*s   FD DEV              %*.*s MODE   SZ|DV R/W",
               wflg_cmd, wflg_cmd, "CMD",
               pid_width, pid_width, "PID",
               ino_width, ino_width, "INUM");
    else
        printf("USER     %-*.*s %*.*s   FD %-*.*s %*.*s MODE           SZ|DV R/W",
               wflg_cmd, wflg_cmd, "CMD",
               pid_width, pid_width, "PID",
               wflg_mnt, wflg_mnt, "PATH",
               ino_width, ino_width, "INUM");
    if (checkfile && fsflg == 0)
        printf(" NAME\n");
    else
        putchar('\n');

    for (plast = &p[cnt]; p < plast; ++p) {
        if (p->kp_stat == SZOMB)
            continue;
        if (!kread((void *)p->kp_paddr, &proc, sizeof(proc))) {
            dprintf(stderr, "can't read proc at %p for pid %d\n",
                    (void *)p->kp_paddr, Pid);
            continue;
        }
        dofiles(p, &proc);
        if (mflg)
            dommap(&proc);
    }
    exit(0);
}