⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fstat.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/*- * Copyright (c) 1988, 1993 *	The Regents of the University of California.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char copyright[] ="@(#) Copyright (c) 1988, 1993\n\	The Regents of the University of California.  All rights reserved.\n";#endif /* not lint */#ifndef lintstatic char sccsid[] = "@(#)fstat.c	8.1 (Berkeley) 6/6/93";#endif /* not lint */#include <sys/param.h>#include <sys/time.h>#include <sys/proc.h>#include <sys/user.h>#include <sys/stat.h>#include <sys/vnode.h>#include <sys/socket.h>#include <sys/socketvar.h>#include <sys/domain.h>#include <sys/protosw.h>#include <sys/unpcb.h>#include <sys/sysctl.h>#include <sys/filedesc.h>#define	KERNEL#include <sys/file.h>#include <ufs/ufs/quota.h>#include <ufs/ufs/inode.h>#undef KERNEL#define NFS#include <sys/mount.h>#include <nfs/nfsv2.h>#include <nfs/rpcv2.h>#include <nfs/nfs.h>#include <nfs/nfsnode.h>#undef NFS#include <net/route.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/ip.h>#include <netinet/in_pcb.h>#include <ctype.h>#include <errno.h>#include <kvm.h>#include <nlist.h>#include <paths.h>#include <pwd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define	TEXT	-1#define	CDIR	-2#define	RDIR	-3#define	TRACE	-4typedef struct devs {	struct	devs *next;	long	fsid;	ino_t	ino;	char	*name;} DEVS;DEVS *devs;struct  filestat {	long	fsid;	long	fileid;	mode_t	mode;	u_long	size;	dev_t	rdev;};#ifdef notdefstruct nlist nl[] = {	{ "" },};#endifint 	fsflg,	/* show files on same filesystem as file(s) argument */	pflg,	/* show files open by a particular pid */	uflg;	/* show files open by a particular (effective) user */int 	checkfile; /* true if restricting to particular files or filesystems */int	nflg;	/* (numerical) display f.s. and rdev as dev_t */int	vflg;	/* display errors in locating kernel data objects etc... */#define dprintf	if (vflg) fprintfstruct file **ofiles;	/* buffer of pointers to file structures */int maxfiles;#define ALLOC_OFILES(d)	\	if ((d) > maxfiles) { \		free(ofiles); \		ofiles = malloc((d) * sizeof(struct file *)); \		if (ofiles == NULL) { \			fprintf(stderr, "fstat: %s\n", strerror(errno)); \			exit(1); \		} \		maxfiles = (d); \	}/* * a kvm_read that returns true if everything is read  */#define KVM_READ(kaddr, paddr, len) \	(kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))kvm_t *kd;int ufs_filestat(), nfs_filestat();void dofiles(), getinetproto(), socktrans();void usage(), vtrans();main(argc, argv)	int argc;	char **argv;{	extern char *optarg;	extern int optind;	register struct passwd *passwd;	struct kinfo_proc *p, *plast;	int arg, ch, what;	char *memf, *nlistf;	int cnt;	arg = 0;	what = KERN_PROC_ALL;	nlistf = memf = NULL;	while ((ch = getopt(argc, argv, "fnp:u:vNM")) != EOF)		switch((char)ch) {		case 'f':			fsflg = 1;			break;		case 'M':			memf = optarg;			break;		case 'N':			nlistf = optarg;			break;		case 'n':			nflg = 1;			break;		case 'p':			if (pflg++)				usage();			if (!isdigit(*optarg)) {				fprintf(stderr,				    "fstat: -p requires a process id\n");				usage();			}			what = KERN_PROC_PID;			arg = atoi(optarg);			break;		case 'u':			if (uflg++)				usage();			if (!(passwd = getpwnam(optarg))) {				fprintf(stderr, "%s: unknown uid\n",				    optarg);				exit(1);			}			what = KERN_PROC_UID;			arg = passwd->pw_uid;			break;		case 'v':			vflg = 1;			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_open(nlistf, memf, NULL, O_RDONLY, NULL)) == NULL) {		fprintf(stderr, "fstat: %s\n", kvm_geterr(kd));		exit(1);	}#ifdef notdef	if (kvm_nlist(kd, nl) != 0) {		fprintf(stderr, "fstat: no namelist: %s\n", kvm_geterr(kd));		exit(1);	}#endif	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {		fprintf(stderr, "fstat: %s\n", kvm_geterr(kd));		exit(1);	}	if (nflg)		printf("%s","USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");	else		printf("%s","USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");	if (checkfile && fsflg == 0)		printf(" NAME\n");	else		putchar('\n');	for (plast = &p[cnt]; p < plast; ++p) {		if (p->kp_proc.p_stat == SZOMB)			continue;		dofiles(p);	}	exit(0);}char	*Uname, *Comm;int	Pid;#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \	switch(i) { \	case TEXT: \		printf(" text"); \		break; \	case CDIR: \		printf("   wd"); \		break; \	case RDIR: \		printf(" root"); \		break; \	case TRACE: \		printf("   tr"); \		break; \	default: \		printf(" %4d", i); \		break; \	}/* * print open files attributed to this process */voiddofiles(kp)	struct kinfo_proc *kp;{	int i, last;	struct file file;	struct filedesc0 filed0;#define	filed	filed0.fd_fd	struct proc *p = &kp->kp_proc;	struct eproc *ep = &kp->kp_eproc;	extern char *user_from_uid();	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);	Pid = p->p_pid;	Comm = p->p_comm;	if (p->p_fd == NULL)		return;	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {		dprintf(stderr, "can't read filedesc at %x for pid %d\n",			p->p_fd, Pid);		return;	}	/*	 * root directory vnode, if one	 */	if (filed.fd_rdir)		vtrans(filed.fd_rdir, RDIR, FREAD);	/*	 * current working directory vnode	 */	vtrans(filed.fd_cdir, CDIR, FREAD);	/*	 * ktrace vnode, if one	 */	if (p->p_tracep)		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);	/*	 * open files	 */#define FPSIZE	(sizeof (struct file *))	ALLOC_OFILES(filed.fd_lastfile+1);	if (filed.fd_nfiles > NDFILE) {		if (!KVM_READ(filed.fd_ofiles, ofiles,		    (filed.fd_lastfile+1) * FPSIZE)) {			dprintf(stderr,			    "can't read file structures at %x for pid %d\n",			    filed.fd_ofiles, Pid);			return;		}	} else		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);	for (i = 0; i <= filed.fd_lastfile; i++) {		if (ofiles[i] == NULL)			continue;		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {			dprintf(stderr, "can't read file %d at %x for pid %d\n",				i, ofiles[i], Pid);			continue;		}		if (file.f_type == DTYPE_VNODE)			vtrans((struct vnode *)file.f_data, i, file.f_flag);		else if (file.f_type == DTYPE_SOCKET) {			if (checkfile == 0)				socktrans((struct socket *)file.f_data, i);		}		else {			dprintf(stderr, 				"unknown file type %d for file %d of pid %d\n",				file.f_type, i, Pid);		}	}}voidvtrans(vp, i, flag)	struct vnode *vp;	int i;	int flag;{	struct vnode vn;	struct filestat fst;	char rw[3], mode[15];	char *badtype = NULL, *filename, *getmnton();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -