#ifndef lint
static char copyright[] =
"@(#) Copyright 2002 Purdue Research Foundation.\nAll rights reserved.\n";
#endif
#include "LsofTest.h"
#if !defined(LT_BIGF)
int
main(argc, argv)
int argc;
char *argv[];
{
char *pn;
if ((pn = (char *)strrchr(argv[0], '/')))
pn++;
else
pn = argv[0];
(void) printf("%s ... %s\n", pn, LT_DONT_DO_TEST);
return(0);
}
#else
#include "lsof_fields.h"
#define OFFTST_STAT 1
#if defined(LT_DIAL_aix)
#define OFFSET_T off64_t
#endif
#if defined(LT_DIAL_bsdi)
#define OFFSET_T off_t
#define OPENF open
#define SEEKF lseek
#define STATF stat
#define STATS struct stat
#endif
#if defined(LT_DIAL_darwin)
# if LT_VERS>=900
#define OFFSET_T off_t
#define OPENF open
#define SEEKF lseek
#define STATF stat
#define STATS struct stat
# endif
#endif
#if defined(LT_DIAL_du)
#define OFFSET_T off_t
#define OPENF open
#define SEEKF lseek
#define STATF stat
#define STATS struct stat
#endif
#if defined(LT_DIAL_freebsd)
#define OFFSET_T off_t
#define OPENF open
#define SEEKF lseek
#define STATF stat
#define STATS struct stat
#endif
#if defined(LT_DIAL_linux)
#undef OFFTST_STAT
#define OFFTST_STAT 0
#define OFFSET_T off_t
#define OPENF open
#define SEEKF lseek
#define STATF stat
#define STATS struct stat
_PROTOTYPE(static int ck_Linux_offset_support,(void));
#endif
#if defined(LT_DIAL_hpux)
#define OFFSET_T off64_t
#endif
#if defined(LT_DIAL_netbsd)
#define OFFSET_T off_t
#define OPENF open
#define SEEKF lseek
#define STATF stat
#define STATS struct stat
#endif
#if defined(LT_DIAL_openbsd)
#define OFFSET_T off_t
#define OPENF open
#define SEEKF lseek
#define STATF stat
#define STATS struct stat
#endif
#if defined(LT_DIAL_ou)
#include <signal.h>
#define IGNORE_SIGXFSZ
#define OFFSET_T off64_t
#endif
#if defined(LT_DIAL_solaris)
#define OFFSET_T off64_t
#endif
#if defined(LT_DIAL_uw)
#include <signal.h>
#define IGNORE_SIGXFSZ
#define OFFSET_T off64_t
#endif
#if !defined(OPENF)
#define OPENF open64
#endif
#if !defined(OFFSET_T)
#define OFFSET_T unsigned long long
#endif
#if !defined(SEEKF)
#define SEEKF lseek64
# endif
#if !defined(STATF)
#define STATF stat64
#endif
#if !defined(STATS)
#define STATS struct stat64
#endif
#define TST_OFFT 0
#define TST_OFFX 1
#define TST_SZ 2
int Fd = -1;
pid_t MyPid = (pid_t)0;
char *Path = (char *)NULL;
char *Pn = (char *)NULL;
_PROTOTYPE(static void cleanup,(void));
_PROTOTYPE(static int tstwlsof,(int tt, char *opt, OFFSET_T sz));
int
main(argc, argv)
int argc;
char *argv[];
{
char buf[2048];
int do_offt = OFFTST_STAT;
char *em;
int i;
int len;
OFFSET_T sz = 0x140000000ll;
char szbuf[64];
char *tcp;
int tofft = 0;
int toffx = 0;
int tsz = 0;
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 (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 defined(LT_DIAL_linux)
do_offt = ck_Linux_offset_support();
#endif
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), "./config.LTbigf%ld",
(long)MyPid);
buf[sizeof(buf) - 1] = '\0';
Path = MkStrCpy(buf, &len);
}
for (i = 0; i < sizeof(buf); i++) {
buf[i] = (char)(i & 0xff);
}
#if defined(IGNORE_SIGXFSZ)
(void) signal(SIGXFSZ, SIG_IGN);
#endif
(void) unlink(Path);
if ((Fd = OPENF(Path, O_RDWR|O_CREAT, 0600)) < 0) {
(void) fprintf(stderr, "ERROR!!! can't open %s\n", Path);
print_hint:
MsgStat = 1;
(void) snprintf(buf, sizeof(buf) - 1, " Errno %d: %s",
errno, strerror(errno));
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsg(buf, Pn);
(void) PrtMsg("Hint: try using \"-p path\" to supply a path in a", Pn);
(void) PrtMsg("file system that has large file support enabled.\n", Pn);
(void) PrtMsg("Hint: try raising the process ulimit file block", Pn);
(void) PrtMsg("size to a value that will permit this test to", Pn);
(void) snprintf(szbuf, sizeof(szbuf) - 1, "%lld", (long long)sz);
szbuf[sizeof(szbuf) - 1] = '\0';
(void) snprintf(buf, sizeof(buf) - 1,
"write a file whose size appears to be %s", szbuf);
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsg(buf, Pn);
(void) PrtMsg("bytes. (The file really isn't that big -- it", Pn);
(void) PrtMsg("just has a large \"hole\" in its mid-section.)\n", Pn);
(void) PrtMsgX("See 00FAQ and 00TEST for more information.", Pn,
cleanup, 1);
}
if (SEEKF(Fd, (OFFSET_T)0, SEEK_SET) < 0) {
(void) fprintf(stderr,
"ERROR!!! can't seek to the beginning of %s\n", Path);
goto print_hint;
}
if (write(Fd, buf, sizeof(buf)) != sizeof(buf)) {
(void) fprintf(stderr,
"ERROR!!! can't write %d bytes to the beginning of %s\n",
(int)sizeof(buf), Path);
goto print_hint;
}
if (SEEKF(Fd, (OFFSET_T)(sz - sizeof(buf)), SEEK_SET) < 0) {
(void) snprintf(szbuf, sizeof(szbuf) - 1, "%lld",
(unsigned long long)(sz - sizeof(buf)));
(void) fprintf(stderr, "ERROR!!! can't seek to %s in %s\n", szbuf,
Path);
goto print_hint;
}
if (write(Fd, buf, sizeof(buf)) != sizeof(buf)) {
(void) fprintf(stderr,
"ERROR!!! can't write %d bytes near the end of %s\n",
(int)sizeof(buf), Path);
goto print_hint;
}
if (fsync(Fd)) {
(void) fprintf(stderr, "ERROR!!! can't fsync %s\n", Path);
goto print_hint;
}
if (!do_offt) {
tofft = toffx = 1;
PrtMsg("WARNING!!! lsof can't return file offsets for this dialect,",
Pn);
PrtMsg(" so offset tests have been disabled.", Pn);
}
tsz = tstwlsof(TST_SZ, "-s", sz);
if (!tofft)
tofft = tstwlsof(TST_OFFT, "-oo20", sz);
if (!toffx)
toffx = tstwlsof(TST_OFFX, "-oo2", sz);
if ((tsz != 1) || (tofft != 1) || (toffx != 1)) {
tcp = (char *)NULL;
xv = 1;
} else {
tcp = "OK";
xv = 0;
}
(void) PrtMsgX(tcp, Pn, cleanup, xv);
return(0);
}
#if defined(LT_DIAL_linux)
static int
ck_Linux_offset_support()
{
char buf[1024];
int bufl = sizeof(buf);
char *opv[5];
int rv = 1;
if (IsLsofExec())
return(0);
opv[0] = "-o";
snprintf(buf, bufl - 1, "-p%d", (int)getpid());
opv[1] = buf;
opv[2] = "-ad0";
opv[3] = "+w";
opv[4] = (char *)NULL;
if (ExecLsof(opv))
return(0);
while(fgets(buf, bufl - 1, LsofFs)) {
if (strstr(buf, "WARNING: can't report offset")) {
rv = 0;
break;
}
}
(void) StopLsof();
return(rv);
}
#endif
static void
cleanup()
{
if (Fd >= 0) {
if (Path) {
(void) unlink(Path);
Path = (char *)NULL;
}
(void) close(Fd);
Fd = -1;
}
}
static int
tstwlsof(tt, opt, sz)
int tt;
char *opt;
OFFSET_T sz;
{
char buf[2048], buf1[2048];
LTfldo_t *cmdp;
LTfldo_t *devp;
char *em;
int ff = 0;
LTfldo_t *fop;
LTfldo_t *inop;
LTdev_t lsofdc;
int nf;
LTfldo_t *nmp;
LTfldo_t *offp;
char *opv[4];
pid_t pid;
int pids = 0;
STATS sb;
LTdev_t stdc;
LTfldo_t *szp;
LTfldo_t *tfop;
int ti;
LTfldo_t *typ;
int xv = 0;
switch (tt) {
case TST_OFFT:
case TST_OFFX:
case TST_SZ:
break;
default:
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! unknown test type: %d", tt);
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsgX(buf, Pn, cleanup, 1);
}
if (STATF(Path, &sb)) {
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! can't stat(2) %s: %s", Path, strerror(errno));
buf[sizeof(buf) - 1] = '\0';
(void) PrtMsgX(buf, Pn, cleanup, 1);
}
if ((em = ConvStatDev(&sb.st_dev, &stdc))) {
(void) PrtMsg(em, Pn);
return(0);
}
ti = 0;
if (opt && *opt)
opv[ti++] = opt;
#if defined(USE_LSOF_C_OPT)
opv[ti++] = "-C";
#else
opv[ti++] = "--";
#endif
opv[ti++] = Path;
opv[ti] = (char *)NULL;
if ((em = ExecLsof(opv))) {
(void) PrtMsg(em, Pn);
return(0);
}
while (!ff && (fop = RdFrLsof(&nf, &em))) {
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;
devp = inop = nmp = offp = 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_NAME:
nmp = fop;
break;
case LSOF_FID_OFFSET:
offp = fop;
break;
case LSOF_FID_SIZE:
szp = fop;
break;
case LSOF_FID_TYPE:
typ = fop;
break;
}
}
if (!devp || !inop || !nmp || !typ)
break;
if (strcasecmp(typ->v, "reg") && strcasecmp(typ->v, "vreg"))
break;
if (ConvLsofDev(devp->v, &lsofdc))
break;
if ((stdc.maj != lsofdc.maj)
|| (stdc.min != lsofdc.min)
|| (stdc.unit != lsofdc.unit))
break;
(void) snprintf(buf, sizeof(buf) - 1, "%llu",
(unsigned long long)sb.st_ino);
buf[sizeof(buf) - 1] = '\0';
if (strcmp(inop->v, buf))
break;
ff = 1;
switch (tt) {
case TST_OFFT:
case TST_SZ:
(void) snprintf(buf, sizeof(buf) - 1,
(tt == TST_SZ) ? "%llu" : "0t%llu",
(unsigned long long)sz);
buf[sizeof(buf) - 1] = '\0';
tfop = (tt == TST_SZ) ? szp : offp;
if (!tfop || strcmp(tfop->v, buf)) {
(void) snprintf(buf1, sizeof(buf1) - 1,
"%s mismatch: expected %s, got %s",
(tt == TST_SZ) ? "size" : "0t offset",
buf,
tfop ? tfop->v : "nothing");
buf1[sizeof(buf1) - 1] = '\0';
(void) PrtMsg(buf1, Pn);
xv = 0;
} else
xv = 1;
break;
case TST_OFFX:
(void) snprintf(buf, sizeof(buf) - 1, "0x%llx",
(unsigned long long)sz);
buf[sizeof(buf) - 1] = '\0';
if (!offp || strcmp(offp->v, buf)) {
(void) snprintf(buf1, sizeof(buf1) - 1,
"0x offset mismatch: expected %s, got %s",
buf,
offp ? offp->v : "nothing");
buf1[sizeof(buf1) - 1] = '\0';
(void) PrtMsg(buf1, Pn);
xv = 0;
} else
xv = 1;
}
break;
}
}
(void) StopLsof();
if (em) {
(void) PrtMsg(em, Pn);
xv = 0;
}
if (!ff) {
(void) snprintf(buf, sizeof(buf) - 1, "%s not found by lsof", Path);
buf[sizeof(buf) - 1] = '\0';
PrtMsg(buf, Pn);
xv = 0;
}
return(xv);
}
#endif