#ifndef lint
static char copyright[] =
"@(#) Copyright 2002 Purdue Research Foundation.\nAll rights reserved.\n";
#endif
#include "LsofTest.h"
#include "lsof_fields.h"
#define DO_TEST
#if defined(LT_DIAL_darwin)
# if LT_VERS<800
#undef DO_TEST
# endif
#endif
int Fd = -1;
pid_t MyPid = (pid_t)0;
int NFstat = 0;
char *Path = (char *)NULL;
char *Pn = (char *)NULL;
_PROTOTYPE(static void cleanup,(void));
_PROTOTYPE(static char *FindNFSfile,(int *ff, char *szbuf));
int
main(argc, argv)
int argc;
char *argv[];
{
char buf[2048];
char *em;
int ff;
int sz;
char szbuf[32];
int ti;
int xv = 0;
if ((Pn = strrchr(argv[0], '/')))
Pn++;
else
Pn = argv[0];
MyPid = getpid();
(void) printf("%s ... ", Pn);
(void) fflush(stdout);
PrtMsg((char *)NULL, Pn);
#if !defined(DO_TEST)
(void) PrtMsgX(LT_DONT_DO_TEST, Pn, cleanup, 0);
#endif
if (ScanArg(argc, argv, "hp:", Pn))
xv = 1;
if (xv || LTopt_h) {
(void) PrtMsg("usage: [-h] [-p path]", Pn);
PrtMsg (" -h print help (this panel)", Pn);
PrtMsgX (" -p path define test file path", Pn, cleanup, xv);
}
if ((em = IsLsofExec()))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((em = CanRdKmem()))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((Path = LTopt_p)) {
if ((Fd = open(Path, O_RDONLY, 0400)) < 0) {
(void) fprintf(stderr, "ERROR!!! can't read-only open %s\n",
Path);
goto print_file_error;
}
NFstat = 1;
szbuf[0] = '\0';
} else {
(void) snprintf(buf, sizeof(buf) - 1, "./config.LTnfs%ld",
(long)MyPid);
buf[sizeof(buf) - 1] = '\0';
Path = MkStrCpy(buf, &ti);
(void) unlink(Path);
if ((Fd = open(Path, O_RDWR|O_CREAT, 0600)) < 0) {
(void) fprintf(stderr, "ERROR!!! can't create %s\n", Path);
print_file_error:
MsgStat = 1;
(void) snprintf(buf, sizeof(buf) - 1, " Errno %d: %s",
errno, strerror(errno));
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsgX(buf, Pn, cleanup, 1);
}
NFstat = 2;
sz = sizeof(buf);
for (ti = 0; ti < sz; ti++) {
buf[ti] = (char)(ti & 0xff);
}
if (write(Fd, buf, sz) != sz) {
(void) fprintf(stderr, "ERROR!!! can't write %d bytes to %s\n",
sz, Path);
goto print_file_error;
}
if (fsync(Fd)) {
(void) fprintf(stderr, "ERROR!!! can't fsync %s\n", Path);
goto print_file_error;
}
(void) snprintf(szbuf, sizeof(szbuf) - 1, "%d", sz);
szbuf[sizeof(szbuf) - 1] = '\0';
}
if ((em = FindNFSfile(&ff, szbuf))) {
(void) PrtMsg(em, Pn);
if (!ff) {
if (NFstat == 1) {
(void) PrtMsg(
"Hint: this test must be able to open for read access",
Pn);
(void) PrtMsg(
"the file at the path supplied with the -p option and",
Pn);
(void) PrtMsg(
"that file must be a regular file (not a directory) on",
Pn);
(void) PrtMsg(
"an NFS file system.\n",
Pn);
(void) PrtMsgX(
"See 00FAQ and 00TEST for more information.",
Pn, cleanup, 1);
} else if (NFstat == 2) {
(void) PrtMsg(
"Hint: the temporary path generated by this test might",
Pn);
(void) PrtMsg(
"not be on an NFS file system, or this test might be",
Pn);
(void) PrtMsg(
"unable to create a file on the NFS file system.\n",
Pn);
(void) PrtMsg(
"As a work-around use the -p option to specify a path to",
Pn);
(void) PrtMsg(
"a regular file (not a directory) on an NFS file system",
Pn);
(void) PrtMsg(
"to which this test will have read access.\n",
Pn);
(void) PrtMsgX(
"See 00FAQ and 00TEST for more information.",
Pn, cleanup, 1);
}
}
}
(void) PrtMsgX("OK", Pn, cleanup, 0);
return(0);
}
static void
cleanup()
{
if (Fd >= 0) {
(void) close(Fd);
Fd = -1;
if (Path) {
if (NFstat == 2)
(void) unlink(Path);
Path = (char *)NULL;
}
}
}
static char *
FindNFSfile(ff, szbuf)
int *ff;
char *szbuf;
{
char buf[2048];
char *cem;
LTfldo_t *cmdp;
LTfldo_t *devp;
LTfldo_t *fop;
char ibuf[64];
LTfldo_t *inop;
LTdev_t lsofdc;
int nf;
char nlkbuf[32];
LTfldo_t *nlkp;
char *opv[5];
char *pem = (char *)NULL;
pid_t pid;
int pids = 0;
struct stat sb;
LTdev_t stdc;
LTfldo_t *szp;
char *tcp;
int ti;
LTfldo_t *typ;
if (!ff || !szbuf)
(void) PrtMsgX("ERROR!!! missing argument to FindNFSfile()",
Pn, cleanup, 1);
*ff = 0;
if (stat(Path, &sb)) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! can't stat(2) %s: %s", Path, strerror(errno));
buf[sizeof(buf) - 1] = '\0';
PrtMsgX(buf, Pn, cleanup, 1);
}
if ((cem = ConvStatDev(&sb.st_dev, &stdc)))
PrtMsgX(cem, Pn, cleanup, 1);
(void) snprintf(ibuf, sizeof(ibuf) - 1, "%u", (unsigned int)sb.st_ino);
ibuf[sizeof(ibuf) - 1] = '\0';
(void) snprintf(nlkbuf, sizeof(nlkbuf) - 1, "%d", (int)sb.st_nlink);
nlkbuf[sizeof(nlkbuf) - 1] = '\0';
ti = 0;
opv[ti++] = "-s";
opv[ti++] = "-Na";
#if defined(USE_LSOF_C_OPT)
opv[ti++] = "-C";
#endif
opv[ti++] = Path;
opv[ti] = (char *)NULL;
if ((cem = ExecLsof(opv)))
return(cem);
while (!*ff && (fop = RdFrLsof(&nf, &cem))) {
if (cem) {
if (pem)
(void) PrtMsg(pem, Pn);
return(cem);
}
switch (fop->ft) {
case LSOF_FID_PID:
pid = (pid_t)atoi(fop->v);
pids = 1;
cmdp = (LTfldo_t *)NULL;
for (fop++, ti = 1; ti < nf; fop++, ti++) {
switch (fop->ft) {
case LSOF_FID_CMD:
cmdp = fop;
break;
}
}
if (!cmdp || (pid != MyPid))
pids = 0;
break;
case LSOF_FID_FD:
if (!pids)
break;
for (ti = 0, tcp = fop->v; *tcp; tcp++) {
if (*tcp == ' ')
continue;
if (((int)*tcp < (int)'0') || ((int)*tcp > (int)'9')) {
ti = -1;
break;
}
ti = (ti * 10) + (int)*tcp - (int)'0';
}
if (Fd != ti)
break;
devp = inop = nlkp, szp = typ = (LTfldo_t *)NULL;
for (fop++, ti = 1; ti < nf; fop++, ti++) {
switch (fop->ft) {
case LSOF_FID_DEVN:
devp = fop;
break;
case LSOF_FID_INODE:
inop = fop;
break;
case LSOF_FID_NLINK:
nlkp = fop;
break;
case LSOF_FID_OFFSET:
break;
case LSOF_FID_SIZE:
szp = fop;
break;
case LSOF_FID_TYPE:
typ = fop;
break;
}
}
if (!devp || !inop || !typ)
break;
if (strcasecmp(typ->v, "reg") && strcasecmp(typ->v, "vreg"))
break;
if ((cem = ConvLsofDev(devp->v, &lsofdc))) {
if (pem)
(void) PrtMsg(pem, Pn);
pem = cem;
break;
}
if ((stdc.maj != lsofdc.maj)
|| (stdc.min != lsofdc.min)
|| (stdc.unit != lsofdc.unit)
|| strcmp(inop->v, ibuf)
) {
break;
}
*ff = 1;
if (!nlkp) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! lsof didn't report a link count for %s", Path);
buf[sizeof(buf) - 1] = '\0';
cem = MkStrCpy(buf, &ti);
if (pem)
(void) PrtMsg(pem, Pn);
pem = cem;
break;
}
if (strcmp(nlkp->v, nlkbuf)) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! wrong link count: expected %s, got %s",
nlkbuf, nlkp->v);
buf[sizeof(buf) - 1] = '\0';
cem = MkStrCpy(buf, &ti);
if (pem)
(void) PrtMsg(pem, Pn);
pem = cem;
break;
}
if (NFstat == 2) {
if (!szp) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! lsof didn't report a size for %s", Path);
buf[sizeof(buf) - 1] = '\0';
cem = MkStrCpy(buf, &ti);
if (pem)
(void) PrtMsg(pem, Pn);
pem = cem;
break;
}
if (strcmp(szp->v, szbuf)) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! wrong file size: expected %s, got %s",
szbuf, szp->v);
buf[sizeof(buf) - 1] = '\0';
cem = MkStrCpy(buf, &ti);
if (pem)
(void) PrtMsg(pem, Pn);
pem = cem;
break;
}
}
(void) StopLsof();
return(pem);
}
}
(void) StopLsof();
if (pem)
(void) PrtMsg(pem, Pn);
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! test file %s not found by lsof", Path);
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
}