#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 defined(LT_KMEM)
#undef DO_TEST
# endif
#endif
int Fd = -1;
pid_t MyPid = (pid_t)0;
char *Path = (char *)NULL;
char *Pn = (char *)NULL;
_PROTOTYPE(static void cleanup,(void));
_PROTOTYPE(static char *FindFile,(char *opt, int *ff, int ie, LTdev_t *tfdc,
char *ibuf, char *xlnk, char *szbuf));
int
main(argc, argv)
int argc;
char *argv[];
{
char buf[2048];
int do_unlink = 1;
char *em;
int ff;
char ibuf[32];
char *opt;
int sz;
char szbuf[32];
LTdev_t tfdc;
struct stat tfsb;
int ti, tj;
char xlnk[32];
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)) {
(void) snprintf(buf, sizeof(buf) - 1, "./config.LTnlink%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);
}
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;
}
if (stat(Path, &tfsb)) {
(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 ((em = ConvStatDev(&tfsb.st_dev, &tfdc)))
PrtMsgX(em, Pn, cleanup, 1);
(void) snprintf(ibuf, sizeof(ibuf) - 1, "%u", (unsigned int)tfsb.st_ino);
ibuf[sizeof(szbuf) - 1] = '\0';
(void) snprintf(xlnk, sizeof(xlnk) - 1, "%d", (int)tfsb.st_nlink);
ibuf[sizeof(szbuf) - 1] = '\0';
(void) snprintf(szbuf, sizeof(szbuf) - 1, "%d", sz);
szbuf[sizeof(szbuf) - 1] = '\0';
(void) FindFile("-Na", &ff, 1, &tfdc, ibuf, xlnk, szbuf);
if (ff) {
(void) snprintf(buf, sizeof(buf) - 1,
"WARNING!!! Test file %s is NFS mounted.", Path);
(void) PrtMsg(buf, Pn);
(void) PrtMsg(
" As a result this test probably won't be able to unlink it and",
Pn);
(void) PrtMsg(
" find its open and unlinked instance with lsof's +L option.",
Pn);
(void) PrtMsg(
" Therefore, that section of this test has been disabled.\n",
Pn);
(void) PrtMsg(
" Hint: supply a path with the -p option to a file in a non-NFS",
Pn);
(void) PrtMsg(
" file system that this test can write and unlink.\n",
Pn);
(void) PrtMsg(
" See 00FAQ and 00TEST for more information.",
Pn);
do_unlink = 0;
}
if ((em = FindFile("+L", &ff, 0, &tfdc, ibuf, xlnk, szbuf)))
(void) PrtMsgX(em, Pn, cleanup, 1);
if (do_unlink) {
if (unlink(Path)) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! unlink(%s) failed: (%s).", Path, strerror(errno));
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsg(buf, Pn);
(void) snprintf(buf, sizeof(buf) - 1,
" %s may be on a ZFS file system, where it", Path);
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsg(buf, Pn);
(void) snprintf(buf, sizeof(buf) - 1,
" is not possible for %s to unlink the file it has open.", Pn);
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsg(buf, Pn);
(void) snprintf(buf, sizeof(buf) - 1,
" To run the %s test, use the \"-p path\" option to specify",
Pn);
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsg(buf, Pn);
(void) PrtMsg(
" a file on a file system -- e.g., UFS -- that supports unlink",
Pn);
(void) PrtMsg(
" while the file is open. Usually /tmp can do that -- e.g.,",
Pn);
(void) snprintf(buf, sizeof(buf) - 1,
" run the test as \"./%s -p /tmp/<name>\".\n", Pn);
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsg(buf, Pn);
(void) PrtMsgX( " See 00FAQ and 00TEST for more information.",
Pn, cleanup, 1);
}
for (opt = "+L1", ti = 0, tj = 30; ti < tj; ti++) {
if ((ti + ti + ti) >= tj)
opt = "+L";
if (!(em = FindFile(opt, &ff, 0, &tfdc, ibuf, "0", szbuf)))
break;
if (ti)
(void) printf(".");
else
(void) printf("waiting for link count update: .");
(void) fflush(stdout);
(void) sleep(2);
}
if (ti) {
printf("\n");
(void) fflush(stdout);
MsgStat = 1;
}
if (em)
(void) PrtMsgX(em, Pn, cleanup, 1);
}
(void) PrtMsgX("OK", Pn, cleanup, 0);
return(0);
}
static void
cleanup()
{
if (Fd >= 0) {
(void) close(Fd);
Fd = -1;
}
if (Path)
(void) unlink(Path);
}
static char *
FindFile(opt, ff, ie, tfdc, ibuf, xlnk, szbuf)
char *opt;
int *ff;
int ie;
LTdev_t *tfdc;
char *ibuf;
char *xlnk;
char *szbuf;
{
char buf[2048];
char *cem;
LTfldo_t *cmdp;
LTfldo_t *devp;
LTfldo_t *fop;
LTfldo_t *inop;
LTdev_t lsofdc;
int nf;
LTfldo_t *nlkp;
char *opv[4];
char *pem = (char *)NULL;
pid_t pid;
int pids = 0;
LTfldo_t *szp;
char *tcp;
int ti;
LTfldo_t *typ;
if (!ff || !ibuf || !szbuf || !tfdc || !xlnk)
(void) PrtMsgX("ERROR!!! missing argument to FindFile()",
Pn, cleanup, 1);
*ff = 0;
ti = 0;
if (opt && *opt)
opv[ti++] = opt;
#if defined(USE_LSOF_C_OPT)
opv[ti++] = "-C";
#endif
if (strcmp(xlnk, "0"))
opv[ti++] = Path;
opv[ti] = (char *)NULL;
if ((cem = ExecLsof(opv))) {
if (ie)
return((char *)NULL);
return(cem);
}
while (!*ff && (fop = RdFrLsof(&nf, &cem))) {
if (cem) {
if (ie)
return((char *)NULL);
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_SIZE:
szp = fop;
break;
case LSOF_FID_TYPE:
typ = fop;
break;
}
}
if (!devp || !inop || !szp || !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 ((tfdc->maj != lsofdc.maj)
|| (tfdc->min != lsofdc.min)
|| (tfdc->unit != lsofdc.unit)
|| strcmp(inop->v, ibuf)
) {
break;
}
*ff = 1;
if (!szp) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! lsof didn't report a file 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;
}
if (!nlkp) {
if (strcmp(xlnk, "0")) {
(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;
}
} else {
if (strcmp(nlkp->v, xlnk)) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! wrong link count: expected %s, got %s",
xlnk, nlkp->v);
buf[sizeof(buf) - 1] = '\0';
cem = MkStrCpy(buf, &ti);
if (pem)
(void) PrtMsg(pem, Pn);
pem = cem;
break;
}
}
(void) StopLsof();
if (ie)
return((char *)NULL);
return(pem);
}
}
(void) StopLsof();
if (!*ff && !ie) {
if (pem)
(void) PrtMsg(pem, Pn);
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! %s test file %s not found by lsof",
strcmp(xlnk, "0") ? "linked" : "unlinked",
Path);
buf[sizeof(buf) - 1] = '\0';
pem = MkStrCpy(buf, &ti);
}
if (ie)
return((char *)NULL);
return(pem);
}