#ifndef lint
static char copyright[] =
"@(#) Copyright 2002 Purdue Research Foundation.\nAll rights reserved.\n";
#endif
#include "LsofTest.h"
#include "lsof_fields.h"
#if defined(LT_DIAL_aix)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_bsdi)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_darwin)
#endif
#if defined(LT_DIAL_du)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_freebsd)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_linux)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_netbsd)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_openbsd)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_hpux)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_ns)
#define USE_FLOCK
#endif
#if defined(LT_DIAL_osr)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_ou)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_openbsd)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_solaris)
#define USE_FCNTL
#endif
#if defined(LT_DIAL_uw)
#define USE_FCNTL
#endif
#if !defined(USE_FLOCK) && !defined(USE_FCNTL)
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
#define FULL_EX_LOCK 0
#define FULL_SH_LOCK 1
#define PART_EX_LOCK 2
#define PART_SH_LOCK 3
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 *lkfile,(int ty));
_PROTOTYPE(static char *tstwlsof,(char *opt, char *xlk));
_PROTOTYPE(static char *unlkfile,(int ty));
int
main(argc, argv)
int argc;
char *argv[];
{
char buf[2048];
char *em;
int ti;
char *tcp;
int tlen;
char *tstR = (char *)NULL;
char *tstr = (char *)NULL;
char *tstW = (char *)NULL;
char *tstw = (char *)NULL;
int xv = 0;
if ((Pn = strrchr(argv[0], '/')))
Pn++;
else
Pn = argv[0];
MyPid = getpid();
(void) printf("%s ... ", Pn);
(void) fflush(stdout);
(void) PrtMsg((char *)NULL, Pn);
if (ScanArg(argc, argv, "hp:", Pn))
xv = 1;
if (xv || LTopt_h) {
(void) PrtMsg ("usage: [-h] [-p path]", Pn);
(void) PrtMsg (" -h print help (this panel)", Pn);
(void) 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), "./config.LTlock%ld",
(long)MyPid);
buf[sizeof(buf) - 1] = '\0';
Path = MkStrCpy(buf, &tlen);
}
for (ti = 0; ti < sizeof(buf); ti++) {
buf[ti] = (char)(ti & 0xff);
}
(void) unlink(Path);
if ((Fd = open(Path, O_RDWR|O_CREAT, 0600)) < 0) {
(void) fprintf(stderr, "ERROR!!! can't open %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);
}
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_file_error;
}
if (fsync(Fd)) {
(void) fprintf(stderr, "ERROR!!! can't fsync %s\n", Path);
goto print_file_error;
}
if (!tstwlsof("-wNa", " ")) {
(void) printf("ERROR!!! %s is NFS-mounted.\n", Path);
MsgStat = 1;
(void) PrtMsg ("Lsof can't report lock information on files that", Pn);
(void) PrtMsg ("are located on file systems mounted from a remote", Pn);
(void) PrtMsg ("NFS server.\n", Pn);
(void) PrtMsg ("Hint: try using \"-p path\" to supply a path in a", Pn);
(void) PrtMsg ("non-NFS file system.\n", Pn);
(void) PrtMsgX("See 00FAQ and 00TEST for more information.", Pn,
cleanup, 1);
}
if ((em = lkfile(FULL_EX_LOCK)))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((tstW = tstwlsof("-w", "W")))
(void) PrtMsg(tstW, Pn);
if ((em = unlkfile(FULL_EX_LOCK)))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((em = lkfile(FULL_SH_LOCK)))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((tstR = tstwlsof("-w", "R")))
(void) PrtMsg(tstR, Pn);
# if defined(USE_FLOCK)
tstr = tstw = (char *)NULL;
# endif
# if defined(USE_FCNTL)
if ((em = unlkfile(FULL_SH_LOCK)))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((em = lkfile(PART_EX_LOCK)))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((tstw = tstwlsof("-w", "w")))
(void) PrtMsg(tstw, Pn);
if ((em = unlkfile(PART_EX_LOCK)))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((em = lkfile(PART_SH_LOCK)))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((tstr = tstwlsof("-w", "r")))
(void) PrtMsg(tstr, Pn);
# endif
if (tstr || tstR || tstw || tstW) {
tcp = (char *)NULL;
xv = 1;
} else {
tcp = "OK";
xv = 0;
}
(void) PrtMsgX(tcp, Pn, cleanup, xv);
return(0);
}
static void
cleanup()
{
if (Fd >= 0) {
(void) close(Fd);
Fd = -1;
if (Path) {
(void) unlink(Path);
Path = (char *)NULL;
}
}
}
static char *
lkfile(ty)
int ty;
{
char buf[2048];
int ti;
# if defined(USE_FLOCK)
int flf;
# endif
# if defined(USE_FCNTL)
struct flock fl;
(void) memset((void *)&fl, 0, sizeof(fl));
switch(ty) {
case FULL_EX_LOCK:
fl.l_type = F_WRLCK;
break;
case FULL_SH_LOCK:
fl.l_type = F_RDLCK;
break;
case PART_EX_LOCK:
fl.l_type = F_WRLCK;
fl.l_len = (off_t)1;
break;
case PART_SH_LOCK:
fl.l_type = F_RDLCK;
fl.l_len = (off_t)1;
break;
default:
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! unknown lock type: %d", ty);
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
}
if (fcntl(Fd, F_SETLK, &fl) != -1)
return((char *)NULL);
(void) snprintf(buf, sizeof(buf) - 1, "ERROR!!! fcntl() lock error: %s",
strerror(errno));
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
# endif
# if defined(USE_FLOCK)
switch(ty) {
case FULL_EX_LOCK:
flf = LOCK_EX;
break;
case FULL_SH_LOCK:
flf = LOCK_SH;
break;
case PART_EX_LOCK:
case PART_SH_LOCK:
return("ERROR!!! flock() doesn't support partial locks");
break;
default:
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! unknown flock() type: %d", ty);
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
}
if (!flock(Fd, flf))
return((char *)NULL);
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! flock() %s lock failed: %s",
(flf == LOCK_EX) ? "exclusive" : "shared",
strerror(errno));
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
# endif
}
static char *
tstwlsof(opt, xlk)
char *opt;
char *xlk;
{
char buf[2048];
LTfldo_t *cmdp;
LTfldo_t *devp;
char *cem;
int ff = 0;
LTfldo_t *fop;
LTfldo_t *inop;
LTfldo_t *lkp;
LTdev_t lsofdc;
int nf;
LTfldo_t *nmp;
char *opv[4];
char *pem = (char *)NULL;
pid_t pid;
int pids = 0;
struct stat sb;
LTdev_t stdc;
char *tcp;
int ti;
LTfldo_t *typ;
if (!xlk || !*xlk)
(void) PrtMsgX("ERROR!!! no expected lock value", Pn, cleanup, 1);
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';
(void) PrtMsgX(buf, Pn, cleanup, 1);
}
if ((cem = ConvStatDev(&sb.st_dev, &stdc)))
(void) PrtMsgX(cem, Pn, cleanup, 1);
ti = 0;
if (opt && *opt)
opv[ti++] = opt;
#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 = lkp = nmp = (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_LOCK:
lkp = fop;
break;
case LSOF_FID_NAME:
nmp = 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, "%u",
(unsigned int)sb.st_ino);
buf[sizeof(buf) - 1] = '\0';
if (strcmp(inop->v, buf))
break;
ff = 1;
if (!lkp || strcmp(lkp->v, xlk)) {
if (pem)
(void) PrtMsg(pem, Pn);
(void) snprintf(buf, sizeof(buf) - 1,
"lock mismatch: expected %s, got \"%s\"", xlk,
lkp ? lkp->v : "(none)");
pem = MkStrCpy(buf, &ti);
}
break;
}
}
(void) StopLsof();
if (!ff) {
if (pem)
(void) PrtMsg(pem, Pn);
(void) snprintf(buf, sizeof(buf) - 1,
"lock test file %s not found by lsof", Path);
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
}
return(pem);
}
static char *
unlkfile(ty)
int ty;
{
char buf[2048];
int ti;
# if defined(USE_FCNTL)
struct flock fl;
(void) memset((void *)&fl, 0, sizeof(fl));
switch(ty) {
case FULL_EX_LOCK:
case FULL_SH_LOCK:
break;
case PART_EX_LOCK:
case PART_SH_LOCK:
fl.l_len = (off_t)1;
break;
default:
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! unknown unlock type: %d", ty);
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
}
fl.l_type = F_UNLCK;
if (fcntl(Fd, F_SETLK, &fl) != -1)
return((char *)NULL);
(void) snprintf(buf, sizeof(buf) - 1, "ERROR!!! fcntl() unlock error: %s",
strerror(errno));
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
# endif
# if defined(USE_FLOCK)
switch(ty) {
case FULL_EX_LOCK:
case FULL_SH_LOCK:
break;
default:
(void) snprintf(buf, sizeof(buf) - 1,
"ERROR!!! unknown unlock type: %s", ty);
buf[sizeof(buf) - 1] = '\0';
return(MkStrCpy(buf, &ti));
}
if (!flock(Fd, LOCK_UN))
return((char *)NULL);
(void) snprintf(buf, sizeof(buf) - 1, "ERROR!!! flock() unlock error: %s",
strerror(errno));
return(MkStrCpy(buf, &ti));
# endif
}
#endif