version 1.97, 2019/01/25 00:19:26 |
version 1.98, 2019/01/28 17:49:50 |
|
|
|
|
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) |
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) |
|
|
|
struct fstat_filter { |
|
int what; |
|
int arg; |
|
}; |
|
|
struct fileargs fileargs = SLIST_HEAD_INITIALIZER(fileargs); |
struct fileargs fileargs = SLIST_HEAD_INITIALIZER(fileargs); |
|
|
int fsflg; /* show files on same filesystem as file(s) argument */ |
int fsflg; /* show files on same filesystem as file(s) argument */ |
int pflg; /* show files open by a particular pid */ |
|
int uflg; /* show files open by a particular (effective) user */ |
int uflg; /* show files open by a particular (effective) user */ |
int checkfile; /* true if restricting to particular files or filesystems */ |
int checkfile; /* true if restricting to particular files or filesystems */ |
int nflg; /* (numerical) display f.s. and rdev as dev_t */ |
int nflg; /* (numerical) display f.s. and rdev as dev_t */ |
|
|
int fuser; /* 1 if we are fuser, 0 if we are fstat */ |
int fuser; /* 1 if we are fuser, 0 if we are fstat */ |
int signo; /* signal to send (fuser only) */ |
int signo; /* signal to send (fuser only) */ |
|
|
|
int nfilter = 0; /* How many uid/pid filters are in place */ |
|
struct fstat_filter *filter = NULL; /* An array of uid/pid filters */ |
|
|
kvm_t *kd; |
kvm_t *kd; |
uid_t uid; |
uid_t uid; |
|
|
|
|
{ |
{ |
struct passwd *passwd; |
struct passwd *passwd; |
struct kinfo_file *kf, *kflast; |
struct kinfo_file *kf, *kflast; |
int arg, ch, what; |
int ch; |
char *memf, *nlistf, *optstr; |
char *memf, *nlistf, *optstr; |
char buf[_POSIX2_LINE_MAX]; |
char buf[_POSIX2_LINE_MAX]; |
const char *errstr; |
const char *errstr; |
|
|
|
|
hideroot = getuid(); |
hideroot = getuid(); |
|
|
arg = -1; |
|
what = KERN_FILE_BYPID; |
|
nlistf = memf = NULL; |
nlistf = memf = NULL; |
oflg = 0; |
oflg = 0; |
|
|
|
|
oflg = 1; |
oflg = 1; |
break; |
break; |
case 'p': |
case 'p': |
if (pflg++) |
if ((filter = recallocarray(filter, nfilter, nfilter + 1, |
usage(); |
sizeof(*filter))) == NULL) |
arg = strtonum(optarg, 0, INT_MAX, &errstr); |
err(1, NULL); |
|
filter[nfilter].arg = strtonum(optarg, 0, INT_MAX, |
|
&errstr); |
if (errstr != NULL) { |
if (errstr != NULL) { |
warnx("-p requires a process id, %s: %s", |
warnx("-p requires a process id, %s: %s", |
errstr, optarg); |
errstr, optarg); |
usage(); |
usage(); |
} |
} |
what = KERN_FILE_BYPID; |
filter[nfilter].what = KERN_FILE_BYPID; |
|
nfilter++; |
break; |
break; |
case 's': |
case 's': |
sflg = 1; |
sflg = 1; |
|
|
} |
} |
break; |
break; |
case 'u': |
case 'u': |
if (uflg++) |
uflg = 1; |
usage(); |
|
if (!fuser) { |
if (!fuser) { |
uid_t uid; |
uid_t uid; |
|
|
|
|
optarg); |
optarg); |
} |
} |
} |
} |
arg = uid; |
if ((filter = recallocarray(filter, nfilter, |
what = KERN_FILE_BYUID; |
nfilter + 1, sizeof(*filter))) == NULL) |
|
err(1, NULL); |
|
filter[nfilter].arg = uid; |
|
filter[nfilter].what = KERN_FILE_BYUID; |
|
nfilter++; |
} |
} |
break; |
break; |
case 'v': |
case 'v': |
|
|
checkfile = 1; |
checkfile = 1; |
} |
} |
|
|
if ((kf = kvm_getfiles(kd, what, arg, sizeof(*kf), &cnt)) == NULL) |
if (nfilter == 1) { |
errx(1, "%s", kvm_geterr(kd)); |
if ((kf = kvm_getfiles(kd, filter[0].what, filter[0].arg, |
|
sizeof(*kf), &cnt)) == NULL) |
|
errx(1, "%s", kvm_geterr(kd)); |
|
} else { |
|
if ((kf = kvm_getfiles(kd, KERN_FILE_BYPID, -1, sizeof(*kf), |
|
&cnt)) == NULL) |
|
errx(1, "%s", kvm_geterr(kd)); |
|
} |
|
|
if (fuser) { |
if (fuser) { |
/* |
/* |
|
|
void |
void |
fstat_dofile(struct kinfo_file *kf) |
fstat_dofile(struct kinfo_file *kf) |
{ |
{ |
|
int i; |
|
|
Uname = user_from_uid(kf->p_uid, 0); |
Uname = user_from_uid(kf->p_uid, 0); |
procuid = &kf->p_uid; |
procuid = &kf->p_uid; |
Pid = kf->p_pid; |
Pid = kf->p_pid; |
Comm = kf->p_comm; |
Comm = kf->p_comm; |
|
|
|
for (i = 0; i < nfilter; i++) { |
|
if (filter[i].what == KERN_FILE_BYPID) { |
|
if (filter[i].arg == Pid) |
|
break; |
|
} else if (filter[i].arg == *procuid) { |
|
break; |
|
} |
|
} |
|
if (i == nfilter && nfilter != 0) |
|
return; |
|
|
switch (kf->f_type) { |
switch (kf->f_type) { |
case DTYPE_VNODE: |
case DTYPE_VNODE: |