#ifndef lint
static char copyright[] =
"@(#) Copyright 2002 Purdue Research Foundation.\nAll rights reserved.\n";
#endif
#include "LsofTest.h"
#include "lsof_fields.h"
#include <sys/socket.h>
#include <sys/un.h>
#if !defined(MAXPATHLEN)
#define MAXPATHLEN 1024
#endif
pid_t MyPid = (pid_t)0;
char *Pn = (char *)NULL;
int SpFd[2] = {-1,-1};
char *Path[2] = {(char *)NULL, (char *)NULL};
_PROTOTYPE(static void cleanup,(void));
_PROTOTYPE(static char *FindUsocks,(void));
int
main(argc, argv)
int argc;
char *argv[];
{
char buf[2048];
char cwd[MAXPATHLEN + 1];
char *em;
int ti, tj;
struct sockaddr_un ua;
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, "h", Pn))
xv = 1;
if (xv || LTopt_h) {
(void) PrtMsg("usage: [-h]", Pn);
PrtMsgX(" -h print help (this panel)", Pn, cleanup, xv);
}
if ((em = IsLsofExec()))
(void) PrtMsgX(em, Pn, cleanup, 1);
if ((em = CanRdKmem()))
(void) PrtMsgX(em, Pn, cleanup, 1);
#if defined(USE_GETCWD)
if (!getcwd(cwd, sizeof(cwd)))
#else
if (!getwd(cwd))
#endif
{
em = "ERROR!!! can't get CWD";
goto print_errno;
}
cwd[sizeof(cwd) - 1] = '\0';
if ((strlen(cwd) + strlen("/config.LT#U9223372036854775807") + 1)
> sizeof(ua.sun_path))
{
strncpy(cwd, "/tmp", sizeof(cwd) - 1);
}
for (ti = 0; ti < 2; ti++) {
(void) snprintf(buf, sizeof(buf) - 1, "%s/config.LT%dU%ld", cwd, ti,
(long)MyPid);
buf[sizeof(buf) - 1] = '\0';
Path[ti] = MkStrCpy(buf, &tj);
(void) unlink(Path[ti]);
}
for (ti = 0; ti < 2; ti++) {
if ((SpFd[ti] = socket(AF_UNIX, SOCK_STREAM, PF_UNSPEC)) < 0) {
em = "socket";
print_errno_by_ti:
(void) snprintf(buf, sizeof(buf) - 1, "ERROR!!! %s(%s) failure",
em, Path[ti]);
buf[sizeof(buf) - 1] = '\0';
em = buf;
print_errno:
PrtMsg(em, Pn);
(void) snprintf(buf, sizeof(buf) - 1, " Errno %d: %s", errno,
strerror(errno));
buf[sizeof(buf) - 1] = '\0';
PrtMsgX(buf, Pn, cleanup, 1);
}
}
for (ti = 0; ti < 2; ti++) {
(void) memset((void *)&ua, 0, sizeof(ua));
ua.sun_family = AF_UNIX;
(void) strncpy(ua.sun_path, Path[ti], sizeof(ua.sun_path));
ua.sun_path[sizeof(ua.sun_path) - 1] = '\0';
if (bind(SpFd[ti], (struct sockaddr *)&ua, sizeof(ua)) < 0) {
em = "bind";
goto print_errno_by_ti;
}
}
if ((em = FindUsocks()))
(void) PrtMsgX(em, Pn, cleanup, 1);
(void) PrtMsgX("OK", Pn, cleanup, 0);
return(0);
}
static void
cleanup()
{
int ti;
for (ti = 0; ti < 2; ti++) {
if (SpFd[ti] >= 0) {
(void) close(SpFd[ti]);
SpFd[ti] = -1;
}
if (Path[ti]) {
(void) unlink(Path[ti]);
(void) free((void *)Path[ti]);
Path[ti] = (char *)NULL;
}
}
}
static char *
FindUsocks()
{
char buf[2048];
char *cem;
LTfldo_t *cmdp;
int ff[2];
LTfldo_t *fop;
int nf;
int nl;
LTfldo_t *nmp;
char *opv[5];
char *pem = (char *)NULL;
pid_t pid;
int pids = 0;
char *tcp;
int ti, tj;
LTfldo_t *typ;
ff[0] = ff[1] = ti = 0;
opv[ti++] = "-aU";
opv[ti++] = "-p";
(void) snprintf(buf, sizeof(buf) - 1, "%ld", (long)MyPid);
buf[sizeof(buf) - 1] = '\0';
opv[ti++] = MkStrCpy(buf, &tj);
#if defined(USE_LSOF_C_OPT)
opv[ti++] = "-C";
#endif
opv[ti] = (char *)NULL;
if ((cem = ExecLsof(opv)))
return(cem);
while (((ff[0] + ff[1]) < 2) && (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';
}
for (tj = 0; tj < 2; tj++) {
if (ff[tj])
continue;
if (SpFd[tj] == ti)
break;
}
if (tj >= 2)
break;
nmp = typ = (LTfldo_t *)NULL;
for (fop++, ti = 1; ti < nf; fop++, ti++) {
switch (fop->ft) {
case LSOF_FID_NAME:
nmp = fop;
break;
case LSOF_FID_TYPE:
typ = fop;
break;
}
}
if (!typ || strcasecmp(typ->v, "unix"))
break;
if (!nmp)
break;
nl = strlen(Path[tj]);
for (tcp = nmp->v; tcp; tcp = strchr(tcp + 1, '/')) {
if (!strncmp(tcp, Path[tj], nl)) {
ff[tj] = 1;
break;
}
}
}
}
(void) StopLsof();
for (ti = 0; ti < 2; ti++) {
if (ff[tj])
continue;
(void) snprintf(buf, sizeof(buf) - 1, "ERROR!!! not found: %s",
Path[ti]);
buf[sizeof(buf) - 1] = '\0';
if (pem)
(void) PrtMsg(pem, Pn);
pem = MkStrCpy(buf, &tj);
}
return(pem);
}