Blame proc.c

Packit Service 603f59
/*
Packit Service 603f59
 * proc.c - common process and file structure functions for lsof
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana
Packit Service 603f59
 * 47907.  All rights reserved.
Packit Service 603f59
 *
Packit Service 603f59
 * Written by Victor A. Abell
Packit Service 603f59
 *
Packit Service 603f59
 * This software is not subject to any license of the American Telephone
Packit Service 603f59
 * and Telegraph Company or the Regents of the University of California.
Packit Service 603f59
 *
Packit Service 603f59
 * Permission is granted to anyone to use this software for any purpose on
Packit Service 603f59
 * any computer system, and to alter it and redistribute it freely, subject
Packit Service 603f59
 * to the following restrictions:
Packit Service 603f59
 *
Packit Service 603f59
 * 1. Neither the authors nor Purdue University are responsible for any
Packit Service 603f59
 *    consequences of the use of this software.
Packit Service 603f59
 *
Packit Service 603f59
 * 2. The origin of this software must not be misrepresented, either by
Packit Service 603f59
 *    explicit claim or by omission.  Credit to the authors and Purdue
Packit Service 603f59
 *    University must appear in documentation and sources.
Packit Service 603f59
 *
Packit Service 603f59
 * 3. Altered versions must be plainly marked as such, and must not be
Packit Service 603f59
 *    misrepresented as being the original software.
Packit Service 603f59
 *
Packit Service 603f59
 * 4. This notice may not be removed or altered.
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
#ifndef lint
Packit Service 603f59
static char copyright[] =
Packit Service 603f59
"@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n";
Packit Service 603f59
static char *rcsid = "$Id: proc.c,v 1.50 2018/02/14 14:20:14 abe Exp $";
Packit Service 603f59
#endif
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
#include "lsof.h"
Packit Service 603f59
Packit Service 603f59
#if	defined(HASPTYEPT)
Packit Service 603f59
_PROTOTYPE(static void prt_ptyinfo,(pxinfo_t *pp, int prt_edev, int ps));
Packit Service 603f59
#endif	/* defined(HASPTYEPT) */
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * add_nma() - add to NAME column addition
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
void
Packit Service 603f59
add_nma(cp, len)
Packit Service 603f59
	char *cp;			/* string to add */
Packit Service 603f59
	int len;			/* string length */
Packit Service 603f59
{
Packit Service 603f59
	int nl;
Packit Service 603f59
Packit Service 603f59
	if (!cp || !len)
Packit Service 603f59
	    return;
Packit Service 603f59
	if (Lf->nma) {
Packit Service 603f59
	    nl = (int) strlen(Lf->nma);
Packit Service 603f59
	    Lf->nma = (char *) realloc((MALLOC_P *)Lf->nma,
Packit Service 603f59
				       (MALLOC_S)(len + nl + 2));
Packit Service 603f59
	} else {
Packit Service 603f59
	    nl = 0;
Packit Service 603f59
	    Lf->nma = (char *) malloc((MALLOC_S)(len + 1));
Packit Service 603f59
	}
Packit Service 603f59
	if (!Lf->nma) {
Packit Service 603f59
	    (void) fprintf(stderr, "%s: no name addition space: PID %ld, FD %s",
Packit Service 603f59
		Pn, (long)Lp->pid, Lf->fd);
Packit Service 603f59
	    Exit(1);
Packit Service 603f59
	}
Packit Service 603f59
	if (nl) {
Packit Service 603f59
	    Lf->nma[nl] = ' ';
Packit Service 603f59
	    (void) strncpy(&Lf->nma[nl + 1], cp, len);
Packit Service 603f59
	    Lf->nma[nl + 1 + len] = '\0';
Packit Service 603f59
	} else {
Packit Service 603f59
	    (void) strncpy(Lf->nma, cp, len);
Packit Service 603f59
	    Lf->nma[len] = '\0';
Packit Service 603f59
	}
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
#if	defined(HASFSTRUCT)
Packit Service 603f59
_PROTOTYPE(static char *alloc_fflbuf,(char **bp, int *al, int lr));
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * alloc_fflbuf() - allocate file flags print buffer
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
static char *
Packit Service 603f59
alloc_fflbuf(bp, al, lr)
Packit Service 603f59
	char **bp;			/* current buffer pointer */
Packit Service 603f59
	int *al;			/* current allocated length */
Packit Service 603f59
	int lr;				/* length required */
Packit Service 603f59
{
Packit Service 603f59
	int sz;
Packit Service 603f59
Packit Service 603f59
	sz = (int)(lr + 1);		/* allocate '\0' space */
Packit Service 603f59
	if (*bp && (sz <= *al))
Packit Service 603f59
	    return(*bp);
Packit Service 603f59
	if (*bp)
Packit Service 603f59
	    *bp = (char *)realloc((MALLOC_P *)*bp, (MALLOC_S)sz);
Packit Service 603f59
	else
Packit Service 603f59
	    *bp = (char *)malloc((MALLOC_S)sz);
Packit Service 603f59
	if (!*bp) {
Packit Service 603f59
	    (void) fprintf(stderr, "%s: no space (%d) for print flags\n",
Packit Service 603f59
		Pn, sz);
Packit Service 603f59
	    Exit(1);
Packit Service 603f59
	}
Packit Service 603f59
	*al = sz;
Packit Service 603f59
	return(*bp);
Packit Service 603f59
}
Packit Service 603f59
#endif	/* defined(HASFSTRUCT) */
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * alloc_lfile() - allocate local file structure space
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
void
Packit Service 603f59
alloc_lfile(nm, num)
Packit Service 603f59
	char *nm;			/* file descriptor name (may be NULL) */
Packit Service 603f59
	int num;			/* file descriptor number -- -1 if
Packit Service 603f59
					 * none */
Packit Service 603f59
{
Packit Service 603f59
	int fds;
Packit Service 603f59
Packit Service 603f59
	if (Lf) {
Packit Service 603f59
/*
Packit Service 603f59
 * If reusing a previously allocated structure, release any allocated
Packit Service 603f59
 * space it was using.
Packit Service 603f59
 */
Packit Service 603f59
	    if (Lf->dev_ch)
Packit Service 603f59
		(void) free((FREE_P *)Lf->dev_ch);
Packit Service 603f59
	    if (Lf->nm)
Packit Service 603f59
		(void) free((FREE_P *)Lf->nm);
Packit Service 603f59
	    if (Lf->nma)
Packit Service 603f59
		(void) free((FREE_P *)Lf->nma);
Packit Service 603f59
Packit Service 603f59
#if	defined(HASLFILEADD) && defined(CLRLFILEADD)
Packit Service 603f59
	    CLRLFILEADD(Lf)
Packit Service 603f59
#endif	/* defined(HASLFILEADD) && defined(CLRLFILEADD) */
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * Othwerise, allocate a new structure.
Packit Service 603f59
 */
Packit Service 603f59
	} else if (!(Lf = (struct lfile *)malloc(sizeof(struct lfile)))) {
Packit Service 603f59
	    (void) fprintf(stderr, "%s: no local file space at PID %d\n",
Packit Service 603f59
		Pn, Lp->pid);
Packit Service 603f59
	    Exit(1);
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * Initialize the structure.
Packit Service 603f59
 */
Packit Service 603f59
	Lf->access = Lf->lock = ' ';
Packit Service 603f59
	Lf->dev_def = Lf->inp_ty = Lf->is_com = Lf->is_nfs = Lf->is_stream
Packit Service 603f59
		    = Lf->lmi_srch = Lf->nlink_def = Lf->off_def = Lf->sz_def
Packit Service 603f59
		    = Lf->rdev_def
Packit Service 603f59
		    = (unsigned char)0;
Packit Service 603f59
	Lf->li[0].af = Lf->li[1].af = 0;
Packit Service 603f59
	Lf->lts.type = -1;
Packit Service 603f59
	Lf->nlink = 0l;
Packit Service 603f59
Packit Service 603f59
#if	defined(HASMNTSTAT)
Packit Service 603f59
	Lf->mnt_stat = (unsigned char)0;
Packit Service 603f59
#endif	/* defined(HASMNTSTAT) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASEPTOPTS)
Packit Service 603f59
	Lf->chend = 0;
Packit Service 603f59
#endif	/* defined(HASEPTOPTS) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASSOOPT)
Packit Service 603f59
	Lf->lts.kai = Lf->lts.ltm = 0;
Packit Service 603f59
	Lf->lts.opt = Lf->lts.qlen = Lf->lts.qlim = Lf->lts.pqlen
Packit Service 603f59
		    = (unsigned int)0;
Packit Service 603f59
	Lf->lts.rbsz = Lf->lts.sbsz = (unsigned long)0;
Packit Service 603f59
	Lf->lts.qlens = Lf->lts.qlims = Lf->lts.pqlens = Lf->lts.rbszs
Packit Service 603f59
		      = Lf->lts.sbszs = (unsigned char)0;
Packit Service 603f59
#endif	/* defined(HASSOOPT) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASSOSTATE)
Packit Service 603f59
	Lf->lts.ss = 0;
Packit Service 603f59
#endif	/* defined(HASSOSTATE) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTCPOPT)
Packit Service 603f59
	Lf->lts.mss = (unsigned long)0;
Packit Service 603f59
	Lf->lts.msss = (unsigned char)0;
Packit Service 603f59
	Lf->lts.topt = (unsigned int)0;
Packit Service 603f59
#endif	/* defined(HASTCPOPT) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTCPTPIQ)
Packit Service 603f59
	Lf->lts.rqs = Lf->lts.sqs = (unsigned char)0;
Packit Service 603f59
#endif	/* defined(HASTCPTPIQ) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTCPTPIW)
Packit Service 603f59
	Lf->lts.rws = Lf->lts.wws = (unsigned char)0;
Packit Service 603f59
#endif	/* defined(HASTCPTPIW) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASFSINO)
Packit Service 603f59
	Lf->fs_ino = 0;
Packit Service 603f59
#endif	/* defined(HASFSINO) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASVXFS) && defined(HASVXFSDNLC)
Packit Service 603f59
	Lf->is_vxfs = 0;
Packit Service 603f59
#endif	/* defined(HASVXFS) && defined(HASVXFSDNLC) */
Packit Service 603f59
Packit Service 603f59
	Lf->inode = (INODETYPE)0;
Packit Service 603f59
	Lf->off = (SZOFFTYPE)0;
Packit Service 603f59
	if (Lp->pss & PS_PRI)
Packit Service 603f59
	    Lf->sf = Lp->sf;
Packit Service 603f59
	else
Packit Service 603f59
	    Lf->sf = 0;
Packit Service 603f59
	Lf->iproto[0] = Lf->type[0] = '\0';
Packit Service 603f59
	if (nm) {
Packit Service 603f59
	    (void) strncpy(Lf->fd, nm, FDLEN - 1);
Packit Service 603f59
	    Lf->fd[FDLEN - 1] = '\0';
Packit Service 603f59
	} else if (num >= 0) {
Packit Service 603f59
	    if (num < 10000)
Packit Service 603f59
		(void) snpf(Lf->fd, sizeof(Lf->fd), "%4d", num);
Packit Service 603f59
	    else
Packit Service 603f59
		(void) snpf(Lf->fd, sizeof(Lf->fd), "*%03d", num % 1000);
Packit Service 603f59
	} else
Packit Service 603f59
	    Lf->fd[0] = '\0';
Packit Service 603f59
	Lf->dev_ch = Lf->fsdir = Lf->fsdev = Lf->nm = Lf->nma = (char *)NULL;
Packit Service 603f59
	Lf->ch = -1;
Packit Service 603f59
Packit Service 603f59
#if	defined(HASNCACHE) && HASNCACHE<2
Packit Service 603f59
	Lf->na = (KA_T)NULL;
Packit Service 603f59
#endif	/* defined(HASNCACHE) && HASNCACHE<2 */
Packit Service 603f59
Packit Service 603f59
	Lf->next = (struct lfile *)NULL;
Packit Service 603f59
	Lf->ntype = Ntype = N_REGLR;
Packit Service 603f59
	Namech[0] = '\0';
Packit Service 603f59
Packit Service 603f59
#if	defined(HASFSTRUCT)
Packit Service 603f59
	Lf->fct = Lf->ffg = Lf->pof = (long)0;
Packit Service 603f59
	Lf->fna = (KA_T)NULL;
Packit Service 603f59
	Lf->fsv = (unsigned char)0;
Packit Service 603f59
#endif	/* defined(HASFSTRUCT) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASLFILEADD) && defined(SETLFILEADD)
Packit Service 603f59
/*
Packit Service 603f59
 * Do local initializations.
Packit Service 603f59
 */
Packit Service 603f59
	SETLFILEADD
Packit Service 603f59
#endif	/* defined(HASLFILEADD) && defined(SETLFILEADD) */
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * See if the file descriptor has been selected.
Packit Service 603f59
 */
Packit Service 603f59
	if (!Fdl || (!nm && num < 0))
Packit Service 603f59
	    return;
Packit Service 603f59
	fds = ck_fd_status(nm, num);
Packit Service 603f59
	switch (FdlTy) {
Packit Service 603f59
	case 0:			/* inclusion list */
Packit Service 603f59
	    if (fds == 2)
Packit Service 603f59
		Lf->sf |= SELFD;
Packit Service 603f59
	    break;
Packit Service 603f59
	case 1:			/* exclusion list */
Packit Service 603f59
	    if (fds != 1)
Packit Service 603f59
		Lf->sf |= SELFD;
Packit Service 603f59
	}
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * alloc_lproc() - allocate local proc structure space
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
void
Packit Service 603f59
alloc_lproc(pid, pgid, ppid, uid, cmd, pss, sf)
Packit Service 603f59
	int pid;			/* Process ID */
Packit Service 603f59
	int pgid;			/* process group ID */
Packit Service 603f59
	int ppid;			/* parent process ID */
Packit Service 603f59
	UID_ARG uid;			/* User ID */
Packit Service 603f59
	char *cmd;			/* command */
Packit Service 603f59
	int pss;			/* process select state */
Packit Service 603f59
	int sf;				/* process select flags */
Packit Service 603f59
{
Packit Service 603f59
	static int sz = 0;
Packit Service 603f59
Packit Service 603f59
	if (!Lproc) {
Packit Service 603f59
	    if (!(Lproc = (struct lproc *)malloc(
Packit Service 603f59
			  (MALLOC_S)(LPROCINCR * sizeof(struct lproc)))))
Packit Service 603f59
	    {
Packit Service 603f59
		(void) fprintf(stderr,
Packit Service 603f59
		    "%s: no malloc space for %d local proc structures\n",
Packit Service 603f59
		    Pn, LPROCINCR);
Packit Service 603f59
		Exit(1);
Packit Service 603f59
	    }
Packit Service 603f59
	    sz = LPROCINCR;
Packit Service 603f59
	} else if ((Nlproc + 1) > sz) {
Packit Service 603f59
	    sz += LPROCINCR;
Packit Service 603f59
	    if (!(Lproc = (struct lproc *)realloc((MALLOC_P *)Lproc,
Packit Service 603f59
			  (MALLOC_S)(sz * sizeof(struct lproc)))))
Packit Service 603f59
	    {
Packit Service 603f59
		(void) fprintf(stderr,
Packit Service 603f59
		    "%s: no realloc space for %d local proc structures\n",
Packit Service 603f59
		    Pn, sz);
Packit Service 603f59
		Exit(1);
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
	Lp = &Lproc[Nlproc++];
Packit Service 603f59
	Lp->pid = pid;
Packit Service 603f59
Packit Service 603f59
#if	defined(HASEPTOPTS)
Packit Service 603f59
	Lp->ept = 0;
Packit Service 603f59
#endif	/* defined(HASEPTOPTS) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTASKS)
Packit Service 603f59
	Lp->tid = 0;
Packit Service 603f59
	Lp->tcmd = (char *)NULL;
Packit Service 603f59
#endif	/* defined(HASTASKS) */
Packit Service 603f59
Packit Service 603f59
	Lp->pgid = pgid;
Packit Service 603f59
	Lp->ppid = ppid;
Packit Service 603f59
	Lp->file = (struct lfile *)NULL;
Packit Service 603f59
	Lp->sf = (short)sf;
Packit Service 603f59
	Lp->pss = (short)pss;
Packit Service 603f59
	Lp->uid = (uid_t)uid;
Packit Service 603f59
/*
Packit Service 603f59
 * Allocate space for the full command name and copy it there.
Packit Service 603f59
 */
Packit Service 603f59
	if (!(Lp->cmd = mkstrcpy(cmd, (MALLOC_S *)NULL))) {
Packit Service 603f59
	    (void) fprintf(stderr, "%s: PID %d, no space for command name: ",
Packit Service 603f59
		Pn, pid);
Packit Service 603f59
	    safestrprt(cmd, stderr, 1);
Packit Service 603f59
	    Exit(1);
Packit Service 603f59
	}
Packit Service 603f59
Packit Service 603f59
#if	defined(HASZONES)
Packit Service 603f59
/*
Packit Service 603f59
 * Clear the zone name pointer.  The dialect's own code will set it.
Packit Service 603f59
 */
Packit Service 603f59
	Lp->zn = (char *)NULL;
Packit Service 603f59
#endif	/* defined(HASZONES) */
Packit Service 603f59
 
Packit Service 603f59
#if	defined(HASSELINUX)
Packit Service 603f59
/*
Packit Service 603f59
 * Clear the security context pointer.  The dialect's own code will
Packit Service 603f59
 * set it.
Packit Service 603f59
 */
Packit Service 603f59
	Lp->cntx = (char *)NULL;
Packit Service 603f59
#endif	/* defined(HASSELINUX) */
Packit Service 603f59
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * ck_fd_status() - check FD status
Packit Service 603f59
 *
Packit Service 603f59
 * return: 0 == FD is neither included nor excluded
Packit Service 603f59
 *	   1 == FD is excluded
Packit Service 603f59
 *	   2 == FD is included
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
extern int
Packit Service 603f59
ck_fd_status(nm, num)
Packit Service 603f59
	char *nm;			/* file descriptor name (may be NULL) */
Packit Service 603f59
	int num;			/* file descriptor number -- -1 if
Packit Service 603f59
					 * none */
Packit Service 603f59
{
Packit Service 603f59
	char *cp;
Packit Service 603f59
	struct fd_lst *fp;
Packit Service 603f59
Packit Service 603f59
	if (!(fp = Fdl) || (!nm && num < 0))
Packit Service 603f59
	    return(0);
Packit Service 603f59
	if ((cp = nm)) {
Packit Service 603f59
	    while (*cp && *cp == ' ')
Packit Service 603f59
		cp++;
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * Check for an exclusion match.
Packit Service 603f59
 */
Packit Service 603f59
	if (FdlTy == 1) {
Packit Service 603f59
	    for (; fp; fp = fp->next) {
Packit Service 603f59
		if (cp) {
Packit Service 603f59
		    if (fp->nm && strcmp(fp->nm, cp) == 0)
Packit Service 603f59
			return(1);
Packit Service 603f59
		    continue;
Packit Service 603f59
		}
Packit Service 603f59
		if (num >= fp->lo && num <= fp->hi)
Packit Service 603f59
		    return(1);
Packit Service 603f59
	    }
Packit Service 603f59
	    return(0);
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * If Fdl isn't an exclusion list, check for an inclusion match.
Packit Service 603f59
 */
Packit Service 603f59
	for (; fp; fp = fp->next) {
Packit Service 603f59
	    if (cp) {
Packit Service 603f59
		if (fp->nm && strcmp(fp->nm, cp) == 0)
Packit Service 603f59
		    return(2);
Packit Service 603f59
		continue;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (num >= fp->lo && num <= fp->hi)
Packit Service 603f59
		return(2);
Packit Service 603f59
	}
Packit Service 603f59
	return(0);
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * comppid() - compare PIDs
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
int
Packit Service 603f59
comppid(a1, a2)
Packit Service 603f59
	COMP_P *a1, *a2;
Packit Service 603f59
{
Packit Service 603f59
	struct lproc **p1 = (struct lproc **)a1;
Packit Service 603f59
	struct lproc **p2 = (struct lproc **)a2;
Packit Service 603f59
Packit Service 603f59
	if ((*p1)->pid < (*p2)->pid)
Packit Service 603f59
	    return(-1);
Packit Service 603f59
	if ((*p1)->pid > (*p2)->pid)
Packit Service 603f59
	    return(1);
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTASKS)
Packit Service 603f59
	if ((*p1)->tid < (*p2)->tid)
Packit Service 603f59
	    return(-1);
Packit Service 603f59
	if ((*p1)->tid > (*p2)->tid)
Packit Service 603f59
	    return(1);
Packit Service 603f59
#endif	/* defined(HASTASKS) */
Packit Service 603f59
Packit Service 603f59
	return(0);
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * ent_inaddr() - enter Internet addresses
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
void
Packit Service 603f59
ent_inaddr(la, lp, fa, fp, af)
Packit Service 603f59
	unsigned char *la;		/* local Internet address */
Packit Service 603f59
	int lp;				/* local port */
Packit Service 603f59
	unsigned char *fa;		/* foreign Internet address -- may
Packit Service 603f59
					 * be NULL to indicate no foreign
Packit Service 603f59
					 * address is known */
Packit Service 603f59
	int fp;				/* foreign port */
Packit Service 603f59
	int af;				/* address family -- e.g, AF_INET,
Packit Service 603f59
					 * AF_INET */
Packit Service 603f59
{
Packit Service 603f59
	int m;
Packit Service 603f59
Packit Service 603f59
	if (la) {
Packit Service 603f59
	    Lf->li[0].af = af;
Packit Service 603f59
Packit Service 603f59
#if	defined(HASIPv6)
Packit Service 603f59
	    if (af == AF_INET6)
Packit Service 603f59
		Lf->li[0].ia.a6 = *(struct in6_addr *)la;
Packit Service 603f59
	    else
Packit Service 603f59
#endif	/* defined(HASIPv6) */
Packit Service 603f59
Packit Service 603f59
		Lf->li[0].ia.a4 = *(struct in_addr *)la;
Packit Service 603f59
	    Lf->li[0].p = lp;
Packit Service 603f59
	} else
Packit Service 603f59
	    Lf->li[0].af = 0;
Packit Service 603f59
	if (fa) {
Packit Service 603f59
	    Lf->li[1].af = af;
Packit Service 603f59
Packit Service 603f59
#if	defined(HASIPv6)
Packit Service 603f59
	    if (af == AF_INET6)
Packit Service 603f59
		Lf->li[1].ia.a6 = *(struct in6_addr *)fa;
Packit Service 603f59
	    else
Packit Service 603f59
#endif	/* defined(HASIPv6) */
Packit Service 603f59
Packit Service 603f59
		Lf->li[1].ia.a4 = *(struct in_addr *)fa;
Packit Service 603f59
	    Lf->li[1].p = fp;
Packit Service 603f59
	} else
Packit Service 603f59
	    Lf->li[1].af = 0;
Packit Service 603f59
/*
Packit Service 603f59
 * If network address matching has been selected, check both addresses.
Packit Service 603f59
 */
Packit Service 603f59
	if ((Selflags & SELNA) && Nwad) {
Packit Service 603f59
	    m = (fa && is_nw_addr(fa, fp, af)) ? 1 : 0;
Packit Service 603f59
	    m |= (la && is_nw_addr(la, lp, af)) ? 1 : 0;
Packit Service 603f59
	    if (m)
Packit Service 603f59
		Lf->sf |= SELNA;
Packit Service 603f59
	}
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * examine_lproc() - examine local process
Packit Service 603f59
 *
Packit Service 603f59
 * return: 1 = last process
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
int
Packit Service 603f59
examine_lproc()
Packit Service 603f59
{
Packit Service 603f59
	int sbp = 0;
Packit Service 603f59
Packit Service 603f59
	if (RptTm)
Packit Service 603f59
	    return(0);
Packit Service 603f59
/*
Packit Service 603f59
 * List the process if the process is selected and:
Packit Service 603f59
 *
Packit Service 603f59
 *	o  listing is limited to a single PID selection -- this one;
Packit Service 603f59
 *
Packit Service 603f59
 *	o  listing is selected by an ANDed option set (not all options)
Packit Service 603f59
 *	   that includes a single PID selection -- this one.
Packit Service 603f59
 */
Packit Service 603f59
	if ((Lp->sf & SELPID) && !AllProc) {
Packit Service 603f59
	    if ((Selflags == SELPID)
Packit Service 603f59
	    ||  (Fand && (Selflags & SELPID))) {
Packit Service 603f59
		sbp = 1;
Packit Service 603f59
		Npuns--;
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
	if (Lp->pss && Npid == 1 && sbp) {
Packit Service 603f59
	    print_init();
Packit Service 603f59
	    (void) print_proc();
Packit Service 603f59
	    PrPass++;
Packit Service 603f59
	    if (PrPass < 2)
Packit Service 603f59
		(void) print_proc();
Packit Service 603f59
	    Lp->pss = 0;
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * Deprecate an unselected (or listed) process.
Packit Service 603f59
 */
Packit Service 603f59
	if ( ! Lp->pss) {
Packit Service 603f59
	    (void) free_lproc(Lp);
Packit Service 603f59
	    Nlproc--;
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * Indicate last-process if listing is limited to PID selections,
Packit Service 603f59
 * and all selected processes have been listed.
Packit Service 603f59
 */
Packit Service 603f59
	return((sbp && Npuns == 0) ? 1 : 0);
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * free_lproc() - free lproc entry and its associated malloc'd space
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
void
Packit Service 603f59
free_lproc(lp)
Packit Service 603f59
	struct lproc *lp;
Packit Service 603f59
{
Packit Service 603f59
	struct lfile *lf, *nf;
Packit Service 603f59
Packit Service 603f59
	for (lf = lp->file; lf; lf = nf) {
Packit Service 603f59
	    if (lf->dev_ch) {
Packit Service 603f59
		(void) free((FREE_P *)lf->dev_ch);
Packit Service 603f59
		lf->dev_ch = (char *)NULL;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (lf->nm) {
Packit Service 603f59
		(void) free((FREE_P *)lf->nm);
Packit Service 603f59
		lf->nm = (char *)NULL;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (lf->nma) {
Packit Service 603f59
		(void) free((FREE_P *)lf->nma);
Packit Service 603f59
		lf->nma = (char *)NULL;
Packit Service 603f59
	    }
Packit Service 603f59
Packit Service 603f59
#if	defined(HASLFILEADD) && defined(CLRLFILEADD)
Packit Service 603f59
	    CLRLFILEADD(lf)
Packit Service 603f59
#endif	/* defined(HASLFILEADD) && defined(CLRLFILEADD) */
Packit Service 603f59
Packit Service 603f59
	    nf = lf->next;
Packit Service 603f59
	    (void) free((FREE_P *)lf);
Packit Service 603f59
	}
Packit Service 603f59
	lp->file = (struct lfile *)NULL;
Packit Service 603f59
	if (lp->cmd) {
Packit Service 603f59
	    (void) free((FREE_P *)lp->cmd);
Packit Service 603f59
	    lp->cmd = (char *)NULL;
Packit Service 603f59
	}
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTASKS)
Packit Service 603f59
	if (lp->tcmd) {
Packit Service 603f59
	    (void) free((FREE_P *)lp->tcmd);
Packit Service 603f59
	    lp->tcmd = (char *)NULL;
Packit Service 603f59
	}
Packit Service 603f59
#endif	/* defined(HASTASKS) */
Packit Service 603f59
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * is_cmd_excl() - is command excluded?
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
int
Packit Service 603f59
is_cmd_excl(cmd, pss, sf)
Packit Service 603f59
	char *cmd;			/* command name */
Packit Service 603f59
	short *pss;			/* process state */
Packit Service 603f59
	short *sf;			/* process select flags */
Packit Service 603f59
{
Packit Service 603f59
	int i;
Packit Service 603f59
	struct str_lst *sp;
Packit Service 603f59
/*
Packit Service 603f59
 * See if the command is excluded by a "-c^<command>" option.
Packit Service 603f59
 */
Packit Service 603f59
	if (Cmdl && Cmdnx) {
Packit Service 603f59
	    for (sp = Cmdl; sp; sp = sp->next) {
Packit Service 603f59
		if (sp->x && !strncmp(sp->str, cmd, sp->len))
Packit Service 603f59
		    return(1);
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * The command is not excluded if no command selection was requested,
Packit Service 603f59
 * or if its name matches any -c <command> specification.
Packit Service 603f59
 * 
Packit Service 603f59
 */
Packit Service 603f59
	if ((Selflags & SELCMD) == 0)
Packit Service 603f59
	    return(0);
Packit Service 603f59
	for (sp = Cmdl; sp; sp = sp->next) {
Packit Service 603f59
	    if (!sp->x && !strncmp(sp->str, cmd, sp->len)) {
Packit Service 603f59
		sp->f = 1;
Packit Service 603f59
		*pss |= PS_PRI;
Packit Service 603f59
		*sf |= SELCMD;
Packit Service 603f59
		return(0);
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * The command name doesn't match any -c <command> specification.  See if it
Packit Service 603f59
 * matches a -c /RE/[bix] specification.
Packit Service 603f59
 */
Packit Service 603f59
	for (i = 0; i < NCmdRxU; i++) {
Packit Service 603f59
	    if (!regexec(&CmdRx[i].cx, cmd, 0, NULL, 0)) {
Packit Service 603f59
		CmdRx[i].mc = 1;
Packit Service 603f59
		*pss |= PS_PRI;
Packit Service 603f59
		*sf |= SELCMD;
Packit Service 603f59
		return(0);
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * The command name matches no -c specification.
Packit Service 603f59
 *
Packit Service 603f59
 * It's excluded if the only selection condition is command name,
Packit Service 603f59
 * or if command name selection is part of an ANDed set.
Packit Service 603f59
 */
Packit Service 603f59
	if (Selflags == SELCMD)
Packit Service 603f59
	    return(1);
Packit Service 603f59
	return (Fand ? 1 : 0);
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * is_file_sel() - is file selected?
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
int
Packit Service 603f59
is_file_sel(lp, lf)
Packit Service 603f59
	struct lproc *lp;		/* lproc structure pointer */
Packit Service 603f59
	struct lfile *lf;		/* lfile structure pointer */
Packit Service 603f59
{
Packit Service 603f59
	if (!lf || !lf->sf)
Packit Service 603f59
	    return(0);
Packit Service 603f59
	if (Lf->sf & SELEXCLF)
Packit Service 603f59
	    return(0);
Packit Service 603f59
Packit Service 603f59
#if	defined(HASSECURITY) && defined(HASNOSOCKSECURITY)
Packit Service 603f59
	if (Myuid && (Myuid != lp->uid)) {
Packit Service 603f59
	    if (!(lf->sf & (SELNA | SELNET)))
Packit Service 603f59
		return(0);
Packit Service 603f59
	}
Packit Service 603f59
#endif	/* defined(HASSECURITY) && defined(HASNOSOCKSECURITY) */
Packit Service 603f59
Packit Service 603f59
	if (AllProc)
Packit Service 603f59
	    return(1);
Packit Service 603f59
	if (Fand && ((lf->sf & Selflags) != Selflags))
Packit Service 603f59
	    return(0);
Packit Service 603f59
	return(1);
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * is_proc_excl() - is process excluded?
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
int
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTASKS)
Packit Service 603f59
is_proc_excl(pid, pgid, uid, pss, sf, tid)
Packit Service 603f59
#else	/* !defined(HASTASKS) */
Packit Service 603f59
is_proc_excl(pid, pgid, uid, pss, sf)
Packit Service 603f59
#endif	/* defined(HASTASKS) */
Packit Service 603f59
Packit Service 603f59
	int pid;			/* Process ID */
Packit Service 603f59
	int pgid;			/* process group ID */
Packit Service 603f59
	UID_ARG uid;			/* User ID */
Packit Service 603f59
	short *pss;			/* process select state for lproc */
Packit Service 603f59
	short *sf;			/* select flags for lproc */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTASKS)
Packit Service 603f59
	int tid;			/* task ID (not a task if zero) */
Packit Service 603f59
#endif	/* defined(HASTASKS) */
Packit Service 603f59
Packit Service 603f59
{
Packit Service 603f59
	int i, j;
Packit Service 603f59
Packit Service 603f59
	*pss = *sf = 0;
Packit Service 603f59
Packit Service 603f59
#if	defined(HASSECURITY)
Packit Service 603f59
/*
Packit Service 603f59
 * The process is excluded by virtue of the security option if it
Packit Service 603f59
 * isn't owned by the owner of this lsof process, unless the
Packit Service 603f59
 * HASNOSOCKSECURITY option is also specified.  In that case the
Packit Service 603f59
 * selected socket files of any process may be listed.
Packit Service 603f59
 */
Packit Service 603f59
# if	!defined(HASNOSOCKSECURITY)
Packit Service 603f59
	if (Myuid && Myuid != (uid_t)uid)
Packit Service 603f59
	    return(1);
Packit Service 603f59
# endif	/* !defined(HASNOSOCKSECURITY) */
Packit Service 603f59
#endif	/* defined(HASSECURITY) */
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * If the excluding of process listing by UID has been specified, see if the
Packit Service 603f59
 * owner of this process is excluded.
Packit Service 603f59
 */
Packit Service 603f59
	if (Nuidexcl) {
Packit Service 603f59
	    for (i = j = 0; (i < Nuid) && (j < Nuidexcl); i++) {
Packit Service 603f59
		if (!Suid[i].excl)
Packit Service 603f59
		    continue;
Packit Service 603f59
		if (Suid[i].uid == (uid_t)uid)
Packit Service 603f59
		    return(1);
Packit Service 603f59
		j++;
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * If the excluding of process listing by PGID has been specified, see if this
Packit Service 603f59
 * PGID is excluded.
Packit Service 603f59
 */
Packit Service 603f59
	if (Npgidx) {
Packit Service 603f59
	    for (i = j = 0; (i < Npgid) && (j < Npgidx); i++) {
Packit Service 603f59
		if (!Spgid[i].x)
Packit Service 603f59
		    continue;
Packit Service 603f59
		if (Spgid[i].i == pgid)
Packit Service 603f59
		    return(1);
Packit Service 603f59
		j++;
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * If the excluding of process listing by PID has been specified, see if this
Packit Service 603f59
 * PID is excluded.
Packit Service 603f59
 */
Packit Service 603f59
	if (Npidx) {
Packit Service 603f59
	    for (i = j = 0; (i < Npid) && (j < Npidx); i++) {
Packit Service 603f59
		if (!Spid[i].x)
Packit Service 603f59
		    continue;
Packit Service 603f59
		if (Spid[i].i == pid)
Packit Service 603f59
		    return(1);
Packit Service 603f59
		j++;
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * If the listing of all processes is selected, then this one is not excluded.
Packit Service 603f59
 *
Packit Service 603f59
 * However, if HASSECURITY and HASNOSOCKSECURITY are both specified, exclude
Packit Service 603f59
 * network selections from the file flags, so that the tests in is_file_sel()
Packit Service 603f59
 * work as expected.
Packit Service 603f59
 */
Packit Service 603f59
	if (AllProc) {
Packit Service 603f59
	    *pss = PS_PRI;
Packit Service 603f59
Packit Service 603f59
#if	defined(HASSECURITY) && defined(HASNOSOCKSECURITY)
Packit Service 603f59
	    *sf = SelAll & ~(SELNA | SELNET);
Packit Service 603f59
#else	/* !defined(HASSECURITY) || !defined(HASNOSOCKSECURITY) */
Packit Service 603f59
	    *sf = SelAll;
Packit Service 603f59
#endif	/* defined(HASSECURITY) && defined(HASNOSOCKSECURITY) */
Packit Service 603f59
Packit Service 603f59
	    return(0);
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * If the listing of processes has been specified by process group ID, see
Packit Service 603f59
 * if this one is included or excluded.
Packit Service 603f59
 */
Packit Service 603f59
	if (Npgidi && (Selflags & SELPGID)) {
Packit Service 603f59
	    for (i = j = 0; (i < Npgid) && (j < Npgidi); i++) {
Packit Service 603f59
		if (Spgid[i].x)
Packit Service 603f59
		    continue;
Packit Service 603f59
		if (Spgid[i].i == pgid) {
Packit Service 603f59
		    Spgid[i].f = 1;
Packit Service 603f59
		    *pss = PS_PRI;
Packit Service 603f59
		    *sf = SELPGID;
Packit Service 603f59
		    if (Selflags == SELPGID)
Packit Service 603f59
			return(0);
Packit Service 603f59
		    break;
Packit Service 603f59
		}
Packit Service 603f59
		j++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if ((Selflags == SELPGID) && !*sf)
Packit Service 603f59
		return(1);
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * If the listing of processes has been specified by PID, see if this one is
Packit Service 603f59
 * included or excluded.
Packit Service 603f59
 */
Packit Service 603f59
	if (Npidi && (Selflags & SELPID)) {
Packit Service 603f59
	    for (i = j = 0; (i < Npid) && (j < Npidi); i++) {
Packit Service 603f59
		if (Spid[i].x)
Packit Service 603f59
		    continue;
Packit Service 603f59
		if (Spid[i].i == pid) {
Packit Service 603f59
		    Spid[i].f = 1;
Packit Service 603f59
		    *pss = PS_PRI;
Packit Service 603f59
		    *sf |= SELPID;
Packit Service 603f59
		    if (Selflags == SELPID)
Packit Service 603f59
			return(0);
Packit Service 603f59
		    break;
Packit Service 603f59
		}
Packit Service 603f59
		j++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if ((Selflags == SELPID) && !*sf)
Packit Service 603f59
		return(1);
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * If the listing of processes has been specified by UID, see if the owner of
Packit Service 603f59
 * this process has been included.
Packit Service 603f59
 */
Packit Service 603f59
	if (Nuidincl && (Selflags & SELUID)) {
Packit Service 603f59
	    for (i = j = 0; (i < Nuid) && (j < Nuidincl); i++) {
Packit Service 603f59
		if (Suid[i].excl)
Packit Service 603f59
		    continue;
Packit Service 603f59
		if (Suid[i].uid == (uid_t)uid) {
Packit Service 603f59
		    Suid[i].f = 1;
Packit Service 603f59
		    *pss = PS_PRI;
Packit Service 603f59
		    *sf |= SELUID;
Packit Service 603f59
		    if (Selflags == SELUID)
Packit Service 603f59
			return(0);
Packit Service 603f59
		    break;
Packit Service 603f59
		}
Packit Service 603f59
		j++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (Selflags == SELUID && (*sf & SELUID) == 0)
Packit Service 603f59
		return(1);
Packit Service 603f59
	}
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTASKS)
Packit Service 603f59
	if ((Selflags & SELTASK) && tid) {
Packit Service 603f59
Packit Service 603f59
	/*
Packit Service 603f59
	 * This is a task and tasks are selected.
Packit Service 603f59
	 */
Packit Service 603f59
	    *pss = PS_PRI;
Packit Service 603f59
	    *sf |= SELTASK;
Packit Service 603f59
	    if ((Selflags == SELTASK)
Packit Service 603f59
	    ||  (Fand && ((*sf & Selflags) == Selflags)))
Packit Service 603f59
		return(0);
Packit Service 603f59
	}
Packit Service 603f59
#endif	/* defined(HASTASKS) */
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * When neither the process group ID, nor the PID, nor the task, nor the UID
Packit Service 603f59
 * is selected:
Packit Service 603f59
 *
Packit Service 603f59
 *	If list option ANDing of process group IDs, PIDs, UIDs or tasks is
Packit Service 603f59
 *	specified, the process is excluded;
Packit Service 603f59
 *
Packit Service 603f59
 *	Otherwise, it's not excluded by the tests of this function.
Packit Service 603f59
 */
Packit Service 603f59
	if ( ! *sf)
Packit Service 603f59
	    return((Fand && (Selflags & (SELPGID|SELPID|SELUID|SELTASK)))
Packit Service 603f59
		   ? 1 : 0);
Packit Service 603f59
/*
Packit Service 603f59
 * When the process group ID, PID, task or UID is selected and the process
Packit Service 603f59
 * group ID, PID, task or UID list option has been specified:
Packit Service 603f59
 *
Packit Service 603f59
 *	If list option ANDing has been specified, and the correct
Packit Service 603f59
 *	combination of selections are in place, reply that the process is no
Packit Service 603f59
 *	excluded;
Packit Service 603f59
 * or
Packit Service 603f59
 *	If list option ANDing has not been specified, reply that the
Packit Service 603f59
 *	process is not excluded by the tests of this function.
Packit Service 603f59
 */
Packit Service 603f59
	if (Selflags & (SELPGID|SELPID|SELUID|SELTASK)) {
Packit Service 603f59
	    if (Fand)
Packit Service 603f59
		return(((Selflags & (SELPGID|SELPID|SELUID|SELTASK)) != *sf)
Packit Service 603f59
			? 1 : 0);
Packit Service 603f59
	    return(0);
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * Finally, when neither the process group ID, nor the PID, nor the UID, nor
Packit Service 603f59
 * the task is selected, and no applicable list option has been specified:
Packit Service 603f59
 *
Packit Service 603f59
 *	If list option ANDing has been specified, this process is
Packit Service 603f59
 *	excluded;
Packit Service 603f59
 *
Packit Service 603f59
 *	Otherwise, it isn't excluded by the tests of this function.
Packit Service 603f59
 */
Packit Service 603f59
	return(Fand ? 1 : 0);
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * link_lfile() - link local file structures
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
void
Packit Service 603f59
link_lfile()
Packit Service 603f59
{
Packit Service 603f59
	if (Lf->sf & SELEXCLF)
Packit Service 603f59
	    return;
Packit Service 603f59
Packit Service 603f59
#if	defined(HASEPTOPTS)
Packit Service 603f59
/*
Packit Service 603f59
 * If endpoint info has been requested, clear the SELPINFO flag from the local
Packit Service 603f59
 * pipe file structure, since it was set only to insure this file would be
Packit Service 603f59
 * linked.  While this might leave no file selection flags set, a later call
Packit Service 603f59
 * to the process_pinfo() function might set some.  Also set the EPT_PIPE flag.
Packit Service 603f59
 */
Packit Service 603f59
	if (FeptE) {
Packit Service 603f59
	    if (Lf->sf & SELPINFO) {
Packit Service 603f59
		Lp->ept |= EPT_PIPE;
Packit Service 603f59
		Lf->sf &= ~SELPINFO;
Packit Service 603f59
	    }
Packit Service 603f59
Packit Service 603f59
# if	defined(HASUXSOCKEPT)
Packit Service 603f59
/*
Packit Service 603f59
 * Process UNIX socket endpoint files the same way by clearing the SELUXINFO
Packit Service 603f59
 * flag and setting the EPT_UXS flag, letting a later call to process_uxsinfo()
Packit Service 603f59
 * set selection flags.
Packit Service 603f59
 */
Packit Service 603f59
	    if (Lf->sf & SELUXSINFO) {
Packit Service 603f59
		Lp->ept |= EPT_UXS;
Packit Service 603f59
		Lf->sf &= ~SELUXSINFO;
Packit Service 603f59
	    }
Packit Service 603f59
# endif	/* defined(HASUXSOCKEPT) */
Packit Service 603f59
Packit Service 603f59
# if	defined(HASPTYEPT)
Packit Service 603f59
/*
Packit Service 603f59
 * Process pseudoterminal endpoint files the same way by clearing the SELPINFO
Packit Service 603f59
 * flag and setting the EPT_PTY flag, letting a later call to process_ptyinfo()
Packit Service 603f59
 * set selection flags.
Packit Service 603f59
 */
Packit Service 603f59
	    if (Lf->sf & SELPTYINFO) {
Packit Service 603f59
		Lp->ept |= EPT_PTY;
Packit Service 603f59
		Lf->sf &= ~SELPTYINFO;
Packit Service 603f59
	    }
Packit Service 603f59
# endif	/* defined(HASPTYEPT) */
Packit Service 603f59
Packit Service 603f59
	}
Packit Service 603f59
#endif	/* defined(HASEPTOPTS) */
Packit Service 603f59
Packit Service 603f59
	if (Lf->sf)
Packit Service 603f59
	    Lp->pss |= PS_SEC;
Packit Service 603f59
	if (Plf)
Packit Service 603f59
	    Plf->next = Lf;
Packit Service 603f59
	else
Packit Service 603f59
	    Lp->file = Lf;
Packit Service 603f59
	Plf = Lf;
Packit Service 603f59
	if (Fnet && (Lf->sf & SELNET))
Packit Service 603f59
	    Fnet = 2;
Packit Service 603f59
	if (Fnfs && (Lf->sf & SELNFS))
Packit Service 603f59
	    Fnfs = 2;
Packit Service 603f59
	if (Ftask && (Lf->sf & SELTASK))
Packit Service 603f59
	    Ftask = 2;
Packit Service 603f59
	Lf = (struct lfile *)NULL;
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
#if	defined(HASEPTOPTS)
Packit Service 603f59
/*
Packit Service 603f59
 * process_pinfo() -- process pipe info, adding it to selected files and
Packit Service 603f59
 *		      selecting pipe end files (if requested)
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
void
Packit Service 603f59
process_pinfo(f)
Packit Service 603f59
	int f;				/* function:
Packit Service 603f59
					 *     0 == process selected pipe
Packit Service 603f59
					 *     1 == process end point
Packit Service 603f59
					 */
Packit Service 603f59
{
Packit Service 603f59
	struct lproc *ep;		/* pipe endpoint process */
Packit Service 603f59
	struct lfile *ef;		/* pipe endpoint file */
Packit Service 603f59
	int i;				/* temporary index */
Packit Service 603f59
	char nma[1024];			/* name addition buffer */
Packit Service 603f59
	pxinfo_t *pp;			/* previous pipe info */
Packit Service 603f59
	
Packit Service 603f59
	if (!FeptE)
Packit Service 603f59
	    return;
Packit Service 603f59
	for (Lf = Lp->file; Lf; Lf = Lf->next) {
Packit Service 603f59
	    if ((Lf->ntype != N_FIFO) || (Lf->inp_ty != 1))
Packit Service 603f59
		continue;
Packit Service 603f59
	    pp = (pxinfo_t *)NULL;
Packit Service 603f59
	    switch(f) {
Packit Service 603f59
	    case 0:
Packit Service 603f59
Packit Service 603f59
	    /*
Packit Service 603f59
	     * Process already selected pipe file.
Packit Service 603f59
	     */
Packit Service 603f59
		if (is_file_sel(Lp, Lf)) {
Packit Service 603f59
Packit Service 603f59
		/*
Packit Service 603f59
		 * This file has been selected by some criterion other than
Packit Service 603f59
		 * its being a pipe.  Look up the pipe's endpoints.
Packit Service 603f59
		 */
Packit Service 603f59
		    do {
Packit Service 603f59
			if ((pp = find_pepti(Lf, pp))) {
Packit Service 603f59
Packit Service 603f59
			/*
Packit Service 603f59
			 * This pipe endpoint is linked to the selected pipe
Packit Service 603f59
			 * file.  Add its PID and FD to the name column
Packit Service 603f59
			 * addition.
Packit Service 603f59
			 */
Packit Service 603f59
			    ep = &Lproc[pp->lpx];
Packit Service 603f59
			    ef = pp->lf;
Packit Service 603f59
			    for (i = 0; i < (FDLEN - 1); i++) {
Packit Service 603f59
				if (ef->fd[i] != ' ')
Packit Service 603f59
				    break;
Packit Service 603f59
			    }
Packit Service 603f59
			    (void) snpf(nma, sizeof(nma) - 1, "%d,%.*s,%s%c",
Packit Service 603f59
				ep->pid, CmdLim, ep->cmd,&ef->fd[i],
Packit Service 603f59
				ef->access);
Packit Service 603f59
			    (void) add_nma(nma, strlen(nma));
Packit Service 603f59
			    if (FeptE == 2) {
Packit Service 603f59
Packit Service 603f59
			    /*
Packit Service 603f59
			     * Endpoint files have been selected, so mark this
Packit Service 603f59
			     * one for selection later. Set the type to PIPE.
Packit Service 603f59
			     */
Packit Service 603f59
				ef->chend = CHEND_PIPE;
Packit Service 603f59
				ep->ept |= EPT_PIPE_END;
Packit Service 603f59
			    }
Packit Service 603f59
			    pp = pp->next;
Packit Service 603f59
			}
Packit Service 603f59
		    } while (pp);
Packit Service 603f59
		}
Packit Service 603f59
		break;
Packit Service 603f59
	    case 1:
Packit Service 603f59
		if (!is_file_sel(Lp, Lf) && (Lf->chend & CHEND_PIPE)) {
Packit Service 603f59
Packit Service 603f59
		/*
Packit Service 603f59
		 * This is an unselected end point file.  Select it and add
Packit Service 603f59
		 * its end point information to its name column addition.
Packit Service 603f59
		 */
Packit Service 603f59
		    Lf->sf = Selflags;
Packit Service 603f59
		    Lp->pss |= PS_SEC;
Packit Service 603f59
		    do {
Packit Service 603f59
			if ((pp = find_pepti(Lf, pp))) {
Packit Service 603f59
			    ep = &Lproc[pp->lpx];
Packit Service 603f59
			    ef = pp->lf;
Packit Service 603f59
			    for (i = 0; i < (FDLEN - 1); i++) {
Packit Service 603f59
				if (ef->fd[i] != ' ')
Packit Service 603f59
				    break;
Packit Service 603f59
			    }
Packit Service 603f59
			    (void) snpf(nma, sizeof(nma) - 1, "%d,%.*s,%s%c",
Packit Service 603f59
				ep->pid, CmdLim, ep->cmd, &ef->fd[i],
Packit Service 603f59
				ef->access);
Packit Service 603f59
			    (void) add_nma(nma, strlen(nma));
Packit Service 603f59
			    pp = pp->next;
Packit Service 603f59
			}
Packit Service 603f59
		    } while (pp);
Packit Service 603f59
		}
Packit Service 603f59
		break;
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
}
Packit Service 603f59
#endif	/* defined(HASEPTOPTS) */
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
#if	defined(HASFSTRUCT)
Packit Service 603f59
/*
Packit Service 603f59
 * print_fflags() - print interpreted f_flag[s]
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
char *
Packit Service 603f59
print_fflags(ffg, pof)
Packit Service 603f59
	long ffg;		/* file structure's flags value */
Packit Service 603f59
	long pof;		/* process open files flags value */
Packit Service 603f59
{
Packit Service 603f59
	int al, ct, fx;
Packit Service 603f59
	static int bl = 0;
Packit Service 603f59
	static char *bp = (char *)NULL;
Packit Service 603f59
	char *sep;
Packit Service 603f59
	int sepl;
Packit Service 603f59
	struct pff_tab *tp;
Packit Service 603f59
	long wf;
Packit Service 603f59
	char xbuf[64];
Packit Service 603f59
/*
Packit Service 603f59
 * Reduce the supplied flags according to the definitions in Pff_tab[] and
Packit Service 603f59
 * Pof_tab[].
Packit Service 603f59
 */
Packit Service 603f59
	for (ct = fx = 0; fx < 2; fx++) {
Packit Service 603f59
	    if (fx == 0) {
Packit Service 603f59
		sep = "";
Packit Service 603f59
		sepl = 0;
Packit Service 603f59
		tp = Pff_tab;
Packit Service 603f59
		wf = ffg;
Packit Service 603f59
	    } else {
Packit Service 603f59
		sep = ";";
Packit Service 603f59
		sepl = 1;
Packit Service 603f59
		tp = Pof_tab;
Packit Service 603f59
		wf = pof;
Packit Service 603f59
	    }
Packit Service 603f59
	    for (; wf && !FsvFlagX; ct += al ) {
Packit Service 603f59
		while (tp->nm) {
Packit Service 603f59
		    if (wf & tp->val)
Packit Service 603f59
			break;
Packit Service 603f59
		    tp++;
Packit Service 603f59
		}
Packit Service 603f59
		if (!tp->nm)
Packit Service 603f59
		    break;
Packit Service 603f59
		al = (int)strlen(tp->nm) + sepl;
Packit Service 603f59
		bp = alloc_fflbuf(&bp, &bl, al + ct);
Packit Service 603f59
		(void) snpf(bp + ct, al + 1, "%s%s", sep, tp->nm);
Packit Service 603f59
		sep = ",";
Packit Service 603f59
		sepl = 1;
Packit Service 603f59
		wf &= ~(tp->val);
Packit Service 603f59
	    }
Packit Service 603f59
	/*
Packit Service 603f59
	 * If flag bits remain, print them in hex.  If hex output was
Packit Service 603f59
	 * specified with +fG, print all flag values, including zero,
Packit Service 603f59
	 * in hex.
Packit Service 603f59
	 */
Packit Service 603f59
	    if (wf || FsvFlagX) {
Packit Service 603f59
		(void) snpf(xbuf, sizeof(xbuf), "0x%lx", wf);
Packit Service 603f59
		al = (int)strlen(xbuf) + sepl;
Packit Service 603f59
		bp = alloc_fflbuf(&bp, &bl, al + ct);
Packit Service 603f59
		(void) snpf(bp + ct, al + 1, "%s%s", sep, xbuf);
Packit Service 603f59
		ct += al;
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * Make sure there is at least a NUL terminated reply.
Packit Service 603f59
 */
Packit Service 603f59
	if (!bp) {
Packit Service 603f59
	    bp = alloc_fflbuf(&bp, &bl, 0);
Packit Service 603f59
	    *bp = '\0';
Packit Service 603f59
	}
Packit Service 603f59
	return(bp);
Packit Service 603f59
}
Packit Service 603f59
#endif	/* defined(HASFSTRUCT) */
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * print_proc() - print process
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
int
Packit Service 603f59
print_proc()
Packit Service 603f59
{
Packit Service 603f59
	char buf[128], *cp;
Packit Service 603f59
	int lc, len, st, ty;
Packit Service 603f59
	int rv = 0;
Packit Service 603f59
	unsigned long ul;
Packit Service 603f59
/*
Packit Service 603f59
 * If nothing in the process has been selected, skip it.
Packit Service 603f59
 */
Packit Service 603f59
	if (!Lp->pss)
Packit Service 603f59
	    return(0);
Packit Service 603f59
	if (Fterse) {
Packit Service 603f59
	    if (Lp->pid == LastPid)		/* eliminate duplicates */
Packit Service 603f59
		return(0);
Packit Service 603f59
	    LastPid = Lp->pid;
Packit Service 603f59
	/*
Packit Service 603f59
	 * The mode is terse and something in the process appears to have
Packit Service 603f59
	 * been selected.  Make sure of that by looking for a selected file,
Packit Service 603f59
	 * so that the HASSECURITY and HASNOSOCKSECURITY option combination
Packit Service 603f59
	 * won't produce a false positive result.
Packit Service 603f59
	 */
Packit Service 603f59
	    for (Lf = Lp->file; Lf; Lf = Lf->next) {
Packit Service 603f59
		if (is_file_sel(Lp, Lf)) {
Packit Service 603f59
		    (void) printf("%d\n", Lp->pid);
Packit Service 603f59
		    return(1);
Packit Service 603f59
		}
Packit Service 603f59
	    }
Packit Service 603f59
	    return(0);
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * If fields have been selected, output the process-only ones, provided
Packit Service 603f59
 * that some file has also been selected.
Packit Service 603f59
 */
Packit Service 603f59
	if (Ffield) {
Packit Service 603f59
	    for (Lf = Lp->file; Lf; Lf = Lf->next) {
Packit Service 603f59
		if (is_file_sel(Lp, Lf))
Packit Service 603f59
		    break;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (!Lf)
Packit Service 603f59
		return(rv);
Packit Service 603f59
	    rv = 1;
Packit Service 603f59
	    (void) printf("%c%d%c", LSOF_FID_PID, Lp->pid, Terminator);
Packit Service 603f59
Packit Service 603f59
#if	defined(HASTASKS)
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_TID].st && Lp->tid)
Packit Service 603f59
		(void) printf("%c%d%c", LSOF_FID_TID, Lp->tid, Terminator);
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_TCMD].st && Lp->tcmd)
Packit Service 603f59
		(void) printf("%c%s%c", LSOF_FID_TCMD, Lp->tcmd, Terminator);
Packit Service 603f59
#endif	/* defined(HASTASKS) */
Packit Service 603f59
Packit Service 603f59
#if	defined(HASZONES)
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_ZONE].st && Fzone && Lp->zn)
Packit Service 603f59
		(void) printf("%c%s%c", LSOF_FID_ZONE, Lp->zn, Terminator);
Packit Service 603f59
#endif	/* defined(HASZONES) */
Packit Service 603f59
 
Packit Service 603f59
#if	defined(HASSELINUX)
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_CNTX].st && Fcntx && Lp->cntx && CntxStatus)
Packit Service 603f59
		(void) printf("%c%s%c", LSOF_FID_CNTX, Lp->cntx, Terminator);
Packit Service 603f59
#endif	/* defined(HASSELINUX) */
Packit Service 603f59
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_PGID].st && Fpgid)
Packit Service 603f59
		(void) printf("%c%d%c", LSOF_FID_PGID, Lp->pgid, Terminator);
Packit Service 603f59
Packit Service 603f59
#if	defined(HASPPID)
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_PPID].st && Fppid)
Packit Service 603f59
		(void) printf("%c%d%c", LSOF_FID_PPID, Lp->ppid, Terminator);
Packit Service 603f59
#endif	/* defined(HASPPID) */
Packit Service 603f59
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_CMD].st) {
Packit Service 603f59
		putchar(LSOF_FID_CMD);
Packit Service 603f59
		safestrprt(Lp->cmd ? Lp->cmd : "(unknown)", stdout, 0);
Packit Service 603f59
		putchar(Terminator);
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_UID].st)
Packit Service 603f59
		(void) printf("%c%d%c", LSOF_FID_UID, (int)Lp->uid, Terminator);
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_LOGIN].st) {
Packit Service 603f59
		cp = printuid((UID_ARG)Lp->uid, &ty;;
Packit Service 603f59
		if (ty == 0)
Packit Service 603f59
		    (void) printf("%c%s%c", LSOF_FID_LOGIN, cp, Terminator);
Packit Service 603f59
	    }
Packit Service 603f59
	    if (Terminator == '\0')
Packit Service 603f59
		putchar('\n');
Packit Service 603f59
	}
Packit Service 603f59
/*
Packit Service 603f59
 * Print files.
Packit Service 603f59
 */
Packit Service 603f59
	for (Lf = Lp->file; Lf; Lf = Lf->next) {
Packit Service 603f59
	    if (!is_file_sel(Lp, Lf))
Packit Service 603f59
		continue;
Packit Service 603f59
	    rv = 1;
Packit Service 603f59
	/*
Packit Service 603f59
	 * If no field output selected, print dialect-specific formatted
Packit Service 603f59
	 * output.
Packit Service 603f59
	 */
Packit Service 603f59
	    if (!Ffield) {
Packit Service 603f59
		print_file();
Packit Service 603f59
		continue;
Packit Service 603f59
	    }
Packit Service 603f59
	    lc = st = 0;
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_FD].st) {
Packit Service 603f59
Packit Service 603f59
	    /*
Packit Service 603f59
	     * Skip leading spaces in the file descriptor.  Print the field
Packit Service 603f59
	     * identifier even if there are no characters after leading
Packit Service 603f59
	     * spaces.
Packit Service 603f59
	     */
Packit Service 603f59
		for (cp = Lf->fd; *cp == ' '; cp++)
Packit Service 603f59
		    ;
Packit Service 603f59
		(void) printf("%c%s%c", LSOF_FID_FD, cp, Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	/*
Packit Service 603f59
	 * Print selected fields.
Packit Service 603f59
	 */
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_ACCESS].st) {
Packit Service 603f59
		(void) printf("%c%c%c",
Packit Service 603f59
		    LSOF_FID_ACCESS, Lf->access, Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_LOCK].st) {
Packit Service 603f59
		(void) printf("%c%c%c", LSOF_FID_LOCK, Lf->lock, Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_TYPE].st) {
Packit Service 603f59
		for (cp = Lf->type; *cp == ' '; cp++)
Packit Service 603f59
		    ;
Packit Service 603f59
		if (*cp) {
Packit Service 603f59
		    (void) printf("%c%s%c", LSOF_FID_TYPE, cp, Terminator);
Packit Service 603f59
		    lc++;
Packit Service 603f59
		}
Packit Service 603f59
	    }
Packit Service 603f59
Packit Service 603f59
#if	defined(HASFSTRUCT)
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_FA].st && (Fsv & FSV_FA)
Packit Service 603f59
	    &&  (Lf->fsv & FSV_FA)) {
Packit Service 603f59
		(void) printf("%c%s%c", LSOF_FID_FA,
Packit Service 603f59
		    print_kptr(Lf->fsa, (char *)NULL, 0), Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_CT].st && (Fsv & FSV_CT)
Packit Service 603f59
	    &&  (Lf->fsv & FSV_CT)) {
Packit Service 603f59
		(void) printf("%c%ld%c", LSOF_FID_CT, Lf->fct, Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_FG].st && (Fsv & FSV_FG)
Packit Service 603f59
	    &&  (Lf->fsv & FSV_FG) && (FsvFlagX || Lf->ffg || Lf->pof)) {
Packit Service 603f59
		(void) printf("%c%s%c", LSOF_FID_FG,
Packit Service 603f59
		    print_fflags(Lf->ffg, Lf->pof), Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_NI].st && (Fsv & FSV_NI)
Packit Service 603f59
	    &&  (Lf->fsv & FSV_NI)) {
Packit Service 603f59
		(void) printf("%c%s%c", LSOF_FID_NI,
Packit Service 603f59
		    print_kptr(Lf->fna, (char *)NULL, 0), Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
#endif	/* defined(HASFSTRUCT) */
Packit Service 603f59
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_DEVCH].st && Lf->dev_ch && Lf->dev_ch[0]) {
Packit Service 603f59
		for (cp = Lf->dev_ch; *cp == ' '; cp++)
Packit Service 603f59
		    ;
Packit Service 603f59
		if (*cp) {
Packit Service 603f59
		    (void) printf("%c%s%c", LSOF_FID_DEVCH, cp, Terminator);
Packit Service 603f59
		    lc++;
Packit Service 603f59
		}
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_DEVN].st && Lf->dev_def) {
Packit Service 603f59
		if (sizeof(unsigned long) > sizeof(dev_t))
Packit Service 603f59
		    ul = (unsigned long)((unsigned int)Lf->dev);
Packit Service 603f59
		else
Packit Service 603f59
		    ul = (unsigned long)Lf->dev;
Packit Service 603f59
		(void) printf("%c0x%lx%c", LSOF_FID_DEVN, ul, Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_RDEV].st && Lf->rdev_def) {
Packit Service 603f59
		if (sizeof(unsigned long) > sizeof(dev_t))
Packit Service 603f59
		    ul = (unsigned long)((unsigned int)Lf->rdev);
Packit Service 603f59
		else
Packit Service 603f59
		    ul = (unsigned long)Lf->rdev;
Packit Service 603f59
		(void) printf("%c0x%lx%c", LSOF_FID_RDEV, ul, Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_SIZE].st && Lf->sz_def) {
Packit Service 603f59
		putchar(LSOF_FID_SIZE);
Packit Service 603f59
Packit Service 603f59
#if	defined(HASPRINTSZ)
Packit Service 603f59
		cp = HASPRINTSZ(Lf);
Packit Service 603f59
#else	/* !defined(HASPRINTSZ) */
Packit Service 603f59
		(void) snpf(buf, sizeof(buf), SzOffFmt_d, Lf->sz);
Packit Service 603f59
		cp = buf;
Packit Service 603f59
#endif	/* defined(HASPRINTSZ) */
Packit Service 603f59
Packit Service 603f59
		(void) printf("%s", cp);
Packit Service 603f59
		putchar(Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_OFFSET].st && Lf->off_def) {
Packit Service 603f59
		putchar(LSOF_FID_OFFSET);
Packit Service 603f59
Packit Service 603f59
#if	defined(HASPRINTOFF)
Packit Service 603f59
		cp = HASPRINTOFF(Lf, 0);
Packit Service 603f59
#else	/* !defined(HASPRINTOFF) */
Packit Service 603f59
		(void) snpf(buf, sizeof(buf), SzOffFmt_0t, Lf->off);
Packit Service 603f59
		cp = buf;
Packit Service 603f59
#endif	/* defined(HASPRINTOFF) */
Packit Service 603f59
Packit Service 603f59
		len = strlen(cp);
Packit Service 603f59
		if (OffDecDig && len > (OffDecDig + 2)) {
Packit Service 603f59
Packit Service 603f59
#if	defined(HASPRINTOFF)
Packit Service 603f59
		    cp = HASPRINTOFF(Lf, 1);
Packit Service 603f59
#else	/* !defined(HASPRINTOFF) */
Packit Service 603f59
		    (void) snpf(buf, sizeof(buf), SzOffFmt_x, Lf->off);
Packit Service 603f59
		    cp = buf;
Packit Service 603f59
#endif	/* defined(HASPRINTOFF) */
Packit Service 603f59
Packit Service 603f59
		}
Packit Service 603f59
		(void) printf("%s", cp);
Packit Service 603f59
		putchar(Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_INODE].st && Lf->inp_ty == 1) {
Packit Service 603f59
		putchar(LSOF_FID_INODE);
Packit Service 603f59
		(void) printf(InodeFmt_d, Lf->inode);
Packit Service 603f59
		putchar(Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_NLINK].st && Lf->nlink_def) {
Packit Service 603f59
		(void) printf("%c%ld%c", LSOF_FID_NLINK, Lf->nlink, Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_PROTO].st && Lf->inp_ty == 2) {
Packit Service 603f59
		for (cp = Lf->iproto; *cp == ' '; cp++)
Packit Service 603f59
		    ;
Packit Service 603f59
		if (*cp) {
Packit Service 603f59
		    (void) printf("%c%s%c", LSOF_FID_PROTO, cp, Terminator);
Packit Service 603f59
		    lc++;
Packit Service 603f59
		}
Packit Service 603f59
	    }
Packit Service 603f59
	    if (FieldSel[LSOF_FIX_STREAM].st && Lf->nm && Lf->is_stream) {
Packit Service 603f59
		if (strncmp(Lf->nm, "STR:", 4) == 0
Packit Service 603f59
		||  strcmp(Lf->iproto, "STR") == 0) {
Packit Service 603f59
		    putchar(LSOF_FID_STREAM);
Packit Service 603f59
		    printname(0);
Packit Service 603f59
		    putchar(Terminator);
Packit Service 603f59
		    lc++;
Packit Service 603f59
		    st++;
Packit Service 603f59
		}
Packit Service 603f59
	    }
Packit Service 603f59
	    if (st == 0 && FieldSel[LSOF_FIX_NAME].st) {
Packit Service 603f59
		putchar(LSOF_FID_NAME);
Packit Service 603f59
		printname(0);
Packit Service 603f59
		putchar(Terminator);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (Lf->lts.type >= 0 && FieldSel[LSOF_FIX_TCPTPI].st) {
Packit Service 603f59
		print_tcptpi(0);
Packit Service 603f59
		lc++;
Packit Service 603f59
	    }
Packit Service 603f59
	    if (Terminator == '\0' && lc)
Packit Service 603f59
		putchar('\n');
Packit Service 603f59
	}
Packit Service 603f59
	return(rv);
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
#if	defined(HASPTYEPT)
Packit Service 603f59
/*
Packit Service 603f59
 * process_ptyinfo() -- process pseudoterminal info, adding it to selected files and
Packit Service 603f59
 *		      selecting pseudoterminal end files (if requested)
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
void
Packit Service 603f59
process_ptyinfo(f)
Packit Service 603f59
	int f;				/* function:
Packit Service 603f59
					 *  0 == process selected pseudoterminal
Packit Service 603f59
					 *  1 == process end point */
Packit Service 603f59
{
Packit Service 603f59
	pxinfo_t *pp;			/* previous pseudoterminal info */
Packit Service 603f59
	int mos;			/* master or slave indicator
Packit Service 603f59
					 *     0 == slave; 1 == master */
Packit Service 603f59
	int pc;				/* print count */
Packit Service 603f59
Packit Service 603f59
	if (!FeptE)
Packit Service 603f59
	    return;
Packit Service 603f59
	for (Lf = Lp->file; Lf; Lf = Lf->next) {
Packit Service 603f59
	    if (Lf->rdev_def && is_pty_ptmx(Lf->rdev))
Packit Service 603f59
		mos = 1;
Packit Service 603f59
	    else if (Lf->rdev_def && is_pty_slave(GET_MAJ_DEV(Lf->rdev)))
Packit Service 603f59
		mos = 0;
Packit Service 603f59
	    else
Packit Service 603f59
		continue;
Packit Service 603f59
Packit Service 603f59
	    pp = (pxinfo_t *)NULL;
Packit Service 603f59
	    switch(f) {
Packit Service 603f59
	    case 0:
Packit Service 603f59
Packit Service 603f59
	    /*
Packit Service 603f59
	     * Process already selected pseudoterminal file.
Packit Service 603f59
	     */
Packit Service 603f59
		if (is_file_sel(Lp, Lf)) {
Packit Service 603f59
Packit Service 603f59
		/*
Packit Service 603f59
		 * This file has been selected by some criterion other than
Packit Service 603f59
		 * its being a pseudoterminal.  Look up the pseudoterminal's
Packit Service 603f59
		 * endpoints.
Packit Service 603f59
		 */
Packit Service 603f59
		    pc = 1;
Packit Service 603f59
		    do {
Packit Service 603f59
			if ((pp = find_ptyepti(Lf, !mos, pp))) {
Packit Service 603f59
Packit Service 603f59
			/*
Packit Service 603f59
			 * This pseudoterminal endpoint is linked to the
Packit Service 603f59
			 * selected pseudoterminal file.  Add its PID, FD and
Packit Service 603f59
			 * access mode to the name column addition.
Packit Service 603f59
			 */
Packit Service 603f59
			    prt_ptyinfo(pp, (mos && pc), (FeptE == 2));
Packit Service 603f59
			    pp = pp->next;
Packit Service 603f59
			    pc = 0;
Packit Service 603f59
			}
Packit Service 603f59
		    } while (pp);
Packit Service 603f59
		}
Packit Service 603f59
		break;
Packit Service 603f59
	    case 1:
Packit Service 603f59
		if (!is_file_sel(Lp, Lf) && (Lf->chend & CHEND_PTY)) {
Packit Service 603f59
Packit Service 603f59
		/*
Packit Service 603f59
		 * This is an unselected end point file.  Select it and add
Packit Service 603f59
		 * its end point information to its name column addition.
Packit Service 603f59
		 */
Packit Service 603f59
		    Lf->sf = Selflags;
Packit Service 603f59
		    Lp->pss |= PS_SEC;
Packit Service 603f59
		    pc = 1;
Packit Service 603f59
		    do {
Packit Service 603f59
			if ((pp = find_ptyepti(Lf, !mos, pp))) {
Packit Service 603f59
			    prt_ptyinfo(pp, (mos && pc), 0);
Packit Service 603f59
			    pp = pp->next;
Packit Service 603f59
			    pc = 0;
Packit Service 603f59
			}
Packit Service 603f59
		    } while (pp);
Packit Service 603f59
		}
Packit Service 603f59
		break;
Packit Service 603f59
	    }
Packit Service 603f59
	}
Packit Service 603f59
}
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
/*
Packit Service 603f59
 * prt_ptyinfo() -- print pseudoterminal information
Packit Service 603f59
 */
Packit Service 603f59
Packit Service 603f59
static void
Packit Service 603f59
prt_ptyinfo(pp, prt_edev, ps)
Packit Service 603f59
	pxinfo_t *pp;			/* peer info */
Packit Service 603f59
	int prt_edev;			/* print the end point device file */
Packit Service 603f59
	int ps;				/* processing status:
Packit Service 603f59
					 *    0 == process immediately
Packit Service 603f59
					 *    1 == process later */
Packit Service 603f59
{
Packit Service 603f59
	struct lproc *ep;		/* pseudoterminal endpoint process */
Packit Service 603f59
	struct lfile *ef;		/* pseudoterminal endpoint file */
Packit Service 603f59
	int i;				/* temporary index */
Packit Service 603f59
	char nma[1024];			/* name addition buffer */
Packit Service 603f59
Packit Service 603f59
	ep = &Lproc[pp->lpx];
Packit Service 603f59
	ef = pp->lf;
Packit Service 603f59
	for (i = 0; i < (FDLEN - 1); i++) {
Packit Service 603f59
	    if (ef->fd[i] != ' ')
Packit Service 603f59
		break;
Packit Service 603f59
	}
Packit Service 603f59
	if (prt_edev) {
Packit Service 603f59
	    (void) snpf(nma, sizeof(nma) - 1, "->/dev/pts/%d %d,%.*s,%s%c",
Packit Service 603f59
			Lf->tty_index,
Packit Service 603f59
			ep->pid, CmdLim, ep->cmd, &ef->fd[i],
Packit Service 603f59
			ef->access);
Packit Service 603f59
	} else {
Packit Service 603f59
	    (void) snpf(nma, sizeof(nma) - 1, "%d,%.*s,%s%c",
Packit Service 603f59
			ep->pid, CmdLim, ep->cmd, &ef->fd[i],
Packit Service 603f59
			ef->access);
Packit Service 603f59
	}
Packit Service 603f59
	(void) add_nma(nma, strlen(nma));
Packit Service 603f59
	if (ps) {
Packit Service 603f59
Packit Service 603f59
	/*
Packit Service 603f59
	 * Endpoint files have been selected, so mark this
Packit Service 603f59
	 * one for selection later. Set the type to PTY.
Packit Service 603f59
	 */
Packit Service 603f59
	    ef->chend = CHEND_PTY;
Packit Service 603f59
	    ep->ept |= EPT_PTY_END;
Packit Service 603f59
	}
Packit Service 603f59
}
Packit Service 603f59
#endif	/* defined(HASPTYEPT) */