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

📄 file.c

📁 linux进程跟踪的工具和源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*#ifdef linux * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl> * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl> * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com> * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl> * 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. The name of the author may not be used to endorse or promote products *    derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. * *	$Id: file.c,v 1.42 2001/03/27 12:17:17 wichert Exp $ */#include "defs.h"#include <dirent.h>#ifdef linux#define dirent kernel_dirent#define dirent64 kernel_dirent64#include <linux/types.h>#include <linux/dirent.h>#undef dirent#else#define kernel_dirent dirent#endif#ifdef linux#  ifdef LINUXSPARCstruct stat {	unsigned short	st_dev;	unsigned int	st_ino;	unsigned short	st_mode;	short		st_nlink;	unsigned short	st_uid;	unsigned short	st_gid;	unsigned short	st_rdev;	unsigned int	st_size;	int		st_atime;	unsigned int	__unused1;	int		st_mtime;	unsigned int	__unused2;	int		st_ctime;	unsigned int	__unused3;	int		st_blksize;	int		st_blocks;	unsigned int	__unused4[2];};#    define stat kernel_stat#    include <asm/stat.h>#    undef stat#  else#    undef dev_t#    undef ino_t#    undef mode_t#    undef nlink_t#    undef uid_t#    undef gid_t#    undef off_t#    undef loff_t#    define dev_t __kernel_dev_t#    define ino_t __kernel_ino_t#    define mode_t __kernel_mode_t#    define nlink_t __kernel_nlink_t#    define uid_t __kernel_uid_t#    define gid_t __kernel_gid_t#    define off_t __kernel_off_t#    define loff_t __kernel_loff_t#    include <asm/stat.h>#    undef dev_t#    undef ino_t#    undef mode_t#    undef nlink_t#    undef uid_t#    undef gid_t#    undef off_t#    undef loff_t#    define dev_t dev_t#    define ino_t ino_t#    define mode_t mode_t#    define nlink_t nlink_t#    define uid_t uid_t#    define gid_t gid_t#    define off_t off_t#    define loff_t loff_t#  endif#  ifdef HPPA	/* asm-parisc/stat.h defines stat64 */#    undef stat64#  endif#  define stat libc_stat#  define stat64 libc_stat64#  include <sys/stat.h>#  undef stat#  undef stat64#  ifdef HPPA#    define stat64 hpux_stat64#  endif#else#  include <sys/stat.h>#endif#include <fcntl.h>#ifdef SVR4#  include <sys/cred.h>#endif /* SVR4 */#ifdef HAVE_SYS_VFS_H#include <sys/vfs.h>#endif#ifdef FREEBSD#include <sys/param.h>#include <sys/mount.h>#include <sys/stat.h>#endif#if HAVE_LONG_LONG_OFF_T/* * Ugly hacks for systems that have typedef long long off_t */#define stat64 stat#define HAVE_STAT64 1	/* Ugly hack */#define	sys_stat64	sys_stat#define sys_fstat64	sys_fstat#define sys_lstat64	sys_lstat#define sys_lseek64	sys_lseek#define sys_truncate64	sys_truncate#define sys_ftruncate64	sys_ftruncate#endif#ifdef MAJOR_IN_SYSMACROS#include <sys/sysmacros.h>#endif#ifdef MAJOR_IN_MKDEV#include <sys/mkdev.h>#endif#ifdef HAVE_SYS_ASYNCH_H#include <sys/asynch.h>#endif#ifdef SUNOS4#include <ustat.h>#endif/* * This is a really dirty trick but it should always work.  Traditional * Unix says r/w/rw are 0/1/2, so we make them true flags 1/2/3 by * adding 1.  Just remember to add 1 to any arg decoded with openmodes. */struct xlat openmodes[] = {	{ O_RDWR+1,	"O_RDWR"	},	{ O_RDONLY+1,	"O_RDONLY"	},	{ O_WRONLY+1,	"O_WRONLY"	},	{ O_NONBLOCK,	"O_NONBLOCK"	},	{ O_APPEND,	"O_APPEND"	},	{ O_CREAT,	"O_CREAT"	},	{ O_TRUNC,	"O_TRUNC"	},	{ O_EXCL,	"O_EXCL"	},	{ O_NOCTTY,	"O_NOCTTY"	},#ifdef O_SYNC	{ O_SYNC,	"O_SYNC"	},#endif#ifdef O_ASYNC	{ O_ASYNC,	"O_ASYNC"	},#endif#ifdef O_DSYNC	{ O_DSYNC,	"O_DSYNC"	},#endif#ifdef O_RSYNC	{ O_RSYNC,	"O_RSYNC"	},#endif#ifdef O_NDELAY	{ O_NDELAY,	"O_NDELAY"	},#endif#ifdef O_PRIV	{ O_PRIV,	"O_PRIV"	},#endif#ifdef O_DIRECT	{ O_DIRECT,	"O_DIRECT"	},#endif#ifdef O_LARGEFILE	{ O_LARGEFILE,	"O_LARGEFILE"   },#endif#ifdef O_DIRECTORY	{ O_DIRECTORY,	"O_DIRECTORY"   },#endif#ifdef O_NOFOLLOW	{ O_NOFOLLOW, 	"O_NOFOLLOW"	},#endif#ifdef FNDELAY	{ FNDELAY,	"FNDELAY"	},#endif#ifdef FAPPEND	{ FAPPEND,	"FAPPEND"	},#endif#ifdef FMARK	{ FMARK,	"FMARK"		},#endif#ifdef FDEFER	{ FDEFER,	"FDEFER"	},#endif#ifdef FASYNC	{ FASYNC,	"FASYNC"	},#endif#ifdef FSHLOCK	{ FSHLOCK,	"FSHLOCK"	},#endif#ifdef FEXLOCK	{ FEXLOCK,	"FEXLOCK"	},#endif#ifdef FCREAT	{ FCREAT,	"FCREAT"	},#endif#ifdef FTRUNC	{ FTRUNC,	"FTRUNC"	},#endif#ifdef FEXCL	{ FEXCL,	"FEXCL"		},#endif#ifdef FNBIO	{ FNBIO,	"FNBIO"		},#endif#ifdef FSYNC	{ FSYNC,	"FSYNC"		},#endif#ifdef FNOCTTY	{ FNOCTTY,	"FNOCTTY"	},#endif	#ifdef O_SHLOCK	{ O_SHLOCK,	"O_SHLOCK"	},#endif#ifdef O_EXLOCK	{ O_EXLOCK,	"O_EXLOCK"	},#endif	{ 0,		NULL		},};intsys_open(tcp)struct tcb *tcp;{	if (entering(tcp)) {		printpath(tcp, tcp->u_arg[0]);		tprintf(", ");		/* flags */		printflags(openmodes, tcp->u_arg[1] + 1);		if (tcp->u_arg[1] & O_CREAT) {			/* mode */			tprintf(", %#lo", tcp->u_arg[2]);		}	}	return 0;}#ifdef LINUXSPARCstruct xlat openmodessol[] = {	{ 0,		"O_RDWR"	},	{ 1,		"O_RDONLY"	},	{ 2,		"O_WRONLY"	},	{ 0x80,		"O_NONBLOCK"	},	{ 8,		"O_APPEND"	},	{ 0x100,	"O_CREAT"	},	{ 0x200,	"O_TRUNC"	},	{ 0x400,	"O_EXCL"	},	{ 0x800,	"O_NOCTTY"	},	{ 0x10,		"O_SYNC"	},	{ 0x40,		"O_DSYNC"	},	{ 0x8000,	"O_RSYNC"	},	{ 4,		"O_NDELAY"	},	{ 0x1000,	"O_PRIV"	},	{ 0,		NULL		},};intsolaris_open(tcp)struct tcb *tcp;{	if (entering(tcp)) {		printpath(tcp, tcp->u_arg[0]);		tprintf(", ");		/* flags */		printflags(openmodessol, tcp->u_arg[1] + 1);		if (tcp->u_arg[1] & 0x100) {			/* mode */			tprintf(", %#lo", tcp->u_arg[2]);		}	}	return 0;}#endifintsys_creat(tcp)struct tcb *tcp;{	if (entering(tcp)) {		printpath(tcp, tcp->u_arg[0]);		tprintf(", %#lo", tcp->u_arg[1]);	}	return 0;}static struct xlat access_flags[] = {	{ F_OK,		"F_OK",		},	{ R_OK,		"R_OK"		},	{ W_OK,		"W_OK"		},	{ X_OK,		"X_OK"		},#ifdef EFF_ONLY_OK	{ EFF_ONLY_OK,	"EFF_ONLY_OK"	},#endif#ifdef EX_OK	{ EX_OK,	"EX_OK"		},#endif	{ 0,		NULL		},};intsys_access(tcp)struct tcb *tcp;{	if (entering(tcp)) {		printpath(tcp, tcp->u_arg[0]);		tprintf(", ");		printflags(access_flags, tcp->u_arg[1]);	}	return 0;}intsys_umask(tcp)struct tcb *tcp;{	if (entering(tcp)) {		tprintf("%#lo", tcp->u_arg[0]);	}	return RVAL_OCTAL;}static struct xlat whence[] = {	{ SEEK_SET,	"SEEK_SET"	},	{ SEEK_CUR,	"SEEK_CUR"	},	{ SEEK_END,	"SEEK_END"	},	{ 0,		NULL		},};#ifndef HAVE_LONG_LONG_OFF_Tintsys_lseek(tcp)struct tcb *tcp;{	off_t offset;	int _whence;	if (entering(tcp)) {		tprintf("%ld, ", tcp->u_arg[0]);		offset = tcp->u_arg[1];		_whence = tcp->u_arg[2];		if (_whence == SEEK_SET)			tprintf("%lu, ", offset);		else			tprintf("%ld, ", offset);				printxval(whence, _whence, "SEEK_???");	} 	return RVAL_UDECIMAL;}#endif#ifdef linuxintsys_llseek (tcp)struct tcb *tcp;{    if (entering(tcp)) {	if (tcp->u_arg[4] == SEEK_SET)	    tprintf("%ld, %llu, ", tcp->u_arg[0],		    (((long long int) tcp->u_arg[1]) << 32		     | (unsigned long long) tcp->u_arg[2]));	else	    tprintf("%ld, %lld, ", tcp->u_arg[0],		    (((long long int) tcp->u_arg[1]) << 32		     | (unsigned long long) tcp->u_arg[2]));    }    else {	long long int off;	if (syserror(tcp) || umove(tcp, tcp->u_arg[3], &off) < 0)	    tprintf("%#lx, ", tcp->u_arg[3]);	else	    tprintf("[%llu], ", off);	printxval(whence, tcp->u_arg[4], "SEEK_???");    }    return 0;}#endif#if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_Tintsys_lseek64 (tcp)struct tcb *tcp;{	if (entering(tcp)) {		long long offset;		ALIGN64 (tcp, 1);	/* FreeBSD aligns off_t args */		offset = LONG_LONG(tcp->u_arg [1], tcp->u_arg[2]);		if (tcp->u_arg[3] == SEEK_SET)			tprintf("%ld, %llu, ", tcp->u_arg[0], offset);		else			tprintf("%ld, %lld, ", tcp->u_arg[0], offset);		printxval(whence, tcp->u_arg[3], "SEEK_???");	}	return RVAL_LUDECIMAL;}#endif#ifndef HAVE_LONG_LONG_OFF_Tintsys_truncate(tcp)struct tcb *tcp;{	if (entering(tcp)) {		printpath(tcp, tcp->u_arg[0]);		tprintf(", %lu", tcp->u_arg[1]);	}	return 0;}#endif#if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_Tintsys_truncate64(tcp)struct tcb *tcp;{	if (entering(tcp)) {		ALIGN64 (tcp, 1);		printpath(tcp, tcp->u_arg[0]);		tprintf(", %llu", LONG_LONG(tcp->u_arg[1],tcp->u_arg[2]));	}	return 0;}#endif#ifndef HAVE_LONG_LONG_OFF_Tintsys_ftruncate(tcp)struct tcb *tcp;{	if (entering(tcp)) {		tprintf("%ld, %lu", tcp->u_arg[0], tcp->u_arg[1]);	}	return 0;}#endif#if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_Tintsys_ftruncate64(tcp)struct tcb *tcp;{	if (entering(tcp)) {		ALIGN64 (tcp, 1);		tprintf("%ld, %llu", tcp->u_arg[0],			LONG_LONG(tcp->u_arg[1] ,tcp->u_arg[2]));	}	return 0;}#endif/* several stats */static struct xlat modetypes[] = {	{ S_IFREG,	"S_IFREG"	},	{ S_IFSOCK,	"S_IFSOCK"	},	{ S_IFIFO,	"S_IFIFO"	},	{ S_IFLNK,	"S_IFLNK"	},	{ S_IFDIR,	"S_IFDIR"	},	{ S_IFBLK,	"S_IFBLK"	},	{ S_IFCHR,	"S_IFCHR"	},	{ 0,		NULL		},};static char *sprintmode(mode)int mode;{	static char buf[64];	char *s;	if ((mode & S_IFMT) == 0)		s = "";	else if ((s = xlookup(modetypes, mode & S_IFMT)) == NULL) {		sprintf(buf, "%#o", mode);		return buf;	}	sprintf(buf, "%s%s%s%s", s,		(mode & S_ISUID) ? "|S_ISUID" : "",		(mode & S_ISGID) ? "|S_ISGID" : "",		(mode & S_ISVTX) ? "|S_ISVTX" : "");	mode &= ~(S_IFMT|S_ISUID|S_ISGID|S_ISVTX);	if (mode)		sprintf(buf + strlen(buf), "|%#o", mode);	s = (*buf == '|') ? buf + 1 : buf;	return *s ? s : "0";}static char *sprinttime(t)time_t t;{	struct tm *tmp;	static char buf[32];	if (t == 0) {		sprintf(buf, "0");		return buf;	}	tmp = localtime(&t);	sprintf(buf, "%02d/%02d/%02d-%02d:%02d:%02d",		tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);	return buf;}#ifdef LINUXSPARCtypedef struct {        int     tv_sec;        int     tv_nsec;} timestruct_t;struct solstat {        unsigned        st_dev;        int             st_pad1[3];     /* network id */        unsigned        st_ino;        unsigned        st_mode;        unsigned        st_nlink;        unsigned        st_uid;        unsigned        st_gid;        unsigned        st_rdev;        int             st_pad2[2];        int             st_size;        int             st_pad3;        /* st_size, off_t expansion */        timestruct_t    st_atime;        timestruct_t    st_mtime;        timestruct_t    st_ctime;        int             st_blksize;        int             st_blocks;        char            st_fstype[16];        int             st_pad4[8];     /* expansion area */};static voidprintstatsol(tcp, addr)struct tcb *tcp;long addr;{	struct solstat statbuf;	if (!addr) {		tprintf("NULL");		return;	}	if (syserror(tcp) || !verbose(tcp)) {		tprintf("%#lx", addr);		return;	}	if (umove(tcp, addr, &statbuf) < 0) {		tprintf("{...}");		return;	}	if (!abbrev(tcp)) {		tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",			(unsigned long) ((statbuf.st_dev >> 18) & 0x3fff),			(unsigned long) (statbuf.st_dev & 0x3ffff),			(unsigned long) statbuf.st_ino,			sprintmode(statbuf.st_mode));		tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",			(unsigned long) statbuf.st_nlink,			(unsigned long) statbuf.st_uid,			(unsigned long) statbuf.st_gid);		tprintf("st_blksize=%lu, ", (unsigned long) statbuf.st_blksize);		tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);	}	else		tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));	switch (statbuf.st_mode & S_IFMT) {	case S_IFCHR: case S_IFBLK:		tprintf("st_rdev=makedev(%lu, %lu), ",			(unsigned long) ((statbuf.st_rdev >> 18) & 0x3fff),			(unsigned long) (statbuf.st_rdev & 0x3ffff));		break;	default:		tprintf("st_size=%u, ", statbuf.st_size);		break;	}	if (!abbrev(tcp)) {		tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));		tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));		tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime));	}	else		tprintf("...}");}#endif /* LINUXSPARC */static struct xlat fileflags[] = {#ifdef FREEBSD	{ UF_NODUMP,	"UF_NODUMP"	},	{ UF_IMMUTABLE,	"UF_IMMUTABLE"	},	{ UF_APPEND,	"UF_APPEND"	},	{ UF_OPAQUE,	"UF_OPAQUE"	},	{ UF_NOUNLINK,	"UF_NOUNLINK"	},	{ SF_ARCHIVED,	"SF_ARCHIVED"	},	{ SF_IMMUTABLE,	"SF_IMMUTABLE"	},	{ SF_APPEND,	"SF_APPEND"	},	{ SF_NOUNLINK,	"SF_NOUNLINK"	},#elif UNIXWARE >= 2#ifdef 	_S_ISMLD	{ _S_ISMLD, 	"_S_ISMLD"	},#endif#ifdef 	_S_ISMOUNTED	{ _S_ISMOUNTED, "_S_ISMOUNTED"	},#endif#endif	{ 0,		NULL		},};#ifdef FREEBSDintsys_chflags(tcp)struct tcb *tcp;{	if (entering(tcp)) {		printpath(tcp, tcp->u_arg[0]);		tprintf(", ");		if (tcp->u_arg[1])			printflags(fileflags, tcp->u_arg[1]);		else			tprintf("0");	}	return 0;}intsys_fchflags(tcp)struct tcb *tcp;{	if (entering(tcp)) {		tprintf("%ld, ", tcp->u_arg[0]);		if (tcp->u_arg[1])			printflags(fileflags, tcp->u_arg[1]);		else			tprintf("0");	}	return 0;}#endif#ifndef HAVE_LONG_LONG_OFF_Tstatic voidrealprintstat(tcp, statbuf)struct tcb *tcp;struct stat *statbuf;{    if (!abbrev(tcp)) {	    tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",		    (unsigned long) major(statbuf->st_dev),		    (unsigned long) minor(statbuf->st_dev),		    (unsigned long) statbuf->st_ino,		    sprintmode(statbuf->st_mode));	    tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",		    (unsigned long) statbuf->st_nlink,		    (unsigned long) statbuf->st_uid,		    (unsigned long) statbuf->st_gid);#ifdef HAVE_ST_BLKSIZE	    tprintf("st_blksize=%lu, ", (unsigned long) statbuf->st_blksize);#endif /* HAVE_ST_BLKSIZE */#ifdef HAVE_ST_BLOCKS	    tprintf("st_blocks=%lu, ", (unsigned long) statbuf->st_blocks);#endif /* HAVE_ST_BLOCKS */    }    else	    tprintf("{st_mode=%s, ", sprintmode(statbuf->st_mode));

⌨️ 快捷键说明

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