From acf7511c6a7358c311c3970f64f584a795979763 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 27 Nov 2026 15:04:02 +0800 Subject: [PATCH 01/18] sun: replace kmem backend with procfs --- configure.ac | 2 +- lib/dialects/sun/Makefile | 8 +- lib/dialects/sun/Mksrc | 2 +- lib/dialects/sun/dfile.c | 110 - lib/dialects/sun/distfile.kvm | 14 - lib/dialects/sun/dlsof.h | 3 - lib/dialects/sun/dmnt.c | 66 - lib/dialects/sun/dnode.c | 5435 +----------------------- lib/dialects/sun/dnode1.c | 414 -- lib/dialects/sun/dnode2.c | 483 --- lib/dialects/sun/dproc.c | 2548 +++-------- lib/dialects/sun/dproto.h | 2 - lib/dialects/sun/dsock.c | 1678 +------- lib/dialects/sun/dstore.c | 2 - lib/dialects/sun/solaris_kaddr_filters | 239 -- 15 files changed, 534 insertions(+), 10472 deletions(-) delete mode 100644 lib/dialects/sun/distfile.kvm delete mode 100644 lib/dialects/sun/dnode1.c delete mode 100644 lib/dialects/sun/dnode2.c delete mode 100644 lib/dialects/sun/solaris_kaddr_filters diff --git a/configure.ac b/configure.ac index 2bacdb7f..70a6ba79 100644 --- a/configure.ac +++ b/configure.ac @@ -362,7 +362,7 @@ AC_CHECK_DECLS([doadump], [], [], [[#define _KERNEL 1 # Detect struct vnode definition for HAS_KVM_VNODE # Linking libkvm is not required in AIX -AS_IF([test x$LSOF_DIALECT_DIR != xaix], [ +AS_IF([test x$LSOF_DIALECT_DIR != xaix && test x$LSOF_DIALECT_DIR != xsun], [ AC_EGREP_CPP([struct vnode], [#define _KVM_VNODE 1 #include ], [AC_DEFINE([HAVE_KVM_VNODE], [1], ["Define to 1 if struct vnode exists"]) LIBS="$LIBS -lkvm"]) diff --git a/lib/dialects/sun/Makefile b/lib/dialects/sun/Makefile index 06ca474b..02b7f8b1 100644 --- a/lib/dialects/sun/Makefile +++ b/lib/dialects/sun/Makefile @@ -17,11 +17,11 @@ CFLAGS= ${CDEFS} ${INCL} ${DEBUG} HDR= lsof.h lsof_fields.h dlsof.h machine.h proto.h dproto.h -SRC= ddev.c dfile.c dmnt.c dnode.c dnode1.c dnode2.c dproc.c dsock.c \ +SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c dsock.c \ dstore.c dprint.c \ arg.c main.c misc.c node.c print.c proc.c store.c usage.c util.c -OBJ= ddev.o dfile.o dmnt.o dnode.o dnode1.o dnode2.o dproc.o dsock.o \ +OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o dsock.o \ dstore.o dprint.o \ arg.o main.o misc.o node.o print.o proc.o store.o usage.o util.o @@ -128,10 +128,6 @@ dmnt.o: ${HDR} dmnt.c dnode.o: ${HDR} dnode.c -dnode1.o: ${HDR} dnode1.c - -dnode2.o: ${HDR} dnode2.c - dproc.o: ${HDR} kernelbase.h dproc.c dsock.o: ${HDR} dsock.c diff --git a/lib/dialects/sun/Mksrc b/lib/dialects/sun/Mksrc index 910206a0..53af4c70 100755 --- a/lib/dialects/sun/Mksrc +++ b/lib/dialects/sun/Mksrc @@ -22,7 +22,7 @@ mksrc() { } D=lib/dialects/sun -L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dnode1.c dnode2.c dproc.c dproto.h dsock.c dstore.c machine.h" +L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dsock.c dstore.c machine.h" mksrc diff --git a/lib/dialects/sun/dfile.c b/lib/dialects/sun/dfile.c index 69f75c8f..8875ea08 100644 --- a/lib/dialects/sun/dfile.c +++ b/lib/dialects/sun/dfile.c @@ -518,118 +518,8 @@ extern int print_v_path(struct lsof_context *ctx, } return (0); } - -/* - * read_v_path() - read path name from vnode's v_path pointer - */ - -extern void read_v_path(struct lsof_context *ctx, - KA_T ka, /* kernel path address */ - char *rb, /* receiving buffer */ - size_t rbl) /* receiving buffer length */ -{ - char *ba; - size_t rl, tl; - - *rb = '\0'; - if (!ka) - return; - for (ba = rb, tl = 0; tl < (rbl - 1); - ba += rl, ka += (KA_T)((char *)ka + rl), tl += rl) { - - /* - * Read v_path VPRDLEN bytes at a time until the local buffer is full - * or a NUL byte is reached. - */ - if ((rl = rbl - 1 - tl) > VPRDLEN) - rl = VPRDLEN; - else if (rl < 1) { - *(rb + rbl - 1) = '\0'; - break; - } - if (!kread(ctx, ka, ba, rl)) { - *(ba + rl) = '\0'; - if (strchr(ba, '\0') < (ba + rl)) - break; - } else { - - /* - * Can't read a full buffer load; try reducing the length one - * byte at a time until it reaches zero. Stop here, since it - * has been established that no more bytes can be read. - */ - for (rl--; rl > 0; rl--) { - if (!kread(ctx, ka, ba, rl)) { - *(ba + rl) = '\0'; - break; - } - } - if (rl <= 0) - *ba = '\0'; - break; - } - } -} #endif /* defined(HAS_V_PATH) */ -/* - * process_file() - process file - */ - -void process_file(struct lsof_context *ctx, /* context */ - KA_T fp) /* kernel file structure address */ -{ - struct file f; - int flag; - -#if defined(FILEPTR) - FILEPTR = &f; -#endif /* defined(FILEPTR) */ - - if (kread(ctx, fp, (char *)&f, sizeof(f))) { - (void)snpf(Namech, Namechl, "can't read file struct from %s", - print_kptr(fp, (char *)NULL, 0)); - enter_nm(ctx, Namech); - return; - } - Lf->off = (SZOFFTYPE)f.f_offset; - Lf->off_def = 1; - - if (f.f_count) { - - /* - * Construct access code. - */ - if ((flag = (f.f_flag & (FREAD | FWRITE))) == FREAD) - Lf->access = LSOF_FILE_ACCESS_READ; - else if (flag == FWRITE) - Lf->access = LSOF_FILE_ACCESS_WRITE; - else if (flag == (FREAD | FWRITE)) - Lf->access = LSOF_FILE_ACCESS_READ_WRITE; - -#if defined(HASFSTRUCT) - /* - * Save file structure values. - */ - Lf->fct = (long)f.f_count; - Lf->fsv |= FSV_CT; - Lf->fsa = fp; - Lf->fsv |= FSV_FA; - Lf->ffg = (long)f.f_flag; - Lf->fsv |= FSV_FG; - Lf->fna = (KA_T)f.f_vnode; - Lf->fsv |= FSV_NI; -#endif /* defined(HASFSTRUCT) */ - - /* - * Solaris file structures contain a vnode pointer. Process it. - */ - process_node(ctx, (KA_T)f.f_vnode); - return; - } - enter_nm(ctx, "no more information"); -} - #if defined(HASIPv6) /* * gethostbyname2() -- an RFC2133-compatible get-host-by-name-two function diff --git a/lib/dialects/sun/distfile.kvm b/lib/dialects/sun/distfile.kvm deleted file mode 100644 index 9559172a..00000000 --- a/lib/dialects/sun/distfile.kvm +++ /dev/null @@ -1,14 +0,0 @@ -# $Id: distfile.kvm,v 1.1 94/05/03 16:04:00 abe Exp $ -# -# distfile.kvm -- an assist to distributing SunOS objects that use -lkvm -# -# This distfile allows Sun4 systems to limit distribution of object files -# to systems of like architecture, using the source architecture definition -# in SKVM and the destination architecture definition in KVM. -# -# The path of the object to be distributed is defined in OBJPATH. - -ifelse(SKVM,KVM, -`( OBJPATH ) -> ( HOST ) - install -b ; -')dnl diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index 3c461675..e4f09273 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -382,7 +382,6 @@ extern int nlist(const char *, struct nlist *); # define AFSAPATHDEF "/usr/vice/etc/modload/libafs" # endif /* defined(HAS_AFS) && !defined(AFSAPATHDEF) */ -# define ALLKMEM "/dev/allkmem" # define COMP_P const void # define CWDLEN (MAXPATHLEN + 1) # define DEVINCR 1024 /* device table malloc() increment */ @@ -528,9 +527,7 @@ typedef struct CTF_request { extern char **Fsinfo; extern int Fsinfomax; -extern int HasALLKMEM; extern int HaveCloneMaj; -extern kvm_t *Kd; struct l_ino { unsigned char dev_def; /* dev member is defined */ diff --git a/lib/dialects/sun/dmnt.c b/lib/dialects/sun/dmnt.c index de1128b0..68308510 100644 --- a/lib/dialects/sun/dmnt.c +++ b/lib/dialects/sun/dmnt.c @@ -328,69 +328,3 @@ struct mounts *readmnt(struct lsof_context *ctx) { Lmist = 1; return (Lmi); } - -/* - * readvfs() - read vfs structure - */ - -struct l_vfs *readvfs(struct lsof_context *ctx, /* context */ - KA_T ka, /* vfs structure kernel address, if - * must be read from kernel */ - struct vfs *la, /* local vfs structure address, non- - * NULL if already read from kernel */ - struct vnode *lv) /* local vnode */ -{ - struct vfs *v, tv; - struct l_vfs *vp; - - if (!ka && !la) - return ((struct l_vfs *)NULL); - for (vp = Lvfs; vp; vp = vp->next) { - if (ka == vp->addr) - return (vp); - } - if (!(vp = (struct l_vfs *)malloc(sizeof(struct l_vfs)))) { - (void)fprintf(stderr, "%s: PID %d, no space for vfs\n", Pn, Lp->pid); - Error(ctx); - } - vp->dir = (char *)NULL; - vp->fsname = (char *)NULL; - -#if defined(HASFSINO) - vp->fs_ino = 0; -#endif /* defined(HASFSINO) */ - - /* - * Read vfs structure from kernel, if necessary. - */ - if (la) - v = la; - else { - v = &tv; - if (kread(ctx, (KA_T)ka, (char *)v, sizeof(tv))) { - (void)free((FREE_P *)vp); - return ((struct l_vfs *)NULL); - } - } - -#if defined(HAS_AFS) - /* - * Fake the device number for an AFS device. - */ - if (v->vfs_fstype == AFSfstype) { - if (!AFSdevStat) - (void)readmnt(ctx); - v->vfs_dev = AFSdevStat ? AFSdev : 0; - } -#endif /* defined(HAS_AFS) */ - - /* - * Complete mount information. - */ - - (void)completevfs(ctx, vp, (dev_t *)&v->vfs_dev); - vp->next = Lvfs; - vp->addr = ka; - Lvfs = vp; - return (vp); -} diff --git a/lib/dialects/sun/dnode.c b/lib/dialects/sun/dnode.c index 077cec5f..3d983674 100644 --- a/lib/dialects/sun/dnode.c +++ b/lib/dialects/sun/dnode.c @@ -1,5423 +1,58 @@ /* - * dnode.c - Solaris node reading functions for lsof - */ - -/* - * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana - * 47907. All rights reserved. - * - * Written by Victor A. Abell - * - * This software is not subject to any license of the American Telephone - * and Telegraph Company or the Regents of the University of California. - * - * Permission is granted to anyone to use this software for any purpose on - * any computer system, and to alter it and redistribute it freely, subject - * to the following restrictions: - * - * 1. Neither the authors nor Purdue University are responsible for any - * consequences of the use of this software. - * - * 2. The origin of this software must not be misrepresented, either by - * explicit claim or by omission. Credit to the authors and Purdue - * University must appear in documentation and sources. + * dnode.c - Solaris /proc node compatibility shims. * - * 3. Altered versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 4. This notice may not be removed or altered. + * The procfs backend fills lfile entries directly from /proc. These + * functions remain only for the mount-table and file-selection support + * code; they never read kernel memory. */ -#ifndef lint -static char copyright[] = - "@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n"; -#endif - #include "common.h" -#if solaris >= 110000 -# include -#endif /* solaris>=110000 */ - -#undef fs_bsize -#include - -#if solaris >= 110000 && defined(HAS_LIBCTF) -/* - * Sockfs support for Solaris 11 via libctf - */ - -/* - * Sockfs internal structure definitions - * - * The structure definitions may look like kernel structures, but they are - * not. They have been defined to have member names that duplicate those - * used by the kernel that are of interest to lsof. Member values are - * obtained via the CTF library, libctf. - * - * Robert Byrnes developed the CTF library access code and contributed it - * to lsof. - */ - -struct soaddr { /* sadly, CTF doesn't grok this - * structure */ - struct sockaddr *soa_sa; /* address */ - t_uscalar_t soa_len; /* length in bytes */ - t_uscalar_t soa_maxlen; /* maximum length */ -}; - -typedef struct sotpi_info { - dev_t sti_dev; /* sonode device */ - struct soaddr sti_laddr; /* local address */ - struct soaddr sti_faddr; /* peer address */ - struct so_ux_addr sti_ux_laddr; /* bound local address */ - struct so_ux_addr sti_ux_faddr; /* bound peer address */ - t_scalar_t sti_serv_type; /* service type */ -} sotpi_info_t; - -/* - * CTF definitions for sockfs - */ - -static int Sockfs_ctfs = 0; /* CTF initialization status for - * sockfs */ - -# if defined(_LP64) -# define SOCKFS_MOD_FORMAT "/kernel/%s/genunix" -# else /* !defined(_LP64) */ -# define SOCKFS_MOD_FORMAT "/kernel/fs/sockfs" -# endif /* defined(_LP64) */ - -/* sockfs module pathname template to - * which the kernel's instruction type - * set is added for CTF access */ - -/* - * Sockfs access definitions and structures - */ - -# define SOADDR_TYPE_NAME "soaddr" - -static CTF_member_t soaddr_members[] = {CTF_MEMBER(soa_sa), -# define MX_soa_sa 0 - - CTF_MEMBER(soa_len), -# define MX_soa_len 1 - - CTF_MEMBER(soa_maxlen), -# define MX_soa_maxlen 2 - - {NULL, 0}}; - -# define SOTPI_INFO_TYPE_NAME "sotpi_info_t" - -static CTF_member_t sotpi_info_members[] = {CTF_MEMBER(sti_dev), -# define MX_sti_dev 0 - - CTF_MEMBER(sti_laddr), -# define MX_sti_laddr 1 - - CTF_MEMBER(sti_faddr), -# define MX_sti_faddr 2 - - CTF_MEMBER(sti_ux_laddr), -# define MX_sti_ux_laddr 3 - - CTF_MEMBER(sti_ux_faddr), -# define MX_sti_ux_faddr 4 - - CTF_MEMBER(sti_serv_type), -# define MX_sti_serv_type 5 - - {NULL, 0}}; - -/* - * CTF sockfs request table - */ - -static CTF_request_t Sockfs_requests[] = { - {SOTPI_INFO_TYPE_NAME, sotpi_info_members}, {NULL, NULL}}; - -/* - * Sockfs function prototypes - */ - -static char *printsockty(int ty); -static int read_nsti(struct lsof_context *ctx, struct sonode *so, - sotpi_info_t *stpi); -#endif /* solaris>=110000 && defined(HAS_LIBCTF) */ - -#if defined(HAS_ZFS) && defined(HAS_LIBCTF) -/* - * ZFS support via libctf - */ - -/* - * ZFS internal structure definitions - * - * The structure definitions may look like kernel structures, but they are - * not. They have been defined to have member names that duplicate those - * used by the kernel that are of interest to lsof. Member values are - * obtained via the CTF library, libctf. - * - * Robert Byrnes developed the CTF library access code and contributed it - * to lsof. - */ - -typedef struct zfsvfs { - vfs_t *z_vfs; /* pointer to VFS */ -} zfsvfs_t; - -typedef struct znode_phys { - uint64_t zp_size; /* file size (ZFS below 5) */ - uint64_t zp_links; /* links (ZFS below 5) */ -} znode_phys_t; - -typedef struct znode { - zfsvfs_t *z_zfsvfs; /* pointer to associated vfs */ - vnode_t *z_vnode; /* pointer to associated vnode */ - uint64_t z_id; /* node ID */ - znode_phys_t *z_phys; /* pointer to persistent znode (ZFS - * below 5) */ - uint64_t z_links; /* links (ZFS 5 and above) */ - uint64_t z_size; /* file size (ZFS 5 and above) */ -} znode_t; - -/* - * CTF definitions for ZFS - */ - -static int ZFS_ctfs = 0; /* CTF initialization status for ZFS */ - -# if defined(_LP64) -# define ZFS_MOD_FORMAT "/kernel/fs/%s/zfs" -# else /* !defined(_LP64) */ -# define ZFS_MOD_FORMAT "/kernel/fs/zfs" -# endif /* defined(_LP64) */ - -/* ZFS module pathname template to - * which the kernel's instruction type - * set is added for CTF access */ - -/* - * ZFS access definitions and structures - */ - -# define ZNODE_TYPE_NAME "znode_t" - -static CTF_member_t znode_members[] = {CTF_MEMBER(z_zfsvfs), -# define MX_z_zfsvfs 0 - - CTF_MEMBER(z_vnode), -# define MX_z_vnode 1 - - CTF_MEMBER(z_id), -# define MX_z_id 2 - - CTF_MEMBER(z_link_node), -# define MX_z_link_node 3 - - CTF_MEMBER(z_phys), -# define MX_z_phys 4 - - CTF_MEMBER(z_links), -# define MX_z_links 5 - - CTF_MEMBER(z_size), -# define MX_z_size 6 - - {NULL, 0}}; - -# define ZNODE_PHYS_TYPE_NAME "znode_phys_t" - -static CTF_member_t znode_phys_members[] = {CTF_MEMBER(zp_size), -# define MX_zp_size 0 - - CTF_MEMBER(zp_links), -# define MX_zp_links 1 - - {NULL, 0}}; - -# define ZFSVFS_TYPE_NAME "zfsvfs_t" - -static CTF_member_t zfsvfs_members[] = {CTF_MEMBER(z_vfs), -# define MX_z_vfs 0 - - {NULL, 0}}; - -/* - * CTF ZFS request table - */ - -static CTF_request_t ZFS_requests[] = { - {ZNODE_TYPE_NAME, znode_members}, - {ZNODE_PHYS_TYPE_NAME, znode_phys_members}, - {ZFSVFS_TYPE_NAME, zfsvfs_members}, - {NULL, NULL}}; - -/* - * Missing members exceptions -- i.e., CTF_getmem won't consider it - * an error if any of these members are undefined. - */ - -typedef struct CTF_exception { - char *tynm; /* type name */ - char *memnm; /* member name */ -} CTF_exception_t; - -static CTF_exception_t CTF_exceptions[] = {{ZNODE_TYPE_NAME, "z_phys"}, - {ZNODE_TYPE_NAME, "z_links"}, - {ZNODE_TYPE_NAME, "z_size"}, - {NULL, NULL}}; - -/* - * ZFS function prototypes - */ - -static int read_nzn(struct lsof_context *ctx, KA_T na, KA_T nza, znode_t *z); -static int read_nznp(struct lsof_context *ctx, KA_T nza, KA_T nzpa, - znode_phys_t *zp); -static int read_nzvfs(struct lsof_context *ctx, KA_T nza, KA_T nzva, - zfsvfs_t *zv); -#endif /* defined(HAS_ZFS) && defined(HAS_LIBCTF) */ - -static struct l_dev *finddev(struct lsof_context *ctx, dev_t *dev, dev_t *rdev, - int flags); - -/* - * Finddev() "look-in " flags - */ - -#define LOOKDEV_TAB 0x01 /* look in device table */ -#define LOOKDEV_CLONE 0x02 /* look in Clone table */ -#define LOOKDEV_PSEUDO 0x04 /* look in Pseudo table */ -#define LOOKDEV_ALL (LOOKDEV_TAB | LOOKDEV_CLONE | LOOKDEV_PSEUDO) -/* look all places */ - -/* - * SAM-FS definitions - */ - -#define SAMFS_NMA_MSG "(limited SAM-FS info)" - -/* - * Voptab definitions - */ - -typedef struct build_v_optab { - char *dnm; /* drive_NL name */ - char *fsys; /* file system type name */ - int nty; /* node type index (i.e., N_*) */ -} build_v_optab_t; - -static build_v_optab_t Build_v_optab[] = { - {"auvops", "autofs", N_AUTO}, - {"avops", "afs", N_AFS}, - {"afsops", "afs", N_AFS}, - {"ctfsadir", NULL, N_CTFSADIR}, - {"ctfsbund", NULL, N_CTFSBUND}, - {"ctfscdir", NULL, N_CTFSCDIR}, - {"ctfsctl", NULL, N_CTFSCTL}, - {"ctfsevt", NULL, N_CTFSEVT}, - {"ctfslate", NULL, N_CTFSLATE}, - {"ctfsroot", NULL, N_CTFSROOT}, - {"ctfsstat", NULL, N_CTFSSTAT}, - {"ctfssym", NULL, N_CTFSSYM}, - {"ctfstdir", NULL, N_CTFSTDIR}, - {"ctfstmpl", NULL, N_CTFSTMPL}, - -#if defined(HASCACHEFS) - {"cvops", NULL, N_CACHE}, -#endif /* defined(HASCACHEFS) */ - - {"devops", "devfs", N_DEV}, - {"doorops", NULL, N_DOOR}, - {"fdops", "fd", N_FD}, - {"fd_ops", "fd", N_FD}, - {"fvops", "fifofs", N_FIFO}, - {"hvops", "hsfs", N_HSFS}, - {"lvops", "lofs", N_LOFS}, - {"mntops", "mntfs", N_MNT}, - {"mvops", "mvfs", N_MVFS}, - {"n3vops", NULL, N_NFS}, - -#if solaris >= 100000 - {"n4vops", NULL, N_NFS4}, -#else /* solaris<100000 */ - {"n4vops", NULL, N_NFS}, -#endif /* solaris>=100000 */ - - {"nmvops", "namefs", N_NM}, - {"nvops", NULL, N_NFS}, - {"pdvops", "pcfs", N_PCFS}, - {"pfvops", "pcfs", N_PCFS}, - {"portvops", NULL, N_PORT}, - {"prvops", "proc", N_PROC}, - {"sam1vops", NULL, N_SAMFS}, - {"sam2vops", NULL, N_SAMFS}, - {"sam3vops", NULL, N_SAMFS}, - {"sam4vops", NULL, N_SAMFS}, - {"sckvops", "sockfs", N_SOCK}, - {"devipnetops", "sdevfs", N_SDEV}, - {"devnetops", "sdevfs", N_SDEV}, - {"devptsops", "sdevfs", N_SDEV}, - {"devvtops", "sdevfs", N_SDEV}, - {"socketvops", "sockfs", N_SOCK}, - {"sdevops", "sdevfs", N_SDEV}, - {"shvops", "sharedfs", N_SHARED}, - {"sncavops", "sockfs", N_SOCK}, - {"stpivops", "sockfs", N_SOCK}, - {"spvops", "specfs", N_REGLR}, - {"tvops", "tmpfs", N_TMP}, - {"uvops", "ufs", N_REGLR}, - {"vvfclops", "vxfs", N_VXFS}, - {"vvfops", "vxfs", N_VXFS}, - {"vvfcops", "vxfs", N_VXFS}, - {"vvops", "vxfs", N_VXFS}, - {"vvops_p", "vxfs", N_VXFS}, - {"zfsdops", "zfs", N_ZFS}, - {"zfseops", "zfs", N_ZFS}, - {"zfsfops", "zfs", N_ZFS}, - {"zfsshops", "zfs", N_ZFS}, - {"zfssymops", "zfs", N_ZFS}, - {"zfsxdops", "zfs", N_ZFS}, - {NULL, NULL, 0} /* table end */ -}; - -typedef struct v_optab { - char *fsys; /* file system type name */ - int fx; /* Fsinfo[] index (-1 if none) */ - int nty; /* node type index (i.e., N_*) */ - KA_T v_op; /* vnodeops address */ - struct v_optab *next; /* next entry */ -} v_optab_t; - -static v_optab_t **FxToVoptab = (v_optab_t **)NULL; -/* table to convert file system index - * to Voptab address[] -- built by - * build_Voptab() */ -static v_optab_t **Voptab = (v_optab_t **)NULL; -/* table to convert vnode v_op - * addresses to file system name and - * node type -- built by build_Voptab() - * and addressed through the HASHVOP() - * macro */ - -#define VOPHASHBINS \ - 256 /* number of Voptab[] hash bins -- \ - * MUST BE A POWER OF TWO! */ - -/* - * Local function prototypes - */ - -static void build_Voptab(struct lsof_context *ctx); -static enum lsof_lock_mode isvlocked(struct lsof_context *ctx, - struct vnode *va); -static int readinode(struct lsof_context *ctx, KA_T ia, struct inode *i); -static void read_mi(struct lsof_context *ctx, KA_T s, dev_t *dev, caddr_t so, - int *so_st, KA_T *so_ad, struct l_dev **sdp); - -#if solaris >= 20500 -# if solaris >= 20600 -static int read_nan(struct lsof_context *ctx, KA_T na, KA_T aa, - struct fnnode *rn); -static int read_nson(struct lsof_context *ctx, KA_T na, KA_T sa, - struct sonode *sn); -static int read_nusa(struct lsof_context *ctx, struct soaddr *so, - struct sockaddr_un *ua); -# else /* solaris<20600 */ -static int read_nan(struct lsof_context *ctx, KA_T na, KA_T aa, - struct autonode *a); -# endif /* solaris>=20600 */ -static int idoorkeep(struct lsof_context *ctx, struct door_node *d); -static int read_ndn(struct lsof_context *ctx, KA_T na, KA_T da, - struct door_node *d); -#endif /* solaris>=20500 */ - -#if solaris >= 110000 -static int read_nsdn(struct lsof_context *ctx, KA_T na, KA_T sa, - struct sdev_node *sdn, struct vattr *sdva); -#endif /* solaris>=110000 */ - -static int read_nfn(struct lsof_context *ctx, KA_T na, KA_T fa, - struct fifonode *f); -static int read_nhn(struct lsof_context *ctx, KA_T na, KA_T ha, - struct hsnode *h); -static int read_nin(struct lsof_context *ctx, KA_T na, KA_T ia, - struct inode *i); -static int read_nmn(struct lsof_context *ctx, KA_T na, KA_T ia, - struct mvfsnode *m); -static int read_npn(struct lsof_context *ctx, KA_T na, KA_T pa, - struct pcnode *p); -static int read_nrn(struct lsof_context *ctx, KA_T na, KA_T ra, - struct rnode *r); - -#if solaris >= 100000 -static int read_nctfsn(struct lsof_context *ctx, int ty, KA_T na, KA_T ca, - char *cn); -static int read_nprtn(struct lsof_context *ctx, KA_T na, KA_T ra, port_t *p); -static int read_nrn4(struct lsof_context *ctx, KA_T na, KA_T ra, - struct rnode4 *r); -#endif /* solaris>=100000 */ - -static int read_nsn(struct lsof_context *ctx, KA_T na, KA_T sa, - struct snode *s); -static int read_ntn(struct lsof_context *ctx, KA_T na, KA_T ta, - struct tmpnode *t); -static int read_nvn(struct lsof_context *ctx, KA_T na, KA_T va, - struct vnode *v); - -#if defined(HASPROCFS) -static int read_npi(struct lsof_context *ctx, KA_T na, struct vnode *v, - struct pid *pids); -#endif /* defined(HASPROCFS) */ - -static char *ent_fa(KA_T *a1, KA_T *a2, char *d, int *len); -static int is_socket(struct lsof_context *ctx, struct vnode *v); -static int read_cni(struct lsof_context *ctx, struct snode *s, struct vnode *rv, - struct vnode *v, struct snode *rs, struct dev_info *di, - char *din, int dinl); - -#if defined(HASCACHEFS) -static int read_ncn(struct lsof_context *ctx, KA_T na, KA_T ca, - struct cnode *cn); -#endif /* defined(HASCACHEFS) */ - -static int read_nln(struct lsof_context *ctx, KA_T na, KA_T la, - struct lnode *ln); -static int read_nnn(struct lsof_context *ctx, KA_T na, KA_T nna, - struct namenode *n); - -#if solaris < 100000 -static void savesockmod(struct so_so *so, struct so_so *sop, int *so_st); -#else /* solaris>=100000 */ -static int read_ndvn(struct lsof_context *ctx, KA_T na, KA_T da, - struct dv_node *dv, dev_t *dev, unsigned char *devs); -#endif /* solaris<100000 */ - -/* - * Local static values - */ - -static KA_T Spvops = (KA_T)0; /* specfs vnodeops address -- saved - * by build_Voptab() */ -static KA_T Vvops[VXVOP_NUM]; /* addresses of: - * vx_fcl_dnodeops_p (VXVOP_FCL) - * fdd_vnops (VXVOP_FDD) - * fdd_chain_vnops (VXVOP_FDDCH), - * vx_vnodeops (VXVOP_REG) - * vx_vnodeops_p (VXVOP_REG_P) - * -- saved by build_Voptab() */ - -/* - * Local macros - * - * GETVOPS() -- get direct or indirect *vnodeops address - * - * HASHVOP() -- hash the vnode's v_op address - */ - -#if defined(VOPNAME_OPEN) && solaris >= 100000 -# define GETVOPS(name, nl, ops) \ - if (get_Nl_value(ctx, name, nl, &ops) < 0) \ - ops = (KA_T)0; \ - else if (kread(ctx, ops, (char *)&ops, sizeof(ops))) \ - ops = (KA_T)0 -#else /* !defined(VOPNAME_OPEN) || solaris<100000 */ -# define GETVOPS(name, nl, ops) \ - if (get_Nl_value(ctx, name, nl, &ops) < 0) \ - ops = (KA_T)0 -#endif /* defined(VOPNAME_OPEN) && solaris>=100000 */ - -#define HASHVOP(ka) \ - ((int)((((ka & 0x1fffffff) * 31415) >> 3) & (VOPHASHBINS - 1))) - -/* - * build_Voptab() -- build Voptab[] - */ - -static void build_Voptab(struct lsof_context *ctx) { - build_v_optab_t *bp; /* Build_v_optab[] pointer */ - int fx; /* temporary file system type index */ - int h; /* hash index */ - int i, j; /* temporary indexes */ - KA_T ka; /* temporary kernel address */ - v_optab_t *nv, *vp, *vpp; /* Voptab[] working pointers */ - int vv = 0; /* number of Vvops[] addresses that - * have been located */ - /* - * If Voptab[] is allocated, return; otherwise allocate space for Voptab[] - * and FxToVoptab[] amd fill them. - */ - if (Voptab) - return; - /* - * During first call, allocate space for Voptab[] and FxToVoptab[]. - */ - - if (!(Voptab = - (v_optab_t **)calloc((MALLOC_S)VOPHASHBINS, sizeof(v_optab_t)))) { - (void)fprintf(stderr, "%s: no space for Voptab\n", Pn); - Error(ctx); - } - if (!(FxToVoptab = - (v_optab_t **)calloc((MALLOC_S)Fsinfomax, sizeof(v_optab_t *)))) { - (void)fprintf(stderr, "%s: no space for FxToVoptab\n", Pn); - Error(ctx); - } - for (i = 0; i < VXVOP_NUM; i++) { - Vvops[i] = (KA_T)NULL; - } - /* - * Use Build_v_optab[] to build Voptab[]. - */ - for (bp = Build_v_optab; bp->dnm; bp++) { - - /* - * Get the kernel address for the symbol. Do nothing if it can't - * be determined. - */ - GETVOPS(bp->dnm, Drive_Nl, ka); - if (!ka) - continue; - /* - * Check the Voptab[] for the address. - */ - h = HASHVOP(ka); - for (vp = Voptab[h], vpp = (v_optab_t *)NULL; vp; vp = vp->next) { - if (vp->v_op == ka) - break; - vpp = vp; - } - if (vp) { - - /* - * Ignore duplicates. - */ - continue; - } - /* - * No Voptab[] entry was found, so allocate space for a new - * v_optab_t structure, determine its file system type index, - * fill it and link it to the Voptab[]. - */ - if (!(nv = (v_optab_t *)malloc((MALLOC_S)sizeof(v_optab_t)))) { - (void)fprintf(stderr, "%s: out of Voptab space at: %s\n", Pn, - bp->dnm); - Error(ctx); - } - nv->fsys = bp->fsys; - nv->fx = -1; - nv->nty = bp->nty; - nv->next = (v_optab_t *)NULL; - nv->v_op = ka; - if (bp->fsys) { - for (i = 0; i < Fsinfomax; i++) { - if (!strcmp(bp->fsys, Fsinfo[i])) { - nv->fx = i; - break; - } - } - } - if (!Voptab[h]) - Voptab[h] = nv; - else - vpp->next = nv; - /* - * Handle special v_op addresses: - * - * special vnode ops; - * VxFS ops. - */ - if (!Spvops) { - if (!strcmp(bp->dnm, "spvops")) - Spvops = ka; - } - for (i = 0; (i < VXVOP_NUM) && (vv < VXVOP_NUM); i++) { - if (Vvops[i]) - continue; - switch (i) { - case VXVOP_FCL: - if (!strcmp(bp->dnm, "vvfclops")) { - Vvops[i] = ka; - vv++; - } - break; - case VXVOP_FDD: - if (!strcmp(bp->dnm, "vvfops")) { - Vvops[i] = ka; - vv++; - } - break; - case VXVOP_FDDCH: - if (!strcmp(bp->dnm, "vvfcops")) { - Vvops[i] = ka; - vv++; - } - break; - case VXVOP_REG: - if (!strcmp(bp->dnm, "vvops")) { - Vvops[i] = ka; - vv++; - } - break; - case VXVOP_REG_P: - if (!strcmp(bp->dnm, "vvops_p")) { - Vvops[i] = ka; - vv++; - } - break; - } - } - } - /* - * Link Voptab[] entries to FxToVoptab[] entries. - */ - for (h = 0; h < VOPHASHBINS; h++) { - for (vp = Voptab[h]; vp; vp = vp->next) { - if (!vp->fsys) - continue; - if (((fx = vp->fx) >= 0) && (fx < Fsinfomax)) { - if (!FxToVoptab[fx]) - FxToVoptab[fx] = vp; - continue; - } - for (i = 0; i < Fsinfomax; i++) { - if (!strcmp(Fsinfo[i], vp->fsys)) { - vp->fx = i; - if (!FxToVoptab[i]) - FxToVoptab[i] = vp; - break; - } - } - } - } -} - -#if defined(HAS_LIBCTF) -/* - * CTF_getmem() -- get CTF members - */ - -int CTF_getmem(struct lsof_context *ctx, /* context*/ - ctf_file_t *f, /* CTF file handle */ - const char *mod, /* module name */ - const char *ty, /* type */ - CTF_member_t *mem) /* member table */ -{ - int err; /* error flag */ - ctf_id_t id; /* CTF ID */ - CTF_member_t *mp; /* member pointer */ - CTF_exception_t *xp; /* exception table pointer */ - int xs; /* exception status */ - /* - * Look up the type. - */ - if ((id = ctf_lookup_by_name(f, ty)) == CTF_ERR) { - (void)fprintf(stderr, "%s: ctf_lookup_by_name: %s: %s: %s\n", Pn, mod, - ty, ctf_errmsg(ctf_errno(f))); - return (1); - } - /* - * Get member offsets. - */ - if (ctf_member_iter(f, id, CTF_memCB, mem) == CTF_ERR) { - (void)fprintf(stderr, "%s: ctf_member_iter: %s: %s: %s\n", Pn, mod, ty, - ctf_errmsg(ctf_errno(f))); - return (1); - } - /* - * Examine members. - */ - for (err = 0, mp = mem; mp->m_name; mp++) { - if (mp->m_offset == CTF_MEMBER_UNDEF) { - - /* - * Check for an undefined member exception. Report an error if - * no exception is found. - */ - for (xp = CTF_exceptions, xs = 0; xp->tynm; xp++) { - if (!strcmp(xp->tynm, ty) && !strcmp(xp->memnm, mp->m_name)) { - xs = 1; - break; - } - } - if (!xs) { - (void)fprintf( - stderr, - "%s: getmembers: %s: %s: %s: struct member undefined\n", Pn, - mod, ty, mp->m_name); - err = 1; - } - } else { - - /* - * Convert bit offsets to byte offsets. - */ - if ((mp->m_offset % NBBY) != 0) { - (void)fprintf( - stderr, - "%s: getmembers: %s: %s: %s: struct member is bit field\n", - Pn, mod, ty, mp->m_name); - err = 1; - } else - mp->m_offset /= NBBY; - } - } - return (err); +void process_node(struct lsof_context *ctx, KA_T f) { + (void)ctx; + (void)f; } -/* - * CTF_init - initialize CTF library access - */ - -void CTF_init(struct lsof_context *ctx, /* context */ - int *i, /* initialization status */ - char *t, /* kernel module template */ - CTF_request_t *r) /* CTF requests */ -{ - int err; /* error status */ - ctf_file_t *f; /* CTF file info handle */ - -# if defined(_LP64) - static char isa[256 + 1]; /* kernel instruction set name */ - static int isas = 0; /* isa[] status */ -# endif /* defined(_LP64) */ - - char kernmod[MAXPATHLEN]; /* kernel module pathname */ - char *kmp; /* kernel module path name pointer */ - static char pfn[256 + 1]; /* system platform name */ - static int pfns = 0; /* pfn[] status: -1 = request failed - * 0 = none requested - * >0 = available */ - char pfxkernmod[MAXPATHLEN]; /* prefixed kernel module name */ - struct stat sb; /* stat(2) buffer */ - - if (*i) - return; - -# if defined(_LP64) - /* - * If CTF access hasn't been initialized and a 64 bit kernel is in use, - * determine the name of the kernel's instruction set, and construct the - * pathname of the kernel module, using the supplied template. - */ - if (!isas) { - if (sysinfo(SI_ARCHITECTURE_K, isa, sizeof(isa) - 1) == -1) { - (void)fprintf(stderr, "%s: sysinfo: %s\n", Pn, strerror(errno)); - Error(ctx); - } - isas = 1; - isa[sizeof(isa) - 1] = '\0'; - } - (void)snprintf(kernmod, sizeof(kernmod) - 1, t, isa); - kernmod[sizeof(kernmod) - 1] = '\0'; -# else /* !defined(_LP64) */ - /* - * If CTF access hasn't been initialized and a 32 bit kernel is in use, the - * supplied template is the module path name. - */ - (void)strncpy(kernmod, t, sizeof(kernmod) - 1); -# endif /* defined(_LP64) */ - - kernmod[sizeof(kernmod) - 1] = '\0'; - kmp = kernmod; - if (statsafely(ctx, kmp, &sb)) { - - /* - * The module at the specified path does not exist or is inaccessible. - * - * Get the platform name and construct a prefix from it for module path - * name and see if that exists and is accessible. - * - * If it is, let CTF_init() use it; otherwise let CTF_init() fail on - * the specified path. - */ - if (pfns >= 0) { - if (!pfns) - pfns = sysinfo(SI_MACHINE, pfn, sizeof(pfn) - 1); - if (pfns > 0) { - pfn[sizeof(pfn) - 1] = '\0'; - (void)snprintf(pfxkernmod, sizeof(pfxkernmod) - 1, - "/platform/%s/%s", pfn, - (kernmod[0] == '/') ? &kernmod[1] : kernmod); - pfxkernmod[sizeof(pfxkernmod) - 1] = '\0'; - if (!stat(pfxkernmod, &sb)) - kmp = pfxkernmod; - } - } - } - /* - * Open the module file and read its CTF info. - */ - if ((f = ctf_open(kmp, &err)) == NULL) { - (void)fprintf(stderr, "%s: ctf_open: %s: %s\n", Pn, kmp, - ctf_errmsg(err)); - /* - * CTF data may be missing from a kernel module (for example in a - * Solaris CBE image whose ZFS module has no CTF section). Treat - * that as unavailable rather than terminating lsof: callers must - * check the state before using the member offsets. - */ - *i = -1; - return; - } - for (err = 0; r->name; r++) { - if (CTF_getmem(ctx, f, kmp, r->name, r->mem)) - err = 1; - } - (void)ctf_close(f); - if (err) { - *i = -1; - return; - } - *i = 1; +int vop2ty(struct lsof_context *ctx, struct vnode *vp, int fx) { + (void)ctx; + (void)vp; + (void)fx; + return N_REGLR; } -/* - * CTF_memCB() - Callback function for ctf_member_iter() - */ - -int CTF_memCB(const char *name, /* structure member name */ - ctf_id_t id, /* CTF ID */ - ulong_t offset, /* member offset */ - void *arg) /* member table */ -{ - CTF_member_t *mp; - /* - * Check for members of interest and record their offsets. - */ - for (mp = (CTF_member_t *)arg; mp->m_name; mp++) { - if (!strcmp(name, mp->m_name)) { - mp->m_offset = offset; - break; - } - } - return (0); +void open_kvm(struct lsof_context *ctx) { + (void)ctx; } -#endif /* defined(HAS_LIBCTF) */ - -/* - * ent_fa() - enter fattach addresses in NAME column addition - */ -static char *ent_fa(KA_T *a1, /* first fattach address (NULL OK) */ - KA_T *a2, /* second fattach address */ - char *d, /* direction ("->" or "<-") */ - int *len) /* returned description length */ -{ - static char buf[1024]; - size_t bufl = sizeof(buf); - char tbuf[32]; - /* - * Form the fattach description. - */ - if (!a1) - -#if solaris < 20600 - (void)snpf(buf, bufl, "(FA:%s%s)", d, print_kptr(*a2, (char *)NULL, 0)); -#else /* solaris>=20600 */ - (void)snpf(buf, bufl, "(FA:%s%s)", d, print_kptr(*a2, (char *)NULL, 0)); -#endif /* solaris<20600 */ - - else - -#if solaris < 20600 - (void)snpf(buf, bufl, "(FA:%s%s%s)", - print_kptr(*a1, tbuf, sizeof(tbuf)), d, - print_kptr(*a2, (char *)NULL, 0)); -#else /* solaris>=20600 */ - (void)snpf(buf, bufl, "(FA:%s%s%s)", - print_kptr(*a1, tbuf, sizeof(tbuf)), d, - print_kptr(*a2, (char *)NULL, 0)); -#endif /* solaris<20600 */ - - *len = (int)strlen(buf); - return (buf); +void close_kvm(struct lsof_context *ctx) { + (void)ctx; } -/* - * is_socket() - is the stream a socket? - */ - -static int is_socket(struct lsof_context *ctx, /* context */ - struct vnode *v) /* vnode pointer */ -{ - char *cp, *ep, *pf; - int i, j, len, n, pfl; - major_t maj; - minor_t min; - static struct tcpudp { - int ds; - major_t maj; - minor_t min; - char *proto; - } tcpudp[] = { - {0, 0, 0, "tcp"}, - {0, 0, 0, "udp"}, - -#if defined(HASIPv6) - {0, 0, 0, "tcp6"}, - {0, 0, 0, "udp6"}, -#endif /* defined(HASIPv6) */ - - }; -#define NTCPUDP (sizeof(tcpudp) / sizeof(struct tcpudp)) - - static int tcpudps = 0; - - if (!v->v_stream) - return (0); - maj = (major_t)GET_MAJ_DEV(v->v_rdev); - min = (minor_t)GET_MIN_DEV(v->v_rdev); - /* - * Fill in tcpudp[], as required. - */ - if (!tcpudps) { - -#if solaris < 80000 - pf = "/devices/pseudo/clone"; -#else /* solaris>=80000 */ - pf = "/devices/pseudo/"; -#endif /* solaris<80000 */ - - for (i = n = 0, pfl = (int)strlen(pf); (i < Ndev) && (n < NTCPUDP); - i++) { - if (strncmp(Devtp[i].name, pf, pfl) || - !(ep = strrchr((cp = &Devtp[i].name[pfl]), ':')) || - (strncmp(++ep, "tcp", 3) && strncmp(ep, "udp", 3))) - continue; - -#if solaris < 80000 - if (*(ep + 3)) -#else /* solaris>=80000 */ - len = (*(ep + 3) == '6') ? 4 : 3; - if (*(ep + len) || ((cp + len) >= ep) || strncmp(cp, ep, len)) -#endif /* solaris<80000 */ - - continue; - for (j = 0; j < NTCPUDP; j++) { - if (!tcpudp[j].ds && !strcmp(ep, tcpudp[j].proto)) { - tcpudp[j].ds = 1; - tcpudp[j].maj = (major_t)GET_MAJ_DEV(Devtp[i].rdev); - tcpudp[j].min = (minor_t)GET_MIN_DEV(Devtp[i].rdev); - n++; - break; - } - } - } - tcpudps = n ? 1 : -1; - } - /* - * Check for known IPv[46] TCP or UDP device. - */ - for (i = 0; (i < NTCPUDP) && (tcpudps > 0); i++) { - if (tcpudp[i].ds - -#if solaris < 80000 - && (maj == tcpudp[i].min) -#else /* solaris>=80000 */ - && (maj == tcpudp[i].maj) -#endif /* solaris<80000 */ - - ) { - process_socket(ctx, (KA_T)v->v_stream, tcpudp[i].proto); - return (1); - } - } - return (0); +int kread(struct lsof_context *ctx, KA_T addr, char *buf, READLEN_T len) { + (void)ctx; + (void)addr; + (void)buf; + (void)len; + return (1); } -/* - * isvlocked() - is Solaris vnode locked? - */ - -static enum lsof_lock_mode isvlocked(struct lsof_context *ctx, /* context */ - struct vnode *va) /* local vnode address */ -{ - -#if solaris < 20500 - struct filock f; - KA_T ff; - KA_T fp; -#endif /* solaris<20500 */ - - int i, l; - -#if solaris >= 20300 - struct lock_descriptor ld; - KA_T lf; - KA_T lp; -# if solaris < 20500 -# define LOCK_END ld.info.li_sleep.sli_flock.l_len -# define LOCK_FLAGS ld.flags -# define LOCK_NEXT ld.next -# define LOCK_OWNER ld.owner.pid -# define LOCK_START ld.start -# define LOCK_TYPE ld.type -# else /* solaris>=20500 */ -# define LOCK_END ld.l_flock.l_len -# define LOCK_FLAGS ld.l_state -# define LOCK_NEXT ld.l_next -# define LOCK_OWNER ld.l_flock.l_pid -# define LOCK_START ld.l_start -# define LOCK_TYPE ld.l_type -# endif /* solaris<20500 */ -#endif /* solaris>=20300 */ - - if (va->v_filocks == NULL) - return LSOF_LOCK_NONE; - -#if solaris < 20500 -# if solaris > 20300 || \ - (solaris == 20300 && defined(P101318) && P101318 >= 45) - if (Ntype == N_NFS) -# endif /* solaris>20300 || (solaris==20300 && defined(P101318) && \ - P101318>=45) */ - - { - ff = fp = (KA_T)va->v_filocks; - i = 0; - do { - if (kread(ctx, fp, (char *)&f, sizeof(f))) - return LSOF_LOCK_NONE; - i++; - if (f.set.l_pid != (pid_t)Lp->pid) - continue; - if (f.set.l_whence == 0 && f.set.l_start == 0 && - f.set.l_len == MAXEND) - l = 1; - else - l = 0; - switch (f.set.l_type & (F_RDLCK | F_WRLCK)) { - case F_RDLCK: - return l ? LSOF_LOCK_READ_FULL : LSOF_LOCK_READ_PARTIAL; - case F_WRLCK: - return l ? LSOF_LOCK_WRITE_FULL : LSOF_LOCK_WRITE_PARTIAL; - case F_RDLCK | F_WRLCK: - return LSOF_LOCK_READ_WRITE; - default: - return LSOF_LOCK_SOLARIS_NFS; - } - } while ((fp = (KA_T)f.next) && (fp != ff) && (i < 10000)); - } -#endif /* solaris<20500 */ - -#if solaris >= 20300 - lf = lp = (KA_T)va->v_filocks; - i = 0; - do { - if (kread(ctx, lp, (char *)&ld, sizeof(ld))) - return LSOF_LOCK_NONE; - i++; - if (!(LOCK_FLAGS & ACTIVE_LOCK) || LOCK_OWNER != (pid_t)Lp->pid) - continue; - if (LOCK_START == 0 && (LOCK_END == 0 - -# if solaris < 20500 - || LOCK_END == MAXEND -# else /* solaris>=20500 */ - || LOCK_END == MAXEND -# endif /* solaris<20500 */ - - )) - l = 1; - else - l = 0; - switch (LOCK_TYPE) { - case F_RDLCK: - return l ? LSOF_LOCK_READ_FULL : LSOF_LOCK_READ_PARTIAL; - case F_WRLCK: - return l ? LSOF_LOCK_WRITE_FULL : LSOF_LOCK_WRITE_PARTIAL; - case (F_RDLCK | F_WRLCK): - return LSOF_LOCK_READ_WRITE; - default: - /* It was 'L' since 1997, dunno what is it */ - return LSOF_LOCK_UNKNOWN; - } - } while ((lp = (KA_T)LOCK_NEXT) && (lp != lf) && (i < 10000)); - return LSOF_LOCK_NONE; -#endif /* solaris>=20300 */ +void initialize(struct lsof_context *ctx) { + (void)ctx; } -/* - * finddev() - look up device by device number - */ - -static struct l_dev *finddev(struct lsof_context *ctx, /* context */ - dev_t *dev, /* device */ - dev_t *rdev, /* raw device */ - int flags) /* look flags -- see LOOKDEV_* symbol - * definitions */ -{ - struct clone *c; - struct l_dev *dp; - struct pseudo *p; - - if (!Sdev) - readdev(ctx, 0); - /* - * Search device table for match. - */ - -#if defined(HASDCACHE) - -finddev_again: - -#endif /* defined(HASDCACHE) */ - - if (flags & LOOKDEV_TAB) { - if ((dp = lkupdev(ctx, dev, rdev, 0, 0))) - return (dp); - } - /* - * Search for clone. - */ - if ((flags & LOOKDEV_CLONE) && Clone) { - for (c = Clone; c; c = c->next) { - if (GET_MAJ_DEV(*rdev) == GET_MIN_DEV(c->cd.rdev)) { - -#if defined(HASDCACHE) - if (DCunsafe && !c->cd.v && !vfy_dev(ctx, &c->cd)) - goto finddev_again; -#endif /* defined(HASDCACHE) */ - - return (&c->cd); - } - } - } - /* - * Search for pseudo device match on major device only. - */ - if ((flags & LOOKDEV_PSEUDO) && Pseudo) { - for (p = Pseudo; p; p = p->next) { - if (GET_MAJ_DEV(*rdev) == GET_MAJ_DEV(p->pd.rdev)) { - -#if defined(HASDCACHE) - if (DCunsafe && !p->pd.v && !vfy_dev(ctx, &p->pd)) - goto finddev_again; -#endif /* defined(HASDCACHE) */ - - return (&p->pd); - } - } - } - return ((struct l_dev *)NULL); +int enter_zone_arg(struct lsof_context *ctx, char *zn) { + (void)ctx; + (void)zn; + return (0); } -#if solaris >= 20500 -/* - * idoorkeep() -- identify door keeper process - */ - -static int idoorkeep(struct lsof_context *ctx, /* context */ - struct door_node *d) /* door's node */ -{ - char buf[1024]; - size_t bufl = sizeof(buf); - struct proc dp; - struct pid dpid; - /* - * Get the proc structure and its pid structure for the door target. - */ - if (!d->door_target || - kread(ctx, (KA_T)d->door_target, (char *)&dp, sizeof(dp))) - return (0); - if (!dp.p_pidp || kread(ctx, (KA_T)dp.p_pidp, (char *)&dpid, sizeof(dpid))) - return (0); - /* - * Form a description of the door. - * - * Put the description in the NAME column addition field. If there's - * already something there, allocate more space and add the door description - * to it. - */ - if (Lp->pid == (int)dpid.pid_id) - (void)snpf(buf, bufl, "(this PID's door)"); - else { - (void)snpf(buf, bufl, "(door to %.64s[%ld])", dp.p_user.u_comm, - (long)dpid.pid_id); - } - (void)add_nma(ctx, buf, (int)strlen(buf)); - return (1); +void build_IPstates(struct lsof_context *ctx) { + (void)ctx; } -#endif /* solaris>=20500 */ - -/* - * process_node() - process vnode - */ - -void process_node(struct lsof_context *ctx, /* context */ - KA_T va) /* vnode kernel space address */ -{ - -#if defined(HASCACHEFS) - struct cnode cn; -#endif /* defined(HASCACHEFS) */ - - dev_t dev, rdev, trdev; - unsigned char devs = 0; - unsigned char fxs = 0; - unsigned char ins = 0; - unsigned char kvs = 0; - unsigned char nns = 0; - unsigned char pnl = 0; - unsigned char rdevs = 0; - unsigned char rvs = 0; - unsigned char rfxs = 0; - unsigned char sdns = 0; - unsigned char tdef; - unsigned char trdevs = 0; - unsigned char unix_sock = 0; - struct dev_info di; - char din[DINAMEL]; - char *ep; - struct fifonode f; - char *fa = (char *)NULL; - int fal; - static int ft = 1; - struct vnode fv, rv; - int fx, rfx; - struct hsnode h; - struct inode i; - int j; - KA_T ka, vka; - struct lnode lo; - struct vfs kv, rkv; - int len, llc, nl, snl, sepl; - struct mvfsnode m; - struct namenode nn; - struct l_vfs *nvfs, *vfs; - struct pcnode pc; - struct pcfs pcfs; - struct rnode r; - KA_T realvp = (KA_T)NULL; - struct snode rs; - struct snode s; - char fd[FDLEN]; - -#if solaris >= 110000 - char *nm, *sep; - size_t nmrl, tl; - struct sdev_node sdn; - struct vattr sdva; - sotpi_info_t sti; - int stis = 0; -#endif /* solaris>=110000 */ - - struct l_dev *sdp = (struct l_dev *)NULL; - size_t sz; - struct tmpnode t; - char tbuf[128], *ty, ubuf[128]; - int tbufx; - enum vtype type; - struct sockaddr_un ua; - static struct vnode *v = (struct vnode *)NULL; - KA_T vs; - int vty = 0; - int vty_tmp; - -#if solaris >= 20500 -# if solaris >= 20600 - struct fnnode fnn; - struct pairaddr { - short f; - unsigned short p; - } *pa; - KA_T peer; - struct sonode so; - KA_T soa, sona; -# else /* solaris<20600 */ - struct autonode au; -# endif /* solaris>=20600 */ - - struct door_node dn; - int dns = 0; -#endif /* solaris >=20500 */ - -#if solaris < 100000 - KA_T so_ad[2]; - struct so_so soso; - int so_st = 0; -#else /* solaris>=100000 */ - union { - ctfs_adirnode_t adir; - ctfs_bunode_t bun; - ctfs_cdirnode_t cdir; - ctfs_ctlnode_t ctl; - ctfs_evnode_t ev; - ctfs_latenode_t late; - ctfs_rootnode_t root; - ctfs_symnode_t sym; - ctfs_tdirnode_t tdir; - ctfs_tmplnode_t tmpl; - } ctfs; - dev_t dv_dev; - struct dv_node dv; - unsigned char dv_devs = 0; - unsigned char dvs = 0; - port_t pn; - struct rnode4 r4; -#endif /* solaris<100000 */ - -#if defined(HASPROCFS) - struct procfsid *pfi; - struct pid pids; -#endif /* defined(HASPROCFS) */ - -#if defined(HAS_AFS) - struct afsnode an; -#endif /* defined(HAS_AFS) */ - -#if defined(HASVXFS) - struct l_ino vx; -#endif /* defined(HASVXFS) */ - -#if defined(HAS_ZFS) - vfs_t zgvfs; - unsigned char zns = 0; - znode_t zn; - zfsvfs_t zvfs; -#endif /* defined(HAS_ZFS) */ - - /* - * Do first-time only operations. - */ - -#if solaris < 100000 - so_ad[0] = so_ad[1] = (KA_T)0; -#endif /* solaris<100000 */ - - if (ft) { - (void)build_Voptab(ctx); - ft = 0; - } - /* - * Read the vnode. - */ - if (!va) { - enter_nm(ctx, "no vnode address"); - return; - } - if (!v) { - - /* - * Allocate space for the vnode or AFS vcache structure. - */ - -#if defined(HAS_AFS) - v = alloc_vcache(); -#else /* !defined(HAS_AFS) */ - v = (struct vnode *)malloc(sizeof(struct vnode)); -#endif /* defined(HAS_AFS) */ - - if (!v) { - (void)fprintf(stderr, "%s: can't allocate %s space\n", Pn, - -#if defined(HAS_AFS) - "vcache" -#else /* !defined(HAS_AFS) */ - "vnode" -#endif /* defined(HAS_AFS) */ - - ); - Error(ctx); - } - } - if (readvnode(ctx, va, v)) { - enter_nm(ctx, Namech); - return; - } - -#if defined(HASNCACHE) - Lf->na = va; -#endif /* defined(HASNCACHE) */ - -#if defined(HASFSTRUCT) - Lf->fna = va; - Lf->fsv |= FSV_NI; -#endif /* defined(HASFSTRUCT) */ - -#if defined(HASLFILEADD) && defined(HAS_V_PATH) - Lf->V_path = (KA_T)v->v_path; -#endif /* defined(HASLFILEADD) && defined(HAS_V_PATH) */ - - vs = (KA_T)v->v_stream; - /* - * Check for a Solaris socket. - */ - if (is_socket(ctx, v)) - return; - /* - * Obtain the Solaris virtual file system structure. - */ - if ((ka = (KA_T)v->v_vfsp)) { - if (kread(ctx, ka, (char *)&kv, sizeof(kv))) { - vka = va; - - vfs_read_error: - - (void)snpf(Namech, Namechl - 1, "vnode at %s: can't read vfs: %s", - print_kptr(vka, tbuf, sizeof(tbuf)), - print_kptr(ka, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return; - } - kvs = 1; - } else - kvs = 0; - /* - * Derive the virtual file system structure's device number from - * its file system ID for NFS and High Sierra file systems. - */ - if (kvs && ((fx = kv.vfs_fstype - 1) >= 0) && (fx < Fsinfomax)) { - fxs = 1; - if (strcmp(Fsinfo[fx], "nfs") == 0 || strcmp(Fsinfo[fx], "nfs3") == 0 || - strcmp(Fsinfo[fx], "hsfs") == 0) - kv.vfs_dev = (dev_t)kv.vfs_fsid.val[0]; - } else { - fx = -1; - fxs = 0; - } - /* - * Determine the Solaris vnode type. - */ - if ((Ntype = vop2ty(ctx, v, fx)) < 0) { - if (v->v_type == VFIFO) { - vty = N_REGLR; - Ntype = N_FIFO; - } else if (vs) { - Ntype = vty = N_STREAM; - Lf->is_stream = 1; - } - if (Ntype < 0) { - (void)snpf(Namech, Namechl - 1, - "unknown file system type%s%s%s, v_op: %s", - fxs ? " (" : "", fxs ? Fsinfo[fx] : "", fxs ? ")" : "", - print_kptr((KA_T)v->v_op, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return; - } - } else { - vty = Ntype; - if (v->v_type == VFIFO) - Ntype = N_FIFO; - else if (vs && Ntype != N_SOCK) { - Ntype = vty = N_STREAM; - Lf->is_stream = 1; - } - } - /* - * See if this Solaris node has been fattach'ed to another node. - * If it has, read the namenode, and enter the node addresses in - * the NAME column addition. - * - * See if it's covering a socket as well and process accordingly. - */ - if (vty == N_NM) { - if (read_nnn(ctx, va, (KA_T)v->v_data, &nn)) - return; - nns = 1; - if (nn.nm_mountpt) - -#if solaris >= 20500 - fa = ent_fa( - (KA_T *)((Ntype == N_FIFO || v->v_type == VDOOR) ? NULL : &va), - (KA_T *)&nn.nm_mountpt, "->", &fal); -#else /* solaris<20500 */ - fa = ent_fa((KA_T *)((Ntype == N_FIFO) ? NULL : &va), - (KA_T *)&nn.nm_mountpt, "->", &fal); -#endif /* solaris>=20500 */ - - if (Ntype != N_FIFO && nn.nm_filevp && - !kread(ctx, (KA_T)nn.nm_filevp, (char *)&rv, sizeof(rv))) { - rvs = 1; - if ((ka = (KA_T)rv.v_vfsp) && - !kread(ctx, ka, (char *)&rkv, sizeof(rkv)) && - ((rfx = rkv.vfs_fstype - 1) >= 0) && (rfx < Fsinfomax)) { - rfxs = 1; - } else { - rfx = fx; - rfxs = fxs; - } - -#if defined(HASNCACHE) - Lf->na = (KA_T)nn.nm_filevp; -#endif /* defined(HASNCACHE) */ - if (is_socket(ctx, &rv)) - return; - } - } - if (Selinet && Ntype != N_SOCK) - return; - /* - * See if this Solaris node is served by spec_vnodeops. - */ - if (Spvops && Spvops == (KA_T)v->v_op) - Ntype = N_SPEC; - /* - * Determine the Solaris lock state. - */ - Lf->lock = isvlocked(ctx, v); - /* - * Establish the Solaris local virtual file system structure. - */ - if (!(ka = (KA_T)v->v_vfsp) || !kvs) - vfs = (struct l_vfs *)NULL; - else if (!(vfs = readvfs(ctx, ka, &kv, v))) { - vka = va; - goto vfs_read_error; - } - /* - * Read the afsnode, autonode, cnode, door_node, fifonode, fnnode, lnode, - * inode, pcnode, rnode, snode, tmpnode, znode, etc. - */ - switch (Ntype) { - case N_SPEC: - - /* - * A N_SPEC node is a node that resides in in an underlying file system - * type -- e.g. NFS, HSFS. Its vnode points to an snode. Subsequent - * node structures are implied by the underlying node type. - */ - if (read_nsn(ctx, va, (KA_T)v->v_data, &s)) - return; - realvp = (KA_T)s.s_realvp; - if (!realvp && s.s_commonvp) { - if (read_cni(ctx, &s, &rv, v, &rs, &di, din, sizeof(din)) == 1) - return; - if (!rv.v_stream) { - if (din[0]) { - (void)snpf(Namech, Namechl, "COMMON: %s", din); - Namech[Namechl - 1] = '\0'; - Lf->is_com = 1; - } - break; - } - } - if (!realvp) { - - /* - * If the snode lacks a real vnode (and also lacks a common vnode), - * it's original type is N_STREAM or N_REGLR, and it has a stream - * pointer, get the module names. - */ - if ((vty == N_STREAM || vty == N_REGLR) && vs) { - Lf->is_stream = 1; - vty = N_STREAM; - -#if solaris < 100000 - read_mi(ctx, vs, (dev_t *)&s.s_dev, (caddr_t)&soso, &so_st, - so_ad, &sdp); -#else /* solaris>=100000 */ - read_mi(ctx, vs, (dev_t *)&s.s_dev, NULL, NULL, NULL, &sdp); -#endif /* solaris<100000 */ - - vs = (KA_T)NULL; - } - } - break; - -#if defined(HAS_AFS) - case N_AFS: - if (readafsnode(ctx, va, v, &an)) - return; - break; -#endif /* defined(HAS_AFS) */ - -#if solaris >= 20500 - case N_AUTO: - -# if solaris < 20600 - if (read_nan(ctx, va, (KA_T)v->v_data, &au)) -# else /* solaris>=20600 */ - if (read_nan(ctx, va, (KA_T)v->v_data, &fnn)) -# endif /* solaris<20600 */ - - return; - break; - -# if solaris >= 100000 - case N_DEV: - if (read_ndvn(ctx, va, (KA_T)v->v_data, &dv, &dv_dev, &dv_devs)) - return; - dvs = 1; - break; -# endif /* solaris>=100000 */ - - case N_DOOR: - if (read_ndn(ctx, va, (KA_T)v->v_data, &dn)) - return; - dns = 1; - break; -#endif /* solaris>=20500 */ - -#if defined(HASCACHEFS) - case N_CACHE: - if (read_ncn(ctx, va, (KA_T)v->v_data, &cn)) - return; - break; -#endif /* defined(HASCACHEFS) */ - -#if solaris >= 100000 - case N_CTFSADIR: - case N_CTFSBUND: - case N_CTFSCDIR: - case N_CTFSCTL: - case N_CTFSEVT: - case N_CTFSLATE: - case N_CTFSROOT: - case N_CTFSSTAT: - case N_CTFSSYM: - case N_CTFSTDIR: - case N_CTFSTMPL: - if (read_nctfsn(ctx, Ntype, va, (KA_T)v->v_data, (char *)&ctfs)) - return; - break; -#endif /* solaris>=100000 */ - -#if solaris >= 20600 - case N_SOCK: - sona = (KA_T)v->v_data; - if (read_nson(ctx, va, sona, &so)) - return; - break; -#endif /* solaris>=20600 */ - - case N_MNT: - /* Information comes from the l_vfs structure. */ - break; - case N_MVFS: - if (read_nmn(ctx, va, (KA_T)v->v_data, &m)) - return; - break; - case N_NFS: - if (read_nrn(ctx, va, (KA_T)v->v_data, &r)) - return; - break; - -#if solaris >= 100000 - case N_NFS4: - if (read_nrn4(ctx, va, (KA_T)v->v_data, &r4)) - return; - break; -#endif /* solaris>=100000 */ - - case N_NM: - if (nns) - realvp = (KA_T)nn.nm_filevp; - -#if defined(HASNCACHE) - Lf->na = (KA_T)nn.nm_filevp; -#endif /* defined(HASNCACHE) */ - - break; - case N_FD: - break; /* no successor node */ - case N_FIFO: - - /* - * Solaris FIFO vnodes are usually linked to a fifonode. One - * exception is a FIFO vnode served by nm_vnodeops; it is linked - * to a namenode, and the namenode points to the fifonode. - * - * Non-pipe fifonodes are linked to a vnode thorough fn_realvp. - */ - if (vty == N_NM && nns) { - if (nn.nm_filevp) { - if (read_nfn(ctx, va, (KA_T)nn.nm_filevp, &f)) - return; - realvp = (KA_T)NULL; - vty = N_FIFO; - } else { - (void)snpf(Namech, Namechl - 1, - "FIFO namenode at %s: no fifonode pointer", - print_kptr((KA_T)v->v_data, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - return; - } - } else { - if (read_nfn(ctx, va, (KA_T)v->v_data, &f)) - return; - realvp = (KA_T)f.fn_realvp; - } - if (!realvp) { - Lf->inode = (INODETYPE)(nns ? nn.nm_vattr.va_nodeid : f.fn_ino); - -#if solaris >= 80000 /* Solaris 8 and above hack! */ -# if defined(_LP64) - if (Lf->inode >= (unsigned long)0xbaddcafebaddcafe) -# else /* !defined(_LP64) */ - if (Lf->inode >= (unsigned long)0xbaddcafe) -# endif /* defined(_LP64) */ - - Lf->inp_ty = 0; - else -#endif /* solaris>=80000 Solaris 8 and above hack! */ - - Lf->inp_ty = 1; - enter_dev_ch(ctx, print_kptr((KA_T)v->v_data, (char *)NULL, 0)); - if (f.fn_flag & ISPIPE) { - (void)snpf(tbuf, sizeof(tbuf), "PIPE"); - tbufx = (int)strlen(tbuf); - } else - tbufx = 0; - -#if solaris < 20500 - if (f.fn_mate) { - (void)snpf(&tbuf[tbufx], sizeof(tbuf) - tbufx, "->%s", - print_kptr((KA_T)f.fn_mate, (char *)NULL, 0)); - tbufx = (int)strlen(tbuf); - } -#else /* solaris>=20500 */ - if (f.fn_dest) { - (void)snpf(&tbuf[tbufx], sizeof(tbuf) - tbufx, "->%s", - print_kptr((KA_T)f.fn_dest, (char *)NULL, 0)); - tbufx = (int)strlen(tbuf); - } -#endif /* solaris<20500 */ - - if (tbufx) - (void)add_nma(ctx, tbuf, tbufx); - break; - } - break; - - case N_HSFS: - if (read_nhn(ctx, va, (KA_T)v->v_data, &h)) - return; - break; - case N_LOFS: - llc = 0; - do { - rvs = 0; - if (read_nln(ctx, va, llc ? (KA_T)rv.v_data : (KA_T)v->v_data, - &lo)) { - return; - } - if (!(realvp = (KA_T)lo.lo_vp)) { - (void)snpf(Namech, Namechl - 1, "lnode at %s: no real vnode", - print_kptr((KA_T)v->v_data, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return; - } - if (read_nvn(ctx, (KA_T)v->v_data, (KA_T)realvp, &rv)) - return; - rvs = 1; - llc++; - if ((ka = (KA_T)rv.v_vfsp) && - !kread(ctx, ka, (char *)&rkv, sizeof(rkv)) && - ((rfx = rkv.vfs_fstype - 1) >= 0) && (rfx < Fsinfomax)) { - rfxs = 1; - } else { - rfx = fx; - rfxs = fxs; - } - if (((vty_tmp = vop2ty(ctx, &rv, rfx)) == N_LOFS) && (llc > 1000)) { - (void)snpf(Namech, Namechl - 1, "lnode at %s: loop > 1000", - print_kptr((KA_T)v->v_data, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return; - } - } while (vty_tmp == N_LOFS); - break; - case N_PCFS: - if (read_npn(ctx, va, (KA_T)v->v_data, &pc)) - return; - break; - -#if solaris >= 100000 - case N_PORT: - if (read_nprtn(ctx, va, (KA_T)v->v_data, &pn)) - return; - break; -#endif /* solaris>=100000 */ - -#if defined(HASPROCFS) - case N_PROC: - if (read_npi(ctx, va, v, &pids)) - return; - break; -#endif /* defined(HASPROCFS) */ - -#if solaris >= 110000 - case N_SDEV: - if (read_nsdn(ctx, va, (KA_T)v->v_data, &sdn, &sdva)) - return; - sdns = 1; - break; -#endif /* solaris>=110000 */ - - case N_SAMFS: - (void)add_nma(ctx, SAMFS_NMA_MSG, (int)strlen(SAMFS_NMA_MSG)); - break; - case N_SHARED: - break; /* No more sharedfs information is available. */ - case N_STREAM: - if (read_nsn(ctx, va, (KA_T)v->v_data, &s)) - return; - if (vs) { - Lf->is_stream = 1; - -#if solaris < 100000 - read_mi(ctx, vs, (dev_t *)&s.s_dev, (caddr_t)&soso, &so_st, so_ad, - &sdp); -#else /* solaris>=100000 */ - read_mi(ctx, vs, (dev_t *)&s.s_dev, NULL, NULL, NULL, &sdp); -#endif /* solaris<100000 */ - - vs = (KA_T)NULL; - } - break; - case N_TMP: - if (read_ntn(ctx, va, (KA_T)v->v_data, &t)) - return; - break; - -#if defined(HASVXFS) - case N_VXFS: - if (read_vxnode(ctx, va, v, vfs, fx, &vx, Vvops)) - return; - break; -#endif /* defined(HASVXFS) */ - -#if defined(HAS_ZFS) - case N_ZFS: - if (read_nzn(ctx, va, (KA_T)v->v_data, &zn)) - return; - zns = 1; - break; -#endif /* defined(HAS_ZFS) */ - - case N_REGLR: - default: - if (read_nin(ctx, va, (KA_T)v->v_data, &i)) - return; - ins = 1; - } - /* - * If the node has a real vnode pointer, follow it. - */ - if (realvp) { - if (rvs) { - *v = rv; - fx = rfx; - fxs = rfxs; - } else { - if (read_nvn(ctx, (KA_T)v->v_data, (KA_T)realvp, v)) - return; - else { - -#if defined(HASNCACHE) - Lf->na = (KA_T)realvp; -#endif /* defined(HASNCACHE) */ - - if ((ka = (KA_T)v->v_vfsp) && - !kread(ctx, ka, (char *)&kv, sizeof(kv))) { - kvs = 1; - } - if (kvs && ((fx = kv.vfs_fstype - 1) >= 0) && - (fx < Fsinfomax)) { - fxs = 1; - } - } - } - /* - * If the original vnode type is N_STREAM, if there is a stream - * pointer and if there is no sdev_node, get the module names. - */ - if (vty == N_STREAM && vs && !sdns) { - Lf->is_stream = 1; - -#if solaris < 100000 - read_mi(ctx, vs, (dev_t *)&s.s_dev, (caddr_t)&soso, &so_st, so_ad, - &sdp); -#else /* solaris>=100000 */ - read_mi(ctx, vs, (dev_t *)&s.s_dev, NULL, NULL, NULL, &sdp); -#endif /* solaris<100000 */ - - vs = (KA_T)NULL; - } - /* - * Get the real vnode's type. - */ - if ((vty = vop2ty(ctx, v, fx)) < 0) { - if (Ntype != N_FIFO && vs) - vty = N_STREAM; - else { - -#if solaris < 100000 - (void)snpf(Namech, Namechl - 1, - "unknown file system type, v_op: %s", - print_kptr((KA_T)v->v_op, (char *)NULL, 0)); -#else /* solaris>=100000 */ - (void)snpf(Namech, Namechl - 1, - "unknown file system type (%s), v_op: %s", - fxs ? Fsinfo[fx] : "unknown", - print_kptr((KA_T)v->v_op, (char *)NULL, 0)); -#endif /* solaris<100000 */ - - Namech[Namechl - 1] = '\0'; - } - } - if (Ntype == N_NM || Ntype == N_AFS) - Ntype = vty; - /* - * Base further processing on the "real" vnode. - */ - Lf->lock = isvlocked(ctx, v); - switch (vty) { - -#if defined(HAS_AFS) - case N_AFS: - if (readafsnode(ctx, va, v, &an)) - return; - break; -#endif /* defined(HAS_AFS) */ - -#if solaris >= 20500 - case N_AUTO: - -# if solaris < 20600 - if (read_nan(ctx, va, (KA_T)v->v_data, &au)) -# else /* solaris>=20600 */ - if (read_nan(ctx, va, (KA_T)v->v_data, &fnn)) -# endif /* solaris<20600 */ - - return; - break; - -# if solaris >= 100000 - case N_DEV: - if (read_ndvn(ctx, va, (KA_T)v->v_data, &dv, &dv_dev, &dv_devs)) - return; - dvs = 1; - break; -# endif /* solaris>=100000 */ - - case N_DOOR: - -# if solaris < 20600 - if (read_ndn(ctx, realvp, (KA_T)v->v_data, &dn)) -# else /* solaris>=20600 */ - if (read_ndn(ctx, va, (KA_T)v->v_data, &dn)) -# endif /* solaris<20500 */ - - return; - dns = 1; - break; -#endif /* solaris>=20500 */ - -#if defined(HASCACHEFS) - case N_CACHE: - if (read_ncn(ctx, va, (KA_T)v->v_data, &cn)) - return; - break; -#endif /* defined(HASCACHEFS) */ - -#if solaris >= 100000 - case N_CTFSADIR: - case N_CTFSBUND: - case N_CTFSCDIR: - case N_CTFSCTL: - case N_CTFSEVT: - case N_CTFSLATE: - case N_CTFSROOT: - case N_CTFSSTAT: - case N_CTFSSYM: - case N_CTFSTDIR: - case N_CTFSTMPL: - if (read_nctfsn(ctx, vty, va, (KA_T)v->v_data, (char *)&ctfs)) - return; - break; -#endif /* solaris>=100000 */ - - case N_HSFS: - if (read_nhn(ctx, va, (KA_T)v->v_data, &h)) - return; - break; - case N_MNT: - /* Information comes from the l_vfs structure. */ - break; - case N_MVFS: - if (read_nmn(ctx, va, (KA_T)v->v_data, &m)) - return; - break; - case N_NFS: - if (read_nrn(ctx, va, (KA_T)v->v_data, &r)) - return; - break; - -#if solaris >= 100000 - case N_NFS4: - if (read_nrn4(ctx, va, (KA_T)v->v_data, &r4)) - return; - break; -#endif /* solaris>=100000 */ - - case N_NM: - if (read_nnn(ctx, va, (KA_T)v->v_data, &nn)) - return; - nns = 1; - break; - -#if solaris >= 100000 - case N_PORT: - if (read_nprtn(ctx, va, (KA_T)v->v_data, &pn)) - return; - break; -#endif /* solaris>=100000 */ - - case N_PCFS: - if (read_npn(ctx, va, (KA_T)v->v_data, &pc)) - return; - break; - case N_SAMFS: - (void)add_nma(ctx, SAMFS_NMA_MSG, (int)strlen(SAMFS_NMA_MSG)); - -#if solaris >= 110000 - case N_SDEV: - if (read_nsdn(ctx, va, (KA_T)v->v_data, &sdn, &sdva)) - return; - if (Lf->is_stream) { - - /* - * This stream's real node is an sdev_node, so it's not really - * a stream. Reverse prior stream settings. - */ - Lf->is_stream = 0; - Namech[0] = '\0'; - } - sdns = 1; - break; -#endif /* solaris>=110000 */ - - break; - -#if solaris >= 20600 - case N_SOCK: - sona = (KA_T)v->v_data; - if (read_nson(ctx, va, sona, &so)) - return; - break; -#endif /* solaris>=20600 */ - - case N_STREAM: - if (vs) { - Lf->is_stream = 1; - -#if solaris < 100000 - read_mi(ctx, vs, (dev_t *)&s.s_dev, (caddr_t)&soso, &so_st, - so_ad, &sdp); -#else /* solaris>=100000 */ - read_mi(ctx, vs, (dev_t *)&s.s_dev, NULL, NULL, NULL, &sdp); -#endif /* solaris<100000 */ - - vs = (KA_T)NULL; - } - break; - case N_TMP: - if (read_ntn(ctx, va, (KA_T)v->v_data, &t)) - return; - break; - -#if defined(HASVXFS) - case N_VXFS: - if (read_vxnode(ctx, va, v, vfs, fx, &vx, Vvops)) - return; - break; -#endif /* defined(HASVXFS) */ - -#if defined(HAS_ZFS) - case N_ZFS: - if (read_nzn(ctx, va, (KA_T)v->v_data, &zn)) - return; - zns = 1; - break; -#endif /* defined(HAS_ZFS) */ - - case N_REGLR: - default: - if (read_nin(ctx, va, (KA_T)v->v_data, &i)) - return; - ins = 1; - } - /* - * If this is a Solaris loopback node, use the "real" node type. - */ - if (Ntype == N_LOFS) - Ntype = vty; - } - /* - * Get device and type for printing. - */ - switch (((Ntype == N_FIFO) || (vty == N_SDEV)) ? vty : Ntype) { - -#if defined(HAS_AFS) - case N_AFS: - dev = an.dev; - devs = 1; - break; -#endif /* defined(HAS_AFS) */ - -#if solaris >= 20500 - case N_AUTO: - if (kvs) { - dev = (dev_t)kv.vfs_fsid.val[0]; - devs = 1; - } - break; - -# if solaris >= 100000 - case N_DEV: - if (dv_devs) { - dev = dv_dev; - devs = 1; - } else if (vfs) { - dev = vfs->dev; - devs = 1; - } - rdev = v->v_rdev; - rdevs = 1; - break; -# endif /* solaris>=100000 */ - - case N_DOOR: - -# if solaris < 20600 - if (kvs) { - dev = (dev_t)kv.vfs_fsid.val[0]; - devs = 1; - } -# else /* solaris>=20600 */ - if (nns) { - dev = (dev_t)nn.nm_vattr.va_fsid; - devs = 1; - } else if (dns) { - dev = (dev_t)dn.door_index; - devs = 1; - } -# endif /* solaris<20600 */ - - break; -#endif /* solaris>=20500 */ - -#if defined(HASCACHEFS) - case N_CACHE: -#endif /* defined(HASCACHEFS) */ - - case N_HSFS: - case N_PCFS: - if (kvs) { - dev = kv.vfs_dev; - devs = 1; - } - break; - -#if solaris >= 100000 - case N_CTFSADIR: - case N_CTFSBUND: - case N_CTFSCDIR: - case N_CTFSCTL: - case N_CTFSEVT: - case N_CTFSLATE: - case N_CTFSROOT: - case N_CTFSSTAT: - case N_CTFSSYM: - case N_CTFSTDIR: - case N_CTFSTMPL: - if (kvs) { - dev = kv.vfs_dev; - devs = 1; - } - break; -#endif /* solaris>=100000 */ - - case N_FD: - if (kvs) { - dev = kv.vfs_dev; - devs = 1; - } - if ((v->v_type == VCHR) || (v->v_type == VBLK)) { - rdev = v->v_rdev; - rdevs = 1; - } - break; - - case N_MNT: - -#if defined(CVFS_DEVSAVE) - if (vfs) { - dev = vfs->dev; - devs = 1; - } -#endif /* defined(CVFS_DEVSAVE) */ - - break; - case N_MVFS: - -#if defined(CVFS_DEVSAVE) - if (vfs) { - dev = vfs->dev; - devs = 1; - } -#endif /* defined(CVFS_DEVSAVE) */ - - break; - case N_NFS: - dev = r.r_attr.va_fsid; - devs = 1; - break; - -#if solaris >= 100000 - case N_NFS4: - dev = r4.r_attr.va_fsid; - devs = 1; - break; -#endif /* solaris>=100000 */ - - case N_NM: - if (nns) { - dev = (dev_t)nn.nm_vattr.va_fsid; - devs = 1; - } else - enter_dev_ch(ctx, " NMFS"); - break; - -#if solaris >= 100000 - case N_PORT: - if (kvs) { - dev = kv.vfs_dev; - devs = 1; - } - break; -#endif /* solaris>=100000 */ - -#if defined(HASPROCFS) - case N_PROC: - if (kvs) { - dev = kv.vfs_dev; - devs = 1; - } - break; -#endif /* defined(HASPROCFS) */ - - case N_SAMFS: - if ((v->v_type == VCHR) || (v->v_type == VBLK)) { - rdev = v->v_rdev; - rdevs = 1; - } else if (vfs) { - dev = vfs->dev; - devs = 1; - } - break; - -#if solaris >= 110000 - case N_SDEV: - if (sdns) { - if (v->v_type == VDIR) { - dev = v->v_rdev; - devs = 1; - } else { - rdev = v->v_rdev; - rdevs = 1; - } - } - break; -#endif /* solaris>=110000 */ - - case N_SHARED: - if (vfs) { - dev = vfs->dev; - devs = 1; - } - break; - -#if solaris >= 20600 - case N_SOCK: - if (so.so_family == AF_UNIX) - - /* - * Process an AF_UNIX socket node. - */ - -# if solaris >= 110000 - { - - /* - * Process a Solaris >= 11 AF_UNIX socket node: - * - * Get its sotpi_info_t structure; - */ - if (read_nsti(ctx, &so, &sti)) - return; - /* - * Get its device numbers. If they are located, start the NAME - * column with the device name, followed by "->". - */ - nm = Namech; - nmrl = Namechl - 1; - Namech[Namechl - 1] = '\0'; - if (!sdp) - sdp = finddev(ctx, &DevDev, &sti.sti_dev, LOOKDEV_ALL); - if (sdp) { - dev = DevDev; - rdev = v->v_rdev; - trdev = sdp->rdev; - devs = rdevs = trdevs = 1; - Lf->inode = (INODETYPE)sdp->inode; - Lf->inp_ty = 1; - (void)snpf(nm, nmrl, "%s", sdp->name); - tl = strlen(nm); - nm += tl; - nmrl -= tl; - sep = "->"; - } else { - devs = rdevs = trdevs = 0; - sep = ""; - } - /* - * Add the socket node's address to the NAME column. - */ - sepl = strlen(sep); - if (sona && ((nmrl - sepl) > 0)) { - (void)snpf(nm, nmrl, "%s%s", sep, - print_kptr(sona, (char *)NULL, 0)); - tl = strlen(nm); - nm += tl; - nmrl -= tl; - } - /* - * Add the service type to the NAME column. - */ - switch (sti.sti_serv_type) { - case T_CLTS: - ty = "dgram"; - break; - case T_COTS: - ty = "stream"; - break; - case T_COTS_ORD: - ty = "stream-ord"; - break; - default: - ty = (char *)NULL; - } - if (ty && (nmrl > 1)) { - (void)snpf(nm, nmrl, " %s", ty); - tl = strlen(nm); - nm += tl; - nmrl -= tl; - } - /* - * Add the vnode and connected addresses to the NAME column, - * as indicated by the socket node state. - */ - if ((so.so_state & SS_ISBOUND) && (nmrl > 36) && - (sti.sti_ux_laddr.soua_magic == SOU_MAGIC_EXPLICIT)) { - (void)snpf(nm, nmrl, " Vn=%s", - print_kptr((KA_T)sti.sti_ux_laddr.soua_vp, - (char *)NULL, 0)); - tl = strlen(nm); - nm += tl; - nmrl -= tl; - } - if ((so.so_state & SS_ISCONNECTED) && (nmrl > 38) && - (sti.sti_ux_faddr.soua_magic == SOU_MAGIC_EXPLICIT)) { - (void)snpf(nm, nmrl, " Conn=%s ", - print_kptr((KA_T)sti.sti_ux_faddr.soua_vp, - (char *)NULL, 0)); - tl = strlen(nm); - nm += tl; - nmrl -= tl; - } - /* - * Put local and connected UNIX addresses in the NAME column, if - * they exist and as indicated by the socket node's state. - */ - if ((so.so_state & SS_ISBOUND) && - ((len = read_nusa(ctx, &sti.sti_laddr, &ua)) > 0) && - (nmrl > (len + 5))) { - if (Sfile && is_file_named(ctx, ua.sun_path, Ntype, VSOCK, 0)) - Lf->sf |= SELNM; - if (len > nmrl) - len = nmrl; - if (len > 0) { - ua.sun_path[len] = '\0'; - (void)snpf(nm, nmrl, " Lcl=%s", ua.sun_path); - tl = strlen(nm); - nm += tl; - nmrl -= tl; - } - } - if ((so.so_state & SS_ISCONNECTED) && - ((len = read_nusa(ctx, &sti.sti_faddr, &ua)) > 0) && - (nmrl > (len + 5))) { - if (Sfile && is_file_named(ctx, ua.sun_path, Ntype, VSOCK, 0)) - Lf->sf |= SELNM; - if (len > nmrl) - len = nmrl; - if (len > 0) { - ua.sun_path[len] = '\0'; - (void)snpf(nm, nmrl, " Rem=%s", ua.sun_path); - tl = strlen(nm); - nm += tl; - nmrl -= tl; - } - } - } else { - - /* - * Process Solaris >= 11 AF_INET, AF_INET6 and AF_ROUTE VSOCK - * nodes. - */ - switch (so.so_family) { - case AF_INET: - case AF_INET6: - case AF_ROUTE: - if (process_VSOCK(ctx, (KA_T)va, v, &so)) - return; - } - } -# else /* solaris<110000 */ - { - - /* - * Process an AF_UNIX socket node for Solaris < 11: - * Locate its device numbers; - * Enter the sonode address as the device (netstat's local - * address); - * Get a non-NULL local sockaddr_un and enter it in Namech; - * Get a non-NULL foreign sockaddr_un and enter it in Namech; - * Check for matches on sockaddr_un.sun_path names. - */ - - if (!sdp) - sdp = finddev(&DevDev, - -# if solaris < 100000 - &so.so_vnode.v_rdev, -# else /* solaris>=100000 */ - &so.so_dev, -# endif /* solaris<100000 */ - - LOOKDEV_ALL); - - if (sdp) { - dev = DevDev; - -# if solaris < 100000 - rdev = so.so_vnode.v_rdev; -# else /* solaris>=100000 */ - rdev = so.so_dev; -# endif /* solaris<100000 */ - - trdev = sdp->rdev; - devs = rdevs = trdevs = 1; - Lf->inode = (INODETYPE)sdp->inode; - Lf->inp_ty = 1; - (void)snpf(Namech, Namechl - 1, "%s", sdp->name); - Namech[Namechl - 1] = '\0'; - } else - devs = 0; - nl = snl = (int)strlen(Namech); - - if ((len = read_nusa(&so.so_laddr, &ua))) { - if (Sfile && is_file_named(ctx, ua.sun_path, Ntype, VSOCK, 0)) - Lf->sf |= SELNM; - sepl = Namech[0] ? 2 : 0; - if (len > (Namechl - nl - sepl - 1)) - len = Namechl - nl - sepl - 1; - if (len > 0) { - ua.sun_path[len] = '\0'; - (void)snpf(&Namech[nl], Namechl - nl, "%s%s", - sepl ? "->" : "", ua.sun_path); - nl += (len + sepl); - } - } - if ((len = read_nusa(&so.so_faddr, &ua))) { - if (Sfile && is_file_named(ctx, ua.sun_path, Ntype, VSOCK, 0)) - Lf->sf |= SELNM; - sepl = Namech[0] ? 2 : 0; - if (len > (Namechl - nl - sepl - 1)) - len = Namechl - nl - sepl - 1; - if (len > 0) { - ua.sun_path[len] = 0; - (void)snpf(&Namech[nl], Namechl - nl, "%s%s", - sepl ? "->" : "", ua.sun_path); - nl += (len + sepl); - } - } - if ((nl == snl) - -# if defined(HASSOUXSOUA) - && so.so_ux_laddr.soua_magic == SOU_MAGIC_IMPLICIT -# else /* !defined(HASSOUXSOUA) */ - && so.so_ux_laddr.sou_magic == SOU_MAGIC_IMPLICIT -# endif /* defined(HASSOUXSOUA) */ - - ) { - - /* - * There are no addresses; this must be a socket pair. - * Print its identity. - */ - pa = (struct pairaddr *)&ua; - if (!(peer = (KA_T)((int)pa->p))) - -# if defined(HASSOUXSOUA) - peer = (KA_T)so.so_ux_laddr.soua_vp; -# else /* !defined(HASSOUXSOUA) */ - peer = (KA_T)so.so_ux_laddr.sou_vp; -# endif /* defined(HASSOUXSOUA) */ - - if (peer) - (void)snpf(ubuf, sizeof(ubuf), "(socketpair: %s)", - print_kptr(peer, (char *)NULL, 0)); - else - (void)snpf(ubuf, sizeof(ubuf), "(socketpair)"); - len = (int)strlen(ubuf); - sepl = Namech[0] ? 2 : 0; - if (len > (Namechl - nl - sepl - 1)) - len = Namechl - nl - sepl - 1; - if (len > 0) { - (void)snpf(&Namech[nl], Namechl - nl, "%s%s", - sepl ? "->" : "", ubuf); - nl += (len + sepl); - } - } - /* - * Add the local and foreign addresses, ala `netstat -f unix` to - * the name. - */ - -# if defined(HASSOUXSOUA) - soa = (KA_T)so.so_ux_faddr.soua_vp; -# else /* !defined(HASSOUXSOUA) */ - soa = (KA_T)so.so_ux_faddr.sou_vp; -# endif /* defined(HASSOUXSOUA) */ - - (void)snpf(ubuf, sizeof(ubuf), "%s(%s%s%s)", Namech[0] ? " " : "", - print_kptr((KA_T)v->v_data, (char *)NULL, 0), - soa ? "->" : "", - soa ? print_kptr(soa, tbuf, sizeof(tbuf)) : ""); - len = (int)strlen(ubuf); - if (len <= (Namechl - nl - 1)) { - (void)snpf(&Namech[nl], Namechl - nl, "%s", ubuf); - nl += len; - } - /* - * If there is a bound vnode, add its address to the name. - */ - - if (so.so_ux_bound_vp) { - (void)snpf( - ubuf, sizeof(ubuf), "%s(Vnode=%s)", Namech[0] ? " " : "", - print_kptr((KA_T)so.so_ux_bound_vp, (char *)NULL, 0)); - len = (int)strlen(ubuf); - if (len <= (Namechl - nl - 1)) { - (void)snpf(&Namech[nl], Namechl - nl, "%s", ubuf); - nl += len; - } - } - } -# endif /* solaris>=110000 */ - - break; - -#endif /* solaris>=20600 */ - - case N_SPEC: - -#if solaris < 100000 - if (((Ntype = vty) == N_STREAM) && so_st) { - if (Funix) - Lf->sf |= SELUNX; - unix_sock = 1; - if (so_ad[0]) { - if (sdp) { - if (vfs) { - dev = vfs->dev; - devs = 1; - } - rdev = sdp->rdev; - rdevs = 1; - Lf->inode = (INODETYPE)sdp->inode; - Lf->inp_ty = 1; - (void)snpf(ubuf, sizeof(ubuf), "(%s%s%s)", - print_kptr(so_ad[0], (char *)NULL, 0), - so_ad[1] ? "->" : "", - so_ad[1] - ? print_kptr(so_ad[1], tbuf, sizeof(tbuf)) - : ""); - } else { - enter_dev_ch(print_kptr(so_ad[0], (char *)NULL, 0)); - if (so_ad[1]) - (void)snpf(ubuf, sizeof(ubuf), "(->%s)", - print_kptr(so_ad[1], (char *)NULL, 0)); - } - if (!Lf->nma && - (Lf->nma = (char *)malloc((int)strlen(ubuf) + 1))) { - (void)snpf(Lf->nma, (int)strlen(ubuf) + 1, "%s", ubuf); - } - } else if (soso.lux_dev.addr.tu_addr.ino) { - if (vfs) { - dev = vfs->dev; - devs = 1; - } - rdev = soso.lux_dev.addr.tu_addr.dev; - rdevs = 1; - } else { - int dc, dl, dr; - -# if solaris < 20400 - dl = (soso.lux_dev.addr.tu_addr.dev >> 16) & 0xffff; - dr = (soso.rux_dev.addr.tu_addr.dev >> 16) & 0xffff; -# else /* solaris>=20400 */ - dl = soso.lux_dev.addr.tu_addr.dev & 0xffff; - dr = soso.rux_dev.addr.tu_addr.dev & 0xffff; -# endif /* solaris<20400 */ - - dc = (dl << 16) | dr; - enter_dev_ch(print_kptr((KA_T)dc, (char *)NULL, 0)); - devs = 0; - } - if (soso.laddr.buf && soso.laddr.len == sizeof(ua)) { - if (kread(ctx, (KA_T)soso.laddr.buf, (char *)&ua, sizeof(ua)) == - 0) { - ua.sun_path[sizeof(ua.sun_path) - 1] = '\0'; - if (ua.sun_path[0]) { - if (Sfile && - is_file_named(ctx, ua.sun_path, Ntype, VSOCK, 0)) - Lf->sf |= SELNM; - len = (int)strlen(ua.sun_path); - nl = (int)strlen(Namech); - sepl = Namech[0] ? 2 : 0; - if (len > (Namechl - nl - sepl - 1)) - len = Namechl - nl - sepl - 1; - if (len > 0) { - ua.sun_path[len] = '\0'; - (void)snpf(&Namech[nl], Namechl - nl, "%s%s", - sepl ? "->" : "", ua.sun_path); - } - } - } - } - } else -#endif /* solaris<100000 */ - - { - if (vfs) { - dev = vfs->dev; - devs = 1; - } - rdev = s.s_dev; - rdevs = 1; - } - break; - case N_STREAM: - if (vfs) { - dev = vfs->dev; - devs = 1; - } - rdev = s.s_dev; - rdevs = 1; - break; - case N_TMP: - dev = t.tn_attr.va_fsid; - devs = 1; - break; - -#if defined(HASVXFS) - case N_VXFS: - dev = vx.dev; - devs = vx.dev_def; - if ((v->v_type == VCHR) || (v->v_type == VBLK)) { - rdev = vx.rdev; - rdevs = vx.rdev_def; - } - break; -#endif /* defined(HASVXFS) */ - -#if defined(HAS_ZFS) - case N_ZFS: - if (zns) { - if (!read_nzvfs(ctx, (KA_T)v->v_data, (KA_T)zn.z_zfsvfs, &zvfs) && - zvfs.z_vfs && - !kread(ctx, (KA_T)zvfs.z_vfs, (char *)&zgvfs, sizeof(zgvfs))) { - dev = zgvfs.vfs_dev; - devs = 1; - } - } - if ((v->v_type == VCHR) || (v->v_type == VBLK)) { - rdev = v->v_rdev; - rdevs = 1; - } - break; -#endif /* defined(HAS_ZFS) */ - - default: - if (ins) { - dev = i.i_dev; - devs = 1; - } else if (nns) { - dev = nn.nm_vattr.va_fsid; - devs = 1; - } else if (vfs) { - dev = vfs->dev; - devs = 1; - } - if ((v->v_type == VCHR) || (v->v_type == VBLK)) { - rdev = v->v_rdev; - rdevs = 1; - } - } - type = v->v_type; - if (devs && vfs && !vfs->dir) { - (void)completevfs(ctx, vfs, &dev); - -#if defined(HAS_AFS) - if (vfs->dir && (Ntype == N_AFS || vty == N_AFS) && !AFSVfsp) - AFSVfsp = (KA_T)v->v_vfsp; -#endif /* defined(HAS_AFS) */ - } - /* - * Obtain the inode number. - */ - switch (vty) { - -#if defined(HAS_AFS) - case N_AFS: - if (an.ino_st) { - Lf->inode = (INODETYPE)an.inode; - Lf->inp_ty = 1; - } - break; -#endif /* defined(HAS_AFS) */ - -#if solaris >= 20500 - case N_AUTO: - -# if solaris < 20600 - Lf->inode = (INODETYPE)au.an_nodeid; -# else /* solaris>=20600 */ - Lf->inode = (INODETYPE)fnn.fn_nodeid; -# endif /* solaris<20600 */ - - Lf->inp_ty = 1; - break; - -# if solaris >= 100000 - case N_DEV: - if (dvs) { - Lf->inode = (INODETYPE)dv.dv_ino; - Lf->inp_ty = 1; - } - break; -# endif /* solaris>=100000 */ - - case N_DOOR: - if (nns && (Lf->inode = (INODETYPE)nn.nm_vattr.va_nodeid)) { - Lf->inp_ty = 1; - break; - } - if (dns) { - if ((Lf->inode = (INODETYPE)dn.door_index)) - Lf->inp_ty = 1; - } - break; -#endif /* solaris>=20500 */ - -#if defined(HASCACHEFS) - case N_CACHE: - Lf->inode = (INODETYPE)cn.c_fileno; - Lf->inp_ty = 1; - break; -#endif /* defined(HASCACHEFS) */ - -#if solaris >= 100000 - case N_CTFSADIR: - case N_CTFSBUND: - case N_CTFSCDIR: - case N_CTFSCTL: - case N_CTFSEVT: - case N_CTFSLATE: - case N_CTFSROOT: - case N_CTFSSTAT: - case N_CTFSSYM: - case N_CTFSTDIR: - case N_CTFSTMPL: - /* Method of computing CTFS inode not known. */ - break; -#endif /* solaris>=10000 */ - - case N_FD: - if (v->v_type == VDIR) - Lf->inode = (INODETYPE)2; - else - Lf->inode = (INODETYPE)(GET_MIN_DEV(v->v_rdev) * 100); - Lf->inp_ty = 1; - break; - case N_HSFS: - Lf->inode = (INODETYPE)h.hs_nodeid; - Lf->inp_ty = 1; - break; - - case N_MNT: - -#if defined(HASFSINO) - if (vfs) { - Lf->inode = vfs->fs_ino; - Lf->inp_ty = 1; - } -#endif /* defined(HASFSINO) */ - - break; - case N_MVFS: - Lf->inode = (INODETYPE)m.m_ino; - Lf->inp_ty = 1; - break; - case N_NFS: - Lf->inode = (INODETYPE)r.r_attr.va_nodeid; - Lf->inp_ty = 1; - break; - -#if solaris >= 100000 - case N_NFS4: - Lf->inode = (INODETYPE)r4.r_attr.va_nodeid; - Lf->inp_ty = 1; - break; -#endif /* solaris>=100000 */ - - case N_NM: - Lf->inode = (INODETYPE)nn.nm_vattr.va_nodeid; - Lf->inp_ty = 1; - break; - -#if defined(HASPROCFS) - case N_PROC: - - /* - * The proc file system inode number is defined when the - * prnode is read. - */ - break; -#endif /* defined(HASPROCFS) */ - - case N_PCFS: - if (kvs && kv.vfs_data && - !kread(ctx, (KA_T)kv.vfs_data, (char *)&pcfs, sizeof(pcfs))) { - -#if solaris >= 70000 -# if defined(HAS_PC_DIRENTPERSEC) - Lf->inode = (INODETYPE)pc_makenodeid( - pc.pc_eblkno, pc.pc_eoffset, pc.pc_entry.pcd_attr, - IS_FAT32(&pcfs) - ? ltohs(pc.pc_entry.pcd_scluster_lo) | - (ltohs(pc.pc_entry.un.pcd_scluster_hi) << 16) - : ltohs(pc.pc_entry.pcd_scluster_lo), - pc_direntpersec(&pcfs)); -# else /* !defined(HAS_PC_DIRENTPERSEC) */ - Lf->inode = (INODETYPE)pc_makenodeid( - pc.pc_eblkno, pc.pc_eoffset, pc.pc_entry.pcd_attr, - IS_FAT32(&pcfs) - ? ltohs(pc.pc_entry.pcd_scluster_lo) | - (ltohs(pc.pc_entry.un.pcd_scluster_hi) << 16) - : ltohs(pc.pc_entry.pcd_scluster_lo), - pcfs.pcfs_entps); -# endif /* defined(HAS_PC_DIRENTPERSEC) */ -#else /* solaris<70000 */ - Lf->inode = (INODETYPE)pc_makenodeid(pc.pc_eblkno, pc.pc_eoffset, - &pc.pc_entry, pcfs.pcfs_entps); -#endif /* solaris>=70000 */ - - Lf->inp_ty = 1; - } - break; - - case N_REGLR: - if (nns) { - if ((Lf->inode = (INODETYPE)nn.nm_vattr.va_nodeid)) - Lf->inp_ty = 1; - } else if (ins) { - if ((Lf->inode = (INODETYPE)i.i_number)) - Lf->inp_ty = 1; - } - break; - case N_SAMFS: - break; /* No more SAM-FS information is available. */ - -#if solaris >= 110000 - case N_SDEV: - if (sdns) { - Lf->inode = (INODETYPE)sdva.va_nodeid; - Lf->inp_ty = 1; - } - break; -#endif /* solaris>=110000 */ - - case N_SHARED: - (void)snpf(Lf->iproto, sizeof(Lf->iproto), "SHARED"); - Lf->inp_ty = 2; - break; - case N_STREAM: - -#if solaris < 100000 - if (so_st && soso.lux_dev.addr.tu_addr.ino) { - if (Lf->inp_ty) { - nl = Lf->nma ? (int)strlen(Lf->nma) : 0; - (void)snpf(ubuf, sizeof(ubuf), "%s(Inode=%lu)", nl ? " " : "", - (unsigned long)soso.lux_dev.addr.tu_addr.ino); - len = nl + (int)strlen(ubuf) + 1; - if (Lf->nma) - Lf->nma = (char *)realloc(Lf->nma, len); - else - Lf->nma = (char *)malloc(len); - if (Lf->nma) - (void)snpf(&Lf->nma[nl], len - nl, "%s", ubuf); - } else { - Lf->inode = (INODETYPE)soso.lux_dev.addr.tu_addr.ino; - Lf->inp_ty = 1; - } - } -#endif /* solaris<100000 */ - - break; - case N_TMP: - Lf->inode = (INODETYPE)t.tn_attr.va_nodeid; - Lf->inp_ty = 1; - break; - -#if defined(HASVXFS) - case N_VXFS: - if (vx.ino_def) { - Lf->inode = (INODETYPE)vx.ino; - Lf->inp_ty = 1; - } else if (type == VCHR) - pnl = 1; - break; -#endif /* defined(HASVXFS) */ - -#if defined(HAS_ZFS) - case N_ZFS: - if (zns) { - Lf->inode = (INODETYPE)zn.z_id; - Lf->inp_ty = 1; - } - break; -#endif /* defined(HAS_ZFS) */ - } - /* - * Obtain the file size. - */ - switch (Ntype) { - -#if defined(HAS_AFS) - case N_AFS: - Lf->sz = (SZOFFTYPE)an.size; - Lf->sz_def = 1; - break; -#endif /* defined(HAS_AFS) */ - -#if solaris >= 20500 - case N_AUTO: - -# if solaris < 20600 - Lf->sz = (SZOFFTYPE)au.an_size; -# else /* solaris >=20600 */ - Lf->sz = (SZOFFTYPE)fnn.fn_size; -# endif /* solaris < 20600 */ - - Lf->sz_def = 1; - break; -#endif /* solaris>=20500 */ - -#if defined(HASCACHEFS) - case N_CACHE: - Lf->sz = (SZOFFTYPE)cn.c_size; - Lf->sz_def = 1; - break; -#endif /* defined(HASCACHEFS) */ - -#if solaris >= 100000 - case N_CTFSADIR: - case N_CTFSBUND: - case N_CTFSCDIR: - case N_CTFSCTL: - case N_CTFSEVT: - case N_CTFSLATE: - case N_CTFSROOT: - case N_CTFSSTAT: - case N_CTFSSYM: - case N_CTFSTDIR: - case N_CTFSTMPL: - /* Method of computing CTFS size not known. */ - break; -#endif /* solaris>=100000 */ - - case N_FD: - if (v->v_type == VDIR) - Lf->sz = (Unof + 2) * 16; - else - Lf->sz = (unsigned long)0; - Lf->sz_def = 1; - break; - -#if solaris >= 20600 - case N_SOCK: - break; -#endif /* solaris>=20600 */ - - case N_HSFS: - Lf->sz = (SZOFFTYPE)h.hs_dirent.ext_size; - Lf->sz_def = 1; - break; - case N_NM: - Lf->sz = (SZOFFTYPE)nn.nm_vattr.va_size; - Lf->sz_def = 1; - break; - -#if solaris >= 100000 - case N_DEV: - break; -#endif /* solaris>=100000 */ - - case N_DOOR: - case N_FIFO: - break; - case N_MNT: - -#if defined(CVFS_SZSAVE) - if (vfs) { - Lf->sz = (SZOFFTYPE)vfs->size; - Lf->sz_def = 1; - } else -#endif /* defined(CVFS_SZSAVE) */ - - break; - case N_MVFS: - /* The location of file size isn't known. */ - break; - case N_NFS: - if (!(type == VCHR || type == VBLK)) { - Lf->sz = (SZOFFTYPE)r.r_size; - Lf->sz_def = 1; - } - break; - -#if solaris >= 100000 - case N_NFS4: - if (!(type == VCHR || type == VBLK)) { - Lf->sz = (SZOFFTYPE)r4.r_size; - Lf->sz_def = 1; - } - break; -#endif /* solaris>=100000 */ - - case N_PCFS: - Lf->sz = (SZOFFTYPE)pc.pc_size; - Lf->sz_def = 1; - break; - -#if solaris >= 100000 - case N_PORT: - Lf->sz = (SZOFFTYPE)pn.port_curr; - Lf->sz_def = 1; - break; -#endif /* solaris>=100000 */ - -#if defined(HASPROCFS) - case N_PROC: - - /* - * The proc file system size is defined when the - * prnode is read. - */ - break; -#endif /* defined(HASPROCFS) */ - - case N_REGLR: - if (type == VREG || type == VDIR) { - if (ins | nns) { - Lf->sz = (SZOFFTYPE)(nns ? nn.nm_vattr.va_size : i.i_size); - Lf->sz_def = 1; - } - } - break; - -#if solaris >= 110000 - case N_SDEV: - if (sdns) { - if (type == VREG || type == VDIR) { - Lf->sz = (SZOFFTYPE)sdva.va_size; - Lf->sz_def = 1; - } - } - break; -#endif /* solaris>=110000 */ - - case N_SAMFS: - break; /* No more SAM-FS information is available. */ - case N_SHARED: - break; /* No more sharedfs information is available. */ - case N_STREAM: - break; - case N_TMP: - Lf->sz = (SZOFFTYPE)t.tn_attr.va_size; - Lf->sz_def = 1; - break; - -#if defined(HASVXFS) - case N_VXFS: - if (type == VREG || type == VDIR) { - Lf->sz = (SZOFFTYPE)vx.sz; - Lf->sz_def = vx.sz_def; - } - break; -#endif /* defined(HASVXFS) */ - -#if defined(HAS_ZFS) - case N_ZFS: - if (zns) { - if (type == VREG || type == VDIR) { - Lf->sz = (SZOFFTYPE)zn.z_size; - Lf->sz_def = 1; - } - } - break; -#endif /* defined(HAS_ZFS) */ - } - /* - * Record link count. - */ - - switch (Ntype) { - -#if defined(HAS_AFS) - case N_AFS: - Lf->nlink = an.nlink; - Lf->nlink_def = an.nlink_st; - break; -#endif /* defined(HAS_AFS) */ - -#if solaris >= 20500 - case N_AUTO: - break; - -# if defined(HASCACHEFS) - case N_CACHE: - Lf->nlink = (long)cn.c_attr.va_nlink; - Lf->nlink_def = 1; - break; -# endif /* defined(HASCACHEFS) */ - -#endif /* solaris>=20500 */ - -#if solaris >= 100000 - case N_CTFSADIR: - case N_CTFSBUND: - case N_CTFSCDIR: - case N_CTFSCTL: - case N_CTFSEVT: - case N_CTFSLATE: - case N_CTFSROOT: - case N_CTFSSTAT: - case N_CTFSSYM: - case N_CTFSTDIR: - case N_CTFSTMPL: - /* Method of computing CTFS link count not known. */ - break; -#endif /* solaris>=100000 */ - - case N_FD: - Lf->nlink = (v->v_type == VDIR) ? 2 : 1; - Lf->nlink_def = 1; - break; - -#if solaris >= 20600 - case N_SOCK: /* no link count */ - break; -#endif /* solaris>=20600 */ - - case N_HSFS: - Lf->nlink = (long)h.hs_dirent.nlink; - Lf->nlink_def = 1; - break; - case N_NM: - Lf->nlink = (long)nn.nm_vattr.va_nlink; - Lf->nlink_def = 1; - break; - -#if solaris >= 100000 - case N_DEV: - if (dvs) { - Lf->nlink = (long)dv.dv_nlink; - Lf->nlink_def = 1; - } - break; -#endif /* solaris>=100000 */ - - case N_DOOR: - Lf->nlink = (long)v->v_count; - Lf->nlink_def = 1; - break; - case N_FIFO: - break; - case N_MNT: - -#if defined(CVFS_NLKSAVE) - if (vfs) { - Lf->nlink = (long)vfs->nlink; - Lf->nlink_def = 1; - } -#endif /* defined(CVFS_NLKSAVE) */ - - break; - case N_MVFS: /* no link count */ - break; - case N_NFS: - Lf->nlink = (long)r.r_attr.va_nlink; - Lf->nlink_def = 1; - break; - -#if solaris >= 100000 - case N_NFS4: - Lf->nlink = (long)r4.r_attr.va_nlink; - Lf->nlink_def = 1; - break; -#endif /* solaris>=100000 */ - - case N_PCFS: - break; - -#if defined(HASPROCFS) - case N_PROC: - break; -#endif /* defined(HASPROCFS) */ - - case N_REGLR: - if (ins) { - Lf->nlink = (long)i.i_nlink; - Lf->nlink_def = 1; - } - break; - case N_SAMFS: - break; /* No more SAM-FS information is available. */ - -#if solaris >= 110000 - case N_SDEV: - if (sdns) { - Lf->nlink = (long)sdva.va_nlink; - Lf->nlink_def = 1; - } - break; -#endif /* solaris>=110000 */ - - case N_SHARED: - break; /* No more sharedfs information is available. */ - case N_STREAM: - break; - case N_TMP: - Lf->nlink = (long)t.tn_attr.va_nlink; - Lf->nlink_def = 1; - break; - -#if defined(HASVXFS) - case N_VXFS: - Lf->nlink = vx.nl; - Lf->nlink_def = vx.nl_def; - break; -#endif /* defined(HASVXFS) */ - -#if defined(HAS_ZFS) - case N_ZFS: - if (zns) { - Lf->nlink = (long)MIN(zn.z_links, UINT32_MAX); - Lf->nlink_def = 1; - } - break; -#endif /* defined(HAS_ZFS) */ - } - if (Nlink && Lf->nlink_def && (Lf->nlink < Nlink)) - Lf->sf |= SELNLINK; - -#if defined(HASVXFS) - /* - * Record a VxFS file. - */ - -# if defined(HASVXFSDNLC) - Lf->is_vxfs = (Ntype == N_VXFS) ? 1 : 0; -# endif /* defined(HASVXFSDNLC) */ -#endif /* defined(HASVXFS) */ - - /* - * Record an NFS selection. - */ - if (Fnfs) { - if ((Ntype == N_NFS) || (Ntype == N_NFS4)) - Lf->sf |= SELNFS; - } - -#if solaris >= 20500 - /* - * If this is a Solaris 2.5 and greater autofs entry, save the autonode name - * (less than Solaris 2.6) or fnnode name (Solaris 2.6 and greater). - */ - if (Ntype == N_AUTO && !Namech[0]) { - -# if solaris < 20600 - if (au.an_name[0]) - (void)snpf(Namech, Namechl - 1, "%s", au.an_name); - Namech[Namechl - 1] = '\0'; -# else /* solaris>=20600 */ - if (fnn.fn_name && (len = fnn.fn_namelen) > 0 && len < (Namechl - 1)) { - if (kread(ctx, (KA_T)fnn.fn_name, Namech, len)) - Namech[0] = '\0'; - else - Namech[len] = '\0'; - } -# endif /* solaris<20600 */ - } - /* - * If there is no local virtual file system pointer, or if its directory and - * file system names are NULL, and if there is a namenode, and if we're - * using the device number from it, see if its nm_mountpt vnode pointer - * leads to a local virtual file system structure with non-NULL directory - * and file system names. If it does, switch to that local virtual file - * system pointer. - */ - if (nns && (!vfs || (!vfs->dir && !vfs->fsname)) && devs && - (dev == nn.nm_vattr.va_fsid) && nn.nm_mountpt) { - if (!readvnode(ctx, (KA_T)nn.nm_mountpt, &fv) && fv.v_vfsp) { - if ((nvfs = readvfs(ctx, (KA_T)fv.v_vfsp, (struct vfs *)NULL, - nn.nm_filevp)) && - !nvfs->dir) { - (void)completevfs(ctx, nvfs, &dev); - } - -# if defined(HASNCACHE) - if (nvfs && nvfs->dir && nvfs->fsname) { - fa = (char *)NULL; - vfs = nvfs; - } -# endif /* defined(HASNCACHE) */ - } - } - -# if defined(HASNCACHE) - /* - * If there's a namenode and its device and node number match this one, - * use the nm_mountpt's address for name cache lookups. - */ - if (nns && devs && (dev == nn.nm_vattr.va_fsid) && (Lf->inp_ty == 1) && - (Lf->inode == (INODETYPE)nn.nm_vattr.va_nodeid)) - Lf->na = (KA_T)nn.nm_mountpt; -# endif /* defined(HASNCACHE) */ -#endif /* solaris>=20500 */ - - /* - * Save the file system names. - */ - if (vfs) { - Lf->fsdir = mkstrcpy(vfs->dir, (MALLOC_S *)NULL); - Lf->fsdev = mkstrcpy(vfs->fsname, (MALLOC_S *)NULL); - -#if defined(HASMNTSTAT) - Lf->mnt_stat = vfs->mnt_stat; -#endif /* defined(HASMNTSTAT) */ - - if (!Lf->fsdir && !Lf->fsdev && kvs && fxs) { - - /* - * The file system names are unknown. - * - * Set the file system device to the file system type and clear - * the doubtful device numbers. - */ - /* Like other fsdev values, this must be owned by the file. */ - Lf->fsdev = mkstrcpy(Fsinfo[fx], (MALLOC_S *)NULL); - devs = 0; - rdevs = 0; - } - -#if defined(HASFSINO) - else - Lf->fs_ino = vfs->fs_ino; -#endif /* defined(HASFSINO) */ - } - /* - * Save the device numbers, and their states. - * - * Format the vnode type, and possibly the device name. - */ - switch (type) { - - case VNON: - Lf->type = LSOF_FILE_VNODE_VNON; - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - break; - case VREG: - case VDIR: - Lf->type = (type == VREG) ? LSOF_FILE_VNODE_VREG : LSOF_FILE_VNODE_VDIR; - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - break; - case VBLK: - Lf->type = LSOF_FILE_VNODE_VBLK; - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - Ntype = N_BLK; - break; - case VCHR: - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - if (unix_sock) { - Lf->type = LSOF_FILE_UNIX; - break; - } - Lf->type = LSOF_FILE_VNODE_VCHR; - if (Lf->is_stream == 0 && Lf->is_com == 0) - Ntype = N_CHR; - break; - -#if solaris >= 20500 - case VDOOR: - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - Lf->type = LSOF_FILE_VNODE_VDOOR; - if (dns) - (void)idoorkeep(ctx, &dn); - break; -#endif /* solaris>=20500 */ - - case VLNK: - Lf->type = LSOF_FILE_VNODE_VLNK; - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - break; - -#if solaris >= 100000 - case VPORT: - Lf->type = LSOF_FILE_VNODE_VPORT; - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - break; -#endif /* solaris>=100000 */ - -#if solaris >= 20600 - case VPROC: - - /* - * The proc file system type is defined when the prnode is read. - */ - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - Lf->type = LSOF_FILE_NONE; - break; -#endif /* solaris>=20600 */ - -#if defined(HAS_VSOCK) - case VSOCK: - -# if solaris >= 20600 - if (so.so_family == AF_UNIX) { - Lf->type = LSOF_FILE_UNIX; - if (Funix) - Lf->sf |= SELUNX; - } else { - if (so.so_family == AF_INET) { - -# if defined(HASIPv6) - Lf->type = LSOF_FILE_IPV4; -# else /* !defined(HASIPv6) */ - Lf->type = LSOF_FILE_INET; -# endif /* defined(HASIPv6) */ - - (void)snpf(Namech, Namechl - 1, printsockty(so.so_type)); - Namech[Namechl - 1] = '\0'; - if (TcpStIn || UdpStIn || TcpStXn || UdpStXn) - Lf->sf |= SELEXCLF; - else if (Fnet && (FnetTy != 6)) - Lf->sf |= SELNET; - } - -# if defined(HASIPv6) - else if (so.so_family == AF_INET6) { - Lf->type = LSOF_FILE_IPV6; - (void)snpf(Namech, Namechl - 1, printsockty(so.so_type)); - Namech[Namechl - 1] = '\0'; - if (TcpStIn || UdpStIn || TcpStXn || UdpStXn) - Lf->sf |= SELEXCLF; - else if (Fnet && (FnetTy != 4)) - Lf->sf |= SELNET; - } -# endif /* defined(HASIPv6) */ - - else { - Lf->type = LSOF_FILE_SOCKET; - (void)printunkaf(ctx, so.so_family, 0); - ep = endnm(ctx, &sz); - (void)snpf(ep, sz, ", %s", printsockty(so.so_type)); - } - } -# endif /* solaris>=20600 */ - - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - break; -#endif /* defined(HAS_VSOCK) */ - - case VBAD: - Lf->type = LSOF_FILE_VNODE_VBAD; - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - break; - case VFIFO: - Lf->type = LSOF_FILE_VNODE_VFIFO; - if (!Lf->dev_ch || Lf->dev_ch[0] == '\0') { - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - } - break; - default: - Lf->dev = dev; - Lf->dev_def = devs; - Lf->rdev = rdev; - Lf->rdev_def = rdevs; - Lf->type = LSOF_FILE_UNKNOWN_RAW; - Lf->unknown_file_type_number = type; - } - Lf->ntype = Ntype; - /* - * If this a Solaris common vnode/snode void some information. - */ - if (Lf->is_com) - Lf->sz_def = Lf->inp_ty = 0; - /* - * If a file attach description remains, put it in the NAME column addition. - */ - if (fa) - (void)add_nma(ctx, fa, fal); - -#if defined(HASBLKDEV) - /* - * If this is a VBLK file and it's missing an inode number, try to - * supply one. - */ - if ((Lf->inp_ty == 0) && (type == VBLK)) - find_bl_ino(ctx); -#endif /* defined(HASBLKDEV) */ - - /* - * If this is a VCHR file and it's missing an inode number, try to - * supply one. - */ - if ((Lf->inp_ty == 0) && (type == VCHR)) { - find_ch_ino(ctx); - /* - * If the VCHR inode number still isn't known and this is a COMMON - * vnode file or a stream, or if a pseudo node ID lookup has been - * requested, see if an inode number can be derived from a pseudo - * or clone device node. - * - * If it can, save the pseudo or clone device for temporary - * use when searching for a match with a named file argument. - */ - if ((Lf->inp_ty == 0) && (Lf->is_com || Lf->is_stream || pnl) && - (Clone || Pseudo)) { - if (!sdp) { - if (rdevs || devs) { - if (Lf->is_stream && !pnl) - sdp = finddev(ctx, devs ? &dev : &DevDev, - rdevs ? &rdev : &Lf->dev, LOOKDEV_CLONE); - else - sdp = finddev(ctx, devs ? &dev : &DevDev, - rdevs ? &rdev : &Lf->dev, LOOKDEV_PSEUDO); - if (!sdp) - sdp = finddev(ctx, devs ? &dev : &DevDev, - rdevs ? &rdev : &Lf->dev, LOOKDEV_ALL); - if (sdp) { - if (!rdevs) { - Lf->rdev = Lf->dev; - Lf->rdev_def = rdevs = 1; - } - if (!devs) { - Lf->dev = DevDev; - devs = Lf->dev_def = 1; - } - } - } - } else { - - /* - * A local device structure has been located. Make sure - * that it's accompanied by device settings. - */ - if (!devs && vfs) { - dev = Lf->dev = vfs->dev; - devs = Lf->dev_def = 1; - } - if (!rdevs) { - Lf->rdev = rdev = sdp->rdev; - Lf->rdev_def = rdevs = 1; - } - } - if (sdp) { - - /* - * Process the local device information. - */ - trdev = sdp->rdev; - Lf->inode = sdp->inode; - Lf->inp_ty = trdevs = 1; - if (!Namech[0] || Lf->is_com) { - (void)snpf(Namech, Namechl - 1, "%s", sdp->name); - Namech[Namechl - 1] = '\0'; - } - if (Lf->is_com && !Lf->nma) { - len = (int)strlen("(COMMON)") + 1; - if (!(Lf->nma = (char *)malloc(len))) { - fd_to_string(Lf->fd_type, Lf->fd_num, fd); - (void)fprintf( - stderr, - "%s: no space for (COMMON): PID %d; FD %s\n", Pn, - Lp->pid, fd); - Error(ctx); - } - (void)snpf(Lf->nma, len, "(COMMON)"); - } - } - } - } - /* - * Record stream status. - */ - if (Lf->inp_ty == 0 && Lf->is_stream && strcmp(Lf->iproto, "STR") == 0) - Lf->inp_ty = 2; - /* - * Test for specified file. - */ - -#if defined(HASPROCFS) - if (Ntype == N_PROC) { - if (Procsrch) { - Procfind = 1; - Lf->sf |= SELNM; - } else { - for (pfi = Procfsid; pfi; pfi = pfi->next) { - if ((pfi->pid && pfi->pid == pids.pid_id) - -# if defined(HASPINODEN) - || (Lf->inp_ty == 1 && Lf->inode == pfi->inode) -# endif /* defined(HASPINODEN) */ - - ) { - pfi->f = 1; - if (!Namech[0]) { - (void)snpf(Namech, Namechl - 1, "%s", pfi->nm); - Namech[Namechl - 1] = '\0'; - } - Lf->sf |= SELNM; - break; - } - } - } - } else -#endif /* defined(HASPROCFS) */ - - { - if (Sfile) { - if (trdevs) { - rdev = Lf->rdev; - Lf->rdev = trdev; - tdef = Lf->rdev_def; - Lf->rdev_def = 1; - } - if (is_file_named(ctx, NULL, Ntype, type, 1)) - Lf->sf |= SELNM; - if (trdevs) { - Lf->rdev = rdev; - Lf->rdev_def = tdef; - } - } - } - /* - * Enter name characters. - */ - if (Namech[0]) - enter_nm(ctx, Namech); -} - -/* - * read_cni() - read common snode information - */ - -static int read_cni(struct lsof_context *ctx, /* context */ - struct snode *s, /* starting snode */ - struct vnode *rv, /* "real" vnode receiver */ - struct vnode *v, /* starting vnode */ - struct snode *rs, /* "real" snode receiver */ - struct dev_info *di, /* dev_info structure receiver */ - char *din, /* device info name receiver */ - int dinl) /* sizeof(*din) */ -{ - char tbuf[32]; - - if (read_nvn(ctx, (KA_T)v->v_data, (KA_T)s->s_commonvp, rv)) - return (1); - if (read_nsn(ctx, (KA_T)s->s_commonvp, (KA_T)rv->v_data, rs)) - return (1); - *din = '\0'; - if (rs->s_dip) { - if (kread(ctx, (KA_T)rs->s_dip, (char *)di, sizeof(struct dev_info))) { - (void)snpf(Namech, Namechl - 1, - "common snode at %s: no dev info: %s", - print_kptr((KA_T)rv->v_data, tbuf, sizeof(tbuf)), - print_kptr((KA_T)rs->s_dip, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - if (di->devi_name && - kread(ctx, (KA_T)di->devi_name, din, dinl - 1) == 0) - din[dinl - 1] = '\0'; - } - return (0); -} - -/* - * readinode() - read inode - */ - -static int readinode(struct lsof_context *ctx, /* context */ - KA_T ia, /* inode kernel address */ - struct inode *i) /* inode buffer */ -{ - if (kread(ctx, (KA_T)ia, (char *)i, sizeof(struct inode))) { - (void)snpf(Namech, Namechl - 1, "can't read inode at %s", - print_kptr((KA_T)ia, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -#if solaris >= 20500 -/* - * read_ndn() - read node's door node - */ - -static int read_ndn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing vnode's address */ - KA_T da, /* door node's address */ - struct door_node *dn) /* door node receiver */ -{ - char tbuf[32]; - - if (!da || kread(ctx, (KA_T)da, (char *)dn, sizeof(struct door_node))) { - (void)snpf(Namech, Namechl - 1, "vnode at %s: can't read door_node: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(da, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=20500 */ - -/* - * read_mi() - read stream's module information - */ - -static void read_mi(struct lsof_context *ctx, /* context */ - KA_T s, /* kernel stream pointer address */ - dev_t *rdev, /* raw device pointer */ - caddr_t so, /* so_so return (Solaris) */ - int *so_st, /* so_so status */ - KA_T *so_ad, /* so_so addresses */ - struct l_dev **sdp) /* returned device pointer */ -{ - struct l_dev *dp; - int i, j, k, nl; - KA_T ka; - struct module_info mi; - char mn[STRNML]; - struct stdata sd; - struct queue q; - struct qinit qi; - KA_T qp; - /* - * If there is no stream pointer, or we can't read the stream head, - * return. - */ - if (!s) - return; - if (kread(ctx, (KA_T)s, (char *)&sd, sizeof(sd))) { - (void)snpf(Namech, Namechl - 1, "can't read stream head: %s", - print_kptr(s, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return; - } - /* - * Follow the stream head to each of its queue structures, retrieving the - * module names from each queue's q_info->qi_minfo->mi_idname chain of - * structures. Separate each additional name from the previous one with - * "->". - * - * Ignore failures to read all but queue structure chain entries. - * - * Ignore module names that end in "head". - */ - k = 0; - Namech[0] = '\0'; - if (!(dp = finddev(ctx, &DevDev, rdev, LOOKDEV_CLONE))) - dp = finddev(ctx, &DevDev, rdev, LOOKDEV_ALL); - if (dp) { - (void)snpf(Namech, Namechl - 1, "%s", dp->name); - Namech[Namechl - 1] = '\0'; - k = (int)strlen(Namech); - *sdp = dp; - } else - (void)snpf(Lf->iproto, sizeof(Lf->iproto), "STR"); - nl = sizeof(mn) - 1; - mn[nl] = '\0'; - qp = (KA_T)sd.sd_wrq; - for (i = 0; qp && i < 20; i++, qp = (KA_T)q.q_next) { - if (!qp || kread(ctx, qp, (char *)&q, sizeof(q))) - break; - if ((ka = (KA_T)q.q_qinfo) == (KA_T)NULL || - kread(ctx, ka, (char *)&qi, sizeof(qi))) - continue; - if ((ka = (KA_T)qi.qi_minfo) == (KA_T)NULL || - kread(ctx, ka, (char *)&mi, sizeof(mi))) - continue; - if ((ka = (KA_T)mi.mi_idname) == (KA_T)NULL || kread(ctx, ka, mn, nl)) - continue; - if ((j = (int)strlen(mn)) < 1) - continue; - if (j >= 4 && strcmp(&mn[j - 4], "head") == 0) - continue; - -#if solaris < 100000 - if (strcmp(mn, "sockmod") == 0) { - - /* - * Save the Solaris sockmod device and inode numbers. - */ - if (so) { - - struct so_so s; - - if (!kread(ctx, (KA_T)q.q_ptr, (char *)&s, sizeof(s))) { - if (!(*so_st)) - so_ad[0] = (KA_T)q.q_ptr; - else - so_ad[1] = (KA_T)q.q_ptr; - (void)savesockmod(&s, (struct so_so *)so, so_st); - } - } - } -#endif /* solaris<100000 */ - - if (k) { - if ((k + 2) > (Namechl - 1)) - break; - (void)snpf(&Namech[k], Namechl - k, "->"); - k += 2; - } - if ((k + j) > (Namechl - 1)) - break; - (void)snpf(&Namech[k], Namechl - k, "%s", mn); - k += j; - } -} - -#if solaris >= 20500 - -/* - * read_nan(na, ca, cn) - read node's autofs node - */ - -static int read_nan(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T aa, /* autofs node address */ - -# if solaris < 20600 - struct autonode *rn) /* autofs node receiver */ -# else /* solaris>=20600 */ - struct fnnode *rn) /* autofs node receiver */ -# endif /* solaris<20600 */ - -{ - char tbuf[32]; - -# if solaris < 20600 - if (!aa || kread(ctx, (KA_T)aa, (char *)rn, sizeof(struct autonode))) -# else /* solaris>=20600 */ - if (!aa || kread(ctx, (KA_T)aa, (char *)rn, sizeof(struct fnnode))) -# endif /* solaris<20600 */ - - { - (void)snpf(Namech, Namechl - 1, - -# if solaris < 20600 - "node at %s: can't read autonode: %s", -# else /* solaris>=20600 */ - "node at %s: can't read fnnode: %s", -# endif /* solaris<20600 */ - - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(aa, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=20500 */ - -#if defined(HASCACHEFS) -/* - * read_ncn(na, ca, cn) - read node's cache node - */ - -static int read_ncn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T ca, /* cache node address */ - struct cnode *cn) /* cache node receiver */ -{ - char tbuf[32]; - - if (!ca || kread(ctx, (KA_T)ca, (char *)cn, sizeof(struct cnode))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read cnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(ca, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* defined(HASCACHEFS) */ - -#if solaris >= 100000 -/* - * read_nctfsn(ty, na, ca, cn) - read node's cache node - */ - -static int read_nctfsn(struct lsof_context *ctx, /* context */ - int ty, /* node type -- i.e., N_CTFS* */ - KA_T na, /* containing node's address */ - KA_T ca, /* cache node address */ - char *cn) /* CTFS node receiver */ -{ - char *cp, *nm, tbuf[32]; - READLEN_T sz; - - switch (ty) { - case N_CTFSADIR: - nm = "ADIR"; - sz = (READLEN_T)sizeof(ctfs_adirnode_t); - break; - case N_CTFSBUND: - nm = "BUND"; - sz = (READLEN_T)sizeof(ctfs_bunode_t); - break; - case N_CTFSCDIR: - nm = "CDIR"; - sz = (READLEN_T)sizeof(ctfs_cdirnode_t); - break; - case N_CTFSCTL: - nm = "CTL"; - sz = (READLEN_T)sizeof(ctfs_ctlnode_t); - break; - case N_CTFSEVT: - nm = "EVT"; - sz = (READLEN_T)sizeof(ctfs_evnode_t); - break; - case N_CTFSLATE: - nm = "LATE"; - sz = (READLEN_T)sizeof(ctfs_latenode_t); - break; - case N_CTFSROOT: - nm = "ROOT"; - sz = (READLEN_T)sizeof(ctfs_rootnode_t); - break; - case N_CTFSSTAT: - nm = "STAT"; - sz = (READLEN_T)sizeof(ctfs_ctlnode_t); - break; - case N_CTFSSYM: - nm = "SYM"; - sz = (READLEN_T)sizeof(ctfs_symnode_t); - break; - case N_CTFSTDIR: - nm = "TDIR"; - sz = (READLEN_T)sizeof(ctfs_tdirnode_t); - break; - case N_CTFSTMPL: - nm = "TMPL"; - sz = (READLEN_T)sizeof(ctfs_tmplnode_t); - break; - default: - (void)snpf(Namech, Namechl - 1, "unknown CTFS node type: %d", ty); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - if (!ca || kread(ctx, (KA_T)ca, cn, sz)) { - (void)snpf(Namech, Namechl - 1, - "node at %s: can't read CTFS %s node: %s", - print_kptr(na, tbuf, sizeof(tbuf)), nm, - print_kptr(ca, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=100000 */ - -/* - * read_nfn() - read node's fifonode - */ - -static int read_nfn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T fa, /* fifonode address */ - struct fifonode *f) /* fifonode receiver */ -{ - char tbuf[32]; - - if (!fa || readfifonode(ctx, fa, f)) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read fifonode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(fa, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -/* - * read_nhn() - read node's High Sierra node - */ - -static int read_nhn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T ha, /* hsnode address */ - struct hsnode *h) /* hsnode receiver */ -{ - char tbuf[32]; - - if (!ha || readhsnode(ctx, ha, h)) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read hsnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(ha, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -/* - * read_nin() - read node's inode - */ - -static int read_nin(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T ia, /* kernel inode address */ - struct inode *i) /* inode receiver */ -{ - char tbuf[32]; - - if (!ia || readinode(ctx, ia, i)) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read inode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(ia, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -/* - * read_nln(na, la, ln) - read node's loopback node - */ - -static int read_nln(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T la, /* loopback node address */ - struct lnode *ln) /* loopback node receiver */ -{ - char tbuf[32]; - - if (!la || kread(ctx, (KA_T)la, (char *)ln, sizeof(struct lnode))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read lnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(la, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -/* - * read_nnn() - read node's namenode - */ - -static int read_nnn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T nna, /* namenode address */ - struct namenode *nn) /* namenode receiver */ -{ - char tbuf[32]; - - if (!nna || kread(ctx, (KA_T)nna, (char *)nn, sizeof(struct namenode))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read namenode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(nna, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -/* - * read_nmn() - read node's mvfsnode - */ - -static int read_nmn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T ma, /* kernel mvfsnode address */ - struct mvfsnode *m) /* mvfsnode receiver */ -{ - char tbuf[32]; - - if (!ma || kread(ctx, (KA_T)ma, (char *)m, sizeof(struct mvfsnode))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read mvfsnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(ma, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -#if defined(HASPROCFS) -/* - * read_npi() - read node's /proc file system information - */ - -static int read_npi(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - struct vnode *v, /* containing vnode */ - struct pid *pids) /* pid structure receiver */ -{ - struct as as; - struct proc p; - struct prnode pr; - char tbuf[32]; - -# if solaris >= 20600 - prcommon_t pc, ppc; - int pcs, ppcs, prpcs, prppcs; - struct proc pp; - kthread_t thread; - pid_t prpid; - id_t prtid; - char *ty = (char *)NULL; -# endif /* solaris>=20600 */ - - if (!v->v_data || kread(ctx, (KA_T)v->v_data, (char *)&pr, sizeof(pr))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read prnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr((KA_T)v->v_data, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - -# if solaris < 20600 - /* - * For Solaris < 2.6: - * * Read the proc structure, get the process size and PID; - * * Return the PID; - * * Enter a name, constructed from the file system and PID; - * * Enter an inode number, constructed from the PID. - */ - if (!pr.pr_proc) { - if (v->v_type == VDIR) { - (void)snpf(Namech, Namechl - 1, "/%s", HASPROCFS); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - Lf->inode = (INODETYPE)PR_ROOTINO; - Lf->inp_ty = 1; - } else { - (void)snpf(Namech, Namechl - 1, "/%s/", HASPROCFS); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - Lf->inp_ty = 0; - } - return (0); - } - if (kread(ctx, (KA_T)pr.pr_proc, (char *)&p, sizeof(p))) { - (void)snpf(Namech, Namechl - 1, "prnode at %s: can't read proc: %s", - print_kptr((KA_T)v->v_data, tbuf, sizeof(tbuf)), - print_kptr((KA_T)pr.pr_proc, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - if (p.p_as && !kread(ctx, (KA_T)p.p_as, (char *)&as, sizeof(as))) { - Lf->sz = (SZOFFTYPE)as.a_size; - Lf->sz_def = 1; - } - if (!p.p_pidp || - kread(ctx, (KA_T)p.p_pidp, (char *)pids, sizeof(struct pid))) { - (void)snpf(Namech, Namechl - 1, "proc struct at %s: can't read pid: %s", - print_kptr((KA_T)pr.pr_proc, tbuf, sizeof(tbuf)), - print_kptr((KA_T)p.p_pidp, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - (void)snpf(Namech, Namechl, "/%s/%d", HASPROCFS, (int)pids->pid_id); - Namech[Namechl - 1] = '\0'; - Lf->inode = (INODETYPE)ptoi(pids->pid_id); - Lf->inp_ty = 1; -# else /* solaris>=20600 */ - /* - * Enter the >= Solaris 2.6 inode number. - */ - Lf->inode = (INODETYPE)pr.pr_ino; - Lf->inp_ty = 1; - /* - * Read the >= Solaris 2.6 prnode common structures. - * - * Return the PID number. - * - * Identify the lwp PID (the thread ID). - */ - if (pr.pr_common && - kread(ctx, (KA_T)pr.pr_common, (char *)&pc, sizeof(pc)) == 0) { - pcs = 1; - if (pc.prc_proc && - kread(ctx, (KA_T)pc.prc_proc, (char *)&p, sizeof(p)) == 0) - prpcs = 1; - else - prpcs = 0; - } else - pcs = prpcs = 0; - if (pr.pr_pcommon && - kread(ctx, (KA_T)pr.pr_pcommon, (char *)&ppc, sizeof(ppc)) == 0) { - ppcs = 1; - if (ppc.prc_proc && - kread(ctx, (KA_T)ppc.prc_proc, (char *)&pp, sizeof(pp)) == 0) - prppcs = 1; - else - prppcs = 0; - } else - ppcs = prppcs = 0; - if (prpcs && p.p_pidp && - kread(ctx, (KA_T)p.p_pidp, (char *)pids, sizeof(struct pid)) == 0) - prpid = pids->pid_id; - else if (prppcs && pp.p_pidp && - kread(ctx, (KA_T)pp.p_pidp, (char *)pids, sizeof(struct pid)) == 0) - prpid = pids->pid_id; - else - pids->pid_id = prpid = (pid_t)0; - if (pcs && pc.prc_thread && - kread(ctx, (KA_T)pc.prc_thread, (char *)&thread, sizeof(kthread_t)) == - 0) - prtid = thread.t_tid; - else if (ppcs && ppc.prc_thread && - kread(ctx, (KA_T)ppc.prc_thread, (char *)&thread, - sizeof(kthread_t)) == 0) - prtid = thread.t_tid; - else - prtid = (id_t)0; - /* - * Identify the Solaris 2.6 /proc file system name, file size, and file - * type. - */ - switch (pr.pr_type) { - case PR_PROCDIR: - (void)snpf(Namech, Namechl - 1, "/%s", HASPROCFS); - Lf->type = LSOF_FILE_PROC_DIR; - break; - case PR_PIDDIR: - (void)snpf(Namech, Namechl - 1, "/%s/%d", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_DIR; - break; - case PR_AS: - (void)snpf(Namech, Namechl - 1, "/%s/%d/as", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_AS; - if (prpcs && - kread(ctx, (KA_T)pc.prc_proc, (char *)&p, sizeof(p)) == 0 && - p.p_as && kread(ctx, (KA_T)p.p_as, (char *)&as, sizeof(as)) == 0) { - Lf->sz = (SZOFFTYPE)as.a_size; - Lf->sz_def = 1; - } - break; - case PR_CTL: - (void)snpf(Namech, Namechl - 1, "/%s/%d/ctl", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_CTRL; - break; - case PR_STATUS: - (void)snpf(Namech, Namechl - 1, "/%s/%d/status", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_STATUS; - break; - case PR_LSTATUS: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lstatus", HASPROCFS, - (int)prpid); - Lf->type = LSOF_FILE_PROC_LSTATUS; - break; - case PR_PSINFO: - (void)snpf(Namech, Namechl - 1, "/%s/%d/psinfo", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_PSINFO; - break; - case PR_LPSINFO: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lpsinfo", HASPROCFS, - (int)prpid); - Lf->type = LSOF_FILE_PROC_LPS_INFO; - break; - case PR_MAP: - (void)snpf(Namech, Namechl - 1, "/%s/%d/map", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_MAP; - break; - case PR_RMAP: - (void)snpf(Namech, Namechl - 1, "/%s/%d/rmap", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_RMAP; - break; - case PR_XMAP: - (void)snpf(Namech, Namechl - 1, "/%s/%d/xmap", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_XMAP; - break; - case PR_CRED: - (void)snpf(Namech, Namechl - 1, "/%s/%d/cred", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_CRED; - break; - case PR_SIGACT: - (void)snpf(Namech, Namechl - 1, "/%s/%d/sigact", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_SIGACT; - break; - case PR_AUXV: - (void)snpf(Namech, Namechl - 1, "/%s/%d/auxv", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_AUXV; - break; - -# if defined(HASPR_LDT) - case PR_LDT: - (void)snpf(Namech, Namechl - 1, "/%s/%d/ldt", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_LDT; - break; -# endif /* defined(HASPR_LDT) */ - - case PR_USAGE: - (void)snpf(Namech, Namechl - 1, "/%s/%d/usage", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_USAGE; - break; - case PR_LUSAGE: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lusage", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_LUSAGE; - break; - case PR_PAGEDATA: - (void)snpf(Namech, Namechl - 1, "/%s/%d/pagedata", HASPROCFS, - (int)prpid); - Lf->type = LSOF_FILE_PROC_PAGE_DATA; - break; - case PR_WATCH: - (void)snpf(Namech, Namechl - 1, "/%s/%d/watch", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_WATCH; - break; - case PR_CURDIR: - (void)snpf(Namech, Namechl - 1, "/%s/%d/cwd", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_CWD; - break; - case PR_ROOTDIR: - (void)snpf(Namech, Namechl - 1, "/%s/%d/root", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_ROOT; - break; - case PR_FDDIR: - (void)snpf(Namech, Namechl - 1, "/%s/%d/fd", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_FD_DIR; - break; - case PR_FD: - (void)snpf(Namech, Namechl - 1, "/%s/%d/fd/%d", HASPROCFS, (int)prpid, - pr.pr_index); - Lf->type = LSOF_FILE_PROC_FD; - break; - case PR_OBJECTDIR: - (void)snpf(Namech, Namechl - 1, "/%s/%d/object", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_OBJ_DIR; - break; - case PR_OBJECT: - (void)snpf(Namech, Namechl - 1, "/%s/%d/object/", HASPROCFS, - (int)prpid); - Lf->type = LSOF_FILE_PROC_OBJ; - break; - case PR_LWPDIR: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lpw", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_LWP_DIR; - break; - case PR_LWPIDDIR: - (void)snpf(Namech, Namechl, "/%s/%d/lwp/%d", HASPROCFS, (int)prpid, - (int)prtid); - Lf->type = LSOF_FILE_PROC_LWP_DIR; - break; - case PR_LWPCTL: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lwp/%d/lwpctl", HASPROCFS, - (int)prpid, (int)prtid); - Lf->type = LSOF_FILE_PROC_LWP_CTL; - break; - case PR_LWPSTATUS: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lwp/%d/lwpstatus", HASPROCFS, - (int)prpid, (int)prtid); - Lf->type = LSOF_FILE_PROC_LWP_STATUS; - break; - case PR_LWPSINFO: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lwp/%d/lwpsinfo", HASPROCFS, - (int)prpid, (int)prtid); - Lf->type = LSOF_FILE_PROC_LWP_SINFO; - break; - case PR_LWPUSAGE: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lwp/%d/lwpusage", HASPROCFS, - (int)prpid, (int)prtid); - Lf->type = LSOF_FILE_PROC_LWP_USAGE; - break; - case PR_XREGS: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lwp/%d/xregs", HASPROCFS, - (int)prpid, (int)prtid); - Lf->type = LSOF_FILE_PROC_LWP_XREGS; - break; - -# if defined(HASPR_GWINDOWS) - case PR_GWINDOWS: - (void)snpf(Namech, Namechl - 1, "/%s/%d/lwp/%d/gwindows", HASPROCFS, - (int)prpid, (int)prtid); - Lf->type = LSOF_FILE_PROC_LWP_GWINDOWS; - break; -# endif /* defined(HASPR_GWINDOWS) */ - -# if defined(PR_PIDFILE) - case PR_PIDFILE: - (void)snpf(Namech, Namechl - 1, "/%s/%d", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_OLD_PID; - break; -# endif /* defined(PR_PIDFILE) */ - -# if defined(PR_LWPIDFILE) - case PR_LWPIDFILE: - (void)snpf(Namech, Namechl - 1, "/%s/%d", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_OLD_LWP; - break; -# endif /* defined(PR_LWPIDFILE) */ - - case PR_OPAGEDATA: - (void)snpf(Namech, Namechl - 1, "/%s/%d", HASPROCFS, (int)prpid); - Lf->type = LSOF_FILE_PROC_OLD_PAGE; - break; - default: - Lf->type = LSOF_FILE_UNKNOWN_RAW; - Lf->unknown_file_type_number = pr.pr_type; - } - /* - * Record the Solaris 2.6 /proc file system inode number. - */ - Lf->inode = (INODETYPE)pr.pr_ino; - Lf->inp_ty = 1; -# endif /* solaris<20600 */ - - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (0); -} -#endif /* defined(HASPROCFS) */ - -/* - * read_npn() - read node's pcnode - */ - -static int read_npn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T pa, /* pcnode address */ - struct pcnode *p) /* pcnode receiver */ -{ - char tbuf[32]; - - if (!pa || kread(ctx, pa, (char *)p, sizeof(struct pcnode))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read pcnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(pa, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -#if solaris >= 100000 -/* - * read_nprtn() - read node's port node - */ - -static int read_nprtn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T pa, /* port node address */ - port_t *p) /* port node receiver */ -{ - char tbuf[32]; - - if (!pa || kread(ctx, pa, (char *)p, sizeof(port_t))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read port node: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(pa, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=100000 */ - -/* - * read_nrn() - read node's rnode - */ - -static int read_nrn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T ra, /* rnode address */ - struct rnode *r) /* rnode receiver */ -{ - char tbuf[32]; - - if (!ra || readrnode(ctx, ra, r)) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read rnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(ra, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -#if solaris >= 100000 -/* - * read_nrn4() - read node's rnode4 - */ - -static int read_nrn4(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T ra, /* rnode address */ - struct rnode4 *r) /* rnode receiver */ -{ - char tbuf[32]; - - if (!ra || kread(ctx, (KA_T)ra, (char *)r, sizeof(struct rnode4))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read rnode4: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(ra, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=100000 */ - -#if solaris >= 110000 -/* - * read_nsdn() - read node's sdev_node - */ - -static int read_nsdn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T sa, /* sdev_node address */ - struct sdev_node *sdn, /* sdev_node receiver */ - struct vattr *sdva) /* sdev_node's vattr receiver */ -{ - KA_T va; - char tbuf[32], tbuf1[32]; - - if (!sa || kread(ctx, (KA_T)sa, (char *)sdn, sizeof(struct sdev_node))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read sdev_node: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(sa, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - if (!(va = (KA_T)sdn->sdev_attr) || - kread(ctx, va, (char *)sdva, sizeof(struct vattr))) { - (void)snpf(Namech, Namechl - 1, - "node at %s; sdev_node at %s: can't read vattr: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(sa, tbuf1, sizeof(tbuf1)), - print_kptr(va, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=110000 */ - -#if solaris >= 20600 -/* - * read_nson() - read node's sonode - */ - -static int read_nson(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T sa, /* sonode address */ - struct sonode *sn) /* sonode receiver */ - -{ - char tbuf[32]; - - if (!sa || kread(ctx, (KA_T)sa, (char *)sn, sizeof(struct sonode))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read sonode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(sa, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=20600 */ - -/* - * read_nsn() - read node's snode - */ - -static int read_nsn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T sa, /* snode address */ - struct snode *s) /* snode receiver */ -{ - char tbuf[32]; - - if (!sa || readsnode(ctx, sa, s)) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read snode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(sa, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -#if solaris >= 110000 -/* - * printsockty() - format a socket type without using lsof CLI state - */ -static char *printsockty(int ty) { - static char buf[64]; - char *cp; - - switch (ty) { -#if defined(SOCK_STREAM) - case SOCK_STREAM: - cp = "STREAM"; - break; -#endif -#if defined(SOCK_DGRAM) - case SOCK_DGRAM: - cp = "DGRAM"; - break; -#endif -#if defined(SOCK_RAW) - case SOCK_RAW: - cp = "RAW"; - break; -#endif -#if defined(SOCK_RDM) - case SOCK_RDM: - cp = "RDM"; - break; -#endif -#if defined(SOCK_SEQPACKET) - case SOCK_SEQPACKET: - cp = "SEQPACKET"; - break; -#endif - default: - (void)snpf(buf, sizeof(buf), "SOCK_%#x", ty); - return (buf); - } - (void)snpf(buf, sizeof(buf), "SOCK_%s", cp); - return (buf); -} - -/* - * read_nsti() - read socket node's info - */ - -static int read_nsti(struct lsof_context *ctx, /* context */ - struct sonode *so, /* socket's sonode */ - sotpi_info_t *stpi) /* local socket info receiver */ -{ - char tbuf[32]; - - (void)CTF_init(ctx, &Sockfs_ctfs, SOCKFS_MOD_FORMAT, Sockfs_requests); - if (Sockfs_ctfs < 0) { - (void)snpf(Namech, Namechl - 1, "sonode: CTF information unavailable"); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - if (!so || !so->so_priv || - CTF_MEMBER_READ(so->so_priv, stpi, sotpi_info_members, sti_dev) || - CTF_MEMBER_READ(so->so_priv, stpi, sotpi_info_members, sti_laddr) || - CTF_MEMBER_READ(so->so_priv, stpi, sotpi_info_members, sti_faddr) || - CTF_MEMBER_READ(so->so_priv, stpi, sotpi_info_members, sti_ux_laddr) || - CTF_MEMBER_READ(so->so_priv, stpi, sotpi_info_members, sti_ux_faddr) || - CTF_MEMBER_READ(so->so_priv, stpi, sotpi_info_members, sti_serv_type)) { - (void)snpf(Namech, Namechl - 1, "sonode at %s: can't read so_priv: %s", - print_kptr((KA_T)so, tbuf, sizeof(tbuf)), - print_kptr((KA_T)so->so_priv, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=110000 */ - -/* - * read_ntn() - read node's tmpnode - */ - -static int read_ntn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T ta, /* tmpnode address */ - struct tmpnode *t) /* tmpnode receiver */ -{ - char tbuf[32]; - - if (!ta || readtnode(ctx, ta, t)) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read tnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(ta, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -#if solaris >= 20600 -/* - * read_nusa() - read sondode's UNIX socket address - */ - -static int read_nusa(struct lsof_context *ctx, /* context */ - struct soaddr *so, /* kernel socket info structure */ - struct sockaddr_un *ua) /* local sockaddr_un address */ -{ - KA_T a; - int len; - int min = offsetof(struct sockaddr_un, sun_path); - - ua->sun_path[0] = '\0'; - - if (!(a = (KA_T)so->soa_sa) || (len = so->soa_len) < (min + 2) || - len > (int)sizeof(struct sockaddr_un) || - kread(ctx, a, (char *)ua, len) || ua->sun_family != AF_UNIX) - return (0); - len -= min; - if (len >= sizeof(ua->sun_path)) - len = sizeof(ua->sun_path) - 1; - ua->sun_path[len] = '\0'; - return ((int)strlen(ua->sun_path)); -} -#endif /* solaris>=20600 */ - -/* - * read_nvn() - read node's vnode - */ - -static int read_nvn(struct lsof_context *ctx, /* context */ - KA_T na, /* node's address */ - KA_T va, /* vnode address */ - struct vnode *v) /* vnode receiver */ -{ - char tbuf[32]; - - if (readvnode(ctx, va, v)) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read real vnode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(va, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -#if defined(HAS_ZFS) -#if solaris >= 110000 -/* - * Solaris 11.4 CBE ZFS has no znode/zfsvfs CTF types. The offsets below - * come from the kernel's own mdb type information: - * - * znode_t: z_zfsvfs=0x00, z_vnode=0x08, z_id=0x10, - * z_size=0xc0, z_links=0x118 (z_phys is absent) - * zfsvfs_t: z_vfs=0x00 - */ -# define ZFS114_OFF_Z_ZFSVFS 0x00 -# define ZFS114_OFF_Z_VNODE 0x08 -# define ZFS114_OFF_Z_ID 0x10 -# define ZFS114_OFF_Z_SIZE 0xc0 -# define ZFS114_OFF_Z_LINKS 0x118 -# define ZFS114_OFF_ZVFS_Z_VFS 0x00 - -static int read_znode114(struct lsof_context *ctx, KA_T na, KA_T nza, - znode_t *zn) { - char tbuf[32]; - - if (!nza) { - (void)snpf(Namech, Namechl - 1, "node at %s: no znode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(nza, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - if (kread(ctx, nza + ZFS114_OFF_Z_ZFSVFS, (char *)&zn->z_zfsvfs, - sizeof(zn->z_zfsvfs)) || - kread(ctx, nza + ZFS114_OFF_Z_VNODE, (char *)&zn->z_vnode, - sizeof(zn->z_vnode)) || - kread(ctx, nza + ZFS114_OFF_Z_ID, (char *)&zn->z_id, - sizeof(zn->z_id)) || - kread(ctx, nza + ZFS114_OFF_Z_SIZE, (char *)&zn->z_size, - sizeof(zn->z_size)) || - kread(ctx, nza + ZFS114_OFF_Z_LINKS, (char *)&zn->z_links, - sizeof(zn->z_links))) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read znode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(nza, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - /* - * A wrong znode_t layout would make the offsets above read unrelated - * kernel memory. The vnode back-pointer is a cheap validity check: on - * every supported layout z_vnode points at the vnode being processed. - * If it does not, report the unsupported layout instead of producing - * bogus file information. - */ - if ((KA_T)zn->z_vnode != na) { - (void)snpf(Namech, Namechl - 1, - "node at %s: unsupported znode layout: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr((KA_T)zn->z_vnode, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - zn->z_phys = (znode_phys_t *)NULL; - return (0); -} - -static int read_zvfs114(struct lsof_context *ctx, KA_T nza, KA_T nzva, - zfsvfs_t *zv) { - char tbuf[32]; - - if (!nzva || kread(ctx, nzva + ZFS114_OFF_ZVFS_Z_VFS, - (char *)&zv->z_vfs, sizeof(zv->z_vfs))) { - (void)snpf(Namech, Namechl - 1, "znode at %s: can't read zfsvfs: %s", - print_kptr(nza, tbuf, sizeof(tbuf)), - print_kptr(nzva, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris >= 110000 */ -/* - * read_nzn() - read node's ZFS node - */ - -static int read_nzn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing node's address */ - KA_T nza, /* znode address */ - znode_t *zn) /* znode receiver */ -{ - int err = 0; /* error flag */ - CTF_member_t *mp; /* member pointer */ - char tbuf[32]; /* temporary buffer */ - znode_phys_t zp; /* physical znode */ - - (void)CTF_init(ctx, &ZFS_ctfs, ZFS_MOD_FORMAT, ZFS_requests); -#if solaris >= 110000 - if (ZFS_ctfs < 0) - return read_znode114(ctx, na, nza, zn); -#endif /* solaris >= 110000 */ - if (!nza || CTF_MEMBER_READ(nza, zn, znode_members, z_zfsvfs) || - CTF_MEMBER_READ(nza, zn, znode_members, z_vnode) || - CTF_MEMBER_READ(nza, zn, znode_members, z_id) || - CTF_MEMBER_READ(nza, zn, znode_members, z_phys) || - CTF_MEMBER_READ(nza, zn, znode_members, z_links) || - CTF_MEMBER_READ(nza, zn, znode_members, z_size)) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read znode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(nza, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - /* - * If the physical znode pointer is defined, read the physizal znode - * and propagate its values to the znode. - */ - if (znode_members[MX_z_phys].m_offset != CTF_MEMBER_UNDEF) { - err = read_nznp(ctx, nza, (KA_T)zn->z_phys, &zp); - if (!err) { - zn->z_links = zp.zp_links; - zn->z_size = zp.zp_size; - } - } else { - - /* - * Make sure z_link and z_size are defined when z_phys isn't. - */ - if (znode_members[MX_z_links].m_offset == CTF_MEMBER_UNDEF) { - (void)snpf(Namech, Namechl - 1, - "node at %s: can't read z_links: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(nza, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - err = 1; - } - if (znode_members[MX_z_size].m_offset == CTF_MEMBER_UNDEF) { - (void)snpf(Namech, Namechl - 1, "node at %s: can't read z_size: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(nza, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - err = 1; - } - } - return (err); -} - -/* - * read_nznp() - read znode's persistent znode - */ - -static int read_nznp(struct lsof_context *ctx, /* context */ - KA_T nza, /* containing znode's address */ - KA_T nzpa, /* persistent znode address */ - znode_phys_t *zp) /* persistent znode receiver */ -{ - char tbuf[32]; - - (void)CTF_init(ctx, &ZFS_ctfs, ZFS_MOD_FORMAT, ZFS_requests); - if (ZFS_ctfs < 0) { - (void)snpf(Namech, Namechl - 1, - "znode at %s: CTF information unavailable", - print_kptr(nza, tbuf, sizeof(tbuf))); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - if (!nzpa || CTF_MEMBER_READ(nzpa, zp, znode_phys_members, zp_size) || - CTF_MEMBER_READ(nzpa, zp, znode_phys_members, zp_links)) { - (void)snpf(Namech, Namechl - 1, - "znode at %s: " - "can't read znode_phys: %s", - print_kptr(nza, tbuf, sizeof(tbuf)), - print_kptr(nzpa, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} - -/* - * read_nzvfs() - read znode's associated vfs - */ - -static int read_nzvfs(struct lsof_context *ctx, /* context */ - KA_T nza, /* containing znode's address */ - KA_T nzva, /* associated vfs address */ - zfsvfs_t *zv) /* associated vfs receiver */ -{ - char tbuf[32]; - - (void)CTF_init(ctx, &ZFS_ctfs, ZFS_MOD_FORMAT, ZFS_requests); -#if solaris >= 110000 - if (ZFS_ctfs < 0) - return read_zvfs114(ctx, nza, nzva, zv); -#endif /* solaris >= 110000 */ - if (!nzva || CTF_MEMBER_READ(nzva, zv, zfsvfs_members, z_vfs)) { - (void)snpf(Namech, Namechl - 1, "znode at %s: can't read zfsvfs: %s", - print_kptr(nza, tbuf, sizeof(tbuf)), - print_kptr(nzva, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* defined(HAS_ZFS) */ - -#if solaris < 100000 -/* - * savesockmod() - save addresses from sockmod so_so structure - */ - -static void -savesockmod(struct so_so *so, /* new so_so structure pointer */ - struct so_so *sop, /* previous so_so structure pointer */ - int *so_st) /* status of *sop (0 if not loaded) */ -{ - -# if solaris < 20500 - dev_t d1, d2, d3; -# endif /* solaris<20500 */ - -# define luxadr lux_dev.addr.tu_addr -# define luxdev lux_dev.addr.tu_addr.dev -# define luxino lux_dev.addr.tu_addr.ino -# define ruxadr rux_dev.addr.tu_addr -# define ruxdev rux_dev.addr.tu_addr.dev -# define ruxino rux_dev.addr.tu_addr.ino - -# if solaris < 20500 - /* - * If either address in the new structure is missing a device number, clear - * its corresponding inode number. Then sort the inode-less device numbers. - */ - if (!so->luxdev) - so->luxino = (ino_t)0; - if (!so->ruxdev) - so->ruxino = (ino_t)0; - if (!so->luxino && !so->ruxino) { - if (so->luxdev > so->ruxdev) { - d2 = so->luxdev; - d1 = so->luxdev = so->ruxdev; - so->ruxdev = d2; - } else { - d1 = so->luxdev; - d2 = so->ruxdev; - } - } else - d1 = d2 = (dev_t)0; - /* - * If the previous structure hasn't been loaded, save the new one in it with - * adjusted or sorted addresses. - */ - if (!*so_st) { - if (so->luxdev && so->luxino) { - *sop = *so; - sop->ruxdev = (dev_t)0; - sop->ruxino = (ino_t)0; - *so_st = 1; - return; - } - if (so->ruxdev && so->ruxino) { - *sop = *so; - sop->luxadr = sop->ruxadr; - sop->ruxdev = (dev_t)0; - sop->ruxino = (ino_t)0; - *so_st = 1; - return; - } - *sop = *so; - *so_st = 1; - return; - } - /* - * See if the new sockmod addresses need to be merged with the previous - * ones: - * - * * Don't merge if the previous so_so structure's lux_dev has a non- - * zero device and a non-zero inode number. - * - * * If either of the device/inode pairs in the new structure is non- - * zero, propagate them to the previous so_so structure. - * - * * Don't merge if the both device numbers in the new structure are - * zero. - */ - if (sop->luxdev && sop->luxino) - return; - if (so->luxdev && so->luxino) { - sop->luxadr = so->luxadr; - sop->ruxdev = (dev_t)0; - sop->ruxino = (ino_t)0; - return; - } - if (so->ruxdev && so->ruxino) { - sop->luxadr = so->ruxadr; - sop->ruxdev = (dev_t)0; - sop->ruxino = (ino_t)0; - return; - } - if (!so->luxdev && !so->ruxdev) - return; - /* - * Check the previous structure's device numbers: - * - * * If both are zero, replace the previous structure with the new one. - * - * * Choose the minimum and maximum non-zero device numbers contained in - * either structure. - */ - if (!sop->luxdev && !sop->ruxdev) { - *sop = *so; - return; - } - if (!sop->luxdev && (d1 || d2)) { - if (d1) { - sop->luxdev = d1; - d1 = (dev_t)0; - } else { - sop->luxdev = d2; - d2 = (dev_t)0; - } - if (sop->luxdev > sop->ruxdev) { - d3 = sop->luxdev; - sop->luxdev = sop->ruxdev; - sop->ruxdev = d3; - } - } - if (!sop->ruxdev && (d1 || d2)) { - if (d1) { - sop->ruxdev = d1; - d1 = (dev_t)0; - } else { - sop->ruxdev = d2; - d2 = (dev_t)0; - } - if (sop->luxdev > sop->ruxdev) { - d3 = sop->luxdev; - sop->luxdev = sop->ruxdev; - sop->ruxdev = d3; - } - } - if (sop->luxdev && sop->ruxdev) { - if (d1) { - if (d1 < sop->luxdev) - sop->luxdev = d1; - else if (d1 > sop->ruxdev) - sop->ruxdev = d1; - } - if (d2) { - if (d2 < sop->luxdev) - sop->luxdev = d2; - else if (d2 > sop->ruxdev) - sop->ruxdev = d2; - } - } -# else /* solaris>=20500 */ - /* - * Save the first sockmod structure. - */ - if (!*so_st) { - *so_st = 1; - *sop = *so; - } -# endif /* solaris<20500 */ -} -#endif /* solaris<100000 */ - -/* - * vop2ty() - convert vnode operation switch address to internal type - */ - -int vop2ty(struct lsof_context *ctx, /* context */ - struct vnode *vp, /* local vnode pointer */ - int fx) /* file system index (-1 if none) */ -{ - int h; - register int i; - KA_T ka; - int nty; - v_optab_t *nv, *v, *vt; - -#if defined(HAS_AFS) - static int afs = 0; /* afs test status: -1 = no AFS - * 0 = not tested - * 1 = AFS */ -#endif /* defined(HAS_AFS) */ - - /* - * Locate the node type by hashing the vnode's v_op address into the - * Voptab[]. - */ - if (!(ka = (KA_T)vp->v_op)) - return (-1); - h = HASHVOP(ka); - for (v = Voptab[h]; v; v = v->next) { - if (ka == v->v_op) - break; - } - if (!v) { - - /* - * If there's no entry in the Voptab[] for the v_op address, see if - * an entry can be found via the file system type and FxToVoptab[]. - */ - if ((fx >= 0) && (fx < Fsinfomax) && (v = FxToVoptab[fx])) { - - /* - * There's an FxToVoptab[] mapping, so add an entry to Voptab[] - * for the v_op address. - */ - if (!(nv = (v_optab_t *)malloc((MALLOC_S)sizeof(v_optab_t)))) { - (void)fprintf(stderr, "%s: can't add \"%s\" to Voptab\n", Pn, - Fsinfo[fx]); - Error(ctx); - } - *nv = *v; - nv->v_op = ka; - h = HASHVOP(ka); - nv->next = Voptab[h]; - Voptab[h] = v = nv; - } - } - if (!v) - return (-1); - -#if defined(HAS_AFS) - /* - * Do special AFS checks. - */ - if (v->nty == N_AFS) { - if (vp->v_data || !vp->v_vfsp) - return (-1); - switch (afs) { - case -1: - return (-1); - case 0: - if (!hasAFS(vp)) { - afs = -1; - return (-1); - } - afs = 1; - return (N_AFS); - case 1: - if ((KA_T)vp->v_vfsp == AFSVfsp) - return (N_AFS); - } - return (-1); - } -#endif /* defined(HAS_AFS) */ - - return (v->nty); -} - -#if solaris >= 100000 -/* - * read_ndvn() -- read node's dv_node - */ - -static int read_ndvn(struct lsof_context *ctx, /* context */ - KA_T na, /* containing vnode's address */ - KA_T da, /* containing vnode's v_data */ - struct dv_node *dv, /* dv_node receiver */ - dev_t *dev, /* underlying file system device - * number receptor */ - unsigned char *devs) /* status of *dev */ -{ - struct vnode rv; - struct snode s; - char tbuf[32]; - struct vfs v; - /* - * Read the snode. - */ - if (!da || kread(ctx, (KA_T)da, (char *)&s, sizeof(s))) { - (void)snpf(Namech, Namechl - 1, - "dv_node vnode at %s: can't read snode: %s", - print_kptr(na, tbuf, sizeof(tbuf)), - print_kptr(da, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - /* - * Read the snode's real vnode. - */ - if (!s.s_realvp || - kread(ctx, (KA_T)s.s_realvp, (char *)&rv, sizeof(struct dv_node))) { - (void)snpf(Namech, Namechl - 1, - "dv_node snode at %s: can't read real vnode: %s", - print_kptr(da, tbuf, sizeof(tbuf)), - print_kptr((KA_T)s.s_realvp, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - /* - * Read the real vnode's dv_node. - */ - if (!rv.v_data || kread(ctx, (KA_T)rv.v_data, (char *)dv, sizeof(rv))) { - (void)snpf(Namech, Namechl - 1, - "dv_node real vnode at %s: can't read dv_node: %s", - print_kptr((KA_T)s.s_realvp, tbuf, sizeof(tbuf)), - print_kptr((KA_T)rv.v_data, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - /* - * Return the device number of the underlying file system, if possible. - */ - if (rv.v_vfsp && !kread(ctx, (KA_T)rv.v_vfsp, (char *)&v, sizeof(v))) { - *dev = v.vfs_dev; - *devs = 1; - } - return (0); +void read_v_path(struct lsof_context *ctx, KA_T ka, char *rb, size_t rbl) { + (void)ctx; + (void)ka; + if (rb && rbl) + rb[0] = '\0'; } -#endif /* solaris<100000 */ diff --git a/lib/dialects/sun/dnode1.c b/lib/dialects/sun/dnode1.c deleted file mode 100644 index 58785dc3..00000000 --- a/lib/dialects/sun/dnode1.c +++ /dev/null @@ -1,414 +0,0 @@ -/* - * dnode1.h - Solaris AFS support - */ - -/* - * Copyright 1996 Purdue Research Foundation, West Lafayette, Indiana - * 47907. All rights reserved. - * - * Written by Victor A. Abell - * - * This software is not subject to any license of the American Telephone - * and Telegraph Company or the Regents of the University of California. - * - * Permission is granted to anyone to use this software for any purpose on - * any computer system, and to alter it and redistribute it freely, subject - * to the following restrictions: - * - * 1. Neither the authors nor Purdue University are responsible for any - * consequences of the use of this software. - * - * 2. The origin of this software must not be misrepresented, either by - * explicit claim or by omission. Credit to the authors and Purdue - * University must appear in documentation and sources. - * - * 3. Altered versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 4. This notice may not be removed or altered. - */ - -#ifndef lint -static char copyright[] = - "@(#) Copyright 1996 Purdue Research Foundation.\nAll rights reserved.\n"; -#endif - -#if defined(HAS_AFS) -# include "lsof.h" - -# include -# define __XDR_INCLUDE__ -# define int32 old_solaris_int32 - -# if solaris >= 20600 -# undef SHARED -# undef PRIVATE -# endif /* solaris>=20600 */ - -# include -# include -# include - -/* - * This is an emulation of the afs_rwlock_t definition that appears in - * the AFS sources in afs/lock.h. - */ - -# if defined(AFS_SUN5_ENV) -# define AFS_NOBOZO_LOCK -# endif /* defined(AFS_SUN5_ENV) */ - -# define INSTRUMENT_LOCKS - -# if defined(AFS_FINEGR_SUNLOCK) -typedef kmutex_ afs_lock_t; -typedef krwlock_t afs_rwlock_t; -# endif /* !defined(AFS_FINEGR_SUNLOCK) */ - -struct afs_lock { - -# if solaris >= 20500 - unsigned char d1[2]; - unsigned short d1_5[3]; -# else /* solaris < 20500 */ - unsigned char d1[4]; -# endif /* solaris>=20500 */ - - struct timeval d2; - -# if defined(INSTRUMENT_LOCKS) - unsigned int d3[3]; -# endif /* defined(INSTRUMENT_LOCKS) */ -}; -typedef struct afs_lock afs_lock_t; -typedef struct afs_lock afs_rwlock_t; - -/* - * This is an emulation of the afs_bozoLock_t definition that appears in - * the AFS sources in afs/lock.h. - */ - -struct afs_bozoLock { - short d1; - char d2[2]; - char *d3; -}; - -# if !defined(AFS_NOBOZO_LOCK) -typedef struct afs_bozoLock afs_bozoLock_t; -# else /* defined(AFS_NOBOZO_LOCK) */ -# if defined(AFS_SUN5_ENV) -typedef kmutex_t afs_bozoLock_t; -# else /* !defined(AFS_SUN5_ENV) */ -typedef struct afs_bozoLock afs_bozoLock_t; -# endif /* defined(AFS_SUN5_ENV) */ -# endif /* !defined(AFS_NOBOZO_LOCK) */ - -# define KERNEL -# include -# undef KERNEL - -/* - * Local function prototypes - */ - -static struct volume *getvolume(struct VenusFid *f, int *vols); -static int is_rootFid(struct vcache *vc, int *rfid); - -/* - * alloc_vcache() - allocate space for vcache structure - */ - -struct vnode *alloc_vcache() { - return ((struct vnode *)malloc(sizeof(struct vcache))); -} - -/* - * ckAFSsym() - check for missing X_AFS_* symbols in AFS name list file - */ - -void ckAFSsym(struct nlist *nl) /* copy of Nl[] when empty */ -{ - char *path = AFSAPATHDEF; - int i; - KA_T v; - -# if defined(HASAOPT) - if (AFSApath) - path = AFSApath; -# endif /* defined(HASAOPT) */ - - /* - * If an alternate AFS name list file was specified, see if it can be read. - */ - if (!is_readable(path, 0)) { - if (!Fwarn) - (void)fprintf(stderr, - "%s: WARNING: can't access AFS name list file: %s\n", - Pn, path); - return; - } - /* - * Read the AFS modload symbols and compare its non-zero values with - * the non-zero values in Nl[]. Quit if there is any mis-match. - */ - if (nlist(path, nl) < 0) - return; - for (i = 0; Nl[i].n_name && Nl[i].n_name[0]; i++) { - if (!nl[i].n_value || !Nl[i].n_value) - continue; - if (nl[i].n_value != Nl[i].n_value) - return; - } - /* - * If any AFS symbol that doesn't have a value in Nl[] has one from - * the AFS modload file, copy its modload value to Nl[]. - */ - if ((i = get_Nl_value("arFid", Drive_Nl, &v)) >= 0 && !Nl[i].n_value && - nl[i].n_value) - Nl[i].n_value = nl[i].n_value; - if ((i = get_Nl_value("avops", Drive_Nl, &v)) >= 0 && !Nl[i].n_value && - nl[i].n_value) - Nl[i].n_value = nl[i].n_value; - if ((i = get_Nl_value("avol", Drive_Nl, &v)) >= 0 && !Nl[i].n_value && - nl[i].n_value) - Nl[i].n_value = nl[i].n_value; -} - -/* - * getvolume() - get volume structure - */ - -static struct volume *getvolume(struct VenusFid *f, /* file ID pointer */ - int *vols) /* afs_volumes status return */ -{ - int i; - static KA_T ka = 0; - KA_T kh; - static struct volume v; - KA_T vp; - static int w = 0; - - if (!ka) { - if (get_Nl_value("avol", Drive_Nl, &ka) < 0 || !ka) { - if (!w && !Fwarn) { - (void)fprintf( - stderr, "%s: WARNING: no kernel address for afs_volumes\n", - Pn); - (void)fprintf( - stderr, - " This may hamper AFS node number reporting.\n"); - w = 1; - } - *vols = 0; - return ((struct volume *)NULL); - } - } - *vols = 1; - i = (NVOLS - 1) & f->Fid.Volume; - kh = (KA_T)((char *)ka + (i * sizeof(struct volume *))); - if (kread(ctx, kh, (char *)&vp, sizeof(vp))) - return ((struct volume *)NULL); - while (vp) { - if (kread(ctx, (KA_T)vp, (char *)&v, sizeof(v))) - return ((struct volume *)NULL); - if (v.volume == f->Fid.Volume && v.cell == f->Cell) - return (&v); - vp = (KA_T)v.next; - } - return ((struct volume *)NULL); -} - -/* - * hasAFS() - test for AFS presence via vfs structure - */ - -int hasAFS(struct vnode *vp) /* vnode pointer */ -{ - struct mounts *mp; - int n; - struct vfs v; - /* - * If this vnode has a v_data pointer, then it probably isn't an AFS vnode; - * return FALSE. - * - * If the vfs struct address of /afs is known and this vnode's v_vfsp - * matches it, return TRUE. - * - * Read this vnode's vfs structure and see if it's device (fsid.val[0]) is - * AFSdev. If it is, record the AFS vfs struct address and return TRUE. - */ - if (AFSVfsp && !vp->v_data && (KA_T)vp->v_vfsp == AFSVfsp) - return (1); - if (!AFSdevStat) - (void)readmnt(); - if (!AFSdevStat || vp->v_data || !vp->v_vfsp || - kread(ctx, (KA_T)vp->v_vfsp, (char *)&v, sizeof(v)) || v.vfs_data) - return (0); - if ((dev_t)v.vfs_fsid.val[0] == AFSdev) { - AFSVfsp = (KA_T)vp->v_vfsp; - return (1); - } - /* - * Search the local mount table for /afs devices. Count /afs devices, - * and skip a device number test for them. A match on device number for - * non-AFS devices produces a FALSE return. - */ - for (mp = readmnt(), n = 0; mp; mp = mp->next) { - if (AFSdevStat && mp->dev == AFSdev && mp->dir && - strcmp(mp->dir, "/afs") == 0 && mp->fsname && - strcmp(mp->fsname, "AFS") == 0) - n++; - else if (mp->dev == (dev_t)v.vfs_fsid.val[0]) - return (0); - } - /* - * If there is exactly one /afs device, assume its vfs struct address is - * the one for this vnode, record it, and return TRUE. - */ - if (n == 1) { - AFSVfsp = (KA_T)vp->v_vfsp; - return (1); - } - return (0); -} - -/* - * is_rootFid() - is the file ID the root file ID - * - * return: 0 = is not root file ID - * 1 = is root file ID - * rfid = 0 if root file ID structure address not available - * 1 if root file ID structure address available - */ - -static int is_rootFid(struct vcache *vc, /* vcache structure */ - int *rfid) /* root file ID pointer status return */ -{ - KA_T arFid; - char *err; - static int f = 0; /* rootFid structure status: - * -1 = unavailable - * 0 = not yet accessed - * 1 = available */ - static struct VenusFid r; - static int w = 0; - - switch (f) { - case -1: - if (vc->v.v_flag & VROOT) { - *rfid = 1; - return (1); - } - *rfid = 0; - return (0); - case 0: - if (get_Nl_value("arFid", Drive_Nl, &arFid) < 0 || !arFid) { - err = "no afs_rootFid kernel address"; - - rfid_unavailable: - - if (!w && !Fwarn) { - (void)fprintf(stderr, "%s: WARNING: AFS root Fid error: %s\n", - Pn, err); - (void)fprintf( - stderr, - " This may hamper AFS node number reporting.\n"); - w = 1; - } - f = -1; - if (vc->v.v_flag & VROOT) { - *rfid = 1; - return (1); - } - *rfid = 0; - return (0); - } - if (kread(ctx, arFid, (char *)&r, sizeof(r))) { - err = "can't read afs_rootFid from kernel"; - goto rfid_unavailable; - } - f = 1; - /* fall through */ - case 1: - *rfid = 1; - if (vc->fid.Fid.Unique == r.Fid.Unique && - vc->fid.Fid.Vnode == r.Fid.Vnode && - vc->fid.Fid.Volume == r.Fid.Volume && vc->fid.Cell == r.Cell) - return (1); - } - *rfid = 0; - return (0); -} - -/* - * readafsnode() - read AFS node - */ - -int readafsnode(KA_T va, /* kernel vnode address */ - struct vnode *v, /* vnode buffer pointer */ - struct afsnode *an) /* afsnode recipient */ -{ - char *cp, tbuf[32]; - KA_T ka; - int len, rfid, vols; - struct vcache *vc; - struct volume *vp; - - cp = ((char *)v + sizeof(struct vnode)); - ka = (KA_T)((char *)va + sizeof(struct vnode)); - len = sizeof(struct vcache) - sizeof(struct vnode); - if (kread(ctx, ka, cp, len)) { - (void)snpf(Namech, Namechl, - "vnode at %s: can't read vcache remainder from %s", - print_kptr(va, tbuf, sizeof(tbuf)), - print_kptr(ka, (char *)NULL, 0)); - enter_nm(ctx, Namech); - return (1); - } - vc = (struct vcache *)v; - if (!AFSdevStat) - (void)readmnt(); - an->dev = AFSdevStat ? AFSdev : 0; - an->size = (unsigned long)vc->m.Length; - an->nlink = (long)vc->m.LinkCount; - an->nlink_st = 1; - /* - * Manufacture the "inode" number. - */ - if (vc->mvstat == 2) { - if ((vp = getvolume(&vc->fid, &vols))) { - an->inode = (INODETYPE)((vp->mtpoint.Fid.Vnode + - (vp->mtpoint.Fid.Volume << 16)) & - 0x7fffffff); - if (an->inode == (INODETYPE)0) { - if (is_rootFid(vc, &rfid)) - an->ino_st = 1; - else if (rfid) { - an->inode = (INODETYPE)2; - an->ino_st = 1; - } else - an->ino_st = 0; - } else - an->ino_st = 1; - } else { - if (vols) { - an->inode = (INODETYPE)2; - an->ino_st = 1; - } else { - if (v->v_flag & VROOT) { - an->inode = (INODETYPE)0; - an->ino_st = 1; - } else - an->ino_st = 0; - } - } - } else { - an->inode = - (INODETYPE)((vc->fid.Fid.Vnode + (vc->fid.Fid.Volume << 16)) & - 0x7fffffff); - an->ino_st = 1; - } - return (0); -} -#endif /* defined(HAS_AFS) */ diff --git a/lib/dialects/sun/dnode2.c b/lib/dialects/sun/dnode2.c deleted file mode 100644 index 7b6e2176..00000000 --- a/lib/dialects/sun/dnode2.c +++ /dev/null @@ -1,483 +0,0 @@ -/* - * dnode2.c - Solaris node functions for lsof - * - * This module must be separate to keep separate the multiple kernel inode - * structure definitions. - */ - -/* - * Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana - * 47907. All rights reserved. - * - * Written by Victor A. Abell - * - * This software is not subject to any license of the American Telephone - * and Telegraph Company or the Regents of the University of California. - * - * Permission is granted to anyone to use this software for any purpose on - * any computer system, and to alter it and redistribute it freely, subject - * to the following restrictions: - * - * 1. Neither the authors nor Purdue University are responsible for any - * consequences of the use of this software. - * - * 2. The origin of this software must not be misrepresented, either by - * explicit claim or by omission. Credit to the authors and Purdue - * University must appear in documentation and sources. - * - * 3. Altered versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 4. This notice may not be removed or altered. - */ - -#ifndef lint -static char copyright[] = - "@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n"; -#endif - -#include "common.h" - -#if defined(HASVXFS) - -# if defined(HASVXFSUTIL) -# include -# define EMSGPFX "vx_inode: " - -static char *add2em(char *em, char *fmt, char *arg); -static char *ckptr(char *em, char *ptr, int len, int slen, char *nm); -static char *getioffs(char **vx, int *vxl, char **dev, int *devl, char **ino, - int *inol, char **nl, int *nll, char **sz, int *szl); -# else /* !defined(HASVXFSUTIL) */ -# if defined(HASVXFS_FS_H) && !defined(HASVXFS_VX_INODE) -# undef fs_bsize -# include -# endif /* defined(HASVXFS_FS_H) && !defined(HASVXFS_VX_INODE) */ - -# if HASVXFS_SOL_H -# include -# endif /* defined(HSVXFS_SOL_H) */ - -# if defined(HASVXFS_SOLARIS_H) && defined(HASVXFS_U64_T) -# include -# endif /* defined(HASVXFS_SOLARIS_H) && defined(HASVXFS_U64_T) */ - -# if defined(HASVXFS_MACHDEP_H) -# if defined(HASVXFS_OFF32_T) && solaris >= 70000 -# define off32_t VXFS_off32_t -# endif /* defined(HASVXFS_OFF32_T) && solaris>=70000 */ -# include -# endif /* defined(HASVXFS_MACHDEP_H) */ - -# if defined(HASVXFS_SOLARIS_H) -struct kdm_vnode { /* dummy for */ - int d1; -}; -# undef fs_bsize -# define uint16_t VXFS_uint16_t - -# if defined(HASVXFS_OFF64_T) -# define off64_t VXFS_off64_t -# endif /* defined(HASVXFS_OFF64_T) */ - -# if defined(HASVXFS_SOLARIS_H) && !defined(HASVXFS_U64_T) -# include -# endif /* defined(HASVXFS_SOLARIS_H) && !defined(HASVXFS_U64_T) */ - -# include -# include -# include -# endif /* defined(HASVXFS_SOLARIS_H) */ - -# include -# endif /* defined(HASVXFSUTIL) */ - -# if defined(HASVXFSUTIL) -static struct vx_ioffsets Ioffsets; /* VXFS inode offsets */ -static int Ioffs_state = -1; /* Ioffsets state: - * -1 = uninitialized - * 0 = initialized - * >0 = initialization error */ - -/* - * access_vxfs_ioffsets() - access the VXFS inode offsets - */ - -extern int access_vxfs_ioffsets() { - - /* - * This operation is done in an external function, so it can be done before - * GID permission has been surrendered. - */ - Ioffs_state = vxfsu_get_ioffsets(&Ioffsets, sizeof(Ioffsets)); - return (Ioffs_state); -} - -/* - * add2em() - add to error message - */ - -static char *add2em(char *em, /* current error message */ - char *fmt, /* message format */ - char *arg) /* format's single string argument */ -{ - MALLOC_S al, eml, nl; - char msg[1024]; - MALLOC_S msgl = (MALLOC_S)sizeof(msg); - - (void)snpf(msg, msgl, fmt, arg); - msg[msgl - 1] = '\0'; - nl = (MALLOC_S)strlen(msg); - if (!em) { - al = (MALLOC_S)strlen(EMSGPFX) + nl + 1; - em = (char *)malloc((MALLOC_S)al); - eml = (MALLOC_S)0; - } else { - if (!(eml = (MALLOC_S)strlen(em))) { - (void)fprintf(stderr, "%s: add2em: previous message empty\n", Pn); - Error(ctx); - } - al = eml + nl + 3; - em = (char *)realloc((MALLOC_P *)em, al); - } - if (!em) { - (void)fprintf(stderr, "%s: no VxFS error message space\n", Pn); - Error(ctx); - } - (void)snpf(em + eml, al - eml, "%s%s%s", eml ? "" : EMSGPFX, - eml ? "; " : "", msg); - return (em); -} - -/* - * ckptr() - check pointer and length - */ - -static char *ckptr(char *em, /* pointer to previous error message */ - char *ptr, /* pointer to check */ - int len, /* pointer's value length */ - int slen, /* value's storage length */ - char *nm) /* element name */ -{ - -# if defined(_LP64) -# define PTR_CAST unsigned long long -# else /* !defined(_LP64) */ -# define PTR_CAST unsigned long -# endif /* defined(_LP64) */ - - PTR_CAST m; - char tbuf[1024]; - - if (!ptr) - return (add2em(em, "no %s pointer", nm ? nm : "(null)")); - if (len > slen) { - (void)snpf(tbuf, sizeof(tbuf) - 1, "%s size, %d, > %d", - nm ? nm : "(null)", len, slen); - tbuf[sizeof(tbuf) - 1] = '\0'; - return (add2em(em, "%s", tbuf)); - } - if ((m = (PTR_CAST)(len - 1)) < (PTR_CAST)1) - return (em); - if ((PTR_CAST)ptr & m) - return (add2em(em, "%s misaligned", nm ? nm : "(null)")); - return (em); -} - -/* - * getioffs() - get the vx_inode offsets - */ - -static char *getioffs(char **vx, /* pointer to allocated vx_inode space */ - int *vxl, /* sizeof(*vx) */ - char **dev, /* pointer to device number element of *vx */ - int *devl, /* sizeof(*dev) */ - char **ino, /* pointer to node number element of *vx */ - int *inol, /* sizeof(*ino) */ - char **nl, /* pointer to nlink element of *vx */ - int *nll, /* sizeof(*nl) */ - char **sz, /* pointer to size element of *vx */ - int *szl) /* sizeof(*sz) */ -{ - char *tv; - int tvl; - - if (Ioffs_state) - return (add2em((char *)NULL, "%s error", "vxfsu_get_ioffsets")); - tvl = (int)(Ioffsets.ioff_dev + Ioffsets.ioff_dev_sz); - if ((Ioffsets.ioff_nlink + Ioffsets.ioff_nlink_sz) > tvl) - tvl = (int)(Ioffsets.ioff_nlink + Ioffsets.ioff_nlink_sz); - if ((Ioffsets.ioff_number + Ioffsets.ioff_number_sz) > tvl) - tvl = (int)(Ioffsets.ioff_number + Ioffsets.ioff_number_sz); - if ((Ioffsets.ioff_size + Ioffsets.ioff_size_sz) > tvl) - tvl = (int)(Ioffsets.ioff_size + Ioffsets.ioff_size_sz); - if (!tvl) - return (add2em((char *)NULL, "zero length %s", "vx_inode")); - if (!(tv = (char *)malloc((MALLOC_S)tvl))) { - (void)fprintf(stderr, "%s: no vx_inode space\n", Pn); - Error(ctx); - } - *vx = tv; - *vxl = tvl; - *dev = tv + Ioffsets.ioff_dev; - *devl = (int)Ioffsets.ioff_dev_sz; - *ino = tv + Ioffsets.ioff_number; - *inol = (int)Ioffsets.ioff_number_sz; - *nl = tv + Ioffsets.ioff_nlink; - *nll = (int)Ioffsets.ioff_nlink_sz; - *sz = tv + Ioffsets.ioff_size; - *szl = (int)Ioffsets.ioff_size_sz; - return ((char *)NULL); -} - -# if defined(HASVXFSRNL) - -# define RNLCINIT 64 /* initial RNL cache size */ -# define RNLCINCR 32 /* RNL cache increment */ - -/* - * print_vxfs_rnl_path() -- print VxFS RNL path - */ - -int print_vxfs_rnl_path(struct lfile *lf) /* file whose name is to be printed */ -{ - char **bp = (char **)NULL; - int i, j, n, p; - typedef struct rmc { /* RNL mount point cache */ - char *mp; /* mount point */ - unsigned char s; /* RNL status: 0 = supported - * 1 = not supported */ - } rmc_t; - static rmc_t *rm = (rmc_t *)NULL; - /* RNL mount point cache */ - static int rma = 0; /* allocated cache entries */ - static int rmu = 0; /* used cache entries */ - size_t sz; - /* - * This must be a VxFS file, it must have an inode and its mount point must - * be known. - */ - if (!lf->is_vxfs || (lf->inp_ty != 1) || !lf->fsdir) - return (0); - /* - * Locate or create an RNL mount point cache entry. - */ - for (i = 0; i < rmu; i++) { - if (rm[i].mp == lf->fsdir) - break; - } - if (i >= rmu) { - - /* - * A new entry must be created. - */ - if (i >= rma) { - - /* - * RNL mount point cache space must be allocated. - */ - rma += rm ? RNLCINCR : RNLCINIT; - sz = (size_t)(rma * sizeof(rmc_t)); - if (rm) - rm = (rmc_t *)realloc((MALLOC_P *)rm, (MALLOC_S)sz); - else - rm = (rmc_t *)malloc((MALLOC_S)sz); - if (!rm) { - (void)fprintf(stderr, "%s: no RNL mount point cache space\n", - Pn); - Error(ctx); - } - } - i = rmu; - rm[rmu].mp = lf->fsdir; - rm[rmu++].s = 0; - } - if (rm[i].s) - return (0); - /* - * Get the RNL path for this mount point and inode. - */ - if (vxfs_inotopath(lf->fsdir, (uint64_t)lf->inode, 0, &bp, &n)) { - if (errno == ENOTSUP) - rm[i].s = 1; - return (0); - } - /* - * Print the first RNL path, then free the allocated function reply space. - */ - if (bp) { - for (j = 0; j < n; j++) { - if (bp[j] && *bp[j]) { - safestrprt(bp[j], stdout, 0); - p = 1; - break; - } - } - for (j = 0; j < n; j++) { - if (bp[j]) - (void)free((FREE_P *)bp[j]); - } - (void)free((FREE_P *)bp); - } else - p = 0; - return (p); -} -# endif /* defined(HASVXFSRNL) */ -# endif /* defined(HASVXFSUTIL) */ - -/* - * read_vxnode() - read Veritas file system inode information - */ - -int read_vxnode(KA_T va, /* containing vnode's address */ - struct vnode *v, /* containing vnode */ - struct l_vfs *vfs, /* local vfs structure */ - int fx, /* file system index (-1 if none) */ - struct l_ino *li, /* local inode value receiver */ - KA_T *vnops) /* table of VxFS v_op values */ -{ - struct vnode cv; - char tbuf[32]; - -# if defined(HASVXFS_VX_INODE) - struct vx_inode vx; - int vxl = (int)sizeof(vx); - dev_t *vxn_dev = (dev_t *)&vx.i_dev; - int *vxn_nlink = (int *)&vx.i_nlink; - unsigned int *vxn_ino = (unsigned int *)&vx.i_number; - SZOFFTYPE *vxn_sz = (SZOFFTYPE *)&vx.i_size; - char *vxp = (char *)&vx; -# else /* !defined(HASVXFS_VX_INODE) */ -# if defined(HASVXFSUTIL) - static char *em = (char *)NULL; - int devl, nll, szl; - static int inol; - static char *vxp = (char *)NULL; - static int vxl = 0; - static dev_t *vxn_dev = (dev_t *)NULL; - static int *vxn_nlink = (int *)NULL; - static char *vxn_ino = (char *)NULL; - static SZOFFTYPE *vxn_sz = (SZOFFTYPE *)NULL; -# else /* !defined(HASVXFSUTIL) */ - struct inode vx; - int vxl = sizeof(vx); - dev_t *vxn_dev = (dev_t *)&vx.i_dev; - int *vxn_nlink = (int *)&vx.i_nlink; - long *vxn_ino = (long *)&vx.i_number; - SZOFFTYPE *vxn_sz = (SZOFFTYPE *)&vx.i_size; - char *vxp = (char &)&vx; -# endif /* defined(HASVXFSUTIL) */ -# endif /* defined(HASVXFS_VX_INODE) */ - - li->dev_def = li->ino_def = li->nl_def = li->rdev_def = li->sz_def = 0; - /* - * See if this is vnode is served by fdd_chain_vnops. If it is, its - * v_data pointer leads to the "real" vnode. - */ - if (v->v_data && v->v_op && (VXVOP_FDDCH < VXVOP_NUM) && - vnops[VXVOP_FDDCH] && ((KA_T)v->v_op == vnops[VXVOP_FDDCH])) { - if (kread(ctx, (KA_T)v->v_data, (char *)&cv, sizeof(cv))) { - (void)snpf(Namech, Namechl, - "node at %s: can't read real vx vnode: %s", - print_kptr(va, tbuf, sizeof(tbuf)), - print_kptr((KA_T)v->v_data, (char *)NULL, 0)); - enter_nm(ctx, Namech); - return (1); - } - -# if defined(HASNCACHE) - Lf->na = (KA_T)v->v_data; -# endif /* defined(HASNCACHE) */ - - *v = cv; - Ntype = vop2ty(v, fx); - } - -# if defined(HASVXFSUTIL) - /* - * If libvxfsutil[64].a is in use, establish the vx_inode size and the - * locations and sizes of its device, link count, node number, and size - * elements. - * - * If an error was detected while determining the vx_inode values, repeat - * the error explanation in the NAME column. - */ - if (!vxp && !em) { - em = getioffs(&vxp, &vxl, (char **)&vxn_dev, &devl, &vxn_ino, &inol, - (char **)&vxn_nlink, &nll, (char **)&vxn_sz, &szl); - if (!em) { - - /* - * Check the returned pointers and their sizes. - */ - em = ckptr(em, (char *)vxn_dev, devl, sizeof(dev_t), "dev"); - em = ckptr(em, (char *)vxn_ino, inol, sizeof(INODETYPE), "ino"); - em = ckptr(em, (char *)vxn_nlink, nll, sizeof(int), "nlink"); - em = ckptr(em, (char *)vxn_sz, szl, sizeof(SZOFFTYPE), "sz"); - } - } - if (em) { - (void)snpf(Namech, Namechl, "%s", em); - (void)enter_nm(ctx, Namech); - return (1); - } -# endif /* !defined(HASVXFSUTIL) */ - - /* - * Read vnode's vx_inode. - */ - if (!v->v_data || kread(ctx, (KA_T)v->v_data, vxp, vxl)) { - (void)snpf(Namech, Namechl, "node at %s: can't read vx_inode: %s", - print_kptr(va, tbuf, sizeof(tbuf)), - print_kptr((KA_T)v->v_data, (char *)NULL, 0)); - (void)enter_nm(ctx, Namech); - return (1); - } - /* - * Return device number, inode number, link count, raw device number, and - * size. - */ - if (vfs && vfs->fsname) { - li->dev = (dev_t)vfs->dev; - li->dev_def = 1; - } else if (vxn_dev) { - li->dev = (dev_t)*vxn_dev; - li->dev_def = 1; - } - if (vxn_ino) { - -# if defined(HASVXFSUTIL) - switch (inol) { - case sizeof(short): - li->ino = (INODETYPE) * ((short *)vxn_ino); - li->ino_def = 1; - break; - case sizeof(unsigned int): - li->ino = (INODETYPE) * ((unsigned int *)vxn_ino); - li->ino_def = 1; - break; - case sizeof(unsigned long long): - li->ino = (INODETYPE) * ((unsigned long long *)vxn_ino); - li->ino_def = 1; - break; - default: - break; - } -# else /* !defined(HASVXFSUTIL) */ - li->ino = (INODETYPE)*vxn_ino; - li->ino_def = 1; -# endif /* defined(HASVXFSUTIL) */ - } - if (vxn_nlink) { - li->nl = (long)*vxn_nlink; - li->nl_def = 1; - } - li->rdev = v->v_rdev; - li->rdev_def = 1; - if (vxn_sz) { - li->sz = (SZOFFTYPE)*vxn_sz; - li->sz_def = 1; - } - return (0); -} -#endif /* defined(HASVXFS) */ diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index c7bc4967..820bf22a 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -1,2125 +1,561 @@ /* - * dproc.c - Solaris lsof functions for accessing process information - */ - -/* - * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana - * 47907. All rights reserved. - * - * Written by Victor A. Abell - * - * This software is not subject to any license of the American Telephone - * and Telegraph Company or the Regents of the University of California. - * - * Permission is granted to anyone to use this software for any purpose on - * any computer system, and to alter it and redistribute it freely, subject - * to the following restrictions: - * - * 1. Neither the authors nor Purdue University are responsible for any - * consequences of the use of this software. - * - * 2. The origin of this software must not be misrepresented, either by - * explicit claim or by omission. Credit to the authors and Purdue - * University must appear in documentation and sources. - * - * 3. Altered versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 4. This notice may not be removed or altered. - */ - -#ifndef lint -static char copyright[] = - "@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n"; -#endif - -#include "common.h" - - -#if solaris < 20500 -# include "kernelbase.h" -#endif /* solaris<20500 */ - -#if defined(HAS_CRED_IMPL_H) -# if solaris >= 110000 -# define _KERNEL -# endif /* solaris>=110000 */ - -# include - -# if solaris >= 110000 -# undef _KERNEL -# endif /* solaris>=110000 */ -#endif /* defined(HAS_CRED_IMPL_H) */ - -/* - * Local definitions - */ - -#if defined(__sparc) || defined(__sparcv9) -# define ARCH64B "sparcv9" -#else /* !defined(__sparc) && !defined(__sparcv9) */ -# if defined(__i386) || defined(__amd64) -# define ARCH64B "amd64" -# endif /* defined(__i386) || defined(__amd64) */ -#endif /* defined(__sparc) || defined(__sparcv9) */ - -#if solaris >= 20501 -# define KVMHASHBN \ - 8192 /* KVM hash bucket count -- MUST BE \ - * A POWER OF 2!!! */ -# define HASHKVM(va) ((int)((va * 31415) >> 3) & (KVMHASHBN - 1)) -/* virtual address hash function */ - -# if solaris < 70000 -# define KAERR (u_longlong_t) - 1 /* kvm_physaddr() error return */ -# define KBUFT char /* kernel read buffer type */ -# define KPHYS u_longlong_t /* kernel physical address type */ -# define KVIRT u_int /* kernel virtual address type */ -# else /* solaris>=70000 */ -# define KAERR (uint64_t) - 1 /* kvm_physaddr() error return */ -# define KBUFT void /* kernel read buffer type */ -# define KPHYS uint64_t /* kernel physical address type */ -# define KVIRT uintptr_t /* kernel virtual address type */ -# endif /* solaris<70000 */ -#endif /* solaris>=20501 */ - -/* - * Local structures - */ - -#if solaris >= 20501 -typedef struct kvmhash { - KVIRT vpa; /* virtual page address */ - KPHYS pa; /* physical address */ - struct kvmhash *nxt; /* next virtual address */ -} kvmhash_t; -#endif /* solaris>=20501 */ - -/* - * Local variables - */ - -#if solaris >= 20501 -static struct as *Kas = (struct as *)NULL; -/* pointer to kernel's address space - * map in kernel virtual memory */ -static kvmhash_t **KVMhb = (kvmhash_t **)NULL; -/* KVM hash buckets */ -static int PageSz = 0; /* page size */ -static int PSMask = 0; /* page size mask */ -static int PSShft = 0; /* page size shift */ - -# if solaris < 70000 -static struct as Kam; /* kernel's address space map */ -static int Kmd = -1; /* memory device file descriptor */ -# endif /* solaris<70000 */ -#endif /* solaris>=20501 */ - -#if solaris >= 20500 -static KA_T Kb = (KA_T)NULL; /* KERNELBASE for Solaris 2.5 */ -#endif /* solaris>=20500 */ - -static int Np; /* number of P[], Pgid[] and Pid[] - * entries */ -static int Npa = 0; /* number of P[], Pgid[] and Pid[] - * entries for which space has been - * allocated */ -static struct proc *P = NULL; /* local proc structure table */ -static int *Pgid = NULL; /* process group IDs for P[] entries */ -static int *Pid = NULL; /* PIDs for P[] entries */ -static KA_T PrAct = (KA_T)NULL; /* kernel's *practive address */ -static gid_t Savedgid; /* saved (effective) GID */ -static KA_T Sgvops; /* [_]segvn_ops address */ -static int Switchgid = 0; /* must switch GIDs for kvm_open() */ - -#if defined(HASZONES) -static znhash_t **ZoneNm = (znhash_t **)NULL; -/* zone names hash buckets */ -#endif /* defined(HASZONES) */ - -/* - * Local function prototypes - */ - -static void get_kernel_access(struct lsof_context *ctx); -static void process_text(struct lsof_context *ctx, KA_T pa); -static void read_proc(struct lsof_context *ctx); -static void readfsinfo(struct lsof_context *ctx); - -#if solaris >= 20501 -static void readkam(struct lsof_context *ctx, KA_T addr); -#endif /* solaris>=20501 */ - -#if solaris >= 20501 && solaris < 70000 -extern u_longlong_t kvm_physaddr(kvm_t *, struct as *, u_int); -#endif /* solaris>=20501 && solaris<70000 */ - -#if defined(HASZONES) -static int hash_zn(char *zn); -#endif /* defined(HASZONES) */ - -/* - * close_kvm() - close kernel virtual memory access - */ - -void close_kvm(struct lsof_context *ctx) { - if (!Kd) - return; - if (Kd) { - if (kvm_close(Kd) != 0) { - (void)fprintf(stderr, "%s: kvm_close failed\n", Pn); - Error(ctx); - } - Kd = (kvm_t *)NULL; - } - -#if solaris >= 20501 && solaris < 70000 - if (Kmd >= 0) { - (void)close(Kmd); - Kmd = -1; - } -#endif /* solaris>=20501 && solaris<70000 */ -} - -/* - * gather_proc_info() - gather process information - */ - -void gather_proc_info(struct lsof_context *ctx) { - short cckreg; /* conditional status of regular file - * checking: - * 0 = unconditionally check - * 1 = conditionally check */ - short ckscko; /* socket file only checking status: - * 0 = none - * 1 = check only socket files, - * including TCP and UDP - * streams with eXPORT data, - * where supported */ - static int ft = 1; - int i, j; - struct proc *p; - int pgid, pid, px; - long pofv; - short pss, sf; - struct user *u; - uid_t uid; - -#if solaris >= 20400 - int k; - -# if !defined(NFPCHUNK) -# define uf_ofile uf_file -# define uf_pofile uf_flag -# define u_flist u_finfo.fi_list -# define u_nofiles u_finfo.fi_nfiles -# define NFPREAD 64 -# else /* defined(NFPCHUNK) */ -# define NFPREAD NFPCHUNK -# endif /* !defined(NFPCHUNK) */ - uf_entry_t uf[NFPREAD]; -#endif /* solaris>=20400 */ -#if solaris >= 20500 - struct cred pc; -#endif /* solaris>=20500 */ - -#if defined(HASZONES) - struct zone z; - int zh; - char zn[ZONENAME_MAX + 1]; - znhash_t *zp, *zpn; -#endif /* defined(HASZONES) */ - - if (ft) { - /* - * Do first-time only operations. - */ - /* - * Get the segment vnodeops address. - */ - if (get_Nl_value(ctx, "sgvops", Drive_Nl, &Sgvops) < 0) - Sgvops = (KA_T)NULL; - ft = 0; - } else if (!HasALLKMEM) { - - /* - * If not the first time and the ALLKMEM device isn't available, it is - * necessary to close and reopen the KVM device, so that kvm_open() - * will acquire a fresh address for the head of the linked list process - * table. - */ - close_kvm(ctx); - open_kvm(ctx); - -#if solaris >= 20501 - /* - * If not the first time and the ALLKMEM device isn't available, - * re-read the kernel's address space map. - */ - readkam(ctx, (KA_T)NULL); -#endif /* solaris>=20501 */ - } - /* - * Define socket and regular file conditional processing flags. - * - * If only socket files have been selected, or socket files have been - * selected, ANDed with other selection options, enable the skipping of - * regular files. - * - * If socket files and some process options have been selected, enable - * conditional skipping of regular file; i.e., regular files will be skipped - * unless they belong to a process selected by one of the specified options. - */ - if (Selflags & SELNW) { - - /* - * Some network files selection options have been specified. - */ - if (Fand || !(Selflags & ~SELNW)) { - - /* - * Selection ANDing or only network file options have been - * specified, so set unconditional skipping of regular files - * and socket file only checking. - */ - cckreg = 0; - ckscko = 1; - } else { - - /* - * If ORed file selection options have been specified, or no - * ORed process selection options have been specified, enable - * unconditional file checking and clear socket file only - * checking. - * - * If only ORed process selection options have been specified, - * enable conditional file skipping and socket file only checking. - */ - if ((Selflags & SELFILE) || !(Selflags & SELPROC)) - cckreg = ckscko = 0; - else - cckreg = ckscko = 1; - } - } else { - - /* - * No network file selection options were specified. Enable - * unconditional file checking and clear socket file only checking. - */ - cckreg = ckscko = 0; - } - /* - * Read the process table. - */ - read_proc(ctx); - /* - * Loop through processes. - */ - for (p = P, px = 0; px < Np; p++, px++) { - - /* - * Get the process ID. - */ - - pgid = Pgid[px]; - pid = Pid[px]; - -#if solaris < 20500 - uid = p->p_uid; -#else /* solaris >=20500 */ - /* - * Read credentials for Solaris 2.5 and above process. - */ - if (kread(ctx, (KA_T)p->p_cred, (char *)&pc, sizeof(pc))) - continue; - uid = pc.cr_uid; -#endif /* solaris<20500 */ - - /* - * See if the process is excluded. - */ - if (is_proc_excl(ctx, pid, pgid, (UID_ARG)uid, &pss, &sf)) - continue; - -#if defined(HASZONES) - /* - * If the -z (zone) option was specified, get the zone name. - */ - zn[0] = zn[sizeof(zn) - 1] = '\0'; - if (p->p_zone && - !kread(ctx, (KA_T)p->p_zone, (char *)&z, sizeof(z))) { - if (!z.zone_name || - kread(ctx, (KA_T)z.zone_name, (char *)&zn, sizeof(zn) - 1)) - zn[0] = '\0'; - } -#endif /* defined(HASZONES) */ - - /* - * Get the user area associated with the process. - */ - u = &p->p_user; - /* - * Allocate a local process structure and start filling it. - */ - if (is_cmd_excl(ctx, u->u_comm, &pss, &sf)) - continue; - if (cckreg) { - - /* - * If conditional checking of regular files is enabled, enable - * socket file only checking, based on the process' selection - * status. - */ - ckscko = (sf & SELPROC) ? 0 : 1; - } - alloc_lproc(ctx, pid, pgid, (int)p->p_ppid, (UID_ARG)uid, u->u_comm, - (int)pss, (int)sf); - Plf = (struct lfile *)NULL; - -#if defined(HASZONES) - /* - * If zone processing is enabled and requested, and if there is a zone - * name: - * - * o Skip processes excluded by zone name. - * o Save zone name. - */ - if (zn[0]) { - zh = hash_zn(zn); - if (ZoneArg) { - - /* - * See if zone name excludes the process. - */ - for (zp = ZoneArg[zh]; zp; zp = zp->next) { - if (!strcmp(zn, zp->zn)) - break; - } - if (!zp) - continue; - zp->f = 1; - Lp->pss |= PS_PRI; - Lp->sf |= SELZONE; - } - /* - * Make sure the zone name is cached, then save a pointer to it in - * the local proc structure. - */ - if (!ZoneNm) { - if (!(ZoneNm = - (znhash_t **)calloc(HASHZONE, sizeof(znhash_t *)))) { - (void)fprintf(stderr, "%s: no space for zone name hash\n", - Pn); - Error(ctx); - } - } - for (zp = ZoneNm[zh]; zp; zp = zp->next) { - if (!strcmp(zn, zp->zn)) - break; - } - if (!zp) { - - /* - * The zone name isn't cached, so cache it. - */ - if (!(zp = (znhash_t *)malloc((MALLOC_S)sizeof(znhash_t)))) { - (void)fprintf(stderr, "%s: no zone name cache space: %s\n", - Pn, zn); - Error(ctx); - } - if (!(zp->zn = mkstrcpy(zn, (MALLOC_S *)NULL))) { - (void)fprintf(stderr, - "%s: no zone name space at PID %d: %s\n", Pn, - (int)Lp->pid, zn); - Error(ctx); - } - zp->next = ZoneNm[zh]; - ZoneNm[zh] = zp; - } - Lp->zn = zp->zn; - } -#endif /* defined(HASZONES) */ - - /* - * Save file count. - */ - Unof = u->u_nofiles; - /* - * Save current working directory information. - */ - if (!ckscko && u->u_cdir) { - alloc_lfile(ctx, LSOF_FD_CWD, -1); - -#if defined(FILEPTR) - FILEPTR = (struct file *)NULL; -#endif /* defined(FILEPTR) */ - - process_node(ctx, (KA_T)u->u_cdir); - if (Lf->sf) - link_lfile(ctx); - } - /* - * Save root directory information. - */ - if (!ckscko && u->u_rdir) { - alloc_lfile(ctx, LSOF_FD_ROOT_DIR, -1); - -#if defined(FILEPTR) - FILEPTR = (struct file *)NULL; -#endif /* defined(FILEPTR) */ - - process_node(ctx, (KA_T)u->u_rdir); - if (Lf->sf) - link_lfile(ctx); - } - /* - * Save information on text files. - */ - if (!ckscko && p->p_as && Sgvops) { - -#if defined(FILEPTR) - FILEPTR = (struct file *)NULL; -#endif /* defined(FILEPTR) */ - - process_text(ctx, (KA_T)p->p_as); - } - /* - * Save information on file descriptors. - * - * Under Solaris the file pointers are stored in dynamically-linked - * ufchunk structures, each containing NFPREAD file pointers. The - * first ufchunk structure is in the user area. - * - * Under Solaris 2.4 the file pointers are in a dynamically allocated, - * contiguous memory block. - */ - -#if solaris < 20400 - for (i = 0, j = 0; i < u->u_nofiles; i++) { - if (++j > NFPCHUNK) { - if (!u->u_flist.uf_next) - break; - if (kread(ctx, (KA_T)u->u_flist.uf_next, (char *)&u->u_flist, - sizeof(struct ufchunk))) - break; - j = 1; - } - if (!u->u_flist.uf_ofile[j - 1]) -#else /* solaris>=20400 */ - for (i = 0, j = NFPREAD; i < u->u_nofiles; i++) { - if (++j > NFPREAD) { - k = u->u_nofiles - i; - if (k > NFPREAD) - k = NFPREAD; - if (kread(ctx, - (KA_T)((unsigned long)u->u_flist + - i * sizeof(uf_entry_t)), - (char *)&uf, k * sizeof(uf_entry_t))) { - break; - } - j = 1; - } - if (!uf[j - 1].uf_ofile) -#endif /* solaris<20400 */ - - continue; - alloc_lfile(ctx, LSOF_FD_NUMERIC, i); - -#if solaris < 20400 - pofv = (long)u->u_flist.uf_pofile[j - 1]; - process_file(ctx, (KA_T)u->u_flist.uf_ofile[j - 1]); -#else /* solaris>=20400 */ - pofv = uf[j - 1].uf_pofile; - process_file(ctx, (KA_T)uf[j - 1].uf_ofile); -#endif /* solaris <20400 */ - - if (Lf->sf) { - -#if defined(HASFSTRUCT) - Lf->pof = pofv; -#endif /* defined(HASFSTRUCT) */ - - link_lfile(ctx); - } - } - /* - * Examine results. - */ - if (examine_lproc(ctx)) - return; - } -} - -/* - * get_kernel_access() - access the required information in the kernel - */ - -static void get_kernel_access(struct lsof_context *ctx) { - int i; - struct stat sb; - KA_T v; - -#if defined(HAS_AFS) - struct nlist *nl = (struct nlist *)NULL; -#endif /* defined(HAS_AFS) */ - - /* - * Check the Solaris or SunOS version number; check the SunOS architecture. - */ - (void)ckkv(ctx, "Solaris", LSOF_VSTR, (char *)NULL, (char *)NULL); - -#if solaris >= 70000 - /* - * Compare the Solaris 7 and above lsof compilation bit size with the kernel - * bit size. - * - * Quit on a mismatch. - */ - { - char *cp, isa[1024]; - short kbits = 32; - -# if defined(_LP64) - short xkbits = 64; -# else /* !defined(_LP64) */ - short xkbits = 32; -# endif /* defined(_LP64) */ - - if (sysinfo(SI_ISALIST, isa, (long)sizeof(isa)) < 0) { - (void)fprintf(stderr, "%s: can't get ISA list: %s\n", Pn, - strerror(errno)); - Error(ctx); - } - for (cp = isa; *cp;) { - if (strncmp(cp, ARCH64B, strlen(ARCH64B)) == 0) { - kbits = 64; - break; - } - if (!(cp = strchr(cp, ' '))) - break; - cp++; - } - if (kbits != xkbits) { - (void)fprintf(stderr, - "%s: FATAL: lsof was compiled for a %d bit kernel,\n", - Pn, (int)xkbits); - (void)fprintf( - stderr, " but this machine has booted a %d bit kernel.\n", - (int)kbits); - Error(ctx); - } - } -#endif /* solaris>=70000 */ - - /* - * Get kernel symbols. - */ - if (Nmlst && !is_readable(ctx, Nmlst, 1)) - Error(ctx); - (void)build_Nl(ctx, Drive_Nl); - -#if defined(HAS_AFS) - if (!Nmlst) { - - /* - * If AFS is defined and we're getting kernel symbol values from - * from N_UNIX, make a copy of Nl[] for possible use with the AFS - * modload file. - */ - if (!(nl = (struct nlist *)malloc(Nll))) { - (void)fprintf(stderr, "%s: no space (%d) for Nl[] copy\n", Pn, Nll); - Error(ctx); - } - (void)memcpy((void *)nl, (void *)Nl, (size_t)Nll); - } -#endif /* defined(HAS_AFS) */ - - if (nlist(Nmlst ? Nmlst : N_UNIX, Nl) < 0) { - (void)fprintf(stderr, "%s: can't read namelist from %s\n", Pn, - Nmlst ? Nmlst : N_UNIX); - Error(ctx); - } - -#if defined(HAS_AFS) - if (nl) { - - /* - * If AFS is defined and we're getting kernel symbol values from - * N_UNIX, and if any X_AFS_* symbols isn't there, see if it is in the - * the AFS modload file. Make sure that other symbols that appear in - * both name list files have the same values. - */ - if ((get_Nl_value(ctx, "arFID", Drive_Nl, &v) >= 0 && !v) || - (get_Nl_value(ctx, "avops", Drive_Nl, &v) >= 0 && !v) || - (get_Nl_value(ctx, "avol", Drive_Nl, &v) >= 0 && !v)) - (void)ckAFSsym(nl); - (void)free((MALLOC_P *)nl); - } -#endif /* defined(HAS_AFS) */ - - /* - * Determine the availability of the ALLKMEM device. If it is available, - * the active processes will be gathered directly from the active process - * chain. - * - * If ALLKMEM isn't available, the active processes will be gathered via the - * kvm_*proc() functions. - */ - if (statsafely(ctx, ALLKMEM, &sb) == 0) - HasALLKMEM = 1; - -#if defined(HASVXFSUTIL) - /* - * If the VXFS utility library is being used, attempt to get the VXFS inode - * offsets before setgid permission is surrendered. - */ - if (access_vxfs_ioffsets() && !Fwarn) { - - /* - * Warn that the VxFS offsets are unavailable. - */ - (void)fprintf(stderr, - "%s: WARNING: vxfsu_get_ioffsets() returned an error.\n", - Pn); - (void)fprintf( - stderr, "%s: WARNING: Thus, no vx_inode information is available\n", - Pn); - (void)fprintf(stderr, - "%s: WARNING: for display or selection of VxFS files.\n", - Pn); - } -#endif /* defined(HASVXFSUTIL) */ - -#if defined(WILLDROPGID) - /* - * If Solaris kernel memory is coming from KMEM, the process is willing to - * surrender GID permission, and the ALLKMEM device is not available, set up - * for GID switching after the first call to open_kvm(). - */ - if (!Memory && !HasALLKMEM) { - Savedgid = getegid(); - if (Setgid) - Switchgid = 1; - } - /* - * If kernel memory isn't coming from KMEM, drop setgid permission - * before attempting to open the (Memory) file. - */ - if (Memory) - (void)dropgid(ctx); -#else /* !defined(WILLDROPGID) */ - /* - * See if the non-KMEM memory file is readable. - */ - if (Memory && !is_readable(ctx, Memory, 1)) - Error(ctx); -#endif /* defined(WILLDROPGID) */ - - /* - * Open access to kernel memory. - */ - open_kvm(ctx); - -#if solaris >= 20500 - /* - * Get the kernel's KERNELBASE value for Solaris 2.5 and above. - */ - v = (KA_T)0; - if (get_Nl_value(ctx, "kbase", Drive_Nl, &v) < 0 || !v || - kread(ctx, (KA_T)v, (char *)&Kb, sizeof(Kb))) { - (void)fprintf(stderr, "%s: can't read kernel base address from %s\n", - Pn, print_kptr(v, (char *)NULL, 0)); - Error(ctx); - } -#endif /* solaris>=20500 */ - - /* - * Get the Solaris clone major device number, if possible. - */ - v = (KA_T)0; - if ((get_Nl_value(ctx, "clmaj", Drive_Nl, &v) < 0) || !v) { - if (get_Nl_value(ctx, "clmaj_alt", Drive_Nl, &v) < 0) - v = (KA_T)0; - } - if (v && kread(ctx, (KA_T)v, (char *)&CloneMaj, sizeof(CloneMaj)) == 0) - HaveCloneMaj = 1; - /* - * If the ALLKMEM device is available, check for the address of the kernel's - * active process chain. If it's not available, clear the ALLKMEM status. - */ - if (HasALLKMEM) { - if ((get_Nl_value(ctx, "pract", Drive_Nl, &PrAct) < 0) || !PrAct) - HasALLKMEM = 0; - } - -#if solaris >= 20501 - /* - * If the ALLKMEM device isn't available, get the kernel's virtual to - * physical map structure for Solaris 2.5.1 and above. - */ - if (!HasALLKMEM) { - if (get_Nl_value(ctx, "kasp", Drive_Nl, &v) >= 0 && v) { - PageSz = getpagesize(); - PSMask = PageSz - 1; - for (i = 1, PSShft = 0; i < PageSz; i <<= 1, PSShft++) - ; - (void)readkam(ctx, v); - } - } -#endif /* solaris>=20501 */ - -#if defined(WILLDROPGID) - /* - * If the ALLKMEM device is available -- i.e., we're not using the - * kvm_*proc() functions to read proc structures -- and if we're willing to - * drop setgid permission, do so. - */ - if (HasALLKMEM) - (void)dropgid(ctx); -#endif /* defined(WILLDROPGID) */ -} - -#if defined(HASZONES) -/* - * enter_zone_arg() - enter zone name argument - */ - -int enter_zone_arg(struct lsof_context *ctx, /* context */ - char *zn) /* zone name */ -{ - int zh; - znhash_t *zp, *zpn; - /* - * Allocate zone argument hash space, as required. - */ - if (!ZoneArg) { - if (!(ZoneArg = (znhash_t **)calloc(HASHZONE, sizeof(znhash_t *)))) { - (void)fprintf(stderr, "%s: no space for zone arg hash\n", Pn); - Error(ctx); - } - } - /* - * Hash the zone name and search the argument hash. - */ - zh = hash_zn(zn); - for (zp = ZoneArg[zh]; zp; zp = zp->next) { - if (!strcmp(zp->zn, zn)) - break; - } - if (zp) { - - /* - * Process a duplicate. - */ - if (!Fwarn) - (void)fprintf(stderr, "%s: duplicate zone name: %s\n", Pn, zn); - return (1); - } - /* - * Create a new hash entry and link it to its bucket. - */ - if (!(zpn = (znhash_t *)malloc((MALLOC_S)sizeof(znhash_t)))) { - (void)fprintf(stderr, "%s no hash space for zone: %s\n", Pn, zn); - Error(ctx); - } - zpn->f = 0; - zpn->zn = zn; - zpn->next = ZoneArg[zh]; - ZoneArg[zh] = zpn; - return (0); -} - -/* - * hash_zn() - hash zone name - */ - -static int hash_zn(char *zn) /* zone name */ -{ - register int i, h; - size_t l; - - if (!(l = strlen(zn))) - return (0); - if (l == 1) - return ((int)*zn & (HASHZONE - 1)); - for (i = h = 0; i < (int)(l - 1); i++) { - h ^= ((int)zn[i] * (int)zn[i + 1]) << ((i * 3) % 13); - } - return (h & (HASHZONE - 1)); -} -#endif /* defined(HASZONES) */ - -/* - * initialize() - perform all initialization - */ - -void initialize(struct lsof_context *ctx) { - get_kernel_access(ctx); - /* - * Read Solaris file system information and construct the clone table. - * - * The clone table is needed to identify sockets. - */ - readfsinfo(ctx); - -#if defined(HASDCACHE) - readdev(ctx, 0); -#else /* !defined(HASDCACHE) */ - read_clone(); -#endif /*defined(HASDCACHE) */ -} - -/* - * kread() - read from kernel memory - */ - -int kread(struct lsof_context *ctx, /* context */ - KA_T addr, /* kernel memory address */ - char *buf, /* buffer to receive data */ - READLEN_T len) /* length to read */ -{ - register int br; - /* - * Because lsof reads kernel data and follows pointers found there at a - * rate considerably slower than the kernel, lsof sometimes acquires - * invalid pointers. If the invalid pointers are fed to kvm_[k]read(), - * a segmentation violation may result, so legal kernel addresses are - * limited by the value of the KERNELBASE symbol (Kb value from the - * kernel's _kernelbase variable for Solaris 2.5 and above). - */ - -#if solaris >= 20500 -# define KVMREAD kvm_kread - if (addr < Kb) -#else /* solaris<20500 */ -# define KVMREAD kvm_read - if (addr < (KA_T)KERNELBASE) -#endif /* solaris>=20500 */ - - return (1); - -#if solaris >= 20501 - - /* - * Do extra address checking for Solaris above 2.5 when the ALLKMEM device - * isn't available. - * - * Make sure the virtual address represents real physical memory by testing - * it with kvm_physaddr(). - * - * For Solaris below 7 read the kernel data with llseek() and read(). For - * Solaris 7 and above use kvm_pread(). - */ - if (Kas && !HasALLKMEM) { - -# if solaris > 20501 - register int b2r; - register char *bp; -# endif /* solaris>20501 */ - - register int h, ip, tb; - register kvmhash_t *kp; - KPHYS pa; - register KVIRT va, vpa; - -# if solaris < 20600 - for (tb = 0, va = (KVIRT)addr; tb < len; tb += br, va += (KVIRT)br) -# else /* solaris>=20600 */ - for (bp = buf, tb = 0, va = (KVIRT)addr; tb < len; - bp += br, tb += br, va += (KVIRT)br) -# endif /* solaris<20600 */ - - { - vpa = (va & (KVIRT)~PSMask) >> PSShft; - ip = (int)(va & (KVIRT)PSMask); - h = HASHKVM(vpa); - for (kp = KVMhb[h]; kp; kp = kp->nxt) { - if (kp->vpa == vpa) { - pa = kp->pa; - break; - } - } - if (!kp) { - if ((pa = kvm_physaddr(Kd, Kas, va)) == KAERR) - return (1); - if (!(kp = (kvmhash_t *)malloc(sizeof(kvmhash_t)))) { - (void)fprintf(stderr, "%s: no kvmhash_t space\n", Pn); - Error(ctx); - } - kp->nxt = KVMhb[h]; - pa = kp->pa = (pa & ~(KPHYS)PSMask); - kp->vpa = vpa; - KVMhb[h] = kp; - } - -# if solaris < 20600 - br = (int)(len - tb); - if ((ip + br) > PageSz) - br = PageSz - ip; -# else /* solaris>=20600 */ - b2r = (int)(len - tb); - if ((ip + b2r) > PageSz) - b2r = PageSz - ip; - pa |= (KPHYS)ip; - -# if solaris < 70000 - if (llseek(Kmd, (offset_t)pa, SEEK_SET) == (offset_t)-1) - return (1); - if ((br = (int)read(Kmd, (void *)bp, (size_t)b2r)) <= 0) - return (1); -# else /* solaris>=70000 */ - if ((br = kvm_pread(Kd, pa, (void *)bp, (size_t)b2r)) <= 0) - return (1); -# endif /* solaris<70000 */ -# endif /* solaris<20600 */ - } - -# if solaris >= 20600 - return (0); -# endif /* solaris>=20600 */ - } -#endif /* solaris>=20501 */ - - /* - * Use kvm_read for Solaris < 2.5; use kvm_kread() Solaris >= 2.5. - */ - br = KVMREAD(Kd, (u_long)addr, buf, len); - return (((READLEN_T)br == len) ? 0 : 1); -} - -/* - * open_kvm() - open kernel virtual memory access - */ - -void open_kvm(struct lsof_context *ctx) { - if (Kd) - return; - -#if defined(WILLDROPGID) - /* - * If this Solaris process began with setgid permission and its been - * surrendered, regain it. - */ - (void)restoregid(ctx); -#endif /* defined(WILLDROPGID) */ - - if (!(Kd = kvm_open(Nmlst, Memory, NULL, O_RDONLY, Pn))) { - (void)fprintf(stderr, "%s: kvm_open(namelist=%s, corefile=%s): %s\n", - Pn, Nmlst ? Nmlst : "default", - Memory ? Memory : "default", strerror(errno)); - Error(ctx); - } - -#if solaris >= 20501 && solaris < 70000 - if ((Kmd = open((Memory ? Memory : KMEM), O_RDONLY)) < 0) { - (void)fprintf(stderr, "%s: open(\"/dev/mem\"): %s\n", Pn, - strerror(errno)); - Error(ctx); - } -#endif /* solaris>=20501 && solaris<70000 */ - -#if defined(WILLDROPGID) - /* - * If this process has setgid permission, and is willing to surrender it, - * do so. - */ - (void)dropgid(ctx); - /* - * If this Solaris process must switch GIDs, enable switching after the - * first call to this function. - */ - if (Switchgid == 1) - Switchgid = 2; -#endif /* define(WILLDROPGID) */ -} - -/* - * process_text() - process text access information + * dproc.c - Solaris /proc process access + * + * This backend deliberately uses only procfs. It does not open /dev/kmem + * or use libkvm. */ -#if solaris >= 90000 -# include +#include "common.h" -/* - * Avl trees are implemented as follows: types in AVL trees contain an - * avl_node_t. These avl_nodes connect to other avl nodes embedded in - * objects of the same type. The avl_tree contains knowledge about the - * size of the structure and the offset of the AVL node in the object - * so we can convert between AVL nodes and (in this case) struct seg. - * - * This code was provided by Casper Dik . - */ +#include -# define READ_AVL_NODE(n, o, s) \ - if (kread(ctx, (KA_T)AVL_NODE2DATA(n, o), (char *)s, sizeof(*s))) \ - return -1 +#include +#include +#include +#include +#include +#include +#include +#include -static int get_first_seg(struct lsof_context *ctx, avl_tree_t *av, - struct seg *s) { - avl_node_t *node = av->avl_root; - size_t off = av->avl_offset; - int count = 0; +#define PROC_BASE "/proc" - while (node != NULL && ++count < MAXSEGS * 2) { - READ_AVL_NODE(node, off, s); - node = s->s_tree.avl_child[0]; - if (node == NULL) - return 0; - } - return -1; +static int nm2id(char *nm, int *id) { + char *ep; + long v; + + if (!nm || !*nm) + return (1); + v = strtol(nm, &ep, 10); + if (!ep || *ep || v < 0 || v > INT_MAX) + return (1); + *id = (int)v; + return (0); } -static int get_next_seg(struct lsof_context *ctx, avl_tree_t *av, - struct seg *s) { - avl_node_t *node = &s->s_tree; - size_t off = av->avl_offset; - int count = 0; +/* + * Solaris /proc//psinfo is the structured 440-byte psinfo_t. Access + * the fields we need at their fixed offsets so this backend does not need + * to define _STRUCTURED_PROC before the kernel headers in dlsof.h. + */ +#define PSINFO_SIZE 440 +#define PSINFO_PID 8 +#define PSINFO_PPID 12 +#define PSINFO_PGID 16 +#define PSINFO_UID 24 +#define PSINFO_EUID 28 +#define PSINFO_FNAME 136 - if (node->avl_child[1]) { - /* - * Has right child, go all the way to the leftmost child of - * the right child. - */ - READ_AVL_NODE(node->avl_child[1], off, s); - while (node->avl_child[0] != NULL && ++count < 2 * MAXSEGS) - READ_AVL_NODE(node->avl_child[0], off, s); - if (count < 2 * MAXSEGS) - return 0; - } else { - /* - * No right child, go up until we find a node we're not a right - * child of. - */ - for (; count < 2 * MAXSEGS; count++) { - int index = AVL_XCHILD(node); - avl_node_t *parent = AVL_XPARENT(node); +/* + * /proc//fdinfo/nn is a variable-length prfdinfo_t followed by + * self-describing pr_misc entries. The layout is from Solaris/illumos + * procfs; it is userland ABI and does not require kernel memory. + */ +#define SUN_FDINFO_HDR 200 +#define SUN_FDINFO_CORE 196 +#define SUN_MISC_PATHNAME 0 +#define SUN_MISC_SOCKETNAME 1 +#define SUN_MISC_PEERSOCKNAME 2 + +struct sun_fdinfo { + int pr_fd; + mode_t pr_mode; + uint64_t pr_ino; + int64_t pr_size; + int64_t pr_offset; + uid_t pr_uid; + gid_t pr_gid; + uint32_t pr_major; + uint32_t pr_minor; + uint32_t pr_rmajor; + uint32_t pr_rminor; + int pr_fileflags; + int pr_fdflags; + short pr_locktype; + pid_t pr_lockpid; + int pr_locksysid; + pid_t pr_peerpid; + int pr_filler[25]; + char pr_peername[16]; +}; - if (parent == NULL) - return -1; +static void enter_path(struct lsof_context *ctx, enum lsof_fd_type fdt, + int fd, const char *path, const struct stat *st); - READ_AVL_NODE(parent, off, s); +struct sun_sockaddr_un { + unsigned short sun_family; + char sun_path[108]; +}; - if (index == 0) - return 0; - } - } - return -1; -} +static void set_sockaddr(struct lfile *lf, int which, const void *p, size_t n, + char *unixpath, size_t unixpathlen) { + const struct sockaddr *sa = (const struct sockaddr *)p; -static void process_text(struct lsof_context *ctx, /* context */ - KA_T pa) /* address space description pointer */ -{ - struct as as; - int i, j, k; - struct seg s; - struct segvn_data vn; - avl_tree_t *avtp; - KA_T v[MAXSEGS]; - /* - * Get address space description. - */ - if (kread(ctx, (KA_T)pa, (char *)&as, sizeof(as))) { - alloc_lfile(ctx, LSOF_FD_PROGRAM_TEXT, -1); - (void)snpf(Namech, Namechl, "can't read text segment list (%s)", - print_kptr(pa, (char *)NULL, 0)); - enter_nm(ctx, Namech); - if (Lf->sf) - link_lfile(ctx); + if (n < sizeof(sa_family_t)) return; + switch (sa->sa_family) { + case AF_INET: { + const struct sockaddr_in *sin = (const struct sockaddr_in *)p; + if (n < sizeof(*sin)) + return; + lf->li[which].af = AF_INET; + lf->li[which].p = ntohs(sin->sin_port); + lf->li[which].ia.a4 = sin->sin_addr; + break; + } +#if defined(AF_INET6) + case AF_INET6: { + const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)p; + if (n < sizeof(*sin6)) + return; + lf->li[which].af = AF_INET6; + lf->li[which].p = ntohs(sin6->sin6_port); + lf->li[which].ia.a6 = sin6->sin6_addr; + break; } - /* - * Loop through the segments. The loop should stop when the segment - * pointer returns to its starting point, but just in case, it's stopped - * when MAXSEGS unique segments have been recorded or 2*MAXSEGS segments - * have been examined. - */ - for (avtp = &as.a_segtree, i = j = 0; (i < MAXSEGS) && (j < 2 * MAXSEGS); - j++) { - if (j ? get_next_seg(ctx, avtp, &s) : get_first_seg(ctx, avtp, &s)) - break; - if ((KA_T)s.s_ops == Sgvops && s.s_data) { - if (kread(ctx, (KA_T)s.s_data, (char *)&vn, sizeof(vn))) - break; - if (vn.vp) { - - /* - * This is a virtual node segment. - * - * If its vnode pointer has not been seen already, record the - * vnode pointer and process the vnode. - */ - for (k = 0; k < i; k++) { - if (v[k] == (KA_T)vn.vp) - break; - } - if (k >= i) { - v[i++] = (KA_T)vn.vp; - alloc_lfile(ctx, LSOF_FD_PROGRAM_TEXT, -1); - -# if defined(FILEPTR) - FILEPTR = (struct file *)NULL; -# endif /* defined(FILEPTR) */ +#endif +#if defined(AF_UNIX) + case AF_UNIX: { + const struct sun_sockaddr_un *unp = (const struct sun_sockaddr_un *)p; + size_t l; - process_node(ctx, (KA_T)vn.vp); - if (Lf->sf) - link_lfile(ctx); - } - } + lf->li[which].af = AF_UNIX; + if (n <= offsetof(struct sun_sockaddr_un, sun_path)) + break; + l = n - offsetof(struct sun_sockaddr_un, sun_path); + if (l >= sizeof(unp->sun_path)) + l = sizeof(unp->sun_path) - 1; + if (unixpath && unixpathlen && l && unp->sun_path[0]) { + if (l >= unixpathlen) + l = unixpathlen - 1; + memcpy(unixpath, unp->sun_path, l); + unixpath[l] = '\0'; } + break; + } +#endif + default: + break; } } -#else /* solaris<90000 */ +static int read_fdinfo(const char *p, struct sun_fdinfo *fi, char *path, + size_t pathlen, unsigned char *sn, size_t *snl, + unsigned char *pn, size_t *pnl) { + unsigned char buf[8192]; + int fd, n; + size_t off; -# if solaris >= 20400 -# define S_NEXT s_next.list -# else /* solaris<20400 */ -# define S_NEXT s_next -# endif /* solaris>=20400 */ - -static void process_text(struct lsof_context *ctx, /* context */ - KA_T pa) /* address space description pointer */ -{ - struct as as; - int i, j, k; - struct seg s; - struct segvn_data vn; - KA_T v[MAXSEGS]; - /* - * Get address space description. - */ - if (kread(ctx, (KA_T)pa, (char *)&as, sizeof(as))) { - alloc_lfile(ctx, LSOF_FD_PROGRAM_TEXT, -1); - (void)snpf(Namech, Namechl, "can't read text segment list (%s)", - print_kptr(pa, (char *)NULL, 0)); - enter_nm(ctx, Namech); - if (Lf->sf) - link_lfile(); - return; - } - /* - * Loop through the segments. The loop should stop when the segment - * pointer returns to its starting point, but just in case, it's stopped - * when MAXSEGS unique segments have been recorded or 2*MAXSEGS segments - * have been examined. - */ - for (s.s_next = as.a_segs, i = j = 0; i < MAXSEGS && j < 2 * MAXSEGS; j++) { - if (!s.S_NEXT || kread(ctx, (KA_T)s.S_NEXT, (char *)&s, sizeof(s))) + *snl = *pnl = 0; + path[0] = '\0'; + if ((fd = open(p, O_RDONLY)) < 0) + return (1); + n = (int)read(fd, buf, sizeof(buf)); + (void)close(fd); + if (n < SUN_FDINFO_HDR) + return (1); + memcpy(fi, buf, sizeof(*fi)); + /* + * The fixed core is 196 bytes. Some Solaris fdinfo records have four + * bytes of alignment before the misc array; detect which by validating + * the first misc header. + */ + off = SUN_FDINFO_CORE; + if (off + 8 <= (size_t)n) { + uint32_t sz, ty; + memcpy(&sz, &buf[off], sizeof(sz)); + memcpy(&ty, &buf[off + 4], sizeof(ty)); + if (sz < 8 || ty > SUN_MISC_PEERSOCKNAME) + off = SUN_FDINFO_HDR; + } + while (off + 8 <= (size_t)n) { + uint32_t sz, ty; + unsigned char *data; + size_t dlen, adv; + + memcpy(&sz, &buf[off], sizeof(sz)); + memcpy(&ty, &buf[off + 4], sizeof(ty)); + if (sz < 8 || off + sz > (size_t)n) break; - if ((KA_T)s.s_ops == Sgvops && s.s_data) { - if (kread(ctx, (KA_T)s.s_data, (char *)&vn, sizeof(vn))) - break; - if (vn.vp) { - - /* - * This is a virtual node segment. - * - * If its vnode pointer has not been seen already, record the - * vnode pointer and process the vnode. - */ - for (k = 0; k < i; k++) { - if (v[k] == (KA_T)vn.vp) - break; - } - if (k >= i) { - v[i++] = (KA_T)vn.vp; - alloc_lfile(ctx, LSOF_FD_PROGRAM_TEXT, -1); - -# if defined(FILEPTR) - FILEPTR = (struct file *)NULL; -# endif /* defined(FILEPTR) */ - - process_node(ctx, (KA_T)vn.vp); - if (Lf->sf) - link_lfile(); - } + data = &buf[off + 8]; + dlen = sz - 8; + switch (ty) { + case SUN_MISC_PATHNAME: + if (dlen && pathlen) { + size_t l = dlen < pathlen - 1 ? dlen : pathlen - 1; + memcpy(path, data, l); + path[l] = '\0'; + } + break; + case SUN_MISC_SOCKETNAME: + if (dlen <= 256) { + memcpy(sn, data, dlen); + *snl = dlen; + } + break; + case SUN_MISC_PEERSOCKNAME: + if (dlen <= 256) { + memcpy(pn, data, dlen); + *pnl = dlen; } + break; + default: + break; } - /* - * Follow the segment link to the starting point in the address - * space description. (The i and j counters place an absolute - * limit on the loop.) - */ - -# if solaris < 20400 - if (s.s_next == as.a_segs) -# else /* solaris>=20400 */ - if (s.s_next.list == as.a_segs.list) -# endif /* solaris<20400 */ - + adv = (sz + 3) & ~3U; + if (adv < sz || off + adv > (size_t)n) break; + off += adv; } + return (0); } -#endif /* solaris>=90000 */ -/* - * readfsinfo() - read file system information - */ +static void set_lock_from_proc(const char *path, struct lfile *lf, + int locktype, SZOFFTYPE size) { +#if defined(F_GETLK) + struct flock fl; + int fd; -static void readfsinfo(struct lsof_context *ctx) { - char buf[FSTYPSZ + 1]; - int i, len; - - if ((Fsinfomax = sysfs(GETNFSTYP)) == -1) { - (void)fprintf(stderr, "%s: sysfs(GETNFSTYP) error: %s\n", Pn, - strerror(errno)); - Error(ctx); - } - if (Fsinfomax == 0) + if (locktype == F_UNLCK) return; - if (!(Fsinfo = (char **)malloc((MALLOC_S)(Fsinfomax * sizeof(char *))))) { - (void)fprintf(stderr, "%s: no space for sysfs info\n", Pn); - Error(ctx); - } - for (i = 1; i <= Fsinfomax; i++) { - if (sysfs(GETFSTYP, i, buf) == -1) { - (void)fprintf(stderr, "%s: sysfs(GETFSTYP) error: %s\n", Pn, - strerror(errno)); - Error(ctx); - } - if (buf[0] == '\0') { - Fsinfo[i - 1] = ""; - continue; - } - buf[FSTYPSZ] = '\0'; - len = strlen(buf) + 1; - if (!(Fsinfo[i - 1] = (char *)malloc((MALLOC_S)len))) { - (void)fprintf(stderr, "%s: no space for file system entry %s\n", Pn, - buf); - Error(ctx); - } - (void)snpf(Fsinfo[i - 1], len, "%s", buf); - -#if defined(HAS_AFS) - if (strcasecmp(buf, "afs") == 0) - AFSfstype = i; -#endif /* defined(HAS_AFS) */ - } + if (locktype == F_RDLCK) + lf->lock = LSOF_LOCK_READ_FULL; + else + lf->lock = LSOF_LOCK_WRITE_FULL; + if ((fd = open(path, O_RDONLY)) < 0) + return; + (void)memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + if (fcntl(fd, F_GETLK, &fl) == 0 && fl.l_type != F_UNLCK) { + int full = (fl.l_start == 0 && (fl.l_len == 0 || fl.l_len >= size)); + if (fl.l_type == F_RDLCK) + lf->lock = full ? LSOF_LOCK_READ_FULL : LSOF_LOCK_READ_PARTIAL; + else if (fl.l_type == F_WRLCK) + lf->lock = full ? LSOF_LOCK_WRITE_FULL : LSOF_LOCK_WRITE_PARTIAL; + } + (void)close(fd); +#else + (void)path; + (void)lf; + (void)locktype; + (void)size; +#endif } -#if solaris >= 20501 -/* - * readkam() - read kernel's address map structure - */ - -static void readkam(struct lsof_context *ctx, /* context */ - KA_T addr) /* kernel virtual address */ -{ - register int i; - register kvmhash_t *kp, *kpp; - static KA_T kas = (KA_T)NULL; - - if (addr) - kas = addr; - Kas = (struct as *)NULL; - -# if solaris < 70000 - if (kas && !kread(ctx, kas, (char *)&Kam, sizeof(Kam))) - Kas = (KA_T)&Kam; -# else /* solaris>=70000 */ - Kas = (struct as *)kas; -# endif /* solaris<70000 */ - - if (Kas) { - if (!KVMhb) { - if (!(KVMhb = - (kvmhash_t **)calloc(KVMHASHBN, sizeof(kvmhash_t *)))) { - (void)fprintf(stderr, - "%s: no space (%d) for KVM hash buckets\n", Pn, - (int)(KVMHASHBN * sizeof(kvmhash_t *))); - Error(ctx); - } - } else if (!addr) { - for (i = 0; i < KVMHASHBN; i++) { - if ((kp = KVMhb[i])) { - while (kp) { - kpp = kp->nxt; - (void)free((void *)kp); - kp = kpp; - } - KVMhb[i] = (kvmhash_t *)NULL; - } - } - } +static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, + const char *fdpath, const char *fipath, + const char *pathpath) { + char nm[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; + char unixpath[MAXPATHLEN + 1]; + unsigned char sock[256], peer[256]; + size_t sl, pl; + struct sun_fdinfo fi; + struct stat st; + ssize_t r; + int has_sock, has_peer; + + if (read_fdinfo(fipath, &fi, path, sizeof(path), sock, &sl, peer, &pl)) + return; + has_sock = has_peer = 0; + unixpath[0] = '\0'; + + if (stat(fdpath, &st) != 0) { + memset(&st, 0, sizeof(st)); + st.st_mode = fi.pr_mode; + st.st_ino = (ino_t)fi.pr_ino; + st.st_size = (off_t)fi.pr_size; + st.st_nlink = 1; + st.st_dev = (dev_t)(((uint64_t)fi.pr_major << 18) | fi.pr_minor); + st.st_rdev = (dev_t)(((uint64_t)fi.pr_rmajor << 18) | fi.pr_rminor); + } + + if (path[0]) + (void)snprintf(nm, sizeof(nm), "%s", path); + else { + r = readlink(pathpath, nm, sizeof(nm) - 1); + if (r < 0) + nm[0] = '\0'; + else + nm[r] = '\0'; } -} -#endif /* solaris>=20501 */ - -/* - * read_proc() - read proc structures - * - * As a side-effect, Kd is set by a call to kvm_open(). - */ - -static void read_proc(struct lsof_context *ctx) { - int ct, ctl, knp, n, try; - MALLOC_S len; - struct proc *p; - KA_T pa, paf, pan; - struct pid pg, pids; - /* - * Try PROCTRYLM times to read a valid proc table. - */ - for (try = 0; try < PROCTRYLM; try++) { - - /* - * Get a proc structure count estimate. - */ - if (get_Nl_value(ctx, "nproc", Drive_Nl, &pa) < 0 || !pa || - kread(ctx, pa, (char *)&knp, sizeof(knp)) || knp < 1) - knp = PROCDFLT; - /* - * Pre-allocate space, as required. - */ - n = knp + PROCDFLT / 4; - if (n > Npa) { - - /* - * Allocate proc structure space. - */ - len = (n * sizeof(struct proc)); - if (P) - P = (struct proc *)realloc((MALLOC_P *)P, len); - else - P = (struct proc *)malloc(len); - if (!P) { - (void)fprintf(stderr, "%s: no proc table space\n", Pn); - Error(ctx); - } - /* - * Pre-allocate PGID and PID number space. - */ - len = (MALLOC_S)(n * sizeof(int)); - if (Pgid) - Pgid = (int *)realloc((MALLOC_P *)Pgid, len); - else - Pgid = (int *)malloc(len); - if (!Pgid) { - (void)fprintf(stderr, "%s: no PGID table space\n", Pn); - Error(ctx); - } - if (Pid) - Pid = (int *)realloc((MALLOC_P *)Pid, len); - else - Pid = (int *)malloc(len); - if (!Pid) { - (void)fprintf(stderr, "%s: no PID table space\n", Pn); - Error(ctx); - } - Npa = n; - } - if (HasALLKMEM) { - - /* - * Prepare for a proc table scan via direct reading of the active - * chain. - */ - if (!PrAct || kread(ctx, PrAct, (char *)&paf, sizeof(pa))) { - (void)fprintf(stderr, "%s: can't read practive from %s\n", Pn, - print_kptr(PrAct, (char *)NULL, 0)); - Error(ctx); - } - ct = 1; - ctl = knp << 3; - pan = paf; - pa = (KA_T)NULL; - } else { - - /* - * Prepare for a proc table scan via the kvm_*proc() functions. - */ - if (kvm_setproc(Kd) != 0) { - (void)fprintf(stderr, "%s: kvm_setproc: %s\n", Pn, - strerror(errno)); - Error(ctx); - } - } - /* - * Accumulate proc structures. - */ - Np = 0; - for (;;) { - if (Np >= Npa) { + enter_path(ctx, LSOF_FD_NUMERIC, fd, nm[0] ? nm : fdpath, &st); + if (!Lf) + return; - /* - * Expand the local proc table. - */ - Npa += PROCDFLT / 2; - len = (MALLOC_S)(Npa * sizeof(struct proc)); - if (!(P = (struct proc *)realloc((MALLOC_P *)P, len))) { - (void)fprintf(stderr, "%s: no more (%d) proc space\n", Pn, - Npa); - Error(ctx); - } - /* - * Expand the PGID and PID tables. - */ - len = (MALLOC_S)(Npa * sizeof(int)); - if (!(Pgid = (int *)realloc((MALLOC_P *)Pgid, len))) { - (void)fprintf(stderr, "%s: no more (%d) PGID space\n", Pn, - Npa); - Error(ctx); - } - if (!(Pid = (int *)realloc((MALLOC_P *)Pid, len))) { - (void)fprintf(stderr, "%s: no more (%d) PID space\n", Pn, - Npa); - Error(ctx); - } + if (fi.pr_offset >= 0) { + Lf->off = (SZOFFTYPE)fi.pr_offset; + Lf->off_def = 1; + } + switch (fi.pr_fileflags & O_ACCMODE) { + case O_RDONLY: + Lf->access = LSOF_FILE_ACCESS_READ; + break; + case O_WRONLY: + Lf->access = LSOF_FILE_ACCESS_WRITE; + break; + default: + Lf->access = LSOF_FILE_ACCESS_READ_WRITE; + break; + } + + if (fi.pr_locktype != F_UNLCK) + set_lock_from_proc(nm[0] ? nm : fdpath, Lf, fi.pr_locktype, + (SZOFFTYPE)st.st_size); + + + if ((fi.pr_mode & S_IFMT) == S_IFSOCK) { + if (sl) { + (void)set_sockaddr(Lf, 0, sock, sl, unixpath, sizeof(unixpath)); + has_sock = 1; + } + if (pl) { + (void)set_sockaddr(Lf, 1, peer, pl, (char *)NULL, 0); + has_peer = 1; + } + if (unixpath[0]) + enter_nm(ctx, unixpath); + else if (Lf->nm) { + free(Lf->nm); + Lf->nm = (char *)NULL; + } + Lf->ntype = Ntype = N_REGLR; + if (has_sock || has_peer) { + switch (Lf->li[0].af) { + case AF_INET: + Lf->type = LSOF_FILE_IPV4; + break; +#if defined(AF_INET6) + case AF_INET6: + Lf->type = LSOF_FILE_IPV6; + break; +#endif +#if defined(AF_UNIX) + case AF_UNIX: + Lf->type = LSOF_FILE_UNIX; + break; +#endif + default: + Lf->type = LSOF_FILE_SOCKET; + break; } - /* - * Read the next proc structure. - */ - if (HasALLKMEM) { - - /* - * If the ALLKMEM device exists, read proc structures directly - * from the active chain. - */ - if (!pa) - pa = paf; - else { - pa = pan; - if ((pan == paf) || (++ct > ctl)) - break; - } - if (!pa) - break; - p = (struct proc *)&P[Np]; - if (kread(ctx, pa, (char *)p, sizeof(struct proc))) - break; - pan = (KA_T)p->p_next; - } else { - - /* - * If the ALLKMEM device doesn't exist, read proc structures - * via kbm_getproc(). - */ - if (!(p = kvm_nextproc(Kd))) - break; + Lf->lts.type = 1; + if (Selflags & SELNET) + Lf->sf |= SELNET; + if (Selflags & SELUNX) + Lf->sf |= SELUNX; +#if defined(SELNA) + if ((Selflags & SELNA) && Nwad) { + unsigned char *la = (unsigned char *)NULL; + unsigned char *fa = (unsigned char *)NULL; + if (Lf->li[0].af == AF_INET) + la = (unsigned char *)&Lf->li[0].ia.a4; +#if defined(AF_INET6) + else if (Lf->li[0].af == AF_INET6) + la = (unsigned char *)&Lf->li[0].ia.a6; +#endif + if (Lf->li[1].af == AF_INET) + fa = (unsigned char *)&Lf->li[1].ia.a4; +#if defined(AF_INET6) + else if (Lf->li[1].af == AF_INET6) + fa = (unsigned char *)&Lf->li[1].ia.a6; +#endif + ent_inaddr(ctx, la, Lf->li[0].p, fa, Lf->li[1].p, + Lf->li[0].af ? Lf->li[0].af : Lf->li[1].af); } - /* - * Check process status. - */ - if (p->p_stat == 0 || p->p_stat == SZOMB) - continue; - -#if solaris >= 20500 - /* - * Check Solaris 2.5 and above p_cred pointer. - */ - if (!p->p_cred) - continue; -#endif /* solaris >=20500 */ - - /* - * Read Solaris PGID and PID numbers. - */ - if (!p->p_pgidp || - kread(ctx, (KA_T)p->p_pgidp, (char *)&pg, sizeof(pg))) - continue; - if (!p->p_pidp || - kread(ctx, (KA_T)p->p_pidp, (char *)&pids, sizeof(pids))) - continue; - /* - * Save the PGID and PID numbers in local tables. - */ - Pgid[Np] = (int)pg.pid_id; - Pid[Np] = (int)pids.pid_id; - /* - * If the proc structure came from kvm_getproc(), save it in the - * local table. - */ - if (!HasALLKMEM) - P[Np] = *p; - Np++; - } - /* - * If not enough processes were saved in the local table, try again. - * - * If the ALLKMEM device isn't available, it is necessary to close and - * reopen the KVM device, so that kvm_open() will acquire a fresh - * address for the head of the linked list process table. - */ - if (Np >= PROCMIN) - break; - if (!HasALLKMEM) { - close_kvm(ctx); - open_kvm(ctx); - } - } - /* - * Quit if no proc structures were stored in the local table. - */ - if (try >= PROCTRYLM) { - (void)fprintf(stderr, "%s: can't read proc table\n", Pn); - Error(ctx); - } - if (Np < Npa && !RptTm) { - - /* - * Reduce the local proc structure table size to its minimum if - * not in repeat mode. - */ - len = (MALLOC_S)(Np * sizeof(struct proc)); - if (!(P = (struct proc *)realloc((MALLOC_P *)P, len))) { - (void)fprintf(stderr, "%s: can't reduce proc table to %d\n", Pn, - Np); - Error(ctx); - } - /* - * Reduce the Solaris PGID and PID tables to their minimum if - * not in repeat mode. - */ - len = (MALLOC_S)(Np * sizeof(int)); - if (!(Pgid = (int *)realloc((MALLOC_P *)Pgid, len))) { - (void)fprintf(stderr, "%s: can't reduce PGID table to %d\n", Pn, - Np); - Error(ctx); - } - if (!(Pid = (int *)realloc((MALLOC_P *)Pid, len))) { - (void)fprintf(stderr, "%s: can't reduce PID table to %d\n", Pn, Np); - Error(ctx); +#endif } - Npa = Np; } + if (Lf && Lf->sf) + link_lfile(ctx); } -#if defined(WILLDROPGID) -/* - * restoregid() -- restore setgid permission, as required - */ +static int read_psinfo(const char *p, unsigned char *ps) { + int fd, n; -void restoregid(struct lsof_context *ctx) { - if (Switchgid == 2 && !Setgid) { - if (setgid(Savedgid) != 0) { - (void)fprintf(stderr, "%s: can't set effective GID to %d: %s\n", Pn, - (int)Savedgid, strerror(errno)); - Error(ctx); - } - Setgid = 1; - } + if ((fd = open(p, O_RDONLY)) < 0) + return (1); + n = (int)read(fd, ps, PSINFO_SIZE); + (void)close(fd); + return (n == PSINFO_SIZE) ? 0 : 1; } -#endif /* defined(WILLDROPGID) */ - -#if defined(HASNCACHE) && solaris >= 90000 - -/* - * Local static values - */ -static int Mhl; /* local name cache hash mask */ -static int Nhl = 0; /* size of local name cache hash - * pointer table */ -struct l_nch { - KA_T vp; /* vnode address */ - KA_T dp; /* parent vnode address */ - struct l_nch *pa; /* parent Ncache address */ - char *nm; /* name */ - int nl; /* name length */ -}; - -static struct l_nch *Ncache = (struct l_nch *)NULL; -/* the local name cache */ -static struct l_nch **Nchash = (struct l_nch **)NULL; -/* Ncache hash pointers */ -static int Ncfirst = 1; /* first-call status */ -static KA_T NegVN = (KA_T)NULL; /* negative vnode address */ -static int Nla = 0; /* entries allocated to Ncache[] */ -static int Nlu = 0; /* entries used in Ncache[] */ - -static struct l_nch *ncache_addr(KA_T v); - -# define ncachehash(v) Nchash + ((((int)(v) >> 2) * 31415) & Mhl) - -static int ncache_isroot(KA_T va, char *cp); - -# define LNCHINCRSZ 64 /* local size increment */ -# define XNC \ - 15 /* extra name characters to read beyond those \ - * in name[] of the ncache_t structure -- this \ - * is an efficiency hint and MUST BE AT LEAST \ - * ONE. */ - -/* - * ncache_addr() - look up a node's local ncache address - */ - -static struct l_nch * - -ncache_addr(KA_T v) /* vnode's address */ -{ - struct l_nch **hp; - - for (hp = ncachehash(v); *hp; hp++) { - if ((*hp)->vp == v) - return (*hp); - } - return ((struct l_nch *)NULL); +static void enter_path(struct lsof_context *ctx, enum lsof_fd_type fdt, + int fd, const char *path, const struct stat *st) { + enum lsof_file_type ft; + enum vtype vt; + int ntype; + + alloc_lfile(ctx, fdt, fd); + Lf->dev = st->st_dev; + Lf->dev_def = 1; + Lf->inode = (INODETYPE)st->st_ino; + Lf->inp_ty = 1; + Lf->nlink = (long)st->st_nlink; + Lf->nlink_def = 1; + Lf->sz = (SZOFFTYPE)st->st_size; + Lf->sz_def = 1; + Lf->access = LSOF_FILE_ACCESS_READ_WRITE; + + switch (st->st_mode & S_IFMT) { + case S_IFREG: + ft = LSOF_FILE_REGULAR; + vt = VREG; + ntype = N_REGLR; + break; + case S_IFDIR: + ft = LSOF_FILE_DIR; + vt = VDIR; + ntype = N_REGLR; + break; + case S_IFCHR: + ft = LSOF_FILE_CHAR; + vt = VCHR; + ntype = N_REGLR; + Lf->rdev = st->st_rdev; + Lf->rdev_def = 1; + break; + case S_IFBLK: + ft = LSOF_FILE_BLOCK; + vt = VBLK; + ntype = N_REGLR; + Lf->rdev = st->st_rdev; + Lf->rdev_def = 1; + break; + case S_IFIFO: + ft = LSOF_FILE_FIFO; + vt = VFIFO; + ntype = N_REGLR; + break; + case S_IFSOCK: + ft = LSOF_FILE_SOCKET; + vt = VSOCK; + ntype = N_REGLR; + break; + default: + ft = LSOF_FILE_UNKNOWN_STAT; + vt = VNON; + ntype = N_REGLR; + break; + } + + Lf->type = ft; + Lf->ntype = Ntype = ntype; + if (path && *path) { + if (Sfile && is_file_named(ctx, (char *)path, Ntype, vt, 1)) + Lf->sf |= SELNM; + enter_nm(ctx, (char *)path); + } else + enter_nm(ctx, "(unknown)"); } -/* - * ncache_isroot() - is head of name cache path a file system root? - */ - -static int ncache_isroot(KA_T va, /* kernel vnode address */ - char *cp) /* partial path */ -{ - char buf[MAXPATHLEN]; - int i; - MALLOC_S len; - struct mounts *mtp; - struct stat sb; - struct vnode v; - static int vca = 0; - static int vcn = 0; - static KA_T *vc = (KA_T *)NULL; +static void process_path(struct lsof_context *ctx, int pid, + enum lsof_fd_type fdt, int fd, const char *procpath, + const char *pathpath) { + char nm[MAXPATHLEN + 1]; + struct stat st; + ssize_t n; - if (!va) - return (0); - /* - * Search the root vnode cache. - */ - for (i = 0; i < vcn; i++) { - if (va == vc[i]) - return (1); - } - /* - * Read the vnode and see if it's a VDIR node with the VROOT flag set. If - * it is, then the path is complete. - * - * If it isn't, and if the file has an inode number, search the mount table - * and see if the file system's inode number is known. If it is, form the - * possible full path, safely stat() it, and see if it's inode number - * matches the one we have for this file. If it does, then the path is - * complete. - */ - if (kread(ctx, (KA_T)va, (char *)&v, sizeof(v)) || v.v_type != VDIR || - !(v.v_flag & VROOT)) { + n = readlink(pathpath, nm, sizeof(nm) - 1); + if (n < 0) + nm[0] = '\0'; + else + nm[n] = '\0'; - /* - * The vnode tests failed. Try the inode tests. - */ - if (Lf->inp_ty != 1 || !Lf->inode || !Lf->fsdir || - (len = strlen(Lf->fsdir)) < 1) - return (0); - if ((len + 1 + strlen(cp) + 1) > sizeof(buf)) - return (0); - for (mtp = readmnt(); mtp; mtp = mtp->next) { - if (!mtp->dir || !mtp->inode) - continue; - if (strcmp(Lf->fsdir, mtp->dir) == 0) - break; - } - if (!mtp) - return (0); - (void)strcpy(buf, Lf->fsdir); - if (buf[len - 1] != '/') - buf[len++] = '/'; - (void)strcpy(&buf[len], cp); - if (statsafely(buf, &sb) != 0 || (INODETYPE)sb.st_ino != Lf->inode) - return (0); - } /* - * Add the vnode address to the root vnode cache. + * stat() on /proc//fd/ returns the underlying file, including + * deleted ZFS files. For cwd/root/exe, stat the procfs symlink itself. */ - if (vcn >= vca) { - vca += 10; - len = (MALLOC_S)(vca * sizeof(KA_T)); - if (!vc) - vc = (KA_T *)malloc(len); - else - vc = (KA_T *)realloc(vc, len); - if (!vc) { - (void)fprintf(stderr, "%s: no space for root vnode table\n", Pn); - Error(ctx); - } - } - vc[vcn++] = va; - return (1); -} - -/* - * ncache_load() - load the kernel's name cache - */ - -void ncache_load() { - char *cp; - struct l_nch **hp, *lc; - int h, i, len, n, xl; - static int iNch = 0; - nc_hash_t *kh; - static KA_T kha = (KA_T)NULL; - static nc_hash_t *khl = (nc_hash_t *)NULL; - KA_T kn; - static ncache_t *nc = (ncache_t *)NULL; - static int Nch = 0; - static int nmo = 0; - KA_T v; - static int xn = 0; - - if (!Fncache) - return; - if (Ncfirst) { - - /* - * Do startup (first-time) functions. - */ - Ncfirst = 0; - /* - * Establish DNLC hash size. - */ - v = (KA_T)0; - if (get_Nl_value(ctx, X_NCSIZE, (struct drive_Nl *)NULL, &v) < 0 || !v || - kread(ctx, (KA_T)v, (char *)&Nch, sizeof(Nch))) { - if (!Fwarn) - (void)fprintf(stderr, - "%s: WARNING: can't read DNLC hash size: %s\n", - Pn, print_kptr(v, (char *)NULL, 0)); - iNch = Nch = 0; - return; - } - if ((iNch = Nch) < 1) { - if (!Fwarn) - (void)fprintf(stderr, "%s: WARNING: DNLC hash size: %d\n", Pn, - Nch); - iNch = Nch = 0; - return; - } - /* - * Get negative vnode address. - */ - if (get_Nl_value(ctx, NCACHE_NEGVN, (struct drive_Nl *)NULL, &NegVN) < 0) - NegVN = (KA_T)NULL; - /* - * Establish DNLC hash address. - */ - v = (KA_T)0; - if (get_Nl_value(ctx, X_NCACHE, (struct drive_Nl *)NULL, (KA_T *)&v) < 0 || - !v || kread(ctx, v, (char *)&kha, sizeof(kha)) || !kha) { - if (!Fwarn) - (void)fprintf(stderr, "%s: WARNING: no DNLC hash address\n", - Pn); - iNch = Nch = 0; + if (stat(procpath, &st) != 0) { + if (!nm[0]) return; - } - /* - * Allocate space for a local copy of the kernel's hash table. - */ - len = Nch * sizeof(nc_hash_t); - if (!(khl = (nc_hash_t *)malloc((MALLOC_S)len))) { - (void)fprintf(stderr, "%s: can't allocate DNLC hash space: %d\n", - Pn, len); - Error(ctx); - } - /* - * Allocate space for a kernel DNLC entry, plus additional name space - * for efficiency. - */ - xn = XNC; - if (!(nc = (ncache_t *)malloc((MALLOC_S)(sizeof(ncache_t) + XNC)))) { - (void)fprintf(stderr, "%s: can't allocate DNLC ncache_t space\n", - Pn); - Error(ctx); - } - nmo = offsetof(struct ncache, name); - /* - * Allocate estimated space for the local cache, based on the - * hash table count and the current average hash length. - */ - v = (KA_T)0; - if ((get_Nl_value(ctx, "hshav", (struct drive_Nl *)NULL, (KA_T *)&v) < 0) || - !v || kread(ctx, v, (char *)&i, sizeof(i)) || (i < 1)) { - i = 16; - if (!Fwarn) { - (void)fprintf(stderr, - "%s: can't read DNLC average hash bucket size,", - Pn); - (void)fprintf(stderr, " using %d\n", i); - } - } - Nla = Nch * i; - if (!(Ncache = (struct l_nch *)calloc(Nla, sizeof(struct l_nch)))) { - - no_local_space: - - (void)fprintf(stderr, "%s: no space for %d byte local name cache\n", - Pn, len); - Error(ctx); - } - } else { - - /* - * Do setup for repeat calls. - */ - if (!iNch || !Nla || !Ncache) + if (stat(nm, &st) != 0) return; - if (Nchash) { - (void)free((FREE_P *)Nchash); - Nchash = (struct l_nch **)NULL; - } - if (Ncache && Nlu) { - - /* - * Free space malloc'd to names in local name cache. - */ - for (i = 0, lc = Ncache; i < Nlu; i++, lc++) { - if (lc->nm) { - (void)free((FREE_P *)lc->nm); - lc->nm = (char *)NULL; - } - } - } - Nch = iNch; - Nlu = 0; - } - /* - * Read the kernel's DNLC hash. - */ - if (kread(ctx, kha, (char *)khl, (Nch * sizeof(nc_hash_t)))) { - if (!Fwarn) - (void)fprintf(stderr, "%s: WARNING: can't read DNLC hash: %s\n", Pn, - print_kptr(kha, (char *)NULL, 0)); - iNch = Nch = 0; - return; } - /* - * Build a local copy of the kernel name cache. - */ - for (i = n = 0, kh = khl, lc = Ncache; i < Nch; i++, kh++) { + enter_path(ctx, fdt, fd, nm[0] ? nm : procpath, &st); + if (Lf && Lf->sf) + link_lfile(ctx); +} - /* - * Skip empty hash buckets. - */ - if (!kh->hash_next || ((KA_T)kh->hash_next == kha)) - continue; - /* - * Process a hash bucket. - */ - for (kn = (KA_T)kh->hash_next, h = 0; - kn && (h < Nch) && (!h || (h && kn != (KA_T)kh->hash_next)); - kn = (KA_T)nc->hash_next, h++) { - if (kread(ctx, kn, (char *)nc, sizeof(ncache_t) + XNC)) - break; - if (!nc->vp || (len = (int)nc->namlen) < 1) - continue; - if (NegVN && ((KA_T)nc->vp == NegVN)) - continue; - if ((len < 3) && (nc->name[0] == '.')) { - if ((len < 2) || (nc->name[1] == '.')) - continue; - } - /* - * If not all the name has been read, read the rest of it, - * allocating more space at the end of the ncache structure as - * required. - */ - if (len > (XNC + 1)) { - if (len > (xn + 1)) { - while (len > (xn + 1)) - xn = xn + xn; - xn = ((xn + 7) & ~7) - 1; - if (!(nc = (ncache_t *)realloc((MALLOC_P *)nc, - (sizeof(ncache_t) + xn)))) { - (void)fprintf(stderr, - "%s: can't extend DNLC ncache_t buffer\n", - Pn); - Error(ctx); - } - } - cp = &nc->name[XNC + 1]; - v = (KA_T)((char *)kn + nmo + XNC + 1); - xl = len - XNC - 1; - if (kread(ctx, v, cp, xl)) - continue; - } - /* - * Allocate space for the name in the local name cache entry. - */ - if (!(cp = (char *)malloc(len + 1))) { - (void)fprintf( - stderr, "%s: can't allocate %d bytes for name cache name\n", - Pn, len + 1); - Error(ctx); - } - (void)strncpy(cp, nc->name, len); - cp[len] = '\0'; - /* - * Make sure there is space for another local name cache entry. - * If not, allocate twice as many entries. - */ - if (n >= Nla) { - Nla = Nla + Nla; - if (!(Ncache = (struct l_nch *)realloc( - Ncache, (MALLOC_S)(Nla * sizeof(struct l_nch))))) { - (void)fprintf(stderr, - "%s: can't enlarge local name cache\n", Pn); - Error(ctx); - } - lc = &Ncache[n]; - } - /* - * Complete the local cache entry. - */ - lc->vp = (KA_T)nc->vp; - lc->dp = (KA_T)nc->dp; - lc->pa = (struct l_nch *)NULL; - lc->nm = cp; - lc->nl = len; - lc++; - n++; - } +void gather_proc_info(struct lsof_context *ctx) { + DIR *pd, *fdd; + struct dirent *dp, *fdp; + unsigned char ps[PSINFO_SIZE]; + char cmd[256]; + char path[MAXPATHLEN + 1]; + char fdpath[MAXPATHLEN + 1]; + char pspath[MAXPATHLEN + 1]; + int pid; + + if (!(pd = opendir(PROC_BASE))) { + (void)fprintf(stderr, "%s: can't open %s: %s\n", Pn, PROC_BASE, + strerror(errno)); + Error(ctx); } - /* - * Reduce memory usage, as required. - */ - if ((Nlu = n) < 1) { - /* - * No DNLC entries were located, an unexpected result. - */ - if (!RptTm && Ncache) { + while ((dp = readdir(pd))) { + short pss, sf; - /* - * If not in repeat mode, free the space that has been malloc'd - * to the local name cache. - */ - (void)free((FREE_P *)Ncache); - Ncache = (struct l_nch *)NULL; - Nla = Nlu = 0; - } - /* - * Issue a warning and disable furthe DNLC processing. - */ - if (!Fwarn) - (void)fprintf(stderr, - "%s: WARNING: unusable local name cache size: %d\n", - Pn, n); - iNch = Nch = 0; - return; - } - if ((Nlu < Nla) && !RptTm) { - len = Nlu * sizeof(struct l_nch); - if (!(Ncache = (struct l_nch *)realloc(Ncache, len))) - goto no_local_space; - Nla = Nlu; - } - /* - * Build a hash table to locate Ncache entries. - */ - for (Nhl = 1; Nhl < Nlu; Nhl <<= 1) - ; - Nhl <<= 1; - Mhl = Nhl - 1; - if (!(Nchash = - (struct l_nch **)calloc(Nhl + Nlu, sizeof(struct l_nch *)))) { - (void)fprintf(stderr, "%s: no space for %d name cache hash pointers\n", - Pn, Nhl + Nlu); - Error(ctx); - } - for (i = 0, lc = Ncache; i < Nlu; i++, lc++) { - for (hp = ncachehash(lc->vp), h = 1; *hp; hp++) { - if ((*hp)->vp == lc->vp && strcmp((*hp)->nm, lc->nm) == 0 && - (*hp)->dp == lc->dp) { - h = 0; - break; - } - } - if (h) - *hp = lc; - } - /* - * Make a final pass through the local cache and convert parent vnode - * addresses to local name cache pointers. - */ - for (i = 0, lc = Ncache; i < Nlu; i++, lc++) { - if (!lc->dp) + if (nm2id(dp->d_name, &pid)) continue; - if (NegVN && (lc->dp == NegVN)) { - lc->pa = (struct l_nch *)NULL; + (void)snpf(pspath, sizeof(pspath), "%s/%d/psinfo", PROC_BASE, pid); + if (read_psinfo(pspath, ps)) continue; - } - lc->pa = ncache_addr(lc->dp); - } -} + { + pid_t ps_pid, ps_ppid, ps_pgid; + uid_t ps_uid, ps_euid; + + memcpy(&ps_pid, &ps[PSINFO_PID], sizeof(ps_pid)); + memcpy(&ps_ppid, &ps[PSINFO_PPID], sizeof(ps_ppid)); + memcpy(&ps_pgid, &ps[PSINFO_PGID], sizeof(ps_pgid)); + memcpy(&ps_uid, &ps[PSINFO_UID], sizeof(ps_uid)); + memcpy(&ps_euid, &ps[PSINFO_EUID], sizeof(ps_euid)); + (void)memcpy(cmd, &ps[PSINFO_FNAME], sizeof(cmd) - 1); + cmd[sizeof(cmd) - 1] = '\0'; + if (ps_pid != pid) + continue; + if (is_proc_excl(ctx, pid, (int)ps_pgid, (UID_ARG)ps_uid, &pss, + &sf) || + is_cmd_excl(ctx, cmd, &pss, &sf)) + continue; -/* - * ncache_lookup() - look up a node's name in the kernel's name cache - */ + alloc_lproc(ctx, pid, (int)ps_pgid, (int)ps_ppid, (UID_ARG)ps_euid, + cmd, (int)pss, (int)sf); + } + Plf = (struct lfile *)NULL; -char *ncache_lookup(char *buf, /* receiving name buffer */ - int blen, /* receiving buffer length */ - int *fp) /* full path reply */ -{ - char *cp = buf; - struct l_nch *lc; - struct mounts *mtp; - int nl, rlen; + (void)snpf(path, sizeof(path), "%s/%d/cwd", PROC_BASE, pid); + (void)snpf(pspath, sizeof(pspath), "%s/%d/path/cwd", PROC_BASE, pid); + process_path(ctx, pid, LSOF_FD_CWD, -1, path, pspath); - *cp = '\0'; - *fp = 0; + (void)snpf(path, sizeof(path), "%s/%d/root", PROC_BASE, pid); + (void)snpf(pspath, sizeof(pspath), "%s/%d/path/root", PROC_BASE, pid); + process_path(ctx, pid, LSOF_FD_ROOT_DIR, -1, path, pspath); -# if defined(HASFSINO) - /* - * If the entry has an inode number that matches the inode number of the - * file system mount point, return an empty path reply. That tells the - * caller to print the file system mount point name only. - */ - if (Lf->inp_ty == 1 && Lf->fs_ino && Lf->inode == Lf->fs_ino) - return (cp); -# endif /* defined(HASFSINO) */ + (void)snpf(path, sizeof(path), "%s/%d/path/a.out", PROC_BASE, pid); + process_path(ctx, pid, LSOF_FD_PROGRAM_TEXT, -1, path, path); - /* - * Look up the name cache entry for the node address. - */ - if (!Nlu || !(lc = ncache_addr(Lf->na))) { + (void)snpf(path, sizeof(path), "%s/%d/fd", PROC_BASE, pid); + if (!(fdd = opendir(path))) + continue; + while ((fdp = readdir(fdd))) { + int fd; - /* - * If the node has no cache entry, see if it's the mount - * point of a known file system. - */ - if (!Lf->fsdir || !Lf->dev_def || Lf->inp_ty != 1) - return ((char *)NULL); - for (mtp = readmnt(); mtp; mtp = mtp->next) { - if (!mtp->dir || !mtp->inode) + if (nm2id(fdp->d_name, &fd)) continue; - if (Lf->dev == mtp->dev && mtp->inode == Lf->inode && - strcmp(mtp->dir, Lf->fsdir) == 0) - return (cp); - } - return ((char *)NULL); - } - /* - * Begin the path assembly. - */ - if ((nl = lc->nl) > (blen - 1)) - return ((char *)NULL); - cp = buf + blen - nl - 1; - rlen = blen - nl - 1; - (void)strcpy(cp, lc->nm); - /* - * Look up the name cache entries that are parents of the node address. - * Quit when: - * - * there's no parent; - * the name is too large to fit in the receiving buffer. - */ - for (;;) { - if (!lc->pa) { - if (ncache_isroot(lc->dp, cp)) - *fp = 1; - break; + (void)snpf(fdpath, sizeof(fdpath), "%s/%s", path, fdp->d_name); + (void)snpf(pspath, sizeof(pspath), "%s/%d/fdinfo/%d", PROC_BASE, + pid, fd); + { + char rpath[MAXPATHLEN + 1]; + (void)snpf(rpath, sizeof(rpath), "%s/%d/path/%d", PROC_BASE, + pid, fd); + process_fdinfo(ctx, pid, fd, fdpath, pspath, rpath); + } } - lc = lc->pa; - if (((nl = lc->nl) + 1) > rlen) - break; - *(cp - 1) = '/'; - cp--; - rlen--; - (void)strncpy((cp - nl), lc->nm, nl); - cp -= nl; - rlen -= nl; + (void)closedir(fdd); } - return (cp); + (void)closedir(pd); } -#endif /* defined(HASNCACHE) && solaris>=90000 */ diff --git a/lib/dialects/sun/dproto.h b/lib/dialects/sun/dproto.h index 014c28f2..9e0d694f 100644 --- a/lib/dialects/sun/dproto.h +++ b/lib/dialects/sun/dproto.h @@ -51,8 +51,6 @@ extern int CTF_memCB(const char *name, ctf_id_t id, ulong_t offset, void *arg); extern int is_file_named(struct lsof_context *ctx, char *p, int nt, enum vtype vt, int ps); -extern struct l_vfs *readvfs(struct lsof_context *ctx, KA_T ka, struct vfs *la, - struct vnode *lv); extern int vop2ty(struct lsof_context *ctx, struct vnode *vp, int fx); #if defined(HAS_AFS) diff --git a/lib/dialects/sun/dsock.c b/lib/dialects/sun/dsock.c index 403e26a1..f47ac44a 100644 --- a/lib/dialects/sun/dsock.c +++ b/lib/dialects/sun/dsock.c @@ -1,1680 +1,8 @@ /* - * dsock.c - Solaris socket processing functions for lsof - */ - -/* - * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana - * 47907. All rights reserved. - * - * Written by Victor A. Abell - * - * This software is not subject to any license of the American Telephone - * and Telegraph Company or the Regents of the University of California. - * - * Permission is granted to anyone to use this software for any purpose on - * any computer system, and to alter it and redistribute it freely, subject - * to the following restrictions: - * - * 1. Neither the authors nor Purdue University are responsible for any - * consequences of the use of this software. - * - * 2. The origin of this software must not be misrepresented, either by - * explicit claim or by omission. Credit to the authors and Purdue - * University must appear in documentation and sources. + * dsock.c - Solaris /proc socket placeholder. * - * 3. Altered versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 4. This notice may not be removed or altered. + * Socket endpoint details are not yet read through /proc; the procfs + * backend still reports the numeric fd and its inode/device. */ -#ifndef lint -static char copyright[] = - "@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n"; -#endif - #include "common.h" - -#if solaris >= 110000 -# include -#endif /* solaris>=110000 */ - -#if defined(HAS_LIBCTF) && solaris >= 110000 -/* - * Icmp_t, rts_t and udp_t structure support for Solaris >=11 via libctf - * - * These structure definitions may look like kernel structures, but they - * are not. They have been defined to have member names that duplicate - * those used by the kernel that are of interest to lsof. Member values - * are obtained via the CTF library, libctf. - * - * Robert Byrnes developed the CTF library access code and contributed it - * to lsof. - */ - -/* - * Icmp_t internal structure definition - */ - -typedef struct icmp_s { - uint_t icmp_state; /* TPI state */ - KA_T *icmp_connp; /* connection structure pointer */ - in6_addr_t icmp_bound_v6src; /* Explicitly bound to address */ - in6_addr_t icmp_v6src; /* Source address of this stream */ - union { - uint_t icmp_dummy; - uint_t icmp_Debug : 1, /* SO_DEBUG option */ - icmp_dontroute : 1, /* SO_DONTROUTE option */ - icmp_broadcast : 1, /* SO_BROADCAST option */ - icmp_reuseaddr : 1, /* SO_REUSEADDR option */ - icmp_useloopback : 1, /* SO_USELOOPBACK option */ - icmp_hdrincl : 1, /* IP_HDRINCL option, etc. */ - icmp_dgram_errind : 1, /* SO_DGRAM_ERRIND option */ - icmp_pad : 25; /* pad to bit 31 */ - } icmp_debug; /* This name identifies a single bit - * variable of the kernel's union, but - * CTF won't read individual bit - * variables, so for CTF's purposes - * it is declared as a uint_t union, - * named by the first bit variable of - * the kernel union, whose address CTF - * groks. */ -} icmp_t; - -/* - * Rts_t internal structure definition - */ - -typedef struct rts_s { - uint_t rts_state; /* Provider interface state */ - -# if defined(HAS_CONN_NEW) - KA_T *rts_connp; /* connection structure pointer */ -# endif /* defined(HAS_CONN_NEW) */ - - union { - uint_t rts_dummy; - uint_t rts_Debug : 1, /* SO_DEBUG option */ - rts_dontroute : 1, /* SO_DONTROUTE option */ - rts_broadcast : 1, /* SO_BROADCAST option */ - rts_reuseaddr : 1, /* SO_REUSEADDR option */ - rts_useloopback : 1, /* SO_USELOOPBACK option */ - icmp_pad : 27; /* padding to bit 31 */ - } rts_debug; /* This name identifies a single bit - * variable, but CTF won't read - * individual bit variables, so for - * CTF's purposes it is declared as a - * uint_t union, named by its first - * bit variable, whose address CTF - * groks. */ -} rts_t; - -/* - * Udp_t internal structure definition - */ - -typedef struct udp { - uint_t udp_state; /* TPI state */ - in_port_t udp_port; /* port bound to this stream */ - in_port_t udp_dstport; /* connected port */ - in6_addr_t udp_v6src; /* source address of this stream */ - in6_addr_t udp_v6dst; /* connected destination */ - ushort_t udp_ipversion; /* version -- IPV[46]_VERSION */ - KA_T *udp_connp; /* connection structure pointer */ - uint_t udp_bits; /* socket option bits */ -} udp_t; - -/* - * CTF definitions for icmp_t, rts_t and udp_t - */ - -static int IRU_ctfs = 0; /* CTF initialization status for - * icmp_t, rts_t and udp_t */ - -# if defined(_LP64) -# define IRU_MOD_FORMAT "/kernel/%s/genunix" -# else /* !defined(_LP64) */ -# define IRU_MOD_FORMAT "/kernel/genunix" -# endif /* defined(_LP64) */ - -/* genunix pathname template to which - * the kernel's instruction type set - * is added for CTF access to icmp_t, - * rts_t and udp_t */ - -/* - * Icmp_t, rts_t and udp_t access definitions and structures - */ - -# define ICMP_T_TYPE_NAME "icmp_t" - -static CTF_member_t icmp_t_members[] = {CTF_MEMBER(icmp_state), -# define MX_icmp_state 0 - -# if defined(HAS_CONN_NEW) - - CTF_MEMBER(icmp_connp), -# define MX_icmp_connp 1 -# else /* !defined(HAS_CONN_NEW) */ - CTF_MEMBER(icmp_bound_v6src), -# define MX_icmp_bound_v6src 1 - - CTF_MEMBER(icmp_v6src), -# define MX_icmp_v6src 2 - - CTF_MEMBER(icmp_debug), -# define MX_icmp_debug 3 -# endif /* defined(HAS_CONN_NEW) */ - - {NULL, 0}}; - -# define RTS_T_TYPE_NAME "rts_t" - -static CTF_member_t rts_t_members[] = {CTF_MEMBER(rts_state), -# define MX_rts_state 0 - -# if defined(HAS_CONN_NEW) - CTF_MEMBER(rts_connp), -# define MX_rts_connp 1 -# else /* !defined(HAS_CONN_NEW) */ - - CTF_MEMBER(rts_debug), -# define MX_rts_debug 1 -# endif /* defined(HAS_CONN_NEW) */ - - {NULL, 0}}; - -# define UDP_T_TYPE_NAME "udp_t" - -static CTF_member_t udp_t_members[] = {CTF_MEMBER(udp_state), -# define MX_udp_state 0 - - CTF_MEMBER(udp_connp), -# define MX_udp_connp 1 - -# if !defined(HAS_CONN_NEW) - CTF_MEMBER(udp_port), -# define MX_udp_port 2 - - CTF_MEMBER(udp_dstport), -# define MX_udp_dstport 3 - - CTF_MEMBER(udp_v6src), -# define MX_udp_v6src 4 - - CTF_MEMBER(udp_v6dst), -# define MX_udp_v6dst 5 - - CTF_MEMBER(udp_ipversion), -# define MX_udp_ipversion 6 - - CTF_MEMBER(udp_bits), -# define MX_udp_bits 7 -# endif /* !defined(HAS_CONN_NEW) */ - - {NULL, 0}}; - -/* - * CTF icmp_t, rts_t and udp_t request table - */ - -static CTF_request_t IRU_requests[] = {{ICMP_T_TYPE_NAME, icmp_t_members}, - {RTS_T_TYPE_NAME, rts_t_members}, - {UDP_T_TYPE_NAME, udp_t_members}, - {NULL, NULL}}; - -/* - * Icmp_t, rts_t and udp_t function prototypes - */ - -static int read_icmp_t(struct lsof_context *ctx, KA_T va, KA_T ph, KA_T ia, - icmp_t *ic); -static int read_rts_t(struct lsof_context *ctx, KA_T va, KA_T ph, KA_T ra, - rts_t *rt); -static int read_udp_t(struct lsof_context *ctx, KA_T ua, udp_t *uc); -#endif /* defined(HAS_LIBCTF) && solaris>=110000 */ - -#if solaris < 80000 || defined(HAS_IPCLASSIFIER_H) -/* - * Make sure the tcpb structure is always defined. - */ - -typedef struct tcpb { - int dummy; -} tcpb_t; -#endif /* solaris<80000 || defined(HAS_IPCLASSIFIER_H) */ - -#if defined(HASIPv6) - -/* - * IPv6_2_IPv4() -- macro to define the address of an IPv4 address contained - * in an IPv6 address - */ - -# define IPv6_2_IPv4(v6) (((uint8_t *)((struct in6_addr *)v6)->s6_addr) + 12) - -/* - * IPv_ADDR_UNSPEC() -- macro to test an IP[46] address for an unspecified - * address value - */ - -# define IPv_ADDR_UNSPEC(af, p) \ - (((af) == AF_INET6) ? (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)p)) \ - : (((struct in_addr *)(p))->s_addr == INADDR_ANY)) -#else /* !defined(HASIPv6) */ - -/* - * IPv_ADDR_UNSPEC() -- IPv4-only form of macro to test for an unspecified - * address value - */ - -# define IPv_ADDR_UNSPEC(af, p) \ - (((struct in_addr *)(p))->s_addr == INADDR_ANY) - -#endif /* !defined(HASIPv6) */ - -#if defined(HASTCPOPT) -# if solaris == 20600 -# include -# endif /* solaris==20600 */ -# include -# include - -# if defined(TH_TIMER_NEEDED) -# define ACK_TIMER TH_TIMER_NEEDED -# else -# if defined(TH_ACK_TIMER_NEEDED) -# define ACK_TIMER TH_ACK_TIMER_NEEDED -# endif /* defined(TH_ACK_TIMER_NEEDED) */ -# endif /* defined(TH_TIMER_NEEDED */ -#endif /* defined(HASTCPOPT) */ - -#if defined(HASSOOPT) -# if solaris < 100000 -# define KEEPALIVE_INTERVAL tcp_keepalive_intrvl -# else /* solaris>=100000 */ -# define KEEPALIVE_INTERVAL tcp_ka_last_intrvl -# endif /* solaris<100000 */ -#endif /* defined(HASSOOPT) */ - -/* - * Local function prototypes - */ - -static void save_TCP_size(struct lsof_context *ctx, tcp_t *tc); -static void save_TCP_states(struct lsof_context *ctx, tcp_t *tc, caddr_t *fa, tcpb_t *tb, caddr_t *xp); - -/* - * build_IPstates() -- build the TCP and UDP state tables - */ - -void build_IPstates(struct lsof_context *ctx) { - if (!TcpSt) { - (void)enter_IPstate(ctx, "TCP", "CLOSED", TCPS_CLOSED); - (void)enter_IPstate(ctx, "TCP", "IDLE", TCPS_IDLE); - (void)enter_IPstate(ctx, "TCP", "BOUND", TCPS_BOUND); - (void)enter_IPstate(ctx, "TCP", "LISTEN", TCPS_LISTEN); - (void)enter_IPstate(ctx, "TCP", "SYN_SENT", TCPS_SYN_SENT); - (void)enter_IPstate(ctx, "TCP", "SYN_RCVD", TCPS_SYN_RCVD); - (void)enter_IPstate(ctx, "TCP", "ESTABLISHED", TCPS_ESTABLISHED); - (void)enter_IPstate(ctx, "TCP", "CLOSE_WAIT", TCPS_CLOSE_WAIT); - (void)enter_IPstate(ctx, "TCP", "FIN_WAIT_1", TCPS_FIN_WAIT_1); - (void)enter_IPstate(ctx, "TCP", "CLOSING", TCPS_CLOSING); - (void)enter_IPstate(ctx, "TCP", "LAST_ACK", TCPS_LAST_ACK); - (void)enter_IPstate(ctx, "TCP", "FIN_WAIT_2", TCPS_FIN_WAIT_2); - (void)enter_IPstate(ctx, "TCP", "TIME_WAIT", TCPS_TIME_WAIT); - (void)enter_IPstate(ctx, "TCP", (char *)NULL, 0); - } - if (!UdpSt) { - (void)enter_IPstate(ctx, "UDP", "Unbound", TS_UNBND); - (void)enter_IPstate(ctx, "UDP", "Wait_BIND_REQ_Ack", TS_WACK_BREQ); - (void)enter_IPstate(ctx, "UDP", "Wait_UNBIND_REQ_Ack", TS_WACK_UREQ); - (void)enter_IPstate(ctx, "UDP", "Idle", TS_IDLE); - (void)enter_IPstate(ctx, "UDP", "Wait_OPT_REQ_Ack", TS_WACK_OPTREQ); - (void)enter_IPstate(ctx, "UDP", "Wait_CONN_REQ_Ack", TS_WACK_CREQ); - (void)enter_IPstate(ctx, "UDP", "Wait_CONN_REQ_Confirm", TS_WCON_CREQ); - (void)enter_IPstate(ctx, "UDP", "Wait_CONN_IND_Response", TS_WRES_CIND); - (void)enter_IPstate(ctx, "UDP", "Wait_CONN_RES_Ack", TS_WACK_CRES); - (void)enter_IPstate(ctx, "UDP", "Wait_Data_Xfr", TS_DATA_XFER); - (void)enter_IPstate(ctx, "UDP", "Wait_Read_Release", TS_WIND_ORDREL); - (void)enter_IPstate(ctx, "UDP", "Wait_Write_Release", TS_WREQ_ORDREL); - (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ6); - (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ7); - (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ9); - (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ10); - (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ11); - (void)enter_IPstate(ctx, "UDP", (char *)NULL, 0); - } -} - -#if solaris >= 110000 -/* - * procss_VSOCK() -- process a VSOCK socket - */ - -# if defined(HAS_CONN_NEW) -/* - * Adjust for changes in the conn_s structure, introduced at OpenSolaris - * level b134. - */ - -# define conn_ulp conn_proto -# define conn_rem V4_PART_OF_V6(connua_v6addr.connua_faddr) -# define conn_src V4_PART_OF_V6(connua_v6addr.connua_laddr) -# endif /* defined(HAS_CONN_NEW) */ - -int process_VSOCK(struct lsof_context *ctx, /* context */ - KA_T va, /* containing vnode address */ - struct vnode *v, /* pointer to containing vnode */ - struct sonode *so) /* pointer to socket's sonode */ -{ - int af; /* address family */ - struct conn_s cs; /* connection info */ - unsigned char *fa = (unsigned char *)NULL; - /* foreign address */ - u_short fp = (u_short)0; /* foreign port */ - u_short lp; /* local port */ - icmp_t ic; /* ICMP control structure */ - KA_T ka; /* temporary kernel address */ - unsigned char *la = (unsigned char *)NULL; - /* local address */ - KA_T pha; /* protocol handle address */ - rts_t rt; /* AF_ROUTE control structure */ - int s; /* state */ - unsigned char *ta = (unsigned char *)NULL; - /* temporary address */ - char tbuf[32], tbuf1[32]; /* temporary buffers */ - tcp_t tc; /* TCP control structure */ - tcph_t th; /* TCP header structure */ - -# if defined(HAS_CONN_NEW) - struct ip_xmit_attr_s xa; - caddr_t *xp = (caddr_t *)NULL; -# else /* !defined(HAS_CONN_NEW) */ - tcph_t *tha = (tcph_t *)NULL; /* TCP header structure address */ -# endif /* defined(HAS_CONN_NEW) */ - - char *ty; /* TCP type */ - udp_t uc; /* local UDP control structure */ - /* - * Read VSOCK's connection information. Enter its address as the protocol - * control block device address. - */ - if (!(pha = (KA_T)so->so_proto_handle)) - return (0); - if (kread(ctx, pha, (char *)&cs, sizeof(cs))) { - (void)snpf(Namech, Namechl, - "vnode at %s; snode at %s; can't read proto handle at: %s", - print_kptr(va, tbuf, sizeof(tbuf)), - print_kptr((KA_T)v->v_data, tbuf1, sizeof(tbuf1)), - print_kptr(pha, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - enter_dev_ch(ctx, print_kptr(pha, (char *)NULL, 0)); - /* - * Process connection info by protocol. - */ - switch ((af = so->so_family)) { - case AF_INET: - case AF_INET6: - - /* - * Set INET type -- IPv4 or IPv6. - */ - if (af == AF_INET) - Lf->type = LSOF_FILE_IPV4; - else - Lf->type = LSOF_FILE_IPV6; - - switch (cs.conn_ulp) { - case IPPROTO_TCP: - - /* - * Process TCP socket; read its control structure. - */ - if (!(ka = (KA_T)cs.conn_proto_priv.cp_tcp) || - kread(ctx, ka, (char *)&tc, sizeof(tc))) { - (void)snpf(Namech, Namechl - 1, - "can't read TCP socket's control structure: %s", - print_kptr((KA_T)ka, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - /* - * Set TCP protocol name in Lf->iproto[]. - */ - (void)snpf(Lf->iproto, IPROTOL - 1, "%s", "TCP"); - Lf->iproto[IPROTOL - 1] = '\0'; - Lf->inp_ty = 2; - /* - * Check for TCP state inclusion or exclusion. - */ - if (TcpNstates) { - if ((s = (int)tc.tcp_state + TcpStOff) < TcpNstates) { - if (TcpStXn) { - if (TcpStX[s]) { - Lf->sf |= SELEXCLF; - return (1); - } - } - if (TcpStIn) { - if (TcpStI[s]) { - TcpStI[s] = 2; - Lf->sf |= SELNET; - } else { - Lf->sf |= SELEXCLF; - return (1); - } - } - } - } - /* - * Set network file selection status. - */ - if (Fnet) { - if (!FnetTy || ((FnetTy == 4) && (af == AF_INET)) || - ((FnetTy == 6) && (af == AF_INET6))) { - Lf->sf |= SELNET; - } - } - /* - * Save local and remote (foreign) TCP address. - */ - if (af == AF_INET6) { - ta = (unsigned char *)&cs.connua_v6addr.connua_faddr; - la = (unsigned char *)&cs.connua_v6addr.connua_laddr; - } else { - ta = (unsigned char *)&cs.conn_rem; - la = (unsigned char *)&cs.conn_src; - } - if (!IPv_ADDR_UNSPEC(af, ta) || (u_short)cs.conn_fport) { - fa = ta; - fp = (u_short)cs.conn_fport; - } - if ((af == AF_INET6) && - ((la && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)la)) || - ((fa && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)fa))))) { - - /* - * Convert IPv4 addresses in IPv6 structures to IPv4 addresses - * in IPv4 structures. Change the address family to AF_INET. - */ - if (la) - la = (unsigned char *)IPv6_2_IPv4(la); - if (fa) - fa = (unsigned char *)IPv6_2_IPv4(fa); - af = AF_INET; - } - lp = (u_short)cs.conn_lport; - (void)ent_inaddr(ctx, la, (int)ntohs(lp), fa, (int)ntohs(fp), af); - /* - * Save TCP state information. - */ - -# if defined(HAS_CONN_NEW) - if ((ka = (KA_T)cs.conn_ixa) && - !kread(ctx, ka, (char *)&xa, sizeof(xa))) { - xp = (caddr_t *)&xa; - } - (void)save_TCP_states(ctx, &tc, (caddr_t *)&cs, (tcpb_t *)NULL, xp); -# else /* !defined(HAS_CONN_NEW) */ - if (tc.tcp_tcp_hdr_len && (ka = (KA_T)tc.tcp_tcph) && - !kread(ctx, ka, (char *)&th, sizeof(th))) { - tha = &th; - } - (void)save_TCP_states(ctx, &tc, (caddr_t *)tha, (tcpb_t *)NULL, - (caddr_t *)NULL); -# endif /* defined(HAS_CONN_NEW) */ - - Lf->lts.type = 0; - Lf->lts.state.i = (int)tc.tcp_state; - /* - * Save TCP size information. - */ - (void)save_TCP_size(ctx, &tc); - break; - case IPPROTO_UDP: - - /* - * Process UDP socket; read its control structure. - */ - if (!(ka = (KA_T)cs.conn_proto_priv.cp_udp) || - kread(ctx, ka, (char *)&uc, sizeof(uc))) { - (void)snpf(Namech, Namechl - 1, - "can't read UDP socket's control structure: %s", - print_kptr((KA_T)ka, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - /* - * Set UDP protocol name in Lf->iproto[]. - */ - (void)snpf(Lf->iproto, IPROTOL - 1, "%s", "UDP"); - Lf->iproto[IPROTOL - 1] = '\0'; - Lf->inp_ty = 2; - /* - * Check for UDP state inclusion or exclusion. - */ - if (UdpNstates) { - if ((s = (int)uc.udp_state + TcpStOff) < UdpNstates) { - if (UdpStXn) { - if (UdpStX[s]) { - Lf->sf |= SELEXCLF; - return (1); - } - } - if (UdpStIn) { - if (UdpStI[s]) { - UdpStI[s] = 2; - Lf->sf |= SELNET; - } else { - Lf->sf |= SELEXCLF; - return (1); - } - } - } - } - /* - * Set network file selection status. - */ - if (Fnet) { - if (!FnetTy || ((FnetTy == 4) && (af == AF_INET)) || - ((FnetTy == 6) && (af == AF_INET6))) { - Lf->sf |= SELNET; - } - } - /* - * Save local and remote (foreign) UDP address. - */ - if (af == AF_INET6) { - ta = (unsigned char *)&cs.connua_v6addr.connua_faddr; - la = (unsigned char *)&cs.connua_v6addr.connua_laddr; - } else { - ta = (unsigned char *)&cs.conn_rem; - la = (unsigned char *)&cs.conn_src; - } - if (!IPv_ADDR_UNSPEC(af, ta) || (u_short)cs.conn_fport) { - fa = ta; - fp = (u_short)cs.conn_fport; - } - lp = (u_short)cs.conn_lport; - (void)ent_inaddr(ctx, la, (int)ntohs(lp), fa, (int)ntohs(fp), af); - /* - * Save UDP state and size information. - */ - Lf->lts.type = 1; - Lf->lts.state.ui = (unsigned int)uc.udp_state; - -# if defined(HASSOOPT) - /* - * Save UDP flags. - */ - union { - uint_t flags; - uint_t udpb_debug : 1, /* SO_DEBUG option */ - udpb_dontroute : 1, /* SO_DONTROUTE option */ - udpb_broadcast : 1, /* SO_BROADCAST option */ - udpb_reuseaddr : 1, /* SO_REUSEADDR option */ - udpb_useloopback : 1, /* SO_USELOOPBACK option */ - udpb_dgram_errind : 1, /* SO_DGRAM_ERRIND option */ - udpb_pad : 26; /* pad to bit 31 */ - } ucf; - - ucf.flags = uc.udp_bits; - if (ucf.udpb_debug) - Lf->lts.opt |= SO_DEBUG; - if (ucf.udpb_dontroute) - Lf->lts.opt |= SO_DONTROUTE; - if (ucf.udpb_broadcast) - Lf->lts.opt |= SO_BROADCAST; - if (ucf.udpb_reuseaddr) - Lf->lts.opt |= SO_REUSEADDR; - if (ucf.udpb_useloopback) - Lf->lts.opt |= SO_USELOOPBACK; - if (ucf.udpb_dgram_errind) - Lf->lts.opt |= SO_DGRAM_ERRIND; -# endif /* defined(HASSOOPT) */ - - break; - case IPPROTO_ICMP: - case IPPROTO_ICMPV6: - - /* - * Process ICMP or ICMP6 socket. - * - * Set protocol name. - */ - if (cs.conn_ulp == IPPROTO_ICMP) - ty = "ICMP"; - else - ty = "ICMP6"; - (void)snpf(Lf->iproto, IPROTOL - 1, "%s", ty); - Lf->iproto[IPROTOL - 1] = '\0'; - Lf->inp_ty = 2; - /* - * Read the ICMP control structure. - */ - if (read_icmp_t(ctx, va, pha, (KA_T)cs.conn_proto_priv.cp_icmp, - &ic)) - return (1); - /* - * Save ICMP size and state information. - */ - Lf->lts.type = 1; - Lf->lts.state.ui = (unsigned int)ic.icmp_state; - /* - * Set network file selection status. - */ - if (Fnet) { - if (!FnetTy || ((FnetTy == 4) && (af == AF_INET)) || - ((FnetTy == 6) && (af == AF_INET6))) { - Lf->sf |= SELNET; - } - } - /* - * Save addresses. - */ - ta = (af == AF_INET6) - ? (unsigned char *)&ic.icmp_bound_v6src - : (unsigned char *)&V4_PART_OF_V6(ic.icmp_bound_v6src); - if (!IPv_ADDR_UNSPEC(af, ta)) - la = ta; - ta = (af == AF_INET6) - ? (unsigned char *)&ic.icmp_v6src - : (unsigned char *)&V4_PART_OF_V6(ic.icmp_v6src); - if (!IPv_ADDR_UNSPEC(af, ta)) - fa = ta; - if (la || fa) - (void)ent_inaddr(ctx, la, 0, fa, 0, af); - -# if defined(HASSOOPT) - /* - * Save ICMP flags. - */ - if (ic.icmp_debug.icmp_Debug) - Lf->lts.opt |= SO_DEBUG; - if (ic.icmp_debug.icmp_dontroute) - Lf->lts.opt |= SO_DONTROUTE; - if (ic.icmp_debug.icmp_broadcast) - Lf->lts.opt |= SO_BROADCAST; - if (ic.icmp_debug.icmp_reuseaddr) - Lf->lts.opt |= SO_REUSEADDR; - if (ic.icmp_debug.icmp_useloopback) - Lf->lts.opt |= SO_USELOOPBACK; - if (ic.icmp_debug.icmp_dgram_errind) - Lf->lts.opt |= SO_DGRAM_ERRIND; -# endif /* defined(HASSOOPT) */ - - break; - default: - (void)snpf(Namech, Namechl - 1, - "unsupported conn_s AF_INET%s protocol: %u", - (af == AF_INET6) ? "6" : "", (unsigned int)cs.conn_ulp); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - break; - case AF_ROUTE: - - /* - * Set INET type -- IPv4 or IPv6. - */ - if (af == AF_INET) - Lf->type = LSOF_FILE_IPV4; - else - Lf->type = LSOF_FILE_IPV6; - /* - * Set protocol name. - */ - (void)strncpy(Lf->iproto, "ROUTE", IPROTOL - 1); - Lf->iproto[IPROTOL - 1] = '\0'; - Lf->inp_ty = 2; - - /* - * Read routing control structure. - */ - if (read_rts_t(ctx, va, pha, (KA_T)cs.conn_proto_priv.cp_rts, &rt)) - return (1); - /* - /* - * Save AF_ROUTE size and state information. - */ - Lf->lts.type = 1; - Lf->lts.state.i = (int)rt.rts_state; - /* - * Set network file selection status. - */ - if (Fnet) { - if (!FnetTy || ((FnetTy == 4) && (af == AF_INET)) || - ((FnetTy == 6) && (af == AF_INET6))) { - Lf->sf |= SELNET; - } - } - -# if defined(HASSOOPT) - /* - * Save ROUTE flags. - */ - if (rt.rts_debug.rts_Debug) - Lf->lts.opt |= SO_DEBUG; - if (rt.rts_debug.rts_dontroute) - Lf->lts.opt |= SO_DONTROUTE; - if (rt.rts_debug.rts_broadcast) - Lf->lts.opt |= SO_BROADCAST; - if (rt.rts_debug.rts_reuseaddr) - Lf->lts.opt |= SO_REUSEADDR; - if (rt.rts_debug.rts_useloopback) - Lf->lts.opt |= SO_USELOOPBACK; -# endif /* defined(HASSOOPT) */ - - break; - default: - (void)printiproto(ctx, (int)cs.conn_ulp); - (void)snpf(Namech, Namechl - 1, "unsupported socket family: %u", - so->so_family); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - Lf->inp_ty = 2; - } - return (1); -} -#endif /* solaris>=110000*/ - -/* - * process_socket() - process Solaris socket - */ - -void process_socket(struct lsof_context *ctx, /* context */ - KA_T sa, /* stream's data address in kernel */ - char *ty) /* socket type name */ -{ - int af; - unsigned char *fa = (unsigned char *)NULL; - int fp = 0; - int i, lp; - -#if solaris < 110000 -# if solaris >= 100000 && defined(HAS_IPCLASSIFIER_H) - struct conn_s ic; -# define ipc_v6laddr conn_srcv6 -# define ipc_v6faddr conn_remv6 -# define ipc_fport conn_fport -# define ipc_lport conn_lport -# else /* solaris<100000 || !defined(HAS_IPCLASSIFIER_H) */ - struct ipc_s ic; -# endif /* solaris>=100000 && defined(HAS_IPCLASSIFIER_H) */ -#else /* solaris>=110000 */ - struct conn_s cs; -#endif /* solaris<110000 */ - - int ics = 0; - unsigned char *la = (unsigned char *)NULL; - struct module_info mi; - KA_T ka; - u_short p; - KA_T pcb = (KA_T)NULL; - struct queue q; - struct qinit qi; - KA_T qp; - u_short *s; - struct stdata sd; - unsigned char *ta; - char tbuf[32]; - -#if solaris < 20600 - struct tcp_s { /* should come from kernel source - * file ../uts/common/inet/tcp.c */ - -# if solaris >= 20400 - struct tcp_s *d1[8]; -# endif /* solaris>=20400 */ - -# if defined(P101318) && P101318 >= 32 - struct tcp_s *d1[6]; -# endif /* defined(P101318) && P101318>=32 */ - - int tcp_state; - queue_t *d3[2]; - mblk_t *d4[2]; - u_long d5; - mblk_t *d6; - u_long d7; - u_long tcp_snxt; /* Senders next seq num */ - u_long tcp_suna; /* Sender unacknowledged */ - u_long tcp_swnd; /* Senders window (relative to suna) */ - u_long d8[5]; - int tcp_hdr_len; /* combined TCP/IP header length */ - tcph_t *tcp_tcph; /* pointer to combined header */ - int d9; - unsigned int d10; - int d11; - mblk_t *d12; - long d13; - mblk_t *d14; - u_long d15; - -# if solaris < 20400 && (!defined(P101318) || P101318 < 32) - mblk_t *d16; -# endif /* solaris<20400 && (!defined(P101318) || P101318<32) */ - - unsigned int d17; - u_long tcp_rnxt; /* Seq we expect to recv next */ - u_long tcp_rwnd; /* Current receive window */ - u_long d18; - long d19[2]; - mblk_t *d20[4]; - u_long d21[5]; - long d22[3]; - -# if solaris < 20500 - u_long d23[2]; - u_long tcp_rack; /* Seq # we have acked */ -# else /* solaris>=20500 */ - u_long d23[3]; -# endif /* solaris<20500 */ - -# if solaris < 20400 - u_long d24[28]; -# else /* solaris>=20400 */ -# if solaris < 20500 - u_long d24[67]; -# else /* solaris>=20500 */ -# if solaris < 20501 - u_long d25[6]; -# else /* solaris>=20501 */ - u_long d25[8]; -# endif /* solaris<20501 */ - u_long tcp_rack; /* Seq # we have acked */ -# if solaris < 20501 - u_long d26[29]; -# else /* solaris>=20501 */ - u_long d26[33]; -# endif /* solaris>=20501 */ -# endif /* solaris<20500 */ -# endif /* solaris<20400 */ - - iph_t tcp_iph; - } tc; -#else /* solaris>=20600 */ - struct tcp_s tc; -#endif /* solaris<20600 */ - -#if solaris >= 80000 && !defined(HAS_IPCLASSIFIER_H) - tcpb_t tcb; -#endif /* solaris>=80000 && !defined(HAS_IPCLASSIFIER_H) */ - - tcpb_t *tcbp = (tcpb_t *)NULL; - int tcs = 0; - tcph_t th; - tcph_t *tha = (tcph_t *)NULL; - -#if solaris < 110000 - struct ud_s { /* should come from kernel source - * file ../uts/common/inet/udp.c */ - uint udp_state; /* TPI state */ - unsigned char d1[2]; - unsigned char udp_port[2]; /* port bound to this stream */ - unsigned char udp_src[4]; /* source address of this stream */ - } uc; -#else /* solaris>=110000 */ - udp_t uc; /* UDP control structure */ -#endif /* solaris<110000 */ - int ucs = 0; - -#if defined(HASIPv6) - if (strrchr(ty, '6')) { - Lf->type = LSOF_FILE_IPV6; - af = AF_INET6; - } else { - Lf->type = LSOF_FILE_IPV4; - af = AF_INET; - } -#else /* !defined(HASIPv6) */ - Lf->type = LSOF_FILE_INET; - af = AF_INET; -#endif /* defined(HASIPv6) */ - - /* - * Set network file selection status. - */ - if (Fnet) { - if (!FnetTy || ((FnetTy == 4) && (af == AF_INET)) - -#if defined(HASIPv6) - || ((FnetTy == 6) && (af == AF_INET6)) -#endif /* defined(HASIPv6) */ - - ) { - if (!TcpStIn && !UdpStIn) - Lf->sf |= SELNET; - } - } - Lf->inp_ty = 2; - /* - * Convert type to upper case protocol name. - */ - if (ty) { - for (i = 0; (ty[i] != '\0') && (i < IPROTOL) && (i < 3); i++) { - if (islower((unsigned char)ty[i])) - Lf->iproto[i] = toupper((unsigned char)ty[i]); - else - Lf->iproto[i] = ty[i]; - } - } else - i = 0; - Lf->iproto[i] = '\0'; - /* - * Read stream queue entries to obtain private IP, TCP, and UDP structures. - */ - if (!sa || readstdata(ctx, sa, &sd)) - qp = (KA_T)NULL; - else - qp = (KA_T)sd.sd_wrq; - for (i = 0; qp && i < 20; i++, qp = (KA_T)q.q_next) { - if (kread(ctx, qp, (char *)&q, sizeof(q))) - break; - if ((ka = (KA_T)q.q_qinfo) == (KA_T)NULL || - kread(ctx, ka, (char *)&qi, sizeof(qi))) - continue; - if ((ka = (KA_T)qi.qi_minfo) == (KA_T)NULL || - kread(ctx, ka, (char *)&mi, sizeof(mi)) || - (ka = (KA_T)mi.mi_idname) == (KA_T)NULL) - continue; - if (kread(ctx, ka, (char *)&tbuf, sizeof(tbuf) - 1)) - continue; - if ((pcb = (KA_T)q.q_ptr) == (KA_T)NULL) - continue; - -#if solaris < 110000 - if (strncasecmp(tbuf, "IP", 2) == 0) { - if (kread(ctx, pcb, (char *)&ic, sizeof(ic)) == 0) - ics = 1; - continue; - } -#endif /* solaris<110000 */ - - if (strncasecmp(tbuf, "TCP", 3) == 0) { - -#if solaris <= 90000 || !defined(HAS_IPCLASSIFIER_H) - if (!kread(ctx, (KA_T)pcb, (char *)&tc, sizeof(tc))) - -# if solaris >= 80000 - { - if (tc.tcp_base && - !kread(ctx, (KA_T)tc.tcp_base, (char *)&tcb, sizeof(tcb))) { - tcs = 1; - tcbp = &tcb; - } - tc.tcp_base = &tcb; /* support for macros */ - tcb.tcpb_tcp = &tc; /* support for macros */ - } -# else /* solaris<80000 */ - tcs = 1; -# endif /* solaris>=80000 */ -#else /* solaris>90000 && defined(HAS_IPCLASSIFIER_H) */ -# if solaris >= 110000 - if (!kread(ctx, pcb, (char *)&cs, sizeof(cs)) && - (cs.conn_ulp == IPPROTO_TCP)) { - ics = 1; - if ((ka = (KA_T)cs.conn_proto_priv.cp_tcp) && - !kread(ctx, ka, (char *)&tc, sizeof(tc))) { - tcs = 1; - } - } -# else /* solaris<110000 */ - if (!kread(ctx, (KA_T)pcb, (char *)&ic, sizeof(ic)) && - ic.conn_tcp && - !kread(ctx, (KA_T)ic.conn_tcp, (char *)&tc, sizeof(tc))) { - ics = tcs = 1; - } -# endif /* solaris>=110000 */ -#endif /* solaris<=90000 || !defined(HAS_IPCLASSIFIER_H) */ - - if (tcs && TcpNstates) { - int s = (int)tc.tcp_state + TcpStOff; - /* - * Check for TCP state inclusion or exclusion. - */ - - if (s < TcpNstates) { - if (TcpStXn) { - if (TcpStX[s]) { - Lf->sf &= ~SELNET; - Lf->sf |= SELEXCLF; - return; - } - } - if (TcpStIn) { - if (TcpStI[s]) { - TcpStI[s] = 2; - Lf->sf |= SELNET; - } else { - Lf->sf &= ~SELNET; - Lf->sf |= SELEXCLF; - return; - } - } - } - } - if (!(Lf->sf & SELNET) && !TcpStIn && UdpStIn) { - if (Fnet) { - if (!FnetTy || (FnetTy == 4) && (af == AF_INET) - -#if defined(HASIPv6) - || (FnetTy == 6) && (af == AF_INET6) -#endif /* defined(HASIPv6) */ - - ) { - Lf->sf |= SELNET; - } - } - } - continue; - } - if (strncasecmp(tbuf, "UDP", 3) == 0) { - -#if solaris < 110000 - if (kread(ctx, pcb, (char *)&uc, sizeof(uc)) == 0) - ucs = 1; -#else /* solaris>=110000 */ - if (!kread(ctx, pcb, (char *)&cs, sizeof(cs)) && - (cs.conn_ulp == IPPROTO_UDP)) { - ics = 1; - if ((ka = (KA_T)cs.conn_proto_priv.cp_udp) && - !read_udp_t(ctx, ka, &uc)) { - ucs = 1; - } - } -#endif /* solaris<110000 */ - - if (ucs && UdpNstates) { - unsigned int s = (unsigned int)uc.udp_state + UdpStOff; - /* - * Check for UDP state inclusion or exclusion. - */ - - if (s < UdpNstates) { - if (UdpStXn) { - if (UdpStX[s]) { - Lf->sf &= ~SELNET; - Lf->sf |= SELEXCLF; - return; - } - } - if (UdpStIn) { - if (UdpStI[s]) { - UdpStI[s] = 2; - Lf->sf |= SELNET; - } else { - Lf->sf |= SELEXCLF; - return; - } - } - } - } - if (!(Lf->sf & SELNET) && TcpStIn && !UdpStIn) { - if (Fnet) { - if (!FnetTy || (FnetTy == 4) && (af == AF_INET) - -#if defined(HASIPv6) - || (FnetTy == 6) && (af == AF_INET6) -#endif /* defined(HASIPv6) */ - - ) { - Lf->sf |= SELNET; - } - } - } - continue; - } - } - if (ics) { - - /* - * Print stream head's q_ptr address as protocol control block address. - */ - if (pcb) - enter_dev_ch(ctx, print_kptr(pcb, (char *)NULL, 0)); - if (strncmp(Lf->iproto, "UDP", 3) == 0) { - - /* - * Save UDP address and TPI state. - */ - -#if solaris < 20600 - la = (unsigned char *)&ic.ipc_udp_addr; - p = (u_short)ic.ipc_udp_port; -#else /* solaris>=20600 */ -# if solaris >= 110000 - af = (uc.udp_ipversion == IPV6_VERSION) ? AF_INET6 : AF_INET; - la = (af == AF_INET6) - ? (unsigned char *)&uc.udp_v6src - : (unsigned char *)&V4_PART_OF_V6(uc.udp_v6src); - p = (u_short)uc.udp_port; -# else /* solaris<110000 */ -# if defined(HASIPv6) - la = (af == AF_INET6) - ? (unsigned char *)&ic.ipc_v6laddr - : (unsigned char *)IPv6_2_IPv4(&ic.ipc_v6laddr); -# else /* !defined(HASIPv6 */ - la = (unsigned char *)&ic.ipc_laddr; -# endif /* defined(HASIPv6) */ - - p = (u_short)ic.ipc_lport; -# endif /* solaris>=110000 */ -#endif /* solaris<20600 */ - -#if solaris < 110000 - if (IPv_ADDR_UNSPEC(af, la) && !p && ucs) { - - /* - * If the ipc_s structure has no local address, use - * the port in the ud_s structure. - */ - s = (u_short *)&uc.udp_port[0]; - p = *s; - } - -# if defined(HASIPv6) - if ((af == AF_INET6) && la && - IN6_IS_ADDR_V4MAPPED((struct in6_addr *)la)) { - - /* - * Convert a local IPv4 address in an IPv6 structure to an IPv4 - * address in an IPv4 structure. Change the address family to - * AF_INET. - */ - la = (unsigned char *)IPv6_2_IPv4(la); - af = AF_INET; - } -# endif /* defined(HASIPv6) */ -#endif /* solaris<110000 */ - - (void)ent_inaddr(ctx, la, (int)ntohs(p), (unsigned char *)NULL, -1, - af); - if (ucs) { - Lf->lts.type = 1; - Lf->lts.state.ui = (unsigned int)uc.udp_state; - } - } else if (strncmp(Lf->iproto, "TCP", 3) == 0) { - if (ics) { - - /* - * Save TCP address. - */ - -#if solaris < 20400 - la = (unsigned char *)&ic.ipc_tcp_addr[0]; - p = (u_short)ic.ipc_tcp_addr[5]; -#else /* solaris>=20400 */ -# if solaris < 20600 - la = (unsigned char *)&ic.ipc_tcp_laddr; - p = (u_short)((short *)&ic.ipc_tcp_ports)[1]; -# else /* solaris>=20600 */ -# if solaris >= 110000 - la = (af == AF_INET6) - ? (unsigned char *)&cs.connua_v6addr.connua_laddr - : (unsigned char *)&cs.conn_src; - lp = cs.conn_lport; -# else /* solaris<110000 */ -# if defined(HASIPv6) - la = (af == AF_INET6) - ? (unsigned char *)&ic.ipc_v6laddr - : (unsigned char *)IPv6_2_IPv4(&ic.ipc_v6laddr); -# else /* !defined(HASIPv6 */ - la = (unsigned char *)&ic.ipc_laddr; -# endif /* defined(HASIPv6) */ - - p = (u_short)ic.ipc_lport; -# endif /* solaris>=110000 */ -# endif /* solaris<20600 */ -#endif /* solaris<20400 */ - -#if solaris < 110000 - if (IPv_ADDR_UNSPEC(af, la) && !p && tcs) { - - /* - * If the ipc_s structure has no local address, use the - * local address in the stream's tcp_iph structure (except - * for Solaris 2.4), and the port number in the stream's - * tcph structure. - */ - -# if solaris != 20400 && solaris < 80000 - la = (unsigned char *)&tc.tcp_iph.iph_src[0]; -# else /* solaris==20400 || solaris<80000 */ -# if solaris >= 100000 && defined(HAS_IPCLASSIFIER_H) - la = (af == AF_INET6) - ? (unsigned char *)&ic.conn_srcv6 - : (unsigned char *)IPv6_2_IPv4(&ic.conn_srcv6); -# else /* solaris<100000 || !defined(HAS_IPCLASSIFIER_H) */ -# if solaris >= 80000 -# if defined(HASIPv6) - la = - (af == AF_INET6) - ? (unsigned char *)&tcb.tcpb_ip_src_v6 - : (unsigned char *)IPv6_2_IPv4(&tcb.tcpb_ip_src_v6); -# else /* !defined(HASIPv6) */ - la = (unsigned char *)&tcb.tcpb_ip_src; -# endif /* defined(HASIPv6) */ -# endif /* solaris>=80000 */ -# endif /* solaris>=100000 && defined(HAS_IPCLASSIFIER_H) */ -# endif /* solaris!=20400 && !defined(HASIPv6) */ - - if (tc.tcp_hdr_len && tc.tcp_tcph && - !kread(ctx, (KA_T)tc.tcp_tcph, (char *)&th, - sizeof(th))) { - tha = &th; - s = (u_short *)&th.th_lport[0]; - p = *s; - } - } -#endif /* solaris<110000 */ - - lp = (int)ntohs(p); - -#if solaris < 20400 - if ((int)ic.ipc_tcp_addr[2] != INADDR_ANY || - ic.ipc_tcp_addr[4] != 0) { - fa = (unsigned char *)&ic.ipc_tcp_addr[2]; - fp = (int)ntohs(ic.ipc_tcp_addr[4]); - } -#else /* solaris>=20400 */ -# if solaris < 20600 - if ((int)ic.ipc_tcp_faddr != INADDR_ANY || - ((u_short *)&ic.ipc_tcp_ports)[0] != 0) { - fa = (unsigned char *)&ic.ipc_tcp_faddr; - fp = (int)ntohs(((u_short *)&ic.ipc_tcp_ports)[0]); - } -# else /* solaris>=20600 */ - -# if solaris >= 110000 - ta = (af == AF_INET6) - ? (unsigned char *)&cs.connua_v6addr.connua_faddr - : (unsigned char *)&cs.conn_rem; - if (!IPv_ADDR_UNSPEC(af, ta) || ((u_short)cs.conn_fport)) { - fa = ta; - fp = (u_short)cs.conn_fport; - } -# else /* solaris<110000 */ -# if defined(HASIPv6) - ta = (af == AF_INET6) - ? (unsigned char *)&ic.ipc_v6faddr - : (unsigned char *)IPv6_2_IPv4(&ic.ipc_v6faddr); -# else /* !defined(HASIPv6) */ - ta = (unsigned char *)&ic.ipc_faddr; -# endif /* defined(HASIPv6) */ - - if (!IPv_ADDR_UNSPEC(af, ta) || ((u_short)ic.ipc_fport)) { - fa = ta; - fp = (int)ntohs(((u_short)ic.ipc_fport)); - } -# endif /* solaris>=110000 */ -# endif /* solaris<20600 */ -#endif /* solaris <20400 */ - -#if defined(HASIPv6) - if ((af == AF_INET6) && - ((la && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)la)) || - ((fa && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)fa))))) { - - /* - * Convert IPv4 addresses in IPv6 structures to IPv4 - * addresses in IPv4 structures. Change the address - * family to AF_INET. - */ - if (la) - la = (unsigned char *)IPv6_2_IPv4(la); - if (fa) - fa = (unsigned char *)IPv6_2_IPv4(fa); - af = AF_INET; - } -#endif /* defined(HASIPv6) */ - - if (fa || la) - (void)ent_inaddr(ctx, la, lp, fa, fp, af); - } - /* - * Save TCP state information. - */ - if (tcs) { - (void)save_TCP_states(ctx, &tc, (caddr_t *)tha, tcbp, - (caddr_t *)NULL); - Lf->lts.type = 0; - Lf->lts.state.i = (int)tc.tcp_state; - } - /* - * Save TCP size information. - */ - - if (tcs) - (void)save_TCP_size(ctx, &tc); - } - } else - (void)strcat(Namech, "no TCP/UDP/IP information available"); - /* - * Enter name characters if there are some. - */ - if (Namech[0]) - enter_nm(ctx, Namech); -} - -#if solaris >= 110000 -/* - * read_icmp_t() - read connections icmp_t info - */ - -static int read_icmp_t(struct lsof_context *ctx, /* context */ - KA_T va, /* containing vnode kernel address */ - KA_T ph, /* containing protocol handle kernel - * address */ - KA_T ia, /* icmp_t structure's kernel address */ - icmp_t *ic) /* local icmp_t receiver */ -{ - char tbuf[32], tbuf1[32]; /* print_kptr() temporary buffers */ - -# if defined(HAS_CONN_NEW) - struct conn_s cs; /* connection structure */ - KA_T ka; /* kernel address */ - - zeromem((char *)ic, sizeof(icmp_t)); -# endif /* defined(HAS_CONN_NEW) */ - - (void)CTF_init(ctx, &IRU_ctfs, IRU_MOD_FORMAT, IRU_requests); - if (!ia || CTF_MEMBER_READ(ia, ic, icmp_t_members, icmp_state) - -# if defined(HAS_CONN_NEW) - || CTF_MEMBER_READ(ia, ic, icmp_t_members, icmp_connp) -# else /* !defined(HAS_CONN_NEW) */ - || CTF_MEMBER_READ(ia, ic, icmp_t_members, icmp_bound_v6src) || - CTF_MEMBER_READ(ia, ic, icmp_t_members, icmp_v6src) || - CTF_MEMBER_READ(ia, ic, icmp_t_members, icmp_debug) -# endif /* defined(HAS_CONN_NEW) */ - - ) { - (void)snpf(Namech, Namechl - 1, - "vnode at %s; proto handle at %s; can't read icmp_t at %s", - print_kptr(va, tbuf, sizeof(tbuf)), - print_kptr(ph, tbuf1, sizeof(tbuf1)), - print_kptr(ia, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - -# if defined(HAS_CONN_NEW) - if ((ka = (KA_T)ic->icmp_connp) && - !kread(ctx, ka, (char *)&cs, sizeof(cs))) { - struct ip_xmit_attr_s xa; - - /* - * Complete the icmp_t structure from the conn_s structure. - */ - ic->icmp_bound_v6src = cs.conn_bound_addr_v6; - ic->icmp_v6src = cs.conn_saddr_v6; - ic->icmp_debug.icmp_Debug = cs.conn_debug; - ic->icmp_debug.icmp_broadcast = cs.conn_broadcast; - ic->icmp_debug.icmp_reuseaddr = cs.conn_reuseaddr; - ic->icmp_debug.icmp_useloopback = cs.conn_useloopback; - ic->icmp_debug.icmp_dgram_errind = cs.conn_dgram_errind; - if ((ka = (KA_T)cs.conn_ixa) && - !kread(ctx, ka, (char *)&xa, sizeof(xa))) { - ic->icmp_debug.icmp_dontroute = - (xa.ixa_flags & IXAF_DONTROUTE) ? 1 : 0; - } - } -# endif /* defined(HAS_CONN_NEW) */ - - return (0); -} - -/* - * read_rts_t() - read connections rts_t info - */ - -static int read_rts_t(struct lsof_context *ctx, /* context */ - KA_T va, /* containing vnode kernel address */ - KA_T ph, /* containing protocol handle kernel - * address */ - KA_T ra, /* rts_t structure's kernel address */ - rts_t *rt) /* local rts_t receiver */ -{ - char tbuf[32], tbuf1[32]; /* print_kptr() temporary buffers */ - -# if defined(HAS_CONN_NEW) - struct conn_s cs; /* connextion structure */ - KA_T ka; /* kernel address */ - - zeromem((char *)rt, sizeof(rts_t)); -# endif /* defined(HAS_CONN_NEW) */ - - (void)CTF_init(ctx, &IRU_ctfs, IRU_MOD_FORMAT, IRU_requests); - if (!ra || CTF_MEMBER_READ(ra, rt, rts_t_members, rts_state) - -# if defined(HAS_CONN_NEW) - || CTF_MEMBER_READ(ra, rt, rts_t_members, rts_connp) -# else /* !defined(HAS_CONN_NEW) */ - || CTF_MEMBER_READ(ra, rt, rts_t_members, rts_debug) -# endif /* defined(HAS_CONN_NEW) */ - - ) { - (void)snpf(Namech, Namechl - 1, - "vnode at %s; proto handle at %s; can't read rts_t at %s", - print_kptr(va, tbuf, sizeof(tbuf)), - print_kptr(ph, tbuf1, sizeof(tbuf1)), - print_kptr(ra, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - -# if defined(HAS_CONN_NEW) - if ((ka = (KA_T)rt->rts_connp) && - !kread(ctx, ka, (char *)&cs, sizeof(struct conn_s))) { - struct ip_xmit_attr_s xa; - - /* - * Fill in rts_debug from the connection structure. - */ - rt->rts_debug.rts_Debug = cs.conn_debug; - rt->rts_debug.rts_broadcast = cs.conn_broadcast; - rt->rts_debug.rts_reuseaddr = cs.conn_reuseaddr; - rt->rts_debug.rts_useloopback = cs.conn_useloopback; - if ((ka = (KA_T)cs.conn_ixa) && - !kread(ctx, ka, (char *)&xa, sizeof(xa))) { - rt->rts_debug.rts_dontroute = - (xa.ixa_flags & IXAF_DONTROUTE) ? 1 : 0; - } - } - -# endif /* defined(HAS_CONN_NEW) */ - - return (0); -} - -/* - * read_udp_t() - read UDP control structure - */ - -static int read_udp_t(struct lsof_context *ctx, /* context */ - KA_T ua, /* ucp_t kernel address */ - udp_t *uc) /* receiving udp_t structure */ -{ - (void)CTF_init(ctx, &IRU_ctfs, IRU_MOD_FORMAT, IRU_requests); - if (!ua || CTF_MEMBER_READ(ua, uc, udp_t_members, udp_state) - -# if defined(HAS_CONN_NEW) - || CTF_MEMBER_READ(ua, uc, udp_t_members, udp_connp) -# else /* !defined(HAS_CONN_NEW) */ - || CTF_MEMBER_READ(ua, uc, udp_t_members, udp_port) || - CTF_MEMBER_READ(ua, uc, udp_t_members, udp_dstport) || - CTF_MEMBER_READ(ua, uc, udp_t_members, udp_v6src) || - CTF_MEMBER_READ(ua, uc, udp_t_members, udp_v6dst) || - CTF_MEMBER_READ(ua, uc, udp_t_members, udp_ipversion) || - CTF_MEMBER_READ(ua, uc, udp_t_members, udp_bits) -# endif /* defined(HAS_CONN_NEW) */ - - ) { - (void)snpf(Namech, Namechl, "can't read udp_t: %s", - print_kptr(ua, (char *)NULL, 0)); - Namech[Namechl - 1] = '\0'; - enter_nm(ctx, Namech); - return (1); - } - return (0); -} -#endif /* solaris>=110000 */ - -/* - * save_TCP_size() -- save TCP size information - */ - -static void save_TCP_size(struct lsof_context *ctx, tcp_t *tc) /* pointer to TCP control structure */ -{ - int rq, sq; - -#if defined(HASTCPTPIQ) || defined(HASTCPTPIW) -# if defined(HASTCPTPIW) - Lf->lts.rw = (int)tc->tcp_rwnd; - Lf->lts.ww = (int)tc->tcp_swnd; - Lf->lts.rws = Lf->lts.wws = 1; -# endif /* defined(HASTCPTPIW) */ - - if ((rq = (int)tc->tcp_rnxt - (int)tc->tcp_rack) < 0) - rq = 0; - if ((sq = (int)tc->tcp_snxt - (int)tc->tcp_suna - 1) < 0) - sq = 0; - -# if defined(HASTCPTPIQ) - Lf->lts.rq = (unsigned long)rq; - Lf->lts.sq = (unsigned long)sq; - Lf->lts.rqs = Lf->lts.sqs = 1; -# endif /* defined(HASTCPTPIQ) */ - - if (Lf->access == LSOF_FILE_ACCESS_READ) - Lf->sz = (SZOFFTYPE)rq; - else if (Lf->access == LSOF_FILE_ACCESS_WRITE) - Lf->sz = (SZOFFTYPE)sq; - else - Lf->sz = (SZOFFTYPE)(rq + sq); - Lf->sz_def = 1; -#endif /* defined(HASTCPTPIQ) || defined(HASTCPTPIW) */ -} - -/* - * save_TCP_states() - save TCP states - */ - -static void save_TCP_states(struct lsof_context *ctx, /* context */ - tcp_t *tc, /* pointer to TCP control structure */ - caddr_t *fa, /* flags address (may be NULL): - * if HAS_CONN_NEW: conn_s * - * if !CONN_HAS_NEW: tcph_t * - */ - tcpb_t *tb, /* pointer to TCP base structure (may - * be NULL) */ - caddr_t *xp) /* pointer to struct ip_xmit_attr_s if - * HAS_CONN_NEW (may be NULL) */ -{ - if (!tc) - return; - -#if defined(HASSOOPT) -# if defined(HAS_CONN_NEW) - if (fa) { - struct conn_s *cs = (struct conn_s *)fa; - - if (cs->conn_broadcast) - Lf->lts.opt |= SO_BROADCAST; - if (cs->conn_debug) - Lf->lts.opt |= SO_DEBUG; - if (cs->conn_dgram_errind) - Lf->lts.opt |= SO_DGRAM_ERRIND; - if (xp && (((ip_xmit_attr_t *)xp)->ixa_flags & IXAF_DONTROUTE)) - Lf->lts.opt |= SO_DONTROUTE; - if (cs->conn_keepalive) { - Lf->lts.opt |= SO_KEEPALIVE; - Lf->lts.kai = (unsigned int)tc->tcp_ka_interval; - } - if (cs->conn_linger) { - Lf->lts.opt |= SO_LINGER; - Lf->lts.ltm = (unsigned int)cs->conn_lingertime; - } - if (cs->conn_oobinline) - Lf->lts.opt |= SO_OOBINLINE; - Lf->lts.pqlen = (unsigned int)tc->tcp_conn_req_cnt_q0; - Lf->lts.qlen = (unsigned int)tc->tcp_conn_req_cnt_q; - Lf->lts.qlim = (unsigned int)tc->tcp_conn_req_max; - Lf->lts.pqlens = Lf->lts.qlens = Lf->lts.qlims = (unsigned char)1; - if (cs->conn_reuseaddr) - Lf->lts.opt |= SO_REUSEADDR; - if (cs->conn_useloopback) - Lf->lts.opt |= SO_USELOOPBACK; -# else /* !defined(HAS_CONN_NEW) */ - if (1) { - if (tc->tcp_broadcast) - Lf->lts.opt |= SO_BROADCAST; - if (tc->tcp_debug) - Lf->lts.opt |= SO_DEBUG; - if (tc->tcp_dgram_errind) - Lf->lts.opt |= SO_DGRAM_ERRIND; - if (tc->tcp_dontroute) - Lf->lts.opt |= SO_DONTROUTE; - if (tc->KEEPALIVE_INTERVAL) { - Lf->lts.opt |= SO_KEEPALIVE; - Lf->lts.kai = (unsigned int)tc->KEEPALIVE_INTERVAL; - } - if (tc->tcp_linger) { - Lf->lts.opt |= SO_LINGER; - Lf->lts.ltm = (unsigned int)tc->tcp_lingertime; - } - if (tc->tcp_oobinline) - Lf->lts.opt |= SO_OOBINLINE; - Lf->lts.pqlen = (unsigned int)tc->tcp_conn_req_cnt_q0; - Lf->lts.qlen = (unsigned int)tc->tcp_conn_req_cnt_q; - Lf->lts.qlim = (unsigned int)tc->tcp_conn_req_max; - Lf->lts.pqlens = Lf->lts.qlens = Lf->lts.qlims = (unsigned char)1; - -# if solaris >= 80000 -# if defined(HAS_IPCLASSIFIER_H) - if (tc->tcp_reuseaddr) -# else /* !defined(HAS_IPCLASSIFIER_H) */ - if (tb && tb->tcpb_reuseaddr) -# endif /* !defined(HAS_IPCLASSIFIER_H) */ - - Lf->lts.opt |= SO_REUSEADDR; -# endif /* solaris>=80000 */ - - if (tc->tcp_useloopback) - Lf->lts.opt |= SO_USELOOPBACK; -# endif /* defined(HAS_CONN_NEW) */ -#endif /* defined(HASSOOPT) */ - -#if defined(HASTCPOPT) -# if defined(ACK_TIMER) -# if !defined(HAS_CONN_NEW) - if (fa && (((tcph_t *)fa)->th_flags[0] & ACK_TIMER)) - Lf->lts.topt |= TF_DELACK; -# endif /* !defined(HAS_CONN_NEW) */ -# endif /* defined(ACK_TIMER) */ - -# if solaris < 80000 || defined(HAS_IPCLASSIFIER_H) - Lf->lts.mss = (unsigned long)tc->tcp_mss; -# else /* solaris>=80000 && !defined(HAS_IPCLASSIFIER_H) */ - if (tb) - Lf->lts.mss = (unsigned long)tb->tcpb_mss; -# endif /* solaris<80000 || defined(HAS_IPCLASSIFIER_H) */ - - Lf->lts.msss = (unsigned char)1; - if (tc->tcp_naglim == 1L) - Lf->lts.topt |= TF_NODELAY; - if (tc->tcp_fin_sent) - Lf->lts.topt |= TF_SENTFIN; - } -#endif /* defined(HASTCPOPT) */ -} diff --git a/lib/dialects/sun/dstore.c b/lib/dialects/sun/dstore.c index 9e600ca6..2701805d 100644 --- a/lib/dialects/sun/dstore.c +++ b/lib/dialects/sun/dstore.c @@ -184,11 +184,9 @@ struct drive_Nl Drive_Nl[] = {{"afsops", "afs_ops"}, char **Fsinfo = NULL; /* file system information */ int Fsinfomax = 0; /* maximum file system type */ -int HasALLKMEM = 0; /* has ALLKMEM device */ int HaveCloneMaj = 0; /* clone major device number has * been identified and is in * CloneMaj */ -kvm_t *Kd = NULL; /* kvm descriptor */ struct l_vfs *Lvfs = NULL; /* local vfs structure table */ struct netclone *Netclone = NULL; /* net clone devices from * /devices/pseudo */ diff --git a/lib/dialects/sun/solaris_kaddr_filters b/lib/dialects/sun/solaris_kaddr_filters deleted file mode 100644 index 7fcd692d..00000000 --- a/lib/dialects/sun/solaris_kaddr_filters +++ /dev/null @@ -1,239 +0,0 @@ - Solaris Kernel Address Filtering in lsof 4.50 and Above - -Current Filter -============== - -Lsof revisions 4.49 and below, have exactly one filter: the kernel -virtual address is checked against the kernel's virtual address -base -- e.g., what's found in the kernel variable kernelbase. For -sun4m that's 0xf0000000, for sun4u, 0x10000000. - -This filter keeps lsof from handing some bad addresses to the -kernel, but not all bad addresses. For example, the virtual address -0x657a682e passes this test on a sun4u machine, but on at least -one sun4u that virtual address translates to the physical address -0x1cf08c30000, which is the address of a register of a qfe interface -on the machine. There is some evidence that a kvm_kread() call for -the 0x657a682e address may crash that sun4u. - -Lsof 4.71 and above use no filter if they detect that /dev/allkmem -exists. That is done because, when /dev/allkmem exists, /dev/kmem has -address filtering in its device driver. - - -====================== -!!!IMPORTANT UPDATE!!! -====================== - -In late May 2002 I learned that Sun had reports of other kernel -crashes, caused by adb, lsof, and mdb, related to incorrect addresses -being supplied to /dev/kmem. (This report was written originally -on July 18, 2000.) - -The problem is described in and fixed or patched: - - Solaris 7: SPARC kernel patch 106541-20 - Intel kernel patch 106542-20 - - Solaris 8: SPARC kernel patch 108528-14 - Intel kernel patch 108529-14 - - Solaris 9: bug 4344513 - -So, if you want to be comfortable using lsof (or adb or mdb) with -Solaris, install the appropriate Solaris 7 or 8 patches, or upgrade -to Solaris 9. - -Note that these patches provide the /dev/allkmem device, whose presence -causes lsof to rely on the address filtering of the /dev/kmem device. - - -New Filters -=========== - -Lsof 4.50 adds additional filters to the kernelbase check. The -filters differ, based on the Solaris version: - - Solaris - Version New Filters - ======= =========== - - 2.5 and below none - 2.5.1 kvm_physaddr() (-lkvm), caching, llseek(), - and /dev/mem - 2.6 kvm_physaddr() (-lkvm), caching, llseek(), - and /dev/mem - 7, 8, and 9 kvm_physaddr() (ioctl()), caching, and - kvm_pread() - - See !!!IMPORTANT NOTICE!! above for - information on a Solaris 9 bug report about, - or Solaris 7 and 8 kernel patches to the - kernel /dev/kmem driver. Those fixes - obviate the need for the kernel address - filtering described in this report. - - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !!! I STRONGLY RECOMMEND YOU INSTALL !!! - !!! THE PATCHES OR UPGRADE TO SOLARIS !!! - !!! 9. !!! - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -kvm_physaddr() (-lkvm) -====================== - -Solaris has an undocumented function called kvm_physaddr() that -will convert a kernel virtual address to a kernel physical address. -(Until Solaris 7 this function doesn't even have a prototype -definition in .) - -I have been assured repeatedly by Casper Dik of Sun that this -function, when given a kernel virtual address, will produce addresses -of physical memory only; it will not produce physical addresses of -interface registers, such as the one for the qfe interface. - -In Solaris 2.5.1 this function runs in application space from within -the KVM library. Since it needs to know the components of the -kernel's address space map, it must read those from kernel memory -each time it is called. That can be time consuming. - -I'm not sure about kvm_physaddr() for Solaris 2.6. It may still -run in application space from within the KVM library, but if so, -it is much faster than its 2.5.1 ancestor. - -kvm_physaddr() (ioctl()) -======================== - -I'm sure that at Solaris 7 and above kvm_physaddr() has moved inside -the kernel and is called with an ioctl(). That makes it much faster -than its ancestors. - -kvm_physaddr() Use -================== - -Lsof 4.50 for Solaris will use one or the other version of -kvm_physaddr() for Solaris 2.5.1, 2.6, 7, and 8. - -Using it for Solaris 2.5.1 causes lsof to take four times as much -real time as it formerly did with only the kernelbase filtering. - -Caching -======= - -To recover the performance lost by kvm_physaddr() on Solaris 2.5.1, -I added virtual-to-physical address caching to lsof's kernel read -function, kread(). This improves Solaris 2.6, 7, and 8 performance, -too, but by a smaller amount. - -It turns out that a typical lsof run may require reading from 16,000 -or more different kernel virtual addresses. However, it also turns -out that those addresses are contained within about 600 distinct -kernel memory pages. - -To exploit this condition lsof caches each virtual page address -that has a corresponding legitimate physical page address for use -in checking later addresses. This caching regains all but a bit -of the performance loss on Solaris 2.5.1. - -Caching can provide some performance gain on Solaris 2.6, 7, and -8, but it's not nearly as large as the gain for 2.5.1, and may -depend on the machine architecture type. - -/dev/mem -======== - -Once lsof has kernel physical addresses, on Solaris 2.5.1 and 2.6 -it seeks to those addresses with llseek() and reads from them via -the /dev/mem device. This contrasts with lsof's pre-4.50 behavior -where it fed kernel virtual addresses to kvm_kread(), letting it -and the kernel do the virtual to physical translations -- and -letting that combined process crash that one unlucky sun4u via its -qfe interface. - -Using /dev/mem requires no more permission for lsof, but it does -require an additional open file descriptor and use of the 64 bit -llseek() function. - -The additional file descriptor is an unfortunate consequence of -the KVM library's opacity. The library usually has /dev/kmem open -to a file descriptor, but lsof can't easily get at that descriptor, -so it opens one of its own. - -On Solaris 2.6 for one test system, a 4 CPU E4000 sun4u, doing -physical kernel address reads from /dev/mem turned out to be faster -than using kvm_kread(). It was marginally faster on a sun4d, and -marginally slower on two sun4m's. - -kvm_pread() -=========== - -Even though it is still undocumented, the kvm_physaddr() function -is represented by a prototype in the Solaris 7 and 8 . -Additionally useful is another undocumented function, kvm_pread() -(for physical read), that also is represented by a prototype -in Solaris 7 and 8. - -Lsof 4.50 for Solaris 7 and 8 uses kvm_pread() instead of opening -a descriptor to /dev/mem, llseek()-ing to physical addresses in -it, and using read(2) to obtain physical address contents. The -bonus of kvm_pread() is two-fold: 1) it does positioning as well -as reading, so there's one less function call; and 2) its combined -operation appears to be faster than llseek() plus read() -- or even -kvm_kread(). - -Combined with the virtual-to-physical address caching, the performance -boost of kvm_pread() makes lsof faster on Solaris 7 and 8 than -previous revisions, using only kernelbase filtering and kvm_kread(). - -Remaining Risks -=============== - -There may remain some extremely small likelihood that lsof will -transmit a bad physical address to the kernel. Here are some -possible failure scenarios: - - * The physical address filters haven't been tested on - the machine whose qfe interface was affected. That's - because the machine's memory configuration was changed - before the test could be run. - - * The kvm_physaddr() function, especially in Solaris - 2.5.1, might fail to map an address correctly. Only - Sun can correct this problem. - - * Because lsof must read the kernel address map from - kernel virtual memory to pass it to the Solaris 2.5.1 - and 2.6 kvm_physaddr() functions, lsof must use kvm_kread() - to read the map. - - There's also the chance that lsof could pass a stale - kernel address map to kvm_physaddr(), because re-reading - it for each call to kvm_physaddr() would lead to - unacceptable performance. When in repeat mode lsof - re-reads the map between each cycle. - - On Solaris 7 and 8, since kvm_physaddr() is inside the - kernel, there's no chance of its having a stale address - map. - - * There's an extremely small chance that a cached - virtual+physical page address could become invalid. - This is so small I think it can be ignored, since the - kernel memory map rarely changes. - - When in repeat mode, lsof clears its virtual+physical - address map between cycles. - - * Lsof still uses Sun's kvm_getproc() (from -lkvm), and - I have no idea what kernel address filtering it does, - if any. - -I wish to acknowledge: Casper Dik of Sun, who provided information -about kvm_physaddr() and helped test the lsof changes; Jim Mewes -of Phone.com, who reported the initial problem and helped test the -lsof changes; and several readers of the lsof-l listserv, who -volunteered to run test programs. - - -Vic Abell -March 16, 2004 From e2da74c655acd03419da5d0a16a1ed2f5c483a34 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 27 Nov 2026 15:14:34 +0800 Subject: [PATCH 02/18] sun: support old procfs psinfo layout --- lib/dialects/sun/dproc.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index 820bf22a..2b0a1e13 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -46,6 +46,15 @@ static int nm2id(char *nm, int *id) { #define PSINFO_EUID 28 #define PSINFO_FNAME 136 +/* Old (non-structured) prpsinfo_t used by some illumos/Solaris systems. */ +#define PSINFO_OLD_SIZE 328 +#define PSINFO_OLD_PID 16 +#define PSINFO_OLD_PPID 20 +#define PSINFO_OLD_PGID 24 +#define PSINFO_OLD_UID 8 +#define PSINFO_OLD_EUID 288 +#define PSINFO_OLD_FNAME 120 + /* * /proc//fdinfo/nn is a variable-length prfdinfo_t followed by * self-describing pr_misc entries. The layout is from Solaris/illumos @@ -372,14 +381,17 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, link_lfile(ctx); } -static int read_psinfo(const char *p, unsigned char *ps) { +static int read_psinfo(const char *p, unsigned char *ps, int *len) { int fd, n; if ((fd = open(p, O_RDONLY)) < 0) return (1); n = (int)read(fd, ps, PSINFO_SIZE); (void)close(fd); - return (n == PSINFO_SIZE) ? 0 : 1; + *len = n; + if (n >= PSINFO_SIZE) + return (0); + return (n == PSINFO_OLD_SIZE) ? 0 : 1; } static void enter_path(struct lsof_context *ctx, enum lsof_fd_type fdt, @@ -501,18 +513,25 @@ void gather_proc_info(struct lsof_context *ctx) { if (nm2id(dp->d_name, &pid)) continue; (void)snpf(pspath, sizeof(pspath), "%s/%d/psinfo", PROC_BASE, pid); - if (read_psinfo(pspath, ps)) + int pslen; + if (read_psinfo(pspath, ps, &pslen)) continue; { + int pidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_PID : PSINFO_PID; + int ppidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_PPID : PSINFO_PPID; + int pgidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_PGID : PSINFO_PGID; + int uidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_UID : PSINFO_UID; + int euidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_EUID : PSINFO_EUID; + int fnameoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_FNAME : PSINFO_FNAME; pid_t ps_pid, ps_ppid, ps_pgid; uid_t ps_uid, ps_euid; - memcpy(&ps_pid, &ps[PSINFO_PID], sizeof(ps_pid)); - memcpy(&ps_ppid, &ps[PSINFO_PPID], sizeof(ps_ppid)); - memcpy(&ps_pgid, &ps[PSINFO_PGID], sizeof(ps_pgid)); - memcpy(&ps_uid, &ps[PSINFO_UID], sizeof(ps_uid)); - memcpy(&ps_euid, &ps[PSINFO_EUID], sizeof(ps_euid)); - (void)memcpy(cmd, &ps[PSINFO_FNAME], sizeof(cmd) - 1); + memcpy(&ps_pid, &ps[pidoff], sizeof(ps_pid)); + memcpy(&ps_ppid, &ps[ppidoff], sizeof(ps_ppid)); + memcpy(&ps_pgid, &ps[pgidoff], sizeof(ps_pgid)); + memcpy(&ps_uid, &ps[uidoff], sizeof(ps_uid)); + memcpy(&ps_euid, &ps[euidoff], sizeof(ps_euid)); + (void)memcpy(cmd, &ps[fnameoff], sizeof(cmd) - 1); cmd[sizeof(cmd) - 1] = '\0'; if (ps_pid != pid) continue; From 8d7952164b643bf4fa73e87e3ebaa8e4977b32c4 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 27 Nov 2026 15:30:00 +0800 Subject: [PATCH 03/18] sun: trust procfs directory PID --- lib/dialects/sun/dproc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index 2b0a1e13..b91d0b77 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -533,8 +533,9 @@ void gather_proc_info(struct lsof_context *ctx) { memcpy(&ps_euid, &ps[euidoff], sizeof(ps_euid)); (void)memcpy(cmd, &ps[fnameoff], sizeof(cmd) - 1); cmd[sizeof(cmd) - 1] = '\0'; + /* Use the /proc directory PID as the authoritative PID. */ if (ps_pid != pid) - continue; + ps_pid = pid; if (is_proc_excl(ctx, pid, (int)ps_pgid, (UID_ARG)ps_uid, &pss, &sf) || is_cmd_excl(ctx, cmd, &pss, &sf)) From 81e6fd6954d60ccd43c6e3f198a431db36a33baf Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 27 Nov 2026 15:51:57 +0800 Subject: [PATCH 04/18] sun: accept illumos 416-byte psinfo --- lib/dialects/sun/dproc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index b91d0b77..8de252f0 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -389,7 +389,7 @@ static int read_psinfo(const char *p, unsigned char *ps, int *len) { n = (int)read(fd, ps, PSINFO_SIZE); (void)close(fd); *len = n; - if (n >= PSINFO_SIZE) + if (n >= 400) return (0); return (n == PSINFO_OLD_SIZE) ? 0 : 1; } From 2764552bd45fb9ed4dfcdfef3613b1b2994fcc24 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 27 Nov 2026 17:49:17 +0800 Subject: [PATCH 05/18] sun: trim procfs compatibility shims --- Makefile.am | 1 - lib/dialects/sun/Makefile | 6 +-- lib/dialects/sun/Mksrc | 2 +- lib/dialects/sun/dnode.c | 98 ++++++++++++++++++++++++++++++-------- lib/dialects/sun/dproc.c | 27 +++++++++++ lib/dialects/sun/dproto.h | 3 -- lib/dialects/sun/dsock.c | 8 ---- lib/dialects/sun/machine.h | 4 +- 8 files changed, 109 insertions(+), 40 deletions(-) delete mode 100644 lib/dialects/sun/dsock.c diff --git a/Makefile.am b/Makefile.am index f512f204..9150b974 100644 --- a/Makefile.am +++ b/Makefile.am @@ -74,7 +74,6 @@ LIBLSOF_SOURCES += lib/dialects/sun/ddev.c \ lib/dialects/sun/dmnt.c \ lib/dialects/sun/dnode.c \ lib/dialects/sun/dproc.c \ - lib/dialects/sun/dsock.c \ lib/dialects/sun/dstore.c \ lib/dialects/sun/dlsof.h \ lib/dialects/sun/dproto.h \ diff --git a/lib/dialects/sun/Makefile b/lib/dialects/sun/Makefile index 02b7f8b1..cf7e89cd 100644 --- a/lib/dialects/sun/Makefile +++ b/lib/dialects/sun/Makefile @@ -17,11 +17,11 @@ CFLAGS= ${CDEFS} ${INCL} ${DEBUG} HDR= lsof.h lsof_fields.h dlsof.h machine.h proto.h dproto.h -SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c dsock.c \ +SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c \ dstore.c dprint.c \ arg.c main.c misc.c node.c print.c proc.c store.c usage.c util.c -OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o dsock.o \ +OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o \ dstore.o dprint.o \ arg.o main.o misc.o node.o print.o proc.o store.o usage.o util.o @@ -130,8 +130,6 @@ dnode.o: ${HDR} dnode.c dproc.o: ${HDR} kernelbase.h dproc.c -dsock.o: ${HDR} dsock.c - dstore.o: ${HDR} dstore.c arg.o: ${HDR} arg.c diff --git a/lib/dialects/sun/Mksrc b/lib/dialects/sun/Mksrc index 53af4c70..7c5ff372 100755 --- a/lib/dialects/sun/Mksrc +++ b/lib/dialects/sun/Mksrc @@ -22,7 +22,7 @@ mksrc() { } D=lib/dialects/sun -L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dsock.c dstore.c machine.h" +L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dstore.c machine.h" mksrc diff --git a/lib/dialects/sun/dnode.c b/lib/dialects/sun/dnode.c index 3d983674..1bc51c05 100644 --- a/lib/dialects/sun/dnode.c +++ b/lib/dialects/sun/dnode.c @@ -6,28 +6,39 @@ * code; they never read kernel memory. */ -#include "common.h" - -void process_node(struct lsof_context *ctx, KA_T f) { - (void)ctx; - (void)f; -} - -int vop2ty(struct lsof_context *ctx, struct vnode *vp, int fx) { - (void)ctx; - (void)vp; - (void)fx; - return N_REGLR; -} +/* + * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana + * 47907. All rights reserved. + * + * Written by Victor A. Abell + * + * This software is not subject to any license of the American Telephone + * and Telegraph Company or the Regents of the University of California. + * + * Permission is granted to anyone to use this software for any purpose on + * any computer system, and to alter it and redistribute it freely, subject + * to the following restrictions: + * + * 1. Neither the authors nor Purdue University are responsible for any + * consequences of the use of this software. + * + * 2. The origin of this software must not be misrepresented, either by + * explicit claim or by omission. Credit to the authors and Purdue + * University must appear in documentation and sources. + * + * 3. Altered versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 4. This notice may not be removed or altered. + */ -void open_kvm(struct lsof_context *ctx) { - (void)ctx; -} -void close_kvm(struct lsof_context *ctx) { - (void)ctx; -} +#include "common.h" +/* + * Common stream/AFS code still references kread. The procfs backend has no + * kernel memory source, so those optional paths simply fail. + */ int kread(struct lsof_context *ctx, KA_T addr, char *buf, READLEN_T len) { (void)ctx; (void)addr; @@ -36,20 +47,65 @@ int kread(struct lsof_context *ctx, KA_T addr, char *buf, READLEN_T len) { return (1); } +/* Required by the lsof core; procfs needs no one-time kernel setup. */ void initialize(struct lsof_context *ctx) { (void)ctx; } +/* Zone-name filtering is not available in the procfs backend. */ int enter_zone_arg(struct lsof_context *ctx, char *zn) { (void)ctx; (void)zn; - return (0); + return (1); } +/* + * build_IPstates() - build the TCP and UDP state tables + */ + void build_IPstates(struct lsof_context *ctx) { - (void)ctx; + if (!TcpSt) { + (void)enter_IPstate(ctx, "TCP", "CLOSED", TCPS_CLOSED); + (void)enter_IPstate(ctx, "TCP", "IDLE", TCPS_IDLE); + (void)enter_IPstate(ctx, "TCP", "BOUND", TCPS_BOUND); + (void)enter_IPstate(ctx, "TCP", "LISTEN", TCPS_LISTEN); + (void)enter_IPstate(ctx, "TCP", "SYN_SENT", TCPS_SYN_SENT); + (void)enter_IPstate(ctx, "TCP", "SYN_RCVD", TCPS_SYN_RCVD); + (void)enter_IPstate(ctx, "TCP", "ESTABLISHED", TCPS_ESTABLISHED); + (void)enter_IPstate(ctx, "TCP", "CLOSE_WAIT", TCPS_CLOSE_WAIT); + (void)enter_IPstate(ctx, "TCP", "FIN_WAIT_1", TCPS_FIN_WAIT_1); + (void)enter_IPstate(ctx, "TCP", "CLOSING", TCPS_CLOSING); + (void)enter_IPstate(ctx, "TCP", "LAST_ACK", TCPS_LAST_ACK); + (void)enter_IPstate(ctx, "TCP", "FIN_WAIT_2", TCPS_FIN_WAIT_2); + (void)enter_IPstate(ctx, "TCP", "TIME_WAIT", TCPS_TIME_WAIT); + (void)enter_IPstate(ctx, "TCP", (char *)NULL, 0); + } + if (!UdpSt) { + (void)enter_IPstate(ctx, "UDP", "Unbound", TS_UNBND); + (void)enter_IPstate(ctx, "UDP", "Wait_BIND_REQ_Ack", TS_WACK_BREQ); + (void)enter_IPstate(ctx, "UDP", "Wait_UNBIND_REQ_Ack", TS_WACK_UREQ); + (void)enter_IPstate(ctx, "UDP", "Idle", TS_IDLE); + (void)enter_IPstate(ctx, "UDP", "Wait_OPT_REQ_Ack", TS_WACK_OPTREQ); + (void)enter_IPstate(ctx, "UDP", "Wait_CONN_REQ_Ack", TS_WACK_CREQ); + (void)enter_IPstate(ctx, "UDP", "Wait_CONN_REQ_Confirm", TS_WCON_CREQ); + (void)enter_IPstate(ctx, "UDP", "Wait_CONN_IND_Response", TS_WRES_CIND); + (void)enter_IPstate(ctx, "UDP", "Wait_CONN_RES_Ack", TS_WACK_CRES); + (void)enter_IPstate(ctx, "UDP", "Wait_Data_Xfr", TS_DATA_XFER); + (void)enter_IPstate(ctx, "UDP", "Wait_Read_Release", TS_WIND_ORDREL); + (void)enter_IPstate(ctx, "UDP", "Wait_Write_Release", TS_WREQ_ORDREL); + (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ6); + (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ7); + (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ9); + (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ10); + (void)enter_IPstate(ctx, "UDP", "Wait_DISCON_REQ_Ack", TS_WACK_DREQ11); + (void)enter_IPstate(ctx, "UDP", (char *)NULL, 0); + } } +/* + * v_path lives in the vnode and is unavailable without kmem. The path is + * obtained from /proc//path instead. + */ void read_v_path(struct lsof_context *ctx, KA_T ka, char *rb, size_t rbl) { (void)ctx; (void)ka; diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index 8de252f0..8ef07473 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -5,6 +5,33 @@ * or use libkvm. */ +/* + * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana + * 47907. All rights reserved. + * + * Written by Victor A. Abell + * + * This software is not subject to any license of the American Telephone + * and Telegraph Company or the Regents of the University of California. + * + * Permission is granted to anyone to use this software for any purpose on + * any computer system, and to alter it and redistribute it freely, subject + * to the following restrictions: + * + * 1. Neither the authors nor Purdue University are responsible for any + * consequences of the use of this software. + * + * 2. The origin of this software must not be misrepresented, either by + * explicit claim or by omission. Credit to the authors and Purdue + * University must appear in documentation and sources. + * + * 3. Altered versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 4. This notice may not be removed or altered. + */ + + #include "common.h" #include diff --git a/lib/dialects/sun/dproto.h b/lib/dialects/sun/dproto.h index 9e0d694f..3f64c4b5 100644 --- a/lib/dialects/sun/dproto.h +++ b/lib/dialects/sun/dproto.h @@ -51,7 +51,6 @@ extern int CTF_memCB(const char *name, ctf_id_t id, ulong_t offset, void *arg); extern int is_file_named(struct lsof_context *ctx, char *p, int nt, enum vtype vt, int ps); -extern int vop2ty(struct lsof_context *ctx, struct vnode *vp, int fx); #if defined(HAS_AFS) extern struct vnode *alloc_vcache(void); @@ -89,8 +88,6 @@ extern int print_vxfs_rnl_path(struct lfile *lf); extern int enter_zone_arg(struct lsof_context *ctx, char *zn); #endif /* defined(HASZONES) */ -extern void close_kvm(struct lsof_context *ctx); -extern void open_kvm(struct lsof_context *ctx); extern void process_socket(struct lsof_context *ctx, KA_T sa, char *ty); #if solaris >= 110000 diff --git a/lib/dialects/sun/dsock.c b/lib/dialects/sun/dsock.c deleted file mode 100644 index f47ac44a..00000000 --- a/lib/dialects/sun/dsock.c +++ /dev/null @@ -1,8 +0,0 @@ -/* - * dsock.c - Solaris /proc socket placeholder. - * - * Socket endpoint details are not yet read through /proc; the procfs - * backend still reports the numeric fd and its inode/device. - */ - -#include "common.h" diff --git a/lib/dialects/sun/machine.h b/lib/dialects/sun/machine.h index e330a183..a59b9d02 100644 --- a/lib/dialects/sun/machine.h +++ b/lib/dialects/sun/machine.h @@ -345,10 +345,10 @@ typedef struct pad_mutex { # if solaris >= 10000 && defined(HAS_V_PATH) /* #define HASNCACHE 1 */ # else /* solaris<10 || !defined(HAS_V_PATH) */ -# define HASNCACHE 1 +# /* HASNCACHE 1 -- kernel DNLC requires kmem, not used here. */ # endif /* solaris>=10000 && defined(HAS_V_PATH) */ -# define NCACHELDPFX open_kvm(); /* do before calling ncache_load() */ +# /* NCACHELDPFX is intentionally empty: this backend has no kvm. */ /* #define NCACHELDSFX ??? */ From 01d9c301fd71c43f330851d642bce5de0cb2e3d0 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 27 Nov 2026 18:15:53 +0800 Subject: [PATCH 06/18] sun: use system psinfo definitions --- Makefile.am | 2 + lib/dialects/sun/Makefile | 6 ++- lib/dialects/sun/Mksrc | 2 +- lib/dialects/sun/dproc.c | 75 +++++--------------------------------- lib/dialects/sun/dpsinfo.c | 64 ++++++++++++++++++++++++++++++++ lib/dialects/sun/dpsinfo.h | 47 ++++++++++++++++++++++++ 6 files changed, 128 insertions(+), 68 deletions(-) create mode 100644 lib/dialects/sun/dpsinfo.c create mode 100644 lib/dialects/sun/dpsinfo.h diff --git a/Makefile.am b/Makefile.am index 9150b974..5f9d31d7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -74,9 +74,11 @@ LIBLSOF_SOURCES += lib/dialects/sun/ddev.c \ lib/dialects/sun/dmnt.c \ lib/dialects/sun/dnode.c \ lib/dialects/sun/dproc.c \ + lib/dialects/sun/dpsinfo.c \ lib/dialects/sun/dstore.c \ lib/dialects/sun/dlsof.h \ lib/dialects/sun/dproto.h \ + lib/dialects/sun/dpsinfo.h \ lib/dialects/sun/machine.h endif diff --git a/lib/dialects/sun/Makefile b/lib/dialects/sun/Makefile index cf7e89cd..311b0aad 100644 --- a/lib/dialects/sun/Makefile +++ b/lib/dialects/sun/Makefile @@ -17,11 +17,11 @@ CFLAGS= ${CDEFS} ${INCL} ${DEBUG} HDR= lsof.h lsof_fields.h dlsof.h machine.h proto.h dproto.h -SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c \ +SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c dpsinfo.c \ dstore.c dprint.c \ arg.c main.c misc.c node.c print.c proc.c store.c usage.c util.c -OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o \ +OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o dpsinfo.o \ dstore.o dprint.o \ arg.o main.o misc.o node.o print.o proc.o store.o usage.o util.o @@ -130,6 +130,8 @@ dnode.o: ${HDR} dnode.c dproc.o: ${HDR} kernelbase.h dproc.c +dpsinfo.o: ${HDR} dpsinfo.c + dstore.o: ${HDR} dstore.c arg.o: ${HDR} arg.c diff --git a/lib/dialects/sun/Mksrc b/lib/dialects/sun/Mksrc index 7c5ff372..396fc49b 100755 --- a/lib/dialects/sun/Mksrc +++ b/lib/dialects/sun/Mksrc @@ -22,7 +22,7 @@ mksrc() { } D=lib/dialects/sun -L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dstore.c machine.h" +L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dpsinfo.c dpsinfo.h dstore.c machine.h" mksrc diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index 8ef07473..6cceb96f 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -45,6 +45,8 @@ #include #include +#include "dpsinfo.h" + #define PROC_BASE "/proc" static int nm2id(char *nm, int *id) { @@ -60,28 +62,6 @@ static int nm2id(char *nm, int *id) { return (0); } -/* - * Solaris /proc//psinfo is the structured 440-byte psinfo_t. Access - * the fields we need at their fixed offsets so this backend does not need - * to define _STRUCTURED_PROC before the kernel headers in dlsof.h. - */ -#define PSINFO_SIZE 440 -#define PSINFO_PID 8 -#define PSINFO_PPID 12 -#define PSINFO_PGID 16 -#define PSINFO_UID 24 -#define PSINFO_EUID 28 -#define PSINFO_FNAME 136 - -/* Old (non-structured) prpsinfo_t used by some illumos/Solaris systems. */ -#define PSINFO_OLD_SIZE 328 -#define PSINFO_OLD_PID 16 -#define PSINFO_OLD_PPID 20 -#define PSINFO_OLD_PGID 24 -#define PSINFO_OLD_UID 8 -#define PSINFO_OLD_EUID 288 -#define PSINFO_OLD_FNAME 120 - /* * /proc//fdinfo/nn is a variable-length prfdinfo_t followed by * self-describing pr_misc entries. The layout is from Solaris/illumos @@ -408,18 +388,6 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, link_lfile(ctx); } -static int read_psinfo(const char *p, unsigned char *ps, int *len) { - int fd, n; - - if ((fd = open(p, O_RDONLY)) < 0) - return (1); - n = (int)read(fd, ps, PSINFO_SIZE); - (void)close(fd); - *len = n; - if (n >= 400) - return (0); - return (n == PSINFO_OLD_SIZE) ? 0 : 1; -} static void enter_path(struct lsof_context *ctx, enum lsof_fd_type fdt, int fd, const char *path, const struct stat *st) { @@ -521,8 +489,7 @@ static void process_path(struct lsof_context *ctx, int pid, void gather_proc_info(struct lsof_context *ctx) { DIR *pd, *fdd; struct dirent *dp, *fdp; - unsigned char ps[PSINFO_SIZE]; - char cmd[256]; + struct sun_psinfo ps; char path[MAXPATHLEN + 1]; char fdpath[MAXPATHLEN + 1]; char pspath[MAXPATHLEN + 1]; @@ -540,37 +507,15 @@ void gather_proc_info(struct lsof_context *ctx) { if (nm2id(dp->d_name, &pid)) continue; (void)snpf(pspath, sizeof(pspath), "%s/%d/psinfo", PROC_BASE, pid); - int pslen; - if (read_psinfo(pspath, ps, &pslen)) + if (read_sun_psinfo(pspath, &ps)) + continue; + if (is_proc_excl(ctx, pid, (int)ps.pgid, (UID_ARG)ps.uid, &pss, + &sf) || + is_cmd_excl(ctx, ps.fname, &pss, &sf)) continue; - { - int pidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_PID : PSINFO_PID; - int ppidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_PPID : PSINFO_PPID; - int pgidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_PGID : PSINFO_PGID; - int uidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_UID : PSINFO_UID; - int euidoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_EUID : PSINFO_EUID; - int fnameoff = (pslen == PSINFO_OLD_SIZE) ? PSINFO_OLD_FNAME : PSINFO_FNAME; - pid_t ps_pid, ps_ppid, ps_pgid; - uid_t ps_uid, ps_euid; - - memcpy(&ps_pid, &ps[pidoff], sizeof(ps_pid)); - memcpy(&ps_ppid, &ps[ppidoff], sizeof(ps_ppid)); - memcpy(&ps_pgid, &ps[pgidoff], sizeof(ps_pgid)); - memcpy(&ps_uid, &ps[uidoff], sizeof(ps_uid)); - memcpy(&ps_euid, &ps[euidoff], sizeof(ps_euid)); - (void)memcpy(cmd, &ps[fnameoff], sizeof(cmd) - 1); - cmd[sizeof(cmd) - 1] = '\0'; - /* Use the /proc directory PID as the authoritative PID. */ - if (ps_pid != pid) - ps_pid = pid; - if (is_proc_excl(ctx, pid, (int)ps_pgid, (UID_ARG)ps_uid, &pss, - &sf) || - is_cmd_excl(ctx, cmd, &pss, &sf)) - continue; - alloc_lproc(ctx, pid, (int)ps_pgid, (int)ps_ppid, (UID_ARG)ps_euid, - cmd, (int)pss, (int)sf); - } + alloc_lproc(ctx, pid, (int)ps.pgid, (int)ps.ppid, (UID_ARG)ps.euid, + ps.fname, (int)pss, (int)sf); Plf = (struct lfile *)NULL; (void)snpf(path, sizeof(path), "%s/%d/cwd", PROC_BASE, pid); diff --git a/lib/dialects/sun/dpsinfo.c b/lib/dialects/sun/dpsinfo.c new file mode 100644 index 00000000..66f51dbf --- /dev/null +++ b/lib/dialects/sun/dpsinfo.c @@ -0,0 +1,64 @@ +/* + * dpsinfo.c - read Solaris/illumos psinfo through system headers + */ + +/* + * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana + * 47907. All rights reserved. + * + * Written by Victor A. Abell + * + * This software is not subject to any license of the American Telephone + * and Telegraph Company or the Regents of the University of California. + * + * Permission is granted to anyone to use this software for any purpose on + * any computer system, and to alter it and redistribute it freely, subject + * to the following restrictions: + * + * 1. Neither the authors nor Purdue University are responsible for any + * consequences of the use of this software. + * + * 2. The origin of this software must not be misrepresented, either by + * explicit claim or by omission. Credit to the authors and Purdue + * University must appear in documentation and sources. + * + * 3. Altered versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 4. This notice may not be removed or altered. + */ + +/* + * Keep this translation unit independent of common.h/dlsof.h. Those headers + * include kernel headers with different macro expectations, while + * needs _STRUCTURED_PROC to expose the userland psinfo_t. + */ +#define _STRUCTURED_PROC 1 +#include +#include +#include +#include + +#include "dpsinfo.h" + +int read_sun_psinfo(const char *path, struct sun_psinfo *out) { + psinfo_t ps; + int fd, n; + + if (!path || !out) + return (1); + if ((fd = open(path, O_RDONLY)) < 0) + return (1); + n = (int)read(fd, &ps, sizeof(ps)); + (void)close(fd); + if (n != (int)sizeof(ps)) + return (1); + out->pid = ps.pr_pid; + out->ppid = ps.pr_ppid; + out->pgid = ps.pr_pgid; + out->uid = ps.pr_uid; + out->euid = ps.pr_euid; + (void)strncpy(out->fname, ps.pr_fname, sizeof(out->fname) - 1); + out->fname[sizeof(out->fname) - 1] = '\0'; + return (0); +} diff --git a/lib/dialects/sun/dpsinfo.h b/lib/dialects/sun/dpsinfo.h new file mode 100644 index 00000000..58d7df6b --- /dev/null +++ b/lib/dialects/sun/dpsinfo.h @@ -0,0 +1,47 @@ +/* + * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana + * 47907. All rights reserved. + * + * Written by Victor A. Abell + * + * This software is not subject to any license of the American Telephone + * and Telegraph Company or the Regents of the University of California. + * + * Permission is granted to anyone to use this software for any purpose on + * any computer system, and to alter it and redistribute it freely, subject + * to the following restrictions: + * + * 1. Neither the authors nor Purdue University are responsible for any + * consequences of the use of this software. + * + * 2. The origin of this software must not be misrepresented, either by + * explicit claim or by omission. Credit to the authors and Purdue + * University must appear in documentation and sources. + * + * 3. Altered versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 4. This notice may not be removed or altered. + */ + +/* + * dpsinfo.h - Solaris/illumos psinfo reader for the procfs backend + */ + +#if !defined(SOLARIS_DPSINFO_H) +# define SOLARIS_DPSINFO_H 1 + +# include + +struct sun_psinfo { + pid_t pid; + pid_t ppid; + pid_t pgid; + uid_t uid; + uid_t euid; + char fname[256]; +}; + +int read_sun_psinfo(const char *path, struct sun_psinfo *out); + +#endif /* SOLARIS_DPSINFO_H */ From 98087fe3c49f5aa64a43f8f210c65a1cbe1ce12b Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 27 Nov 2026 18:27:01 +0800 Subject: [PATCH 07/18] sun: drop CTF and v_path remnants --- configure.ac | 8 +-- lib/dialects/sun/dfile.c | 102 ------------------------------------- lib/dialects/sun/dlsof.h | 50 ------------------ lib/dialects/sun/dnode.c | 11 ---- lib/dialects/sun/dproto.h | 19 ------- lib/dialects/sun/machine.h | 3 ++ 6 files changed, 4 insertions(+), 189 deletions(-) diff --git a/configure.ac b/configure.ac index 70a6ba79..65496732 100644 --- a/configure.ac +++ b/configure.ac @@ -194,7 +194,7 @@ AS_CASE([${host_os}], CFLAGS="$CFLAGS -Dsolaris=$LSOF_VERS" # Link system libraries - LIBS="$LIBS -lelf -lsocket -lnsl" + LIBS="$LIBS -lsocket -lnsl" # Handle missing header mkdir -p ./solaris11/sys @@ -540,15 +540,9 @@ AC_CHECK_HEADERS([sys/zone.h]) AC_CHECK_TYPES([pad_mutex_t], [], [], [#define _KERNEL #include ]) -# Detect libctf.h header for HAS_LIBCTF -AC_CHECK_HEADERS([libctf.h], [LIBS="$LIBS -lctf"]) - # Detect pc_direntpersec macro for HAS_PC_DIRENTPERSEC AC_CHECK_DECLS([pc_direntpersec((struct pcfs *)NULL)], [], [], [#include ]) -# Detect sys/fs/zfs.h header for HAS_ZFS -AC_CHECK_HEADERS([sys/fs/zfs.h]) - # Detect sys/cred_impl.h header for HAS_CRED_IMPL_H AC_CHECK_HEADERS([sys/cred_impl.h]) diff --git a/lib/dialects/sun/dfile.c b/lib/dialects/sun/dfile.c index 8875ea08..bfc48a22 100644 --- a/lib/dialects/sun/dfile.c +++ b/lib/dialects/sun/dfile.c @@ -417,108 +417,6 @@ char *print_dev(struct lfile *lf, /* file whose device is to be printed */ } #endif /* defined(HASPRINTDEV) */ -#if defined(HAS_V_PATH) - -/* - * Local definitions - */ - -# define VPRDLEN ((MAXPATHLEN + 7) / 8) /* v_path read length increment */ - -/* - * print_v_path() - print path name from vnode's v_path pointer - */ - -extern int print_v_path(struct lsof_context *ctx, - struct lfile *lf) /* local file structure */ -{ - char buf[MAXPATHLEN + 1]; - unsigned char del = 0; - unsigned char aperr = 0; - -# if defined(HASMNTSTAT) - struct stat sb; -# endif /* defined(HASMNTSTAT) */ - -# if defined(HASVXFS) && defined(HASVXFSRNL) - if (lf->is_vxfs && (lf->inp_ty == 1) && lf->fsdir) { - if (print_vxfs_rnl_path(lf)) - return (1); - } -# endif /* defined(HASVXFS) && defined(HASVXFSRNL) */ - - (void)read_v_path(ctx, (KA_T)lf->V_path, buf, (size_t)sizeof(buf)); - if (buf[0]) { - -# if defined(HASMNTSTAT) - if (!lf->mnt_stat && lf->dev_def && (lf->inp_ty == 1)) { - - /* - * No problem was detected in applying stat(2) to this mount point. - * If the device and inode for the file are known, it is probably - * safe and worthwhile to apply stat(2) to the v_path. - */ - if (!statsafely(ctx, buf, &sb)) { - - /* - * The stat(2) succeeded. See if the device and inode match. - * If they both don't match, ignore the v_path. - */ - if ((lf->dev != sb.st_dev) || - (lf->inode != (INODETYPE)sb.st_ino)) { - return (0); - } - } else { - - /* - * The stat(2) failed. - * - * If the error reply is ENOENT and the -X option hasn't been - * specified, ignore the v_path. - * - * If the error reply is ENOENT, the -X option has been - * specified and the file's link count is zero, report the - * v_path with the "(deleted)" notation. - * - * If the error reply is EACCES or EPERM, report the v_path, - * followed by "(?)", because lsof probably lacks permission - * to apply stat(2) to v_path. - */ - switch (errno) { - case EACCES: - case EPERM: - aperr = 1; - break; - case ENOENT: - -# if defined(HASXOPT) - if (Fxopt && lf->nlink_def && !lf->nlink) { - del = 1; - break; - } -# endif /* defined(HASXOPT) */ - - return (0); - default: - return (0); - } - } - } -# endif /* defined(HASMNTSTAT) */ - - /* - * Print the v_path. - */ - safestrprt(buf, stdout, 0); - if (del) - safestrprt(" (deleted)", stdout, 0); - else if (aperr) - safestrprt(" (?)", stdout, 0); - return (1); - } - return (0); -} -#endif /* defined(HAS_V_PATH) */ #if defined(HASIPv6) /* diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index e4f09273..60920d95 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -485,45 +485,6 @@ extern struct clone *Clone; extern major_t CloneMaj; -# if defined(HAS_LIBCTF) -/* - * Definitions for using the CTF library, libctf. - */ - -# include - -# define CTF_MEMBER_UNDEF ~0UL /* undefined member type */ - /* CTF_member_t element definition */ - -/* - * Member structure definition, initialized by CTF_MEMBER() macro calls - */ - -typedef struct CTF_member { - char *m_name; /* Member name. */ - ulong_t m_offset; /* Member offset, initially in bits, - * later bytes */ -} CTF_member_t; - -/* - * CTF request structure - */ - -typedef struct CTF_request { - char *name; /* structure name */ - CTF_member_t *mem; /* member table */ -} CTF_request_t; - -/* - * CTF macroes - */ - -# define CTF_MEMBER(name) \ - { #name, CTF_MEMBER_UNDEF } -# define CTF_MEMBER_READ(ka, s, members, member) \ - kread(ctx, (KA_T)(ka) + members[MX_##member].m_offset, \ - (char *)&s->member, sizeof(s->member)) -# endif /* defined(HAS_LIBCTF) */ extern char **Fsinfo; extern int Fsinfomax; @@ -679,17 +640,6 @@ extern int Unof; /* u_nofiles value */ * Definitions for rnch.c */ -# if defined(HASNCACHE) -# include - -# if !defined(NC_NAMLEN) -# define HASDNLCPTR 1 -# endif /* !defined(NC_NAMLEN) */ - -# if solaris >= 80000 -# define NCACHE_NEGVN "negative_cache_vnode" -# endif /* solaris>=80000 */ -# endif /* defined(HASNCACHE) */ struct lsof_context_dialect {}; diff --git a/lib/dialects/sun/dnode.c b/lib/dialects/sun/dnode.c index 1bc51c05..843c90be 100644 --- a/lib/dialects/sun/dnode.c +++ b/lib/dialects/sun/dnode.c @@ -101,14 +101,3 @@ void build_IPstates(struct lsof_context *ctx) { (void)enter_IPstate(ctx, "UDP", (char *)NULL, 0); } } - -/* - * v_path lives in the vnode and is unavailable without kmem. The path is - * obtained from /proc//path instead. - */ -void read_v_path(struct lsof_context *ctx, KA_T ka, char *rb, size_t rbl) { - (void)ctx; - (void)ka; - if (rb && rbl) - rb[0] = '\0'; -} diff --git a/lib/dialects/sun/dproto.h b/lib/dialects/sun/dproto.h index 3f64c4b5..eb7b8f0f 100644 --- a/lib/dialects/sun/dproto.h +++ b/lib/dialects/sun/dproto.h @@ -41,13 +41,6 @@ extern int access_vxfs_ioffsets(void); extern void completevfs(struct lsof_context *ctx, struct l_vfs *vfs, dev_t *dev); -#if defined(HAS_LIBCTF) -extern int CTF_getmem(struct lsof_context *ctx, ctf_file_t *f, const char *mod, - const char *ty, CTF_member_t *mem); -extern void CTF_init(struct lsof_context *ctx, int *i, char *t, - CTF_request_t *r); -extern int CTF_memCB(const char *name, ctf_id_t id, ulong_t offset, void *arg); -#endif /* defined(HAS_LIBCTF) */ extern int is_file_named(struct lsof_context *ctx, char *p, int nt, enum vtype vt, int ps); @@ -69,11 +62,6 @@ extern int rw_pseudo_sect(struct lsof_context *ctx, int m); extern struct hostent *gethostbyname2(const char *nm, int proto); #endif /* defined(HASIPv6) */ -#if defined(HAS_V_PATH) -extern int print_v_path(struct lsof_context *ctx, struct lfile *lf); -extern void read_v_path(struct lsof_context *ctx, KA_T ka, char *rb, - size_t rbl); -#endif /* defined(HAS_V_PATH) */ #if defined(HASVXFS) extern int read_vxnode(struct lsof_context *ctx, KA_T va, struct vnode *v, @@ -88,13 +76,6 @@ extern int print_vxfs_rnl_path(struct lfile *lf); extern int enter_zone_arg(struct lsof_context *ctx, char *zn); #endif /* defined(HASZONES) */ -extern void process_socket(struct lsof_context *ctx, KA_T sa, char *ty); - -#if solaris >= 110000 -extern int process_VSOCK(struct lsof_context *ctx, KA_T va, struct vnode *v, - struct sonode *so); -#endif /* solaris>=11000 */ - extern void read_clone(struct lsof_context *ctx); #if solaris < 20500 diff --git a/lib/dialects/sun/machine.h b/lib/dialects/sun/machine.h index a59b9d02..39a29f08 100644 --- a/lib/dialects/sun/machine.h +++ b/lib/dialects/sun/machine.h @@ -35,6 +35,9 @@ #if !defined(LSOF_MACHINE_H) # define LSOF_MACHINE_H 1 +/* v_path and kernel DNLC are not available through procfs. */ +# undef HAS_V_PATH + # if defined(HAS_LGRP_ROOT_CONFLICT) /* * must be #include'd early on some older Solaris systems at From 83f79a8c4233f6b6ad8d73688b8f905f1f2a18bd Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 00:21:18 +0800 Subject: [PATCH 08/18] sun: drop kernel headers and legacy support --- Makefile.am | 2 - configure.ac | 40 --- lib/dialects/sun/Makefile | 6 +- lib/dialects/sun/Mksrc | 2 +- lib/dialects/sun/dlsof.h | 482 +++++++++++-------------------------- lib/dialects/sun/dmnt.c | 35 +-- lib/dialects/sun/dproc.c | 26 +- lib/dialects/sun/dproto.h | 20 -- lib/dialects/sun/dpsinfo.c | 64 ----- lib/dialects/sun/dpsinfo.h | 47 ---- lib/dialects/sun/dstore.c | 139 ----------- lib/dialects/sun/machine.h | 143 ----------- tests/LsofTest.h | 3 + 13 files changed, 163 insertions(+), 846 deletions(-) delete mode 100644 lib/dialects/sun/dpsinfo.c delete mode 100644 lib/dialects/sun/dpsinfo.h diff --git a/Makefile.am b/Makefile.am index 5f9d31d7..9150b974 100644 --- a/Makefile.am +++ b/Makefile.am @@ -74,11 +74,9 @@ LIBLSOF_SOURCES += lib/dialects/sun/ddev.c \ lib/dialects/sun/dmnt.c \ lib/dialects/sun/dnode.c \ lib/dialects/sun/dproc.c \ - lib/dialects/sun/dpsinfo.c \ lib/dialects/sun/dstore.c \ lib/dialects/sun/dlsof.h \ lib/dialects/sun/dproto.h \ - lib/dialects/sun/dpsinfo.h \ lib/dialects/sun/machine.h endif diff --git a/configure.ac b/configure.ac index 65496732..47a7f82c 100644 --- a/configure.ac +++ b/configure.ac @@ -524,46 +524,6 @@ AS_IF([test "x$ac_cv_sizeof_dev_t" = x8], [ LSOF_TEST_CFLAGS="$LSOF_TEST_CFLAGS -DLT_DEV64" ]) -# Detect inet/ipclassifier.h header for HAS_IPCLASSIFIER_H -AC_CHECK_HEADERS([inet/ipclassifier.h], [], [], [#define _KERNEL - #include ]) - -# Detect struct conn_s.conn_ixa header for HAS_CONN_NEW -AC_CHECK_MEMBERS([struct conn_s.conn_ixa], [], [], [#define _KERNEL - #include - #include ]) - -# Detect sys/zone.h header for HASZONES -AC_CHECK_HEADERS([sys/zone.h]) - -# Detect pad_mutex_t for HAS_PAD_MUTEX -AC_CHECK_TYPES([pad_mutex_t], [], [], [#define _KERNEL - #include ]) - -# Detect pc_direntpersec macro for HAS_PC_DIRENTPERSEC -AC_CHECK_DECLS([pc_direntpersec((struct pcfs *)NULL)], [], [], [#include ]) - -# Detect sys/cred_impl.h header for HAS_CRED_IMPL_H -AC_CHECK_HEADERS([sys/cred_impl.h]) - -# Detect struct vnode.v_path for HAS_V_PATH -AC_CHECK_MEMBERS([struct vnode.v_path], [ - LSOF_TEST_CFLAGS="$LSOF_TEST_CFLAGS -DLT_VPATH" -], [], [#include ]) - -# Detect VSOCK for HAS_VSOCK -AC_CHECK_DECLS([VSOCK], [], [], [#include ]) - -# Detect struct aio_req for HAS_AIO_REQ_STRUCT -AC_CHECK_TYPES([struct aio_req], [], [], [#define _KERNEL - #include ]) - -# Detect sys/rgm.h header for HAS_SYS_RGM_H -AC_CHECK_HEADERS([sys/rgm.h]) - -# Detect rpc/rpc_tags.h header for HAS_RPC_RPC_TAGS_H -AC_CHECK_HEADERS([rpc/rpc_tags.h]) - # Generate Makefile from Makefile.in/am AC_CONFIG_FILES([Makefile]) diff --git a/lib/dialects/sun/Makefile b/lib/dialects/sun/Makefile index 311b0aad..cf7e89cd 100644 --- a/lib/dialects/sun/Makefile +++ b/lib/dialects/sun/Makefile @@ -17,11 +17,11 @@ CFLAGS= ${CDEFS} ${INCL} ${DEBUG} HDR= lsof.h lsof_fields.h dlsof.h machine.h proto.h dproto.h -SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c dpsinfo.c \ +SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c \ dstore.c dprint.c \ arg.c main.c misc.c node.c print.c proc.c store.c usage.c util.c -OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o dpsinfo.o \ +OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o \ dstore.o dprint.o \ arg.o main.o misc.o node.o print.o proc.o store.o usage.o util.o @@ -130,8 +130,6 @@ dnode.o: ${HDR} dnode.c dproc.o: ${HDR} kernelbase.h dproc.c -dpsinfo.o: ${HDR} dpsinfo.c - dstore.o: ${HDR} dstore.c arg.o: ${HDR} arg.c diff --git a/lib/dialects/sun/Mksrc b/lib/dialects/sun/Mksrc index 396fc49b..7c5ff372 100755 --- a/lib/dialects/sun/Mksrc +++ b/lib/dialects/sun/Mksrc @@ -22,7 +22,7 @@ mksrc() { } D=lib/dialects/sun -L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dpsinfo.c dpsinfo.h dstore.c machine.h" +L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dstore.c machine.h" mksrc diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index 60920d95..bf31afb5 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -38,334 +38,159 @@ # include # include # include - -# if solaris < 20600 -# define _KMEMUSER 1 -# else /* solaris>=20600 */ -# include -# endif /* solaris<20600 */ - +# include +# include +# include +# include +# include +# include # include # include - -# if defined(HASZONES) -# define _KERNEL -# include -# undef _KERNEL -# endif /* defined(HASZONES) */ - -# include -# include +# include # include # include # include # include # include -# include -# include +# include +# include +# include +# include # include # include # include - -# if solaris >= 110000 -# define _KERNEL -# endif /* solaris>=110000 */ - # include +# include +# include +# include +# include -# if solaris >= 110000 -# undef _KERNEL -# endif /* solaris>=110000 */ - -# if solaris >= 70000 -# include -# include -# endif /* solaris>=70000 */ - -# define _KERNEL -# define MI_HRTIMING -# include - -# if solaris < 20600 -# undef staticf -# endif /* solaris<20600 */ - -# include - -# if solaris >= 70000 -# include -# endif /* solaris>=70000 */ - -# if solaris < 20600 -# include -# endif /* solaris<20600 */ - -# if solaris >= 80000 -# include -# include -# endif /* solaris>=80000 */ - -# if defined(HAS_IPCLASSIFIER_H) -# define ffs __kernel_ffs -# define inet_ntop __inet_ntop -# define inet_pton __inet_pton -# define longjmp __kernel_longjmp -# define setjmp __kernel_setjmp -# if solaris >= 110000 -# define printf __kernel_printf -# define snprintf __kernel_snprintf -# define sprintf __kernel_sprintf -# define strsignal __kernel_strsignal -# define swab __kernel_swab -# define vprintf __kernel_vprintf -# define vsprintf __kernel_vsprintf -# define vsnprintf __kernel_vsnprintf -# define fls __kernel_fls -# define exit __kernel_exit -# endif /* solaris>=110000 */ -# include -# undef ffs -# undef inet_ntop -# undef inet_pton -# undef longjmp -# undef setjmp -# if solaris >= 110000 -# undef printf -# undef snprintf -# undef sprintf -# undef strsignal -# undef swab -# undef vprintf -# undef vsprintf -# undef vsnprintf -# undef fls -# undef exit -# endif /* solaris>=110000 */ -# endif /* defined(HAS_IPCLASSIFIER_H) */ - -# include -# undef _KERNEL -# undef MI_HRTIMING -# define exit kernel_exit -# define rval_t char -# define strsignal kernel_strsignal -# include - -# if defined(HAS_SOCKET_PROTO_H) -# define _KERNEL 1 /* DEBUG */ -# endif /* HAS_SOCKET_PROTO_H */ - -# include - -# if defined(HAS_SOCKET_PROTO_H) -# undef _KERNEL /* DEBUG */ -# endif /* HAS_SOCKET_PROTO_H */ - -# undef exit -# undef rval_t -# undef strsignal - -# if solaris >= 80000 -# define _KERNEL 1 -# endif /* solaris>=80000 */ - -# include - -# if solaris >= 80000 -# undef _KERNEL -# endif /* solaris>=80000 */ - -# include -# include -# include - -# if solaris < 20600 -# undef MAX -# undef MIN -# endif /* solaris<20600 */ - -# include -# include -# include -# include -# include -# include - -# if solaris >= 20600 -# define _KERNEL -# endif /* solaris>=20600 */ - -# include - -# if solaris >= 20600 -# undef _KERNEL -# endif /* solaris>=20600 */ - -# include - -# if solaris >= 20500 -# include -# if solaris >= 110000 -# define _KERNEL -# if defined(HAS_SYS_RGM_H) -# include -# endif /* defined(HAS_SYS_RGM_H) */ -# if defined(HAS_RPC_RPC_TAGS_H) -# include -# endif /* defined(HAS_RPC_RPC_TAGS_H) */ -# undef _KERNEL -# endif /* solaris>=110000 */ -# include -# include - -# if solaris >= 110000 -# define _KERNEL -# include -# undef _KERNEL -# endif /* solaris>=110000 */ - -# include -# include -# define _KERNEL -# include - -# if solaris >= 100000 -# define printf lsof_printf -# define snprintf lsof_snprintf -# define sprintf lsof_sprintf -# define swab lsof_swab -# define vprintf lsof_vprintf -# define vsnprintf lsof_vsnprintf -# define vsprintf lsof_vsprintf -# include -# undef printf -# undef snprintf -# undef sprintf -# undef swab -# undef vprintf -# undef vsnprintf -# undef vsprintf -# include -# include -# include -# endif /* solaris>=100000 */ - -# include -# undef _KERNEL -# endif /* solaris>=20500 */ - -# if !defined(_NETDB_H_) -# include -# endif /* !defined(_NETDB_H_) */ - -# include -# include - -# if solaris >= 20300 -# if solaris < 20400 /* - * The lock_descriptor structure definition is missing from Solaris 2.3. + * The procfs backend has no kernel structures. Define the small set of + * types and node/vnode constants that common lsof code still references. */ - -struct lock_descriptor { - struct lock_descriptor *prev; - struct lock_descriptor *next; - struct vnode *vnode; - struct owner { - pid_t pid; - long sysid; - } owner; - int flags; - short type; - off_t start; - off_t end; - struct lock_info { - struct active_lock_info { - struct lock_descriptor *ali_stack; - } li_active; - struct sleep_lock_info { - struct flock sli_flock; - /* Ignore the rest. */ - } li_sleep; - } info; +typedef uintptr_t KA_T; +typedef uint32_t major_t; +typedef uint32_t minor_t; + +# if !defined(MAXPATHLEN) +# define MAXPATHLEN 1024 +# endif +# if !defined(MAXCOMLEN) +# define MAXCOMLEN 16 +# endif + +# define GET_MAJ_DEV(d) ((major_t)((d) >> L_BITSMINOR & L_MAXMAJ)) +# define GET_MIN_DEV(d) ((minor_t)((d) & L_MAXMIN)) + +/* TCP/TPI state values from the Solaris networking headers. */ +# define TCPS_CLOSED 0 +# define TCPS_IDLE 1 +# define TCPS_BOUND 2 +# define TCPS_LISTEN 3 +# define TCPS_SYN_SENT 4 +# define TCPS_SYN_RCVD 5 +# define TCPS_ESTABLISHED 6 +# define TCPS_CLOSE_WAIT 7 +# define TCPS_FIN_WAIT_1 8 +# define TCPS_CLOSING 9 +# define TCPS_LAST_ACK 10 +# define TCPS_FIN_WAIT_2 11 +# define TCPS_TIME_WAIT 12 + +# define TS_UNBND 0 +# define TS_WACK_BREQ 1 +# define TS_WACK_UREQ 2 +# define TS_IDLE 3 +# define TS_WACK_OPTREQ 4 +# define TS_WACK_CREQ 5 +# define TS_WCON_CREQ 6 +# define TS_WRES_CIND 7 +# define TS_WACK_CRES 8 +# define TS_DATA_XFER 9 +# define TS_WIND_ORDREL 10 +# define TS_WREQ_ORDREL 11 +# define TS_WACK_DREQ6 12 +# define TS_WACK_DREQ7 13 +# define TS_WACK_DREQ9 14 +# define TS_WACK_DREQ10 15 +# define TS_WACK_DREQ11 16 + +enum vtype { + VNON, + VREG, + VDIR, + VBLK, + VCHR, + VLNK, + VSOCK, + VFIFO, + VBAD, + VDOOR, + VPORT, + VMPC, + VV_ROOT }; -# define ACTIVE_LOCK 0x008 /* lock is active */ -# else /* solaris>=20400 */ -# include -# endif /* solaris<20400 */ -# endif /* solaris>=20300 */ - -# include -# include -# include -# include -# include - -# if defined(HASCACHEFS) -# include -# endif /* defined(HACACHEFS) */ - -# include -# include -# include -# include -# include - -# if solaris >= 20600 -# undef SLOCKED -# endif /* solaris>=20600 */ - -# include -# include - -# if solaris >= 110000 -# define _KERNEL -# endif /* solaris>=110000 */ - -# include - -# if solaris >= 110000 -# undef _KERNEL -# endif /* solaris>=110000 */ - -# if solaris >= 100000 -# define _KERNEL -# endif /* solaris >= 100000 */ - -# include - -# if solaris >= 100000 -# include -# include -# define xvattr_t vattr_t -# include -# undef xvattr_t -# endif /* solaris>=100000 */ - -# if solaris >= 100000 -# undef _KERNEL -# endif /* solaris >= 100000 */ - -# include -# include -# if defined(HASPROCFS) -# include -# endif /* defined(HASPROCFS) */ - -# include -# include -# include -# include -# include -# include -# include -# include -# include - -# if solaris < 100000 -# include -# endif /* solaris<100000 */ +enum node_type { + N_UNKN, + N_REGLR, + N_BLK, + N_CHR, + N_FIFO, + N_SOCK, + N_STREAM, + N_COM, + N_MNT, + N_FD, + N_PROC, + N_SPEC, + N_PIPE, + N_DOOR, + N_PORT, + N_DEV, + N_NM, + N_OBJF, + N_PSEU, + N_SHARED, + N_AFPFS, + N_AFS, + N_ANON_INODE, + N_AUSX, + N_AUTO, + N_CACHE, + N_CDFS, + N_CFS, + N_CTFSADIR, + N_CTFSBUND, + N_CTFSCDIR, + N_CTFSCTL, + N_CTFSEVT, + N_CTFSLATE, + N_CTFSROOT, + N_CTFSSTAT, + N_CTFSSYM, + N_CTFSTDIR, + N_CTFSTMPL, + N_HSFS, + N_KERN, + N_LOFS, + N_MPC, + N_MQUEUE, + N_MVFS, + N_NFS, + N_NFS4, + N_PCFS, + N_SAMFS, + N_SANFS, + N_SDEV, + N_TMP, + N_UFS, + N_UNIX, + N_VXFS, + N_XFS, + N_ZFS +}; /* * Structure for Atria's MVFS nodes @@ -376,29 +201,12 @@ struct mvfsnode { unsigned long m_ino; /* node number */ }; -extern int nlist(const char *, struct nlist *); - -# if defined(HAS_AFS) && !defined(AFSAPATHDEF) -# define AFSAPATHDEF "/usr/vice/etc/modload/libafs" -# endif /* defined(HAS_AFS) && !defined(AFSAPATHDEF) */ - # define COMP_P const void # define CWDLEN (MAXPATHLEN + 1) # define DEVINCR 1024 /* device table malloc() increment */ # define DINAMEL 32 # define DIRTYPE dirent -# if solaris >= 100000 -# define GET_MAJ_DEV(d) ((major_t)(d >> L_BITSMINOR & L_MAXMAJ)) -# define GET_MIN_DEV(d) ((minor_t)(d & L_MAXMIN)) -# endif /* solaris >= 100000 */ - -# if solaris >= 70000 -typedef uintptr_t KA_T; -# else /* solaris<70000 */ -typedef void *KA_T; -# endif /* solaris>=70000 */ - # if solaris >= 70000 # define KA_T_FMT_X "0x%p" # endif /* solaris>=70000 */ @@ -420,7 +228,6 @@ typedef void *KA_T; # define MAXSEGS 100 /* maximum text segments */ # define MAXSYSCMDL MAXCOMLEN /* max system command name length */ # define NETCLNML 8 -# define N_UNIX "/dev/ksyms" # define PROCMIN 5 /* processes that make a "good" scan */ # if defined(HASPROCFS) @@ -430,7 +237,6 @@ typedef void *KA_T; # define PROCDFLT \ 256 /* default size for local proc table -- \ * MUST BE > 4!!! */ -# define PROCSIZE sizeof(struct proc) # define PROCTRYLM 5 /* times to try to read proc table */ # define QSORT_P char # define READLEN_T int @@ -456,25 +262,11 @@ typedef void *KA_T; * modifier */ # endif /* solaris>=20501 */ -# define U_SIZE sizeof(struct user) /* * Global storage definitions (including their structure definitions) */ -# if defined(HAS_AFS) - -# if defined(HASAOPT) -extern char *AFSApath; /* alternate AFS name list path - * (from -a) */ -# endif /* defined(HASAOPT) */ - -extern dev_t AFSdev; /* AFS file system device number */ -extern int AFSdevStat; /* AFS file system device number - * status: 0 = unknown; 1 = known */ -extern int AFSfstype; /* AFS file system type index */ -extern KA_T AFSVfsp; /* AFS struct vfs kernel pointer */ -# endif /* defined(HAS_AFS) */ struct clone { struct l_dev cd; /* device, inode, name, and verify */ diff --git a/lib/dialects/sun/dmnt.c b/lib/dialects/sun/dmnt.c index 68308510..1b6cc8cf 100644 --- a/lib/dialects/sun/dmnt.c +++ b/lib/dialects/sun/dmnt.c @@ -101,11 +101,6 @@ struct mounts *readmnt(struct lsof_context *ctx) { #endif /* defined(HASPROCFS) */ unsigned char stat; - char *zopt; - -#if defined(HASZONES) - int zwarn = 0; -#endif /* definesd(HASZONES) */ if (Lmi || Lmist) return (Lmi); @@ -207,14 +202,7 @@ struct mounts *readmnt(struct lsof_context *ctx) { dopte = (char *)NULL; if (!Fwarn) { -#if defined(HASZONES) - if ((zopt = hasmntopt(mp, "zone")) && dopte) - zwarn++; -#else /* !defined(HASZONES) */ - zopt = (char *)NULL; -#endif /* defined(HASZONES) */ - - if (!zopt || !dopte) { + if (!dopte) { (void)fprintf(stderr, "%s: WARNING: can't stat() ", Pn); safestrprt(mp->mnt_fstype, stderr, 0); (void)fprintf(stderr, " file system "); @@ -295,30 +283,9 @@ struct mounts *readmnt(struct lsof_context *ctx) { mtp->fs_mode = sb.st_mode; Lmi = mtp; -#if defined(HAS_AFS) - /* - * If an AFS device number hasn't yet been defined, look for it. - */ - if (!AFSdevStat && mtp->dir && strcmp(mtp->dir, "/afs") == 0 && - mtp->fsname && strcmp(mtp->fsname, "AFS") == 0) { - AFSdev = mtp->dev; - AFSdevStat = 1; - } -#endif /* defined(HAS_AFS) && solaris>=20600 */ } (void)fclose(mfp); -#if defined(HASZONES) - /* - * If some zone file systems were encountered, issue a warning. - */ - if (!Fwarn && zwarn) { - (void)fprintf(stderr, "%s: WARNING: can't stat() %d zone file system%s", - Pn, zwarn, (zwarn == 1) ? "" : "s"); - (void)fprintf(stderr, "; using dev= option%s\n", - (zwarn == 1) ? "" : "s"); - } -#endif /* defined(HASZONES) */ /* * Clean up and return local mount info table address. diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index 6cceb96f..e66b46d5 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -32,6 +32,9 @@ */ +#define _STRUCTURED_PROC 1 +#include + #include "common.h" #include @@ -45,10 +48,19 @@ #include #include -#include "dpsinfo.h" #define PROC_BASE "/proc" +static int read_psinfo(const char *path, psinfo_t *ps) { + int fd, n; + + if ((fd = open(path, O_RDONLY)) < 0) + return (1); + n = (int)read(fd, ps, sizeof(*ps)); + (void)close(fd); + return (n == (int)sizeof(*ps)) ? 0 : 1; +} + static int nm2id(char *nm, int *id) { char *ep; long v; @@ -489,7 +501,7 @@ static void process_path(struct lsof_context *ctx, int pid, void gather_proc_info(struct lsof_context *ctx) { DIR *pd, *fdd; struct dirent *dp, *fdp; - struct sun_psinfo ps; + psinfo_t ps; char path[MAXPATHLEN + 1]; char fdpath[MAXPATHLEN + 1]; char pspath[MAXPATHLEN + 1]; @@ -507,15 +519,15 @@ void gather_proc_info(struct lsof_context *ctx) { if (nm2id(dp->d_name, &pid)) continue; (void)snpf(pspath, sizeof(pspath), "%s/%d/psinfo", PROC_BASE, pid); - if (read_sun_psinfo(pspath, &ps)) + if (read_psinfo(pspath, &ps)) continue; - if (is_proc_excl(ctx, pid, (int)ps.pgid, (UID_ARG)ps.uid, &pss, + if (is_proc_excl(ctx, pid, (int)ps.pr_pgid, (UID_ARG)ps.pr_uid, &pss, &sf) || - is_cmd_excl(ctx, ps.fname, &pss, &sf)) + is_cmd_excl(ctx, ps.pr_fname, &pss, &sf)) continue; - alloc_lproc(ctx, pid, (int)ps.pgid, (int)ps.ppid, (UID_ARG)ps.euid, - ps.fname, (int)pss, (int)sf); + alloc_lproc(ctx, pid, (int)ps.pr_pgid, (int)ps.pr_ppid, + (UID_ARG)ps.pr_euid, ps.pr_fname, (int)pss, (int)sf); Plf = (struct lfile *)NULL; (void)snpf(path, sizeof(path), "%s/%d/cwd", PROC_BASE, pid); diff --git a/lib/dialects/sun/dproto.h b/lib/dialects/sun/dproto.h index eb7b8f0f..ea220cf7 100644 --- a/lib/dialects/sun/dproto.h +++ b/lib/dialects/sun/dproto.h @@ -34,9 +34,6 @@ * $Id: dproto.h,v 1.21 2010/01/18 19:03:54 abe Exp $ */ -#if defined(HASVXFSUTIL) -extern int access_vxfs_ioffsets(void); -#endif /* defined(HASVXFSUTIL) */ extern void completevfs(struct lsof_context *ctx, struct l_vfs *vfs, dev_t *dev); @@ -45,12 +42,6 @@ extern void completevfs(struct lsof_context *ctx, struct l_vfs *vfs, extern int is_file_named(struct lsof_context *ctx, char *p, int nt, enum vtype vt, int ps); -#if defined(HAS_AFS) -extern struct vnode *alloc_vcache(void); -extern void ckAFSsym(struct nlist *nl); -extern int hasAFS(struct vnode *vp); -extern int readafsnode(KA_T va, struct vnode *v, struct afsnode *an); -#endif /* defined(HAS_AFS) */ #if defined(HASDCACHE) extern int rw_clone_sect(struct lsof_context *ctx, int m); @@ -63,18 +54,7 @@ extern struct hostent *gethostbyname2(const char *nm, int proto); #endif /* defined(HASIPv6) */ -#if defined(HASVXFS) -extern int read_vxnode(struct lsof_context *ctx, KA_T va, struct vnode *v, - struct l_vfs *vfs, int fx, struct l_ino *li, - KA_T *vnops); -# if defined(HASVXFSRNL) -extern int print_vxfs_rnl_path(struct lfile *lf); -# endif /* defined(HASVXFSRNL) */ -#endif /* defined(HASVXFS) */ -#if defined(HASZONES) -extern int enter_zone_arg(struct lsof_context *ctx, char *zn); -#endif /* defined(HASZONES) */ extern void read_clone(struct lsof_context *ctx); diff --git a/lib/dialects/sun/dpsinfo.c b/lib/dialects/sun/dpsinfo.c deleted file mode 100644 index 66f51dbf..00000000 --- a/lib/dialects/sun/dpsinfo.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * dpsinfo.c - read Solaris/illumos psinfo through system headers - */ - -/* - * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana - * 47907. All rights reserved. - * - * Written by Victor A. Abell - * - * This software is not subject to any license of the American Telephone - * and Telegraph Company or the Regents of the University of California. - * - * Permission is granted to anyone to use this software for any purpose on - * any computer system, and to alter it and redistribute it freely, subject - * to the following restrictions: - * - * 1. Neither the authors nor Purdue University are responsible for any - * consequences of the use of this software. - * - * 2. The origin of this software must not be misrepresented, either by - * explicit claim or by omission. Credit to the authors and Purdue - * University must appear in documentation and sources. - * - * 3. Altered versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 4. This notice may not be removed or altered. - */ - -/* - * Keep this translation unit independent of common.h/dlsof.h. Those headers - * include kernel headers with different macro expectations, while - * needs _STRUCTURED_PROC to expose the userland psinfo_t. - */ -#define _STRUCTURED_PROC 1 -#include -#include -#include -#include - -#include "dpsinfo.h" - -int read_sun_psinfo(const char *path, struct sun_psinfo *out) { - psinfo_t ps; - int fd, n; - - if (!path || !out) - return (1); - if ((fd = open(path, O_RDONLY)) < 0) - return (1); - n = (int)read(fd, &ps, sizeof(ps)); - (void)close(fd); - if (n != (int)sizeof(ps)) - return (1); - out->pid = ps.pr_pid; - out->ppid = ps.pr_ppid; - out->pgid = ps.pr_pgid; - out->uid = ps.pr_uid; - out->euid = ps.pr_euid; - (void)strncpy(out->fname, ps.pr_fname, sizeof(out->fname) - 1); - out->fname[sizeof(out->fname) - 1] = '\0'; - return (0); -} diff --git a/lib/dialects/sun/dpsinfo.h b/lib/dialects/sun/dpsinfo.h deleted file mode 100644 index 58d7df6b..00000000 --- a/lib/dialects/sun/dpsinfo.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana - * 47907. All rights reserved. - * - * Written by Victor A. Abell - * - * This software is not subject to any license of the American Telephone - * and Telegraph Company or the Regents of the University of California. - * - * Permission is granted to anyone to use this software for any purpose on - * any computer system, and to alter it and redistribute it freely, subject - * to the following restrictions: - * - * 1. Neither the authors nor Purdue University are responsible for any - * consequences of the use of this software. - * - * 2. The origin of this software must not be misrepresented, either by - * explicit claim or by omission. Credit to the authors and Purdue - * University must appear in documentation and sources. - * - * 3. Altered versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 4. This notice may not be removed or altered. - */ - -/* - * dpsinfo.h - Solaris/illumos psinfo reader for the procfs backend - */ - -#if !defined(SOLARIS_DPSINFO_H) -# define SOLARIS_DPSINFO_H 1 - -# include - -struct sun_psinfo { - pid_t pid; - pid_t ppid; - pid_t pgid; - uid_t uid; - uid_t euid; - char fname[256]; -}; - -int read_sun_psinfo(const char *path, struct sun_psinfo *out); - -#endif /* SOLARIS_DPSINFO_H */ diff --git a/lib/dialects/sun/dstore.c b/lib/dialects/sun/dstore.c index 2701805d..ed2b8e87 100644 --- a/lib/dialects/sun/dstore.c +++ b/lib/dialects/sun/dstore.c @@ -39,149 +39,10 @@ static char copyright[] = * Global storage definitions */ -#if defined(HAS_AFS) - -# if defined(HASAOPT) -char *AFSApath = (char *)NULL; /* alternate AFS name list path - * (from -a) */ -# endif /* defined(HASAOPT) */ - -dev_t AFSdev; /* AFS file system device number */ -int AFSdevStat = 0; /* AFSdev status: 0 = unknown; - * 1 = known */ -int AFSfstype = -1; /* AFS file system type index */ -KA_T AFSVfsp = (KA_T)NULL; /* AFS vfs struct kernel address */ -#endif /* defined(HAS_AFS) */ struct clone *Clone = NULL; /* clone list */ major_t CloneMaj; /* clone major device number */ -/* - * Drive_Nl -- table to drive the building of Nl[] via build_Nl() - * (See lsof.h and misc.c.) - */ - -struct drive_Nl Drive_Nl[] = {{"afsops", "afs_ops"}, - {"arFid", "afs_rootFid"}, - {"avops", "afs_vnodeops"}, - {"Avops", "Afs_vnodeops"}, - {"avol", "afs_volumes"}, - {"auvops", "auto_vnodeops"}, - {"ctfsadir", "ctfs_ops_adir"}, - {"ctfsbund", "ctfs_ops_bundle"}, - {"ctfscdir", "ctfs_ops_cdir"}, - { - "ctfsctl", - "ctfs_ops_ctl", - }, - { - "ctfsevt", - "ctfs_ops_event", - }, - { - "ctfslate", - "ctfs_ops_latest", - }, - { - "ctfsroot", - "ctfs_ops_root", - }, - { - "ctfsstat", - "ctfs_ops_stat", - }, - { - "ctfssym", - "ctfs_ops_sym", - }, - { - "ctfstdir", - "ctfs_ops_tdir", - }, - { - "ctfstmpl", - "ctfs_ops_tmpl", - }, - {"cvops", "cachefs_vnodeops"}, - {"clmaj", "clonemaj"}, - {"clmaj_alt", "clone_major"}, - {"fdops", "fdvnodeops"}, - {"fd_ops", "fd_vnodeops"}, - {"fvops", "fifo_vnodeops"}, - {"hvops", "hsfs_vnodeops"}, - {"lvops", "lo_vnodeops"}, - {"mntops", "mntvnodeops"}, - {"mvops", "mvfs_vnodeops"}, - -#if solaris < 90000 - {X_NCACHE, "ncache"}, - {X_NCSIZE, "ncsize"}, -#else /* solaris>=90000 */ - {X_NCACHE, "nc_hash"}, - {X_NCSIZE, "nc_hashsz"}, - {"hshav", "nc_hashavelen"}, -#endif /* solaris<90000 */ - -#if defined(NCACHE_NEGVN) - {NCACHE_NEGVN, NCACHE_NEGVN}, -#endif /* defined(NCACHE_NEGVN) */ - - {"nvops", "nfs_vnodeops"}, - {"n3vops", "nfs3_vnodeops"}, - {"n4vops", "nfs4_vnodeops"}, - {"nmvops", "nm_vnodeops"}, - {"nproc", "nproc"}, - {"pdvops", "pcfs_dvnodeops"}, - {"pfvops", "pcfs_fvnodeops"}, - {"portvops", "port_vnodeops"}, - {"pract", "practive"}, - {"prvops", "prvnodeops"}, - {"sam1vops", "samfs_vnodeops"}, - {"sam2vops", "samfs_client_vnodeops"}, - {"sam3vops", "samfs_vnodeopsp"}, - {"sam4vops", "samfs_client_vnodeopsp"}, - {"sdevops", "sdev_vnodeops"}, - {"sgvops", "segvn_ops"}, - {"shvops", "sharefs_ops_data"}, - {"sckvops", "sock_vnodeops"}, - {"socketvops", "socket_vnodeops"}, - {"spvops", "spec_vnodeops"}, - {"sncavops", "socknca_vnodeops"}, - {"stpivops", "socktpi_vnodeops"}, - {"tvops", "tmp_vnodeops"}, - {"uvops", "ufs_vnodeops"}, - {"vvfops", "fdd_vnops"}, - {"vvfcops", "fdd_chain_vnops"}, - {"vvfclops", "vx_fcl_vnodeops_p"}, - {"vvops", "vx_vnodeops"}, - {"vvops_p", "vx_vnodeops_p"}, - -#if solaris >= 20500 - {"devops", "dv_vnodeops"}, - {"doorops", "door_vnodeops"}, - {"kbase", "_kernelbase"}, -#endif /* solaris>=20500 */ - -#if solaris >= 20501 - {"kasp", "kas"}, -#endif /* solaris>=20501 */ - -#if solaris >= 110000 - {"devipnetops", "devipnet_vnodeops"}, - {"devnetops", "devnet_vnodeops"}, - {"devptsops", "devpts_vnodeops"}, - {"devvtops", "devvt_vnodeops"}, -#endif /* solaris>=110000 */ - - {"zfsdops", "zfs_dvnodeops"}, - {"zfseops", "zfs_evnodeops"}, - {"zfsfops", "zfs_fvnodeops"}, - {"zfsshops", "zfs_sharevnodeops"}, - {"zfssymops", "zfs_symvnodeops"}, - {"zfsxdops", "zfs_xdvnodeops"}, - {"", ""}, - {NULL, NULL}}; - char **Fsinfo = NULL; /* file system information */ int Fsinfomax = 0; /* maximum file system type */ int HaveCloneMaj = 0; /* clone major device number has diff --git a/lib/dialects/sun/machine.h b/lib/dialects/sun/machine.h index 39a29f08..cfdcacb2 100644 --- a/lib/dialects/sun/machine.h +++ b/lib/dialects/sun/machine.h @@ -35,96 +35,6 @@ #if !defined(LSOF_MACHINE_H) # define LSOF_MACHINE_H 1 -/* v_path and kernel DNLC are not available through procfs. */ -# undef HAS_V_PATH - -# if defined(HAS_LGRP_ROOT_CONFLICT) -/* - * must be #include'd early on some older Solaris systems at - * version 9 and Solaris 10 before _KMEMUSER or _KERNEL are defined to avoid - * a conflict with the use of lgrp_root as an external symbol in - * and a macro in . - */ - -# include -# endif /* defined(HAS_LGRP_ROOT_CONFLICT) */ - -# if solaris >= 100000 -# if !defined(HAS_AIO_REQ_STRUCT) -/* - * When lacks one, define a dummy aio_req structure for - * Solaris >= 10 systems. - * - * If this definition causes compilation errors for , especially - * with Solaris 11, it may be necessary to comment out the following structure - * definition. I don't know a test that will determine the possibility of - * compilation errors. - */ - -typedef struct aio_req { - int dummy; -} aio_req_t; - -# endif /* !defined(HAS_AIO_REQ_STRUCT) */ - -/* - * Include so it won't be corrupted for 32 bit compilations - * when _KERNEL is defined for some include files in dlsof.h. - * - * Daniel Trinkle identified this requirement. - */ - -# include -# endif /* solaris>=100000 */ - -# if solaris >= 20600 -/* - * must be #include'd for Solaris >= 2.6 while _KMEMUSER is - * defined. Since also #include's and - * is #include'd from lsof.h, we must perform some early #include magic - * here to set things up properly. - */ - -# define _KMEMUSER 1 -# define __BIT_TYPES_DEFINED__ \ - 1 /* work around to keep the BIND \ - * from colliding with \ - * the Solaris */ - -# if defined(HAS_PAD_MUTEX) -/* - * Some versions of Solaris 11 need to have the pad_mutex_t typedef defined. - * However, it is only defined by when _KERNEL is defined, and - * doing that causes other difficulties. - * - * So is included here, followed by a copy of its pad_mutex_t - * typedef, all outside the _KERNEL definition. - * - * This brute force work-around was supplied by Carson Gaspar. - */ - -# include -typedef struct pad_mutex { - kmutex_t pad_mutex; -# if defined(_LP64) - char pad_pad[64 - sizeof(kmutex_t)]; -# endif /* defined(_LP64) */ -} pad_mutex_t; -# endif /* defined(HAS_PAD_MUTEX) */ - -# include - -# if solaris >= 80000 -# include -# include -# define _KERNEL 1 -# include -# undef _KERNEL -# define ipa_32 s6_ipaddr.ipa_32 -# endif /* solaris>=80000 */ - -# endif /* solaris>=20600 */ - /* * CAN_USE_CLNT_CREATE is defined for those dialects where RPC clnt_create() * can be used to obtain a CLIENT handle in lieu of clnttcp_create(). @@ -160,7 +70,6 @@ typedef struct pad_mutex { * supplied with the -A option. */ -# define HASAOPT 1 /* * HASBLKDEV is defined for those dialects that want block device information @@ -216,7 +125,6 @@ typedef struct pad_mutex { * HASFIFONODE is defined for those dialects that have FIFO nodes. */ -# define HASFIFONODE 1 /* * HASFSINO is defined for those dialects that have the file system @@ -241,7 +149,6 @@ typedef struct pad_mutex { * HASNOFSNADDR -- has no file structure node address */ -# define HASFSTRUCT 1 /* #define FSV_DEFAULT FSV_? | FSV_? | FSV_? */ /* #define HASNOFSADDR 1 has no file structure address */ /* #define HASNOFSFLAGS 1 has no file structure flags */ @@ -258,7 +165,6 @@ typedef struct pad_mutex { * HASHSNODE is defined for those dialects that have High Sierra nodes. */ -# define HASHSNODE 1 /* * HASINODE is defined for those dialects that have inodes and wish to @@ -286,7 +192,6 @@ typedef struct pad_mutex { * reading the kernel's name list from an optional file. */ -# define HASKOPT 1 /* * HASLFILEADD is defined for those dialects that need additional elements @@ -306,20 +211,12 @@ typedef struct pad_mutex { * private lfile elements are used. */ -# if solaris >= 10000 && defined(HAS_V_PATH) -# define HASLFILEADD KA_T V_path; -# define CLRLFILEADD(lf) (lf)->V_path = (KA_T)NULL; -# define SETLFILEADD Lf->V_path = (KA_T)NULL; -# endif /* solaris>=10000 && defined(HAS_V_PATH) */ /* * HASMNTSTAT indicates the dialect supports the mount stat(2) result option * in its l_vfs and mounts structures. */ -# if solaris >= 10000 && defined(HAS_V_PATH) -# define HASMNTSTAT 1 -# endif /* solaris>=10000 && defined(HAS_V_PATH) */ /* * HASMNTSUP is defined for those dialects that support the mount supplement @@ -333,34 +230,12 @@ typedef struct pad_mutex { * kernel memory from an alternate file. */ -# define HASMOPT 1 - -/* - * HASNCACHE is defined for those dialects that have a kernel name cache - * that lsof can search. A value of 1 directs printname() to prefix the - * cache value with the file system directory name; 2, avoid the prefix. - * - * NCACHELDPFX is a set of C commands to execute before calling ncache_load(). - * - * NCACHELDSFX is a set of C commands to execute after calling ncache_load(). - */ - -# if solaris >= 10000 && defined(HAS_V_PATH) -/* #define HASNCACHE 1 */ -# else /* solaris<10 || !defined(HAS_V_PATH) */ -# /* HASNCACHE 1 -- kernel DNLC requires kmem, not used here. */ -# endif /* solaris>=10000 && defined(HAS_V_PATH) */ - -# /* NCACHELDPFX is intentionally empty: this backend has no kvm. */ - -/* #define NCACHELDSFX ??? */ /* * HASNLIST is defined for those dialects that use nlist() to access * kernel symbols. */ -# define HASNLIST 1 /* * HASPIPEFN is defined for those dialects that have a special function to @@ -428,15 +303,6 @@ typedef struct pad_mutex { * returns non-zero if it prints a name to stdout. */ -# if solaris >= 10000 && defined(HAS_V_PATH) -# define HASPRIVNMCACHE print_v_path -# else /* solaris<10 || !defined(HAS_V_PATH) */ -# if defined(HASVXFSRNL) -# define HASPRIVNMCACHE print_vxfs_rnl_path -# else /* !defined(HASVXFSRNL) */ -/* #define HASPRIVNMCACHE */ -# endif /* defined(HASVXFSRNL) */ -# endif /* solaris>=10000 && defined(HAS_V_PATH) */ /* * HASPRIVPRIPP is defined for dialects that have a private function for @@ -478,7 +344,6 @@ typedef struct pad_mutex { * HASRNODE is defined for those dialects that have rnodes. */ -# define HASRNODE 1 /* * Define HASSECURITY to restrict the listing of all open files to the @@ -521,7 +386,6 @@ typedef struct pad_mutex { * HASSNODE is defined for those dialects that have snodes. */ -# define HASSNODE 1 /* * HASTASKS is defined for those dialects that have task reporting support. @@ -562,7 +426,6 @@ typedef struct pad_mutex { * HASSTREAMS is defined for those dialects that support streams. */ -# define HASSTREAMS 1 /* * HASTCPTPIQ is defined for dialects where it is possible to report the @@ -594,7 +457,6 @@ typedef struct pad_mutex { * HASTMPNODE is defined for those dialects that have tmpnodes. */ -# define HASTMPNODE 1 /* * HASVNODE is defined for those dialects that use the Sun virtual file system @@ -602,7 +464,6 @@ typedef struct pad_mutex { * R4 usually don't. */ -# define HASVNODE 1 /* * HASXOPT is defined for those dialects that have an X option. It @@ -610,10 +471,6 @@ typedef struct pad_mutex { * option's default binary value -- 0 or 1. */ -# if solaris >= 10000 && defined(HAS_V_PATH) -# define HASXOPT "report deleted paths" -# define HASXOPT_VALUE 0 -# endif /* solaris>=10000 && defined(HAS_V_PATH) */ /* * INODETYPE and INODEPSPEC define the internal node number type and its diff --git a/tests/LsofTest.h b/tests/LsofTest.h index 25f8d27f..9d705f64 100644 --- a/tests/LsofTest.h +++ b/tests/LsofTest.h @@ -241,6 +241,9 @@ typedef int pid_t; * Solaris-specific items */ +/* The procfs backend has no kernel DNLC, so -C is not available. */ +# undef USE_LSOF_C_OPT + # include # include # include From 78af8087f6b4a0b9765771047fc28d3ef0a080c9 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 01:30:37 +0800 Subject: [PATCH 09/18] sun: remove kread stub and kernel pointer field --- lib/dialects/sun/dlsof.h | 1 - lib/dialects/sun/dnode.c | 12 ------------ 2 files changed, 13 deletions(-) diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index bf31afb5..1cc82ba6 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -296,7 +296,6 @@ struct l_ino { }; struct l_vfs { - KA_T addr; /* kernel address */ char *dir; /* mounted directory */ char *fsname; /* file system name */ dev_t dev; /* device */ diff --git a/lib/dialects/sun/dnode.c b/lib/dialects/sun/dnode.c index 843c90be..b7263a0b 100644 --- a/lib/dialects/sun/dnode.c +++ b/lib/dialects/sun/dnode.c @@ -35,18 +35,6 @@ #include "common.h" -/* - * Common stream/AFS code still references kread. The procfs backend has no - * kernel memory source, so those optional paths simply fail. - */ -int kread(struct lsof_context *ctx, KA_T addr, char *buf, READLEN_T len) { - (void)ctx; - (void)addr; - (void)buf; - (void)len; - return (1); -} - /* Required by the lsof core; procfs needs no one-time kernel setup. */ void initialize(struct lsof_context *ctx) { (void)ctx; From 2912324e3d36c108bc4b20f095212775f0020078 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 01:47:05 +0800 Subject: [PATCH 10/18] sun: eliminate KA_T kernel pointer type --- lib/dialects/sun/dlsof.h | 8 ++------ lib/node.c | 2 ++ lib/proto.h | 6 ++++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index 1cc82ba6..a22106ff 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -69,7 +69,8 @@ * The procfs backend has no kernel structures. Define the small set of * types and node/vnode constants that common lsof code still references. */ -typedef uintptr_t KA_T; +# define LSOF_NO_KA_T 1 + typedef uint32_t major_t; typedef uint32_t minor_t; @@ -207,10 +208,6 @@ struct mvfsnode { # define DINAMEL 32 # define DIRTYPE dirent -# if solaris >= 70000 -# define KA_T_FMT_X "0x%p" -# endif /* solaris>=70000 */ - # if solaris >= 20501 # define KMEM "/dev/mem" # else /* solaris<20501 */ @@ -239,7 +236,6 @@ struct mvfsnode { * MUST BE > 4!!! */ # define PROCTRYLM 5 /* times to try to read proc table */ # define QSORT_P char -# define READLEN_T int # define STRNCPY_L int # define STRNML 32 /* stream name length (maximum) */ diff --git a/lib/node.c b/lib/node.c index 4da6c67b..9e2d9077 100644 --- a/lib/node.c +++ b/lib/node.c @@ -30,6 +30,7 @@ #include "common.h" +#if !defined(LSOF_NO_KA_T) /* * print_kptr() - print kernel pointer */ @@ -43,6 +44,7 @@ char *print_kptr(KA_T kp, /* kernel pointer address */ (void)snpf(buf ? buf : dbuf, buf ? bufl : sizeof(dbuf), KA_T_FMT_X, kp); return (buf ? buf : dbuf); } +#endif /* !defined(LSOF_NO_KA_T) */ #if defined(HASCDRNODE) /* diff --git a/lib/proto.h b/lib/proto.h index fb3c5fcc..39f7d0f9 100644 --- a/lib/proto.h +++ b/lib/proto.h @@ -172,7 +172,9 @@ extern int is_proc_excl(struct lsof_context *ctx, int pid, int pgid, # endif /* defined(HASTASKS) */ extern int is_readable(struct lsof_context *ctx, char *path, int msg); +# if !defined(LSOF_NO_KA_T) extern int kread(struct lsof_context *ctx, KA_T addr, char *buf, READLEN_T len); +# endif /* !defined(LSOF_NO_KA_T) */ extern void link_lfile(struct lsof_context *ctx); extern struct l_dev *lkupdev(struct lsof_context *ctx, dev_t *dev, dev_t *rdev, int i, int r); @@ -186,7 +188,9 @@ extern int printdevname(struct lsof_context *ctx, dev_t *dev, dev_t *rdev, extern void print_file(struct lsof_context *ctx); extern void print_init(struct lsof_context *ctx); extern void printname(struct lsof_context *ctx, int nl); +# if !defined(LSOF_NO_KA_T) extern char *print_kptr(KA_T kp, char *buf, size_t bufl); +# endif /* !defined(LSOF_NO_KA_T) */ extern int print_proc(struct lsof_context *ctx); extern void json_open_envelope(void); extern void json_close_envelope(void); @@ -200,8 +204,10 @@ extern char lock_to_char(enum lsof_lock_mode access); extern void file_type_to_string(enum lsof_file_type type, uint32_t unknown_file_type_number, char *buf, size_t buf_len); +# if !defined(LSOF_NO_KA_T) extern void process_file(struct lsof_context *ctx, KA_T fp); extern void process_node(struct lsof_context *ctx, KA_T f); +# endif /* !defined(LSOF_NO_KA_T) */ extern char *Readlink(struct lsof_context *ctx, char *arg); extern void readdev(struct lsof_context *ctx, int skip); extern struct mounts *readmnt(struct lsof_context *ctx); From 0452cf2daf7bccbcc340055b50f5e12ed8577cf3 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 02:24:39 +0800 Subject: [PATCH 11/18] sun: remove more unused code and warnings --- lib/dialects/sun/dfile.c | 13 +---- lib/dialects/sun/dlsof.h | 107 +------------------------------------ lib/dialects/sun/dmnt.c | 10 +--- lib/dialects/sun/dnode.c | 13 ++--- lib/dialects/sun/dproc.c | 5 +- lib/dialects/sun/dproto.h | 8 --- lib/dialects/sun/dstore.c | 55 ------------------- lib/dialects/sun/machine.h | 24 --------- src/dialects/sun/dprint.c | 29 +++++++++- 9 files changed, 38 insertions(+), 226 deletions(-) diff --git a/lib/dialects/sun/dfile.c b/lib/dialects/sun/dfile.c index bfc48a22..a7f00f0d 100644 --- a/lib/dialects/sun/dfile.c +++ b/lib/dialects/sun/dfile.c @@ -77,17 +77,6 @@ static char copyright[] = * mod (mod must be a power of 2) */ #if solaris < 20500 -/* - * get_max_fd() - get maximum file descriptor plus one - */ - -int get_max_fd() { - struct rlimit r; - - if (getrlimit(RLIMIT_NOFILE, &r)) - return (-1); - return (r.rlim_cur); -} #endif /* solaris<20500 */ /* @@ -186,7 +175,7 @@ void hashSfile(struct lsof_context *ctx) { } else continue; } else { - if (!hvc || (GET_MAJ_DEV(s->rdev) != cmaj)) + if (!hvc || ((major_t)GET_MAJ_DEV(s->rdev) != (major_t)cmaj)) continue; sh = &HbyCd[SFHASHDEVINO(0, GET_MIN_DEV(s->rdev), 0, SFCDHASH)]; HbyCdCt++; diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index a22106ff..655775f6 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -123,14 +123,8 @@ enum vtype { VDIR, VBLK, VCHR, - VLNK, VSOCK, - VFIFO, - VBAD, - VDOOR, - VPORT, - VMPC, - VV_ROOT + VFIFO }; enum node_type { @@ -139,58 +133,8 @@ enum node_type { N_BLK, N_CHR, N_FIFO, - N_SOCK, N_STREAM, - N_COM, - N_MNT, - N_FD, - N_PROC, - N_SPEC, - N_PIPE, - N_DOOR, - N_PORT, - N_DEV, - N_NM, - N_OBJF, - N_PSEU, - N_SHARED, - N_AFPFS, - N_AFS, - N_ANON_INODE, - N_AUSX, - N_AUTO, - N_CACHE, - N_CDFS, - N_CFS, - N_CTFSADIR, - N_CTFSBUND, - N_CTFSCDIR, - N_CTFSCTL, - N_CTFSEVT, - N_CTFSLATE, - N_CTFSROOT, - N_CTFSSTAT, - N_CTFSSYM, - N_CTFSTDIR, - N_CTFSTMPL, - N_HSFS, - N_KERN, - N_LOFS, - N_MPC, - N_MQUEUE, - N_MVFS, - N_NFS, - N_NFS4, - N_PCFS, - N_SAMFS, - N_SANFS, - N_SDEV, - N_TMP, - N_UFS, - N_UNIX, - N_VXFS, - N_XFS, - N_ZFS + N_ANON_INODE }; /* @@ -203,9 +147,7 @@ struct mvfsnode { }; # define COMP_P const void -# define CWDLEN (MAXPATHLEN + 1) # define DEVINCR 1024 /* device table malloc() increment */ -# define DINAMEL 32 # define DIRTYPE dirent # if solaris >= 20501 @@ -222,19 +164,12 @@ struct mvfsnode { # define MAXEND 0x7fffffff # endif /* !defined(MAXEND) */ -# define MAXSEGS 100 /* maximum text segments */ # define MAXSYSCMDL MAXCOMLEN /* max system command name length */ -# define NETCLNML 8 -# define PROCMIN 5 /* processes that make a "good" scan */ # if defined(HASPROCFS) # define PR_ROOTINO 2 /* root inode for proc file system */ # endif /* defined(HASPROCFS) */ -# define PROCDFLT \ - 256 /* default size for local proc table -- \ - * MUST BE > 4!!! */ -# define PROCTRYLM 5 /* times to try to read proc table */ # define QSORT_P char # define STRNCPY_L int # define STRNML 32 /* stream name length (maximum) */ @@ -244,12 +179,6 @@ struct mvfsnode { * Enable large file support. */ -# if solaris >= 20600 -# define fstat fstat64 -# define lstat lstat64 -# define stat stat64 -# endif /* solaris>=20600 */ - # define SZOFFTYPE unsigned long long /* size and offset internal storage * type */ @@ -274,8 +203,6 @@ extern struct clone *Clone; extern major_t CloneMaj; -extern char **Fsinfo; -extern int Fsinfomax; extern int HaveCloneMaj; struct l_ino { @@ -300,12 +227,6 @@ struct l_vfs { INODETYPE fs_ino; /* file system inode number */ # endif /* defined(HASFSINO) */ -# if defined(HASMNTSTAT) - unsigned char mnt_stat; /* mount point stat() status: - * 0 = succeeded - * 1 = failed */ -# endif /* defined(HASMNTSTAT) */ - # if solaris >= 80000 nlink_t nlink; /* directory link count */ off_t size; /* directory size */ @@ -313,7 +234,6 @@ struct l_vfs { struct l_vfs *next; /* forward link */ }; -extern struct l_vfs *Lvfs; struct mounts { char *dir; /* directory (mounted on) */ @@ -337,11 +257,6 @@ struct mounts { off_t size; /* directory st_size */ # endif /* solaris>=80000 */ -# if defined(HASMNTSTAT) - unsigned char stat; /* mount point stat() status: - * 0 = succeeded - * 1 = failed */ -# endif /* defined(HASMNTSTAT) */ }; struct pseudo { @@ -350,15 +265,7 @@ struct pseudo { }; extern struct pseudo *Pseudo; -/* - * Solaris 11 sdev definitions - */ -# define SDVOP_IP 0 /* Sdev[] devipnet_vnodeops index */ -# define SDVOP_NET 1 /* Sdev[] devnet_vnodeops index */ -# define SDVOP_PTS 2 /* Sdev[] devpts_vnodeops index */ -# define SDVOP_VT 3 /* Sdev[] devvt_vnodeops index */ -# define SDVOP_NUM 4 /* number of Sdev[] entries */ struct sfile { char *aname; /* file name argument */ @@ -373,18 +280,8 @@ struct sfile { int f; /* file found flag */ struct sfile *next; /* forward link */ }; -extern int Unof; /* u_nofiles value */ -/* - * VxFS definitions - */ -# define VXVOP_FCL 0 /* Vvops[] vx_fcl_vnodeops_p index */ -# define VXVOP_FDD 1 /* Vvops[] fdd_vnops index */ -# define VXVOP_FDDCH 2 /* Vvops[] fdd_chain_vnops index */ -# define VXVOP_REG 3 /* Vvops[] vx_vnodeops index */ -# define VXVOP_REG_P 4 /* Vvops[] vx_vnodeops_p index */ -# define VXVOP_NUM 5 /* number of Vvops[] entries */ /* * Kernel name list definitions diff --git a/lib/dialects/sun/dmnt.c b/lib/dialects/sun/dmnt.c index 1b6cc8cf..511e7252 100644 --- a/lib/dialects/sun/dmnt.c +++ b/lib/dialects/sun/dmnt.c @@ -100,7 +100,6 @@ struct mounts *readmnt(struct lsof_context *ctx) { int procfs = 0; #endif /* defined(HASPROCFS) */ - unsigned char stat; if (Lmi || Lmist) return (Lmi); @@ -185,7 +184,6 @@ struct mounts *readmnt(struct lsof_context *ctx) { ))) continue; - stat = 1; } else if (statsafely(ctx, dn, &sb)) { if (dopt) { if (!(dopte = getmntdev(ctx, dopt, devl, &sb, @@ -219,9 +217,7 @@ struct mounts *readmnt(struct lsof_context *ctx) { } if (!dopt) continue; - stat = 1; - } else - stat = 0; + } /* * Allocate and fill a local mount structure. */ @@ -246,10 +242,6 @@ struct mounts *readmnt(struct lsof_context *ctx) { mtp->size = sb.st_size; #endif /* solaris>=80000 */ -#if defined(HASMNTSTAT) - mtp->stat = stat; -#endif /* defined(HASMNTSTAT) */ - #if defined(HASPROCFS) if (strcmp(sb.st_fstype, HASPROCFS) == 0) { diff --git a/lib/dialects/sun/dnode.c b/lib/dialects/sun/dnode.c index b7263a0b..b441dc64 100644 --- a/lib/dialects/sun/dnode.c +++ b/lib/dialects/sun/dnode.c @@ -1,9 +1,8 @@ /* - * dnode.c - Solaris /proc node compatibility shims. + * dnode.c - Solaris procfs initialization and IP state tables. * - * The procfs backend fills lfile entries directly from /proc. These - * functions remain only for the mount-table and file-selection support - * code; they never read kernel memory. + * File data comes directly from /proc; these functions only provide the + * lsof core hooks and the TCP/UDP state tables. No kernel memory is read. */ /* @@ -40,12 +39,6 @@ void initialize(struct lsof_context *ctx) { (void)ctx; } -/* Zone-name filtering is not available in the procfs backend. */ -int enter_zone_arg(struct lsof_context *ctx, char *zn) { - (void)ctx; - (void)zn; - return (1); -} /* * build_IPstates() - build the TCP and UDP state tables diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index e66b46d5..4e975c5d 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -258,7 +258,8 @@ static void set_lock_from_proc(const char *path, struct lfile *lf, fl.l_start = 0; fl.l_len = 0; if (fcntl(fd, F_GETLK, &fl) == 0 && fl.l_type != F_UNLCK) { - int full = (fl.l_start == 0 && (fl.l_len == 0 || fl.l_len >= size)); + int full = (fl.l_start == 0 && + (fl.l_len == 0 || fl.l_len >= (off_t)size)); if (fl.l_type == F_RDLCK) lf->lock = full ? LSOF_LOCK_READ_FULL : LSOF_LOCK_READ_PARTIAL; else if (fl.l_type == F_WRLCK) @@ -503,7 +504,7 @@ void gather_proc_info(struct lsof_context *ctx) { struct dirent *dp, *fdp; psinfo_t ps; char path[MAXPATHLEN + 1]; - char fdpath[MAXPATHLEN + 1]; + char fdpath[MAXPATHLEN + 64]; char pspath[MAXPATHLEN + 1]; int pid; diff --git a/lib/dialects/sun/dproto.h b/lib/dialects/sun/dproto.h index ea220cf7..e582396d 100644 --- a/lib/dialects/sun/dproto.h +++ b/lib/dialects/sun/dproto.h @@ -57,11 +57,3 @@ extern struct hostent *gethostbyname2(const char *nm, int proto); extern void read_clone(struct lsof_context *ctx); - -#if solaris < 20500 -extern int get_max_fd(void); -#endif /* solaris<20500 */ - -#if defined(WILLDROPGID) -extern void restoregid(struct lsof_context *ctx); -#endif /* defined(WILLDROPGID) */ diff --git a/lib/dialects/sun/dstore.c b/lib/dialects/sun/dstore.c index ed2b8e87..e9a71bb8 100644 --- a/lib/dialects/sun/dstore.c +++ b/lib/dialects/sun/dstore.c @@ -39,68 +39,13 @@ static char copyright[] = * Global storage definitions */ - struct clone *Clone = NULL; /* clone list */ major_t CloneMaj; /* clone major device number */ -char **Fsinfo = NULL; /* file system information */ -int Fsinfomax = 0; /* maximum file system type */ int HaveCloneMaj = 0; /* clone major device number has * been identified and is in * CloneMaj */ -struct l_vfs *Lvfs = NULL; /* local vfs structure table */ -struct netclone *Netclone = NULL; /* net clone devices from - * /devices/pseudo */ - -#if defined(HASFSTRUCT) -/* - * Pff_tab[] - table for printing file flags - */ - -struct pff_tab Pff_tab[] = {{(long)FREAD, FF_READ}, - {(long)FWRITE, FF_WRITE}, - {(long)FNDELAY, FF_NDELAY}, - {(long)FAPPEND, FF_APPEND}, - {(long)FSYNC, FF_SYNC}, - -# if defined(FREVOKED) - {(long)FREVOKED, FF_REVOKED}, -# endif /* defined(FREVOKED) */ - - {(long)FDSYNC, FF_DSYNC}, - {(long)FRSYNC, FF_RSYNC}, - -# if defined(FOFFMAX) - {(long)FOFFMAX, FF_LARGEFILE}, -# endif /* defined(FFOFFMAX) */ - - {(long)FNONBLOCK, FF_NBLOCK}, - {(long)FNOCTTY, FF_NOCTTY}, - {(long)FASYNC, FF_ASYNC}, - {(long)FNODSYNC, FF_NODSYNC}, - {(long)0, NULL}}; - -/* - * Pof_tab[] - table for print process open file flags - */ - -struct pff_tab Pof_tab[] = { - -# if defined(UF_EXCLOSE) - {(long)UF_EXCLOSE, POF_CLOEXEC}, -# endif /* defined(UF_EXCLOSE) */ - -# if defined(FD_CLOEXEC) - {(long)FD_CLOEXEC, POF_CLOEXEC}, -# endif /* defined(FD_CLOEXEC) */ - -# if defined(UF_FDLOCK) - {(long)UF_FDLOCK, POF_FDLOCK}, -# endif /* defined(UF_FDLOCK) */ - {(long)0, NULL}}; -#endif /* defined(HASFSTRUCT) */ struct pseudo *Pseudo = NULL; /* non-clone devices from * /devices/pseudo */ -int Unof; /* u_nofiles value */ diff --git a/lib/dialects/sun/machine.h b/lib/dialects/sun/machine.h index cfdcacb2..021be851 100644 --- a/lib/dialects/sun/machine.h +++ b/lib/dialects/sun/machine.h @@ -55,15 +55,6 @@ # define DEVDEV_PATH "/devices" # endif /* solaris<100000 */ -/* - * GET_MAX_FD is defined for those dialects that provide a function other than - * getdtablesize() to obtain the maximum file descriptor number plus one. - */ - -# if solaris < 20500 -# define GET_MAX_FD get_max_fd -# endif /* solaris<20500 */ - /* * HASAOPT is defined for those dialects that have AFS support; it specifies * that the default path to an alternate AFS kernel name list file may be @@ -180,13 +171,6 @@ /* #define HASINTSIGNAL 1 */ -/* - * HASKERNIDCK is defined for those dialects that support the comparison of - * the build to running kernel identity. - */ - -# define HASKERNIDCK 1 - /* * HASKOPT is defined for those dialects that support the -k option of * reading the kernel's name list from an optional file. @@ -539,14 +523,6 @@ /* #define WARNINGSTATE 1 warnings are enabled by default */ -/* - * WILLDROPGID is defined for those dialects whose lsof executable runs - * setgid(not_real_GID) and whose setgid power can be relinquished after - * the dialect's initialize() function has been executed. - */ - -# define WILLDROPGID 1 - /* * zeromem is a macro that uses bzero or memset. */ diff --git a/src/dialects/sun/dprint.c b/src/dialects/sun/dprint.c index 2e9e2f84..691ac461 100644 --- a/src/dialects/sun/dprint.c +++ b/src/dialects/sun/dprint.c @@ -1,3 +1,29 @@ +/* + * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana + * 47907. All rights reserved. + * + * Written by Victor A. Abell + * + * This software is not subject to any license of the American Telephone + * and Telegraph Company or the Regents of the University of California. + * + * Permission is granted to anyone to use this software for any purpose on + * any computer system, and to alter it and redistribute it freely, subject + * to the following restrictions: + * + * 1. Neither the authors nor Purdue University are responsible for any + * consequences of the use of this software. + * + * 2. The origin of this software must not be misrepresented, either by + * explicit claim or by omission. Credit to the authors and Purdue + * University must appear in documentation and sources. + * + * 3. Altered versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 4. This notice may not be removed or altered. + */ + /* * dprint.c - Solaris CLI printing functions */ @@ -32,7 +58,8 @@ void print_tcptpi(struct lsof_context *ctx, /* context */ case 1: /* TPI */ if (!UdpSt) (void)build_IPstates(ctx); - if ((u = Lf->lts.state.ui + UdpStOff) < 0 || u >= UdpNstates) { + u = Lf->lts.state.ui + UdpStOff; + if (u >= (unsigned)UdpNstates) { (void)snpf(sbuf, sizeof(sbuf), "UNKNOWN_UDP_STATE_%u", Lf->lts.state.ui); cp = sbuf; From a08564c846103715467286d2a986f596246898ec Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 17:30:31 +0800 Subject: [PATCH 12/18] sun: assume Solaris 11 and enable socket/name tests --- lib/dialects/sun/dfile.c | 33 -------------------- lib/dialects/sun/dlsof.h | 30 ++++-------------- lib/dialects/sun/dmnt.c | 6 ++-- lib/dialects/sun/dproc.c | 37 +++++++++++++++++++--- lib/dialects/sun/machine.h | 38 +++++------------------ tests/case-23-multi-address-hostname.bash | 32 +++++++++++++++++-- tests/case-24-dual-stack-socket.bash | 4 +-- 7 files changed, 80 insertions(+), 100 deletions(-) diff --git a/lib/dialects/sun/dfile.c b/lib/dialects/sun/dfile.c index a7f00f0d..ce52aa95 100644 --- a/lib/dialects/sun/dfile.c +++ b/lib/dialects/sun/dfile.c @@ -76,9 +76,6 @@ static char copyright[] = * minor raw device, and inode, modulo * mod (mod must be a power of 2) */ -#if solaris < 20500 -#endif /* solaris<20500 */ - /* * hashSfile() - hash Sfile entries for use in is_file_named() searches */ @@ -375,36 +372,6 @@ int is_file_named(struct lsof_context *ctx, /* context */ return (0); } -#if defined(HASPRINTDEV) -/* - * print_dev() - print device - */ - -char *print_dev(struct lfile *lf, /* file whose device is to be printed */ - dev_t *dev) /* device to be printed */ -{ - static char buf[128]; - /* - * Avoid the Solaris major() and minor() functions from makedev(3C) to get - * printable major/minor numbers. - * - * We would like to use the L_MAXMAJ definition from all - * the time, but it's not always correct in all versions of Solaris. - */ - (void)snpf(buf, sizeof(buf), "%d,%d", - (int)((*dev >> L_BITSMINOR) & - -# if solaris >= 20501 - L_MAXMAJ -# else /* solaris<20501 */ - 0x3fff -# endif /* solaris>=20501 */ - - ), - (int)(*dev & L_MAXMIN)); - return (buf); -} -#endif /* defined(HASPRINTDEV) */ #if defined(HASIPv6) diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index 655775f6..2a90a603 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -150,12 +150,6 @@ struct mvfsnode { # define DEVINCR 1024 /* device table malloc() increment */ # define DIRTYPE dirent -# if solaris >= 20501 -# define KMEM "/dev/mem" -# else /* solaris<20501 */ -# define KMEM "/dev/kmem" -# endif /* solaris>=20501 */ - # define MALLOC_P char # define FREE_P MALLOC_P # define MALLOC_S unsigned @@ -174,18 +168,12 @@ struct mvfsnode { # define STRNCPY_L int # define STRNML 32 /* stream name length (maximum) */ -# if solaris >= 20501 /* - * Enable large file support. + * Large file support. */ - -# define SZOFFTYPE unsigned long long -/* size and offset internal storage - * type */ -# define SZOFFPSPEC \ - "ll" /* SZOFFTYPE printf specification \ - * modifier */ -# endif /* solaris>=20501 */ +# define SZOFFTYPE unsigned long long +/* size and offset internal storage type */ +# define SZOFFPSPEC "ll" /* SZOFFTYPE printf specification modifier */ /* @@ -227,10 +215,8 @@ struct l_vfs { INODETYPE fs_ino; /* file system inode number */ # endif /* defined(HASFSINO) */ -# if solaris >= 80000 nlink_t nlink; /* directory link count */ off_t size; /* directory size */ -# endif /* solaris>=80000 */ struct l_vfs *next; /* forward link */ }; @@ -252,10 +238,8 @@ struct mounts { char *fstype; /* file system type */ # endif /* defined(HASFSTYPE) */ -# if solaris >= 80000 nlink_t nlink; /* directory st_nlink */ off_t size; /* directory st_size */ -# endif /* solaris>=80000 */ }; @@ -315,10 +299,8 @@ struct sfile { # define CVFS_DEVSAVE 1 -# if solaris >= 80000 -# define CVFS_NLKSAVE 1 -# define CVFS_SZSAVE 1 -# endif /* solaris>=80000 */ +# define CVFS_NLKSAVE 1 +# define CVFS_SZSAVE 1 /* * Definitions for rnch.c diff --git a/lib/dialects/sun/dmnt.c b/lib/dialects/sun/dmnt.c index 511e7252..1f6c7d0c 100644 --- a/lib/dialects/sun/dmnt.c +++ b/lib/dialects/sun/dmnt.c @@ -59,14 +59,14 @@ static char *getmntdev(struct lsof_context *ctx, /* context */ if (!(opte = x2dev(o + l + 1, &s->st_dev))) return ((char *)NULL); -#if solaris >= 70000 && L_BITSMAJOR != L_BITSMAJOR32 +#if L_BITSMAJOR != L_BITSMAJOR32 /* * If this is a Solaris 7 system with a 64 bit kernel, convert the 32 bit * device number to a 64 bit device number. */ s->st_dev = (((s->st_dev >> L_BITSMINOR32) & L_MAXMAJ32) << L_BITSMINOR) | (s->st_dev & L_MAXMIN32); -#endif /* solaris>=70000 && L_BITSMAJOR!=L_BITSMAJOR32 */ +#endif /* L_BITSMAJOR != L_BITSMAJOR32 */ s->st_mode = S_IFDIR | 0777; @@ -237,10 +237,8 @@ struct mounts *readmnt(struct lsof_context *ctx) { mtp->inode = (INODETYPE)sb.st_ino; mtp->mode = sb.st_mode; -#if solaris >= 80000 mtp->nlink = sb.st_nlink; mtp->size = sb.st_size; -#endif /* solaris>=80000 */ #if defined(HASPROCFS) if (strcmp(sb.st_fstype, HASPROCFS) == 0) { diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index 4e975c5d..10f7f1af 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -84,6 +84,7 @@ static int nm2id(char *nm, int *id) { #define SUN_MISC_PATHNAME 0 #define SUN_MISC_SOCKETNAME 1 #define SUN_MISC_PEERSOCKNAME 2 +#define SUN_MISC_SOCKOPT_TYPE 10 struct sun_fdinfo { int pr_fd; @@ -105,6 +106,7 @@ struct sun_fdinfo { pid_t pr_peerpid; int pr_filler[25]; char pr_peername[16]; + int sock_type; }; static void enter_path(struct lsof_context *ctx, enum lsof_fd_type fdt, @@ -182,7 +184,8 @@ static int read_fdinfo(const char *p, struct sun_fdinfo *fi, char *path, (void)close(fd); if (n < SUN_FDINFO_HDR) return (1); - memcpy(fi, buf, sizeof(*fi)); + memcpy(fi, buf, SUN_FDINFO_CORE); + fi->sock_type = -1; /* * The fixed core is 196 bytes. Some Solaris fdinfo records have four * bytes of alignment before the misc array; detect which by validating @@ -227,6 +230,13 @@ static int read_fdinfo(const char *p, struct sun_fdinfo *fi, char *path, *pnl = dlen; } break; + case SUN_MISC_SOCKOPT_TYPE: + if (dlen >= sizeof(uint_t)) { + uint_t st; + memcpy(&st, data, sizeof(st)); + fi->sock_type = (int)st; + } + break; default: break; } @@ -370,9 +380,28 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, Lf->type = LSOF_FILE_SOCKET; break; } - Lf->lts.type = 1; - if (Selflags & SELNET) - Lf->sf |= SELNET; + if ((Lf->li[0].af == AF_INET || Lf->li[0].af == AF_INET6) && + fi.sock_type == SOCK_STREAM) { + (void)strncpy(Lf->iproto, "TCP", sizeof(Lf->iproto) - 1); + Lf->iproto[sizeof(Lf->iproto) - 1] = '\0'; + Lf->inp_ty = 2; + Lf->lts.type = 0; + } else if ((Lf->li[0].af == AF_INET || + Lf->li[0].af == AF_INET6) && + fi.sock_type == SOCK_DGRAM) { + (void)strncpy(Lf->iproto, "UDP", sizeof(Lf->iproto) - 1); + Lf->iproto[sizeof(Lf->iproto) - 1] = '\0'; + Lf->inp_ty = 2; + Lf->lts.type = 1; + } else + Lf->lts.type = 1; + if (Selflags & SELNET) { + int family_ok = !FnetTy || + (FnetTy == 4 && Lf->li[0].af == AF_INET) || + (FnetTy == 6 && Lf->li[0].af == AF_INET6); + if (family_ok) + Lf->sf |= SELNET; + } if (Selflags & SELUNX) Lf->sf |= SELUNX; #if defined(SELNA) diff --git a/lib/dialects/sun/machine.h b/lib/dialects/sun/machine.h index 021be851..2ec323ba 100644 --- a/lib/dialects/sun/machine.h +++ b/lib/dialects/sun/machine.h @@ -40,20 +40,14 @@ * can be used to obtain a CLIENT handle in lieu of clnttcp_create(). */ -# if solaris >= 20501 -# define CAN_USE_CLNT_CREATE 1 -# endif /* solaris>=20501 */ +# define CAN_USE_CLNT_CREATE 1 /* * DEVDEV_PATH defines the path to the directory that contains device * nodes. */ -# if solaris < 100000 -# define DEVDEV_PATH "/dev" -# else /* solaris>=100000 */ -# define DEVDEV_PATH "/devices" -# endif /* solaris<100000 */ +# define DEVDEV_PATH "/devices" /* * HASAOPT is defined for those dialects that have AFS support; it specifies @@ -258,9 +252,6 @@ * called from print_file(). */ -# if solaris < 100000 -# define HASPRINTDEV print_dev -# endif /* solaris<100000 */ /* #define HASPRINTINO print_ino? */ /* #define HASPRINTNM print_nm? */ @@ -383,11 +374,9 @@ * options. */ -# if solaris >= 20600 -# define HASSOOPT 1 /* has socket option information */ +# define HASSOOPT 1 /* has socket option information */ /* #define HASSOSTATE 1 has socket state information */ -# define HASTCPOPT 1 /* has TCP options or flags */ -# endif /* solaris>=20600 */ +# define HASTCPOPT 1 /* has TCP options or flags */ /* * Define HASSPECDEVD to be the name of a function that handles the results @@ -416,18 +405,14 @@ * TCP/TPI Recv-Q and Send-Q values produced by netstat. */ -# if solaris == 20300 || solaris >= 20500 -# define HASTCPTPIQ 1 -# endif /* solaris==20300 || solaris>=20500 */ +# define HASTCPTPIQ 1 /* * HASTCPTPIW is defined for dialects where it is possible to report the * TCP/TPI send and receive window sizes produced by netstat. */ -# if solaris == 20300 || solaris >= 20500 -# define HASTCPTPIW 1 -# endif /* solaris==20300 || solaris>=20500 */ +# define HASTCPTPIW 1 /* * HASTCPUDPSTATE is defined for dialects that have TCP and UDP state @@ -464,13 +449,9 @@ * These are defined here, because they must be used in dlsof.h. */ -# if solaris >= 20501 -# define INODETYPE unsigned long long +# define INODETYPE unsigned long long /* inode number internal storage type */ -# define INODEPSPEC \ - "ll" /* INODETYPE printf specification \ - * modifier */ -# endif /* solaris>=20501 */ +# define INODEPSPEC "ll" /* INODETYPE printf specification modifier */ /* * UID_ARG defines the size of a User ID number when it is passed @@ -500,9 +481,6 @@ /* #define USE_LIB_READMNT 1 rmnt.c */ /* #define USE_LIB_RNAM 1 rnam.c */ -# if solaris < 90000 -# define USE_LIB_RNCH 1 /* rnch.c */ -# endif /* solaris<90000 */ /* #define USE_LIB_RNMH 1 rnmh.c */ /* #define USE_LIB_SNPF 1 snpf.c */ diff --git a/tests/case-23-multi-address-hostname.bash b/tests/case-23-multi-address-hostname.bash index a0b8a875..37933cef 100755 --- a/tests/case-23-multi-address-hostname.bash +++ b/tests/case-23-multi-address-hostname.bash @@ -12,8 +12,8 @@ if ! command -v python3 >/dev/null 2>&1; then exit 77 fi -host=$(hostname) -naddr=$(python3 - "$host" <<'PY' +count_addresses() { + python3 - "$1" <<'PY' import socket import sys @@ -27,7 +27,33 @@ for family in (socket.AF_INET, socket.AF_INET6): count = max(count, len({info[4][0] for info in infos})) print(count) PY -) +} + +host=$(hostname) +naddr=$(count_addresses "$host") +hosts_backup= +restore_hosts() { + if [ -n "$hosts_backup" ]; then + cat "$hosts_backup" > /etc/hosts + rm -f "$hosts_backup" + fi +} +trap restore_hosts EXIT HUP INT TERM + +if [ "${naddr:-0}" -lt 2 ] && [ "$(id -u)" -eq 0 ] && [ -w /etc/hosts ]; then + # Make the regression deterministic without relying on external DNS. + # The temporary name has two IPv4 addresses; only name resolution is + # needed by this test, not connectivity. + hosts_backup=$(mktemp) || hosts_backup= + if [ -n "$hosts_backup" ] && cp /etc/hosts "$hosts_backup"; then + host=lsof-multi-address.test + printf '127.0.0.1 %s\n127.0.0.2 %s\n' "$host" "$host" \ + >> /etc/hosts + naddr=$(count_addresses "$host") + else + restore_hosts + fi +fi if [ "${naddr:-0}" -lt 2 ]; then echo "SKIP: $host resolves to fewer than two addresses in one family" \ diff --git a/tests/case-24-dual-stack-socket.bash b/tests/case-24-dual-stack-socket.bash index 5fbcc63e..ddae98ae 100755 --- a/tests/case-24-dual-stack-socket.bash +++ b/tests/case-24-dual-stack-socket.bash @@ -9,9 +9,9 @@ source tests/common.bash case "${dialect:-}" in - linux|darwin|freebsd|netbsd|openbsd) ;; + linux|darwin|freebsd|netbsd|openbsd|solaris) ;; *) - echo "SKIP: dual-stack socket test is only for the BSD and Linux dialects" \ + echo "SKIP: dual-stack socket test is only for the supported Unix dialects" \ >> "$report" exit 77 ;; From e6e85585d54aacb0eb45215fb8a5ced805c909d6 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 17:49:05 +0800 Subject: [PATCH 13/18] sun: derive socket protocol from fdinfo on illumos --- lib/dialects/sun/dproc.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index 10f7f1af..cd1743bc 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -85,6 +85,7 @@ static int nm2id(char *nm, int *id) { #define SUN_MISC_SOCKETNAME 1 #define SUN_MISC_PEERSOCKNAME 2 #define SUN_MISC_SOCKOPT_TYPE 10 +#define SUN_MISC_TCP_CONGESTION 11 struct sun_fdinfo { int pr_fd; @@ -237,6 +238,9 @@ static int read_fdinfo(const char *p, struct sun_fdinfo *fi, char *path, fi->sock_type = (int)st; } break; + case SUN_MISC_TCP_CONGESTION: + fi->sock_type = SOCK_STREAM; + break; default: break; } @@ -393,6 +397,12 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, Lf->iproto[sizeof(Lf->iproto) - 1] = '\0'; Lf->inp_ty = 2; Lf->lts.type = 1; + } else if (Lf->li[0].af == AF_INET || + Lf->li[0].af == AF_INET6) { + (void)strncpy(Lf->iproto, "UDP", sizeof(Lf->iproto) - 1); + Lf->iproto[sizeof(Lf->iproto) - 1] = '\0'; + Lf->inp_ty = 2; + Lf->lts.type = 1; } else Lf->lts.type = 1; if (Selflags & SELNET) { From f4b8a9cb9413c9967795e6cc2b0c336ce8db532e Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 18:12:40 +0800 Subject: [PATCH 14/18] sun: remove dead feature conditionals --- lib/dialects/sun/ddev.c | 12 -- lib/dialects/sun/dfile.c | 2 - lib/dialects/sun/dproc.c | 23 +--- lib/dialects/sun/dproto.h | 7 +- lib/dialects/sun/machine.h | 5 - src/dialects/sun/dprint.c | 260 ------------------------------------- 6 files changed, 5 insertions(+), 304 deletions(-) diff --git a/lib/dialects/sun/ddev.c b/lib/dialects/sun/ddev.c index 53265a04..f35e878b 100644 --- a/lib/dialects/sun/ddev.c +++ b/lib/dialects/sun/ddev.c @@ -271,11 +271,7 @@ void read_clone(struct lsof_context *ctx) { Error(ctx); } -#if defined(USE_STAT) - if (stat(fp, &sb) != 0) -#else /* !defined(USE_STAT) */ if (lstat(fp, &sb) != 0) -#endif /* defined(USE_STAT) */ { if (!Fwarn) { @@ -482,11 +478,7 @@ void readdev(struct lsof_context *ctx, /* context */ Error(ctx); } -#if defined(USE_STAT) - if (stat(fp, &sb) != 0) -#else /* !defined(USE_STAT) */ if (lstat(fp, &sb) != 0) -#endif /* defined(USE_STAT) */ { if (errno == ENOENT) /* symbolic link to nowhere? */ @@ -998,11 +990,7 @@ int vfy_dev(struct lsof_context *ctx, /* context */ if (!DCunsafe || dp->v) return (1); -# if defined(USE_STAT) - if (stat(dp->name, &sb) != 0 -# else /* !defined(USE_STAT) */ if (lstat(dp->name, &sb) != 0 -# endif /* defined(USE_STAT) */ || dp->rdev != sb.st_rdev || dp->inode != (INODETYPE)sb.st_ino) { (void)rereaddev(ctx); diff --git a/lib/dialects/sun/dfile.c b/lib/dialects/sun/dfile.c index ce52aa95..2770c98b 100644 --- a/lib/dialects/sun/dfile.c +++ b/lib/dialects/sun/dfile.c @@ -372,8 +372,6 @@ int is_file_named(struct lsof_context *ctx, /* context */ return (0); } - - #if defined(HASIPv6) /* * gethostbyname2() -- an RFC2133-compatible get-host-by-name-two function diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index cd1743bc..dde45d49 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -134,7 +134,6 @@ static void set_sockaddr(struct lfile *lf, int which, const void *p, size_t n, lf->li[which].ia.a4 = sin->sin_addr; break; } -#if defined(AF_INET6) case AF_INET6: { const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)p; if (n < sizeof(*sin6)) @@ -144,8 +143,6 @@ static void set_sockaddr(struct lfile *lf, int which, const void *p, size_t n, lf->li[which].ia.a6 = sin6->sin6_addr; break; } -#endif -#if defined(AF_UNIX) case AF_UNIX: { const struct sun_sockaddr_un *unp = (const struct sun_sockaddr_un *)p; size_t l; @@ -164,7 +161,6 @@ static void set_sockaddr(struct lfile *lf, int which, const void *p, size_t n, } break; } -#endif default: break; } @@ -253,8 +249,7 @@ static int read_fdinfo(const char *p, struct sun_fdinfo *fi, char *path, } static void set_lock_from_proc(const char *path, struct lfile *lf, - int locktype, SZOFFTYPE size) { -#if defined(F_GETLK) + int locktype, SZOFFTYPE size) { struct flock fl; int fd; @@ -280,12 +275,6 @@ static void set_lock_from_proc(const char *path, struct lfile *lf, lf->lock = full ? LSOF_LOCK_WRITE_FULL : LSOF_LOCK_WRITE_PARTIAL; } (void)close(fd); -#else - (void)path; - (void)lf; - (void)locktype; - (void)size; -#endif } static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, @@ -370,16 +359,12 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, case AF_INET: Lf->type = LSOF_FILE_IPV4; break; -#if defined(AF_INET6) case AF_INET6: Lf->type = LSOF_FILE_IPV6; break; -#endif -#if defined(AF_UNIX) case AF_UNIX: Lf->type = LSOF_FILE_UNIX; break; -#endif default: Lf->type = LSOF_FILE_SOCKET; break; @@ -414,26 +399,20 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, } if (Selflags & SELUNX) Lf->sf |= SELUNX; -#if defined(SELNA) if ((Selflags & SELNA) && Nwad) { unsigned char *la = (unsigned char *)NULL; unsigned char *fa = (unsigned char *)NULL; if (Lf->li[0].af == AF_INET) la = (unsigned char *)&Lf->li[0].ia.a4; -#if defined(AF_INET6) else if (Lf->li[0].af == AF_INET6) la = (unsigned char *)&Lf->li[0].ia.a6; -#endif if (Lf->li[1].af == AF_INET) fa = (unsigned char *)&Lf->li[1].ia.a4; -#if defined(AF_INET6) else if (Lf->li[1].af == AF_INET6) fa = (unsigned char *)&Lf->li[1].ia.a6; -#endif ent_inaddr(ctx, la, Lf->li[0].p, fa, Lf->li[1].p, Lf->li[0].af ? Lf->li[0].af : Lf->li[1].af); } -#endif } } if (Lf && Lf->sf) diff --git a/lib/dialects/sun/dproto.h b/lib/dialects/sun/dproto.h index e582396d..7d923a95 100644 --- a/lib/dialects/sun/dproto.h +++ b/lib/dialects/sun/dproto.h @@ -49,11 +49,12 @@ extern void clr_sect(struct lsof_context *ctx); extern int rw_pseudo_sect(struct lsof_context *ctx, int m); #endif /* defined(HASDCACHE) */ -#if defined(HASIPv6) -extern struct hostent *gethostbyname2(const char *nm, int proto); -#endif /* defined(HASIPv6) */ extern void read_clone(struct lsof_context *ctx); + +#if defined(HASIPv6) +extern struct hostent *gethostbyname2(const char *nm, int proto); +#endif /* defined(HASIPv6) */ diff --git a/lib/dialects/sun/machine.h b/lib/dialects/sun/machine.h index 2ec323ba..3e7b0752 100644 --- a/lib/dialects/sun/machine.h +++ b/lib/dialects/sun/machine.h @@ -374,9 +374,6 @@ * options. */ -# define HASSOOPT 1 /* has socket option information */ -/* #define HASSOSTATE 1 has socket state information */ -# define HASTCPOPT 1 /* has TCP options or flags */ /* * Define HASSPECDEVD to be the name of a function that handles the results @@ -405,14 +402,12 @@ * TCP/TPI Recv-Q and Send-Q values produced by netstat. */ -# define HASTCPTPIQ 1 /* * HASTCPTPIW is defined for dialects where it is possible to report the * TCP/TPI send and receive window sizes produced by netstat. */ -# define HASTCPTPIW 1 /* * HASTCPUDPSTATE is defined for dialects that have TCP and UDP state diff --git a/src/dialects/sun/dprint.c b/src/dialects/sun/dprint.c index 691ac461..c69590ba 100644 --- a/src/dialects/sun/dprint.c +++ b/src/dialects/sun/dprint.c @@ -77,269 +77,9 @@ void print_tcptpi(struct lsof_context *ctx, /* context */ } } -#if defined(HASTCPTPIQ) - if (Ftcptpi & TCPTPI_QUEUES) { - if (Lf->lts.rqs) { - if (Ffield) - putchar(LSOF_FID_TCPTPI); - else { - if (ps) - putchar(' '); - else - putchar('('); - } - (void)printf("QR=%lu", Lf->lts.rq); - if (Ffield) - putchar(Terminator); - ps++; - } - if (Lf->lts.sqs) { - if (Ffield) - putchar(LSOF_FID_TCPTPI); - else { - if (ps) - putchar(' '); - else - putchar('('); - } - (void)printf("QS=%lu", Lf->lts.sq); - if (Ffield) - putchar(Terminator); - ps++; - } - } -#endif /* defined(HASTCPTPIQ) */ - -#if defined(HASSOOPT) - if (Ftcptpi & TCPTPI_FLAGS) { - int opt; - - if ((opt = Lf->lts.opt) || Lf->lts.pqlens || Lf->lts.qlens || - Lf->lts.qlims || Lf->lts.rbszs || Lf->lts.sbsz) { - char sep = ' '; - - if (Ffield) - sep = LSOF_FID_TCPTPI; - else if (!ps) - sep = '('; - (void)printf("%cSO", sep); - ps++; - sep = '='; -# if defined(SO_BROADCAST) - if (opt & SO_BROADCAST) { - (void)printf("%cBROADCAST", sep); - opt &= ~SO_BROADCAST; - sep = ','; - } -# endif /* defined(SO_BROADCAST) */ - -# if defined(SO_DEBUG) - if (opt & SO_DEBUG) { - (void)printf("%cDEBUG", sep); - opt &= ~SO_DEBUG; - sep = ','; - } -# endif /* defined(SO_DEBUG) */ -# if defined(SO_DGRAM_ERRIND) - if (opt & SO_DGRAM_ERRIND) { - (void)printf("%cDGRAM_ERRIND", sep); - opt &= ~SO_DGRAM_ERRIND; - sep = ','; - } -# endif /* defined(SO_DGRAM_ERRIND) */ - -# if defined(SO_DONTROUTE) - if (opt & SO_DONTROUTE) { - (void)printf("%cDONTROUTE", sep); - opt &= ~SO_DONTROUTE; - sep = ','; - } -# endif /* defined(SO_DONTROUTE) */ - -# if defined(SO_KEEPALIVE) - if (opt & SO_KEEPALIVE) { - (void)printf("%cKEEPALIVE", sep); - if (Lf->lts.kai) - (void)printf("=%d", Lf->lts.kai); - opt &= ~SO_KEEPALIVE; - sep = ','; - } -# endif /* defined(SO_KEEPALIVE) */ - -# if defined(SO_LINGER) - if (opt & SO_LINGER) { - (void)printf("%cLINGER", sep); - if (Lf->lts.ltm) - (void)printf("=%d", Lf->lts.ltm); - opt &= ~SO_LINGER; - sep = ','; - } -# endif /* defined(SO_LINGER) */ - -# if defined(SO_OOBINLINE) - if (opt & SO_OOBINLINE) { - (void)printf("%cOOBINLINE", sep); - opt &= ~SO_OOBINLINE; - sep = ','; - } -# endif /* defined(SO_OOBINLINE) */ - - if (Lf->lts.pqlens) { - (void)printf("%cPQLEN=%u", sep, Lf->lts.pqlen); - sep = ','; - } - if (Lf->lts.qlens) { - (void)printf("%cQLEN=%u", sep, Lf->lts.qlen); - sep = ','; - } - if (Lf->lts.qlims) { - (void)printf("%cQLIM=%u", sep, Lf->lts.qlim); - sep = ','; - } - if (Lf->lts.rbszs) { - (void)printf("%cRCVBUF=%lu", sep, Lf->lts.rbsz); - sep = ','; - } - -# if defined(SO_REUSEADDR) - if (opt & SO_REUSEADDR) { - (void)printf("%cREUSEADDR", sep); - opt &= ~SO_REUSEADDR; - sep = ','; - } -# endif /* defined(SO_REUSEADDR) */ - - if (Lf->lts.sbszs) { - (void)printf("%cSNDBUF=%lu", sep, Lf->lts.sbsz); - sep = ','; - } - -# if defined(SO_TIMESTAMP) - if (opt & SO_TIMESTAMP) { - (void)printf("%cTIMESTAMP", sep); - opt &= ~SO_TIMESTAMP; - sep = ','; - } -# endif /* defined(SO_TIMESTAMP) */ -# if defined(SO_USELOOPBACK) - if (opt & SO_USELOOPBACK) { - (void)printf("%cUSELOOPBACK", sep); - opt &= ~SO_USELOOPBACK; - sep = ','; - } -# endif /* defined(SO_USELOOPBACK) */ - - if (opt) - (void)printf("%cUNKNOWN=%#x", sep, opt); - if (Ffield) - putchar(Terminator); - } - } -#endif /* defined(HASSOOPT) */ - -#if defined(HASTCPOPT) - if (Ftcptpi & TCPTPI_FLAGS) { - int topt; - - if ((topt = Lf->lts.topt) || Lf->lts.msss) { - char sep = ' '; - - if (Ffield) - sep = LSOF_FID_TCPTPI; - else if (!ps) - sep = '('; - (void)printf("%cTF", sep); - ps++; - sep = '='; - -# if defined(TF_ACKNOW) - if (topt & TF_ACKNOW) { - (void)printf("%cACKNOW", sep); - topt &= ~TF_ACKNOW; - sep = ','; - } -# endif /* defined(TF_ACKNOW) */ - -# if defined(TF_DELACK) - if (topt & TF_DELACK) { - (void)printf("%cDELACK", sep); - topt &= ~TF_DELACK; - sep = ','; - } -# endif /* defined(TF_DELACK) */ - - if (Lf->lts.msss) { - (void)printf("%cMSS=%lu", sep, Lf->lts.mss); - sep = ','; - } - -# if defined(TF_NODELAY) - if (topt & TF_NODELAY) { - (void)printf("%cNODELAY", sep); - topt &= ~TF_NODELAY; - sep = ','; - } -# endif /* defined(TF_NODELAY) */ - -# if defined(TF_NOOPT) - if (topt & TF_NOOPT) { - (void)printf("%cNOOPT", sep); - topt &= ~TF_NOOPT; - sep = ','; - } -# endif /* defined(TF_NOOPT) */ - -# if defined(TF_SENTFIN) - if (topt & TF_SENTFIN) { - (void)printf("%cSENTFIN", sep); - topt &= ~TF_SENTFIN; - sep = ','; - } -# endif /* defined(TF_SENTFIN) */ - - if (topt) - (void)printf("%cUNKNOWN=%#x", sep, topt); - if (Ffield) - putchar(Terminator); - } - } -#endif /* defined(HASTCPOPT) */ - -#if defined(HASTCPTPIW) - if (Ftcptpi & TCPTPI_WINDOWS) { - if (Lf->lts.rws) { - if (Ffield) - putchar(LSOF_FID_TCPTPI); - else { - if (ps) - putchar(' '); - else - putchar('('); - } - (void)printf("WR=%lu", Lf->lts.rw); - if (Ffield) - putchar(Terminator); - ps++; - } - if (Lf->lts.wws) { - if (Ffield) - putchar(LSOF_FID_TCPTPI); - else { - if (ps) - putchar(' '); - else - putchar('('); - } - (void)printf("WW=%lu", Lf->lts.ww); - if (Ffield) - putchar(Terminator); - ps++; - } - } -#endif /* defined(HASTCPTPIW) */ if (Ftcptpi && !Ffield && ps) putchar(')'); From 89dbd6b3295090bdb415b4154a787c5db2732446 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 18:39:18 +0800 Subject: [PATCH 15/18] sun: use system prfdinfo and mark unknown socket state --- lib/dialects/sun/dproc.c | 197 ++++++++++++++++---------------------- src/dialects/sun/dprint.c | 10 +- 2 files changed, 85 insertions(+), 122 deletions(-) diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index dde45d49..a5622b3b 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -74,42 +74,6 @@ static int nm2id(char *nm, int *id) { return (0); } -/* - * /proc//fdinfo/nn is a variable-length prfdinfo_t followed by - * self-describing pr_misc entries. The layout is from Solaris/illumos - * procfs; it is userland ABI and does not require kernel memory. - */ -#define SUN_FDINFO_HDR 200 -#define SUN_FDINFO_CORE 196 -#define SUN_MISC_PATHNAME 0 -#define SUN_MISC_SOCKETNAME 1 -#define SUN_MISC_PEERSOCKNAME 2 -#define SUN_MISC_SOCKOPT_TYPE 10 -#define SUN_MISC_TCP_CONGESTION 11 - -struct sun_fdinfo { - int pr_fd; - mode_t pr_mode; - uint64_t pr_ino; - int64_t pr_size; - int64_t pr_offset; - uid_t pr_uid; - gid_t pr_gid; - uint32_t pr_major; - uint32_t pr_minor; - uint32_t pr_rmajor; - uint32_t pr_rminor; - int pr_fileflags; - int pr_fdflags; - short pr_locktype; - pid_t pr_lockpid; - int pr_locksysid; - pid_t pr_peerpid; - int pr_filler[25]; - char pr_peername[16]; - int sock_type; -}; - static void enter_path(struct lsof_context *ctx, enum lsof_fd_type fdt, int fd, const char *path, const struct stat *st); @@ -166,10 +130,42 @@ static void set_sockaddr(struct lfile *lf, int which, const void *p, size_t n, } } -static int read_fdinfo(const char *p, struct sun_fdinfo *fi, char *path, - size_t pathlen, unsigned char *sn, size_t *snl, - unsigned char *pn, size_t *pnl) { - unsigned char buf[8192]; +#if !defined(PR_FDINFO_LEN_MAX) +# define PR_FDINFO_LEN_MAX (sizeof(prfdinfo_t) + 4 * MAXPATHLEN + 8192) +#endif + +static void set_lock_from_proc(const char *path, struct lfile *lf, + int locktype, SZOFFTYPE size) { + struct flock fl; + int fd; + + if (locktype == F_UNLCK) + return; + if (locktype == F_RDLCK) + lf->lock = LSOF_LOCK_READ_FULL; + else + lf->lock = LSOF_LOCK_WRITE_FULL; + if ((fd = open(path, O_RDONLY)) < 0) + return; + (void)memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + if (fcntl(fd, F_GETLK, &fl) == 0 && fl.l_type != F_UNLCK) { + int full = (fl.l_start == 0 && + (fl.l_len == 0 || fl.l_len >= (off_t)size)); + if (fl.l_type == F_RDLCK) + lf->lock = full ? LSOF_LOCK_READ_FULL : LSOF_LOCK_READ_PARTIAL; + else if (fl.l_type == F_WRLCK) + lf->lock = full ? LSOF_LOCK_WRITE_FULL : LSOF_LOCK_WRITE_PARTIAL; + } + (void)close(fd); +} + +static int read_fdinfo(const char *p, prfdinfo_t *fi, int *socktype, + char *path, size_t pathlen, unsigned char *sn, + size_t *snl, unsigned char *pn, size_t *pnl) { int fd, n; size_t off; @@ -177,106 +173,69 @@ static int read_fdinfo(const char *p, struct sun_fdinfo *fi, char *path, path[0] = '\0'; if ((fd = open(p, O_RDONLY)) < 0) return (1); - n = (int)read(fd, buf, sizeof(buf)); + n = (int)read(fd, fi, PR_FDINFO_LEN_MAX); (void)close(fd); - if (n < SUN_FDINFO_HDR) + if (n < (int)offsetof(prfdinfo_t, pr_misc)) return (1); - memcpy(fi, buf, SUN_FDINFO_CORE); - fi->sock_type = -1; + *socktype = -1; /* - * The fixed core is 196 bytes. Some Solaris fdinfo records have four - * bytes of alignment before the misc array; detect which by validating - * the first misc header. + * prfdinfo_t ends with a variable array of pr_misc_header_t records. + * Use the system layout and misc types instead of hard-coded offsets. */ - off = SUN_FDINFO_CORE; - if (off + 8 <= (size_t)n) { - uint32_t sz, ty; - memcpy(&sz, &buf[off], sizeof(sz)); - memcpy(&ty, &buf[off + 4], sizeof(ty)); - if (sz < 8 || ty > SUN_MISC_PEERSOCKNAME) - off = SUN_FDINFO_HDR; - } - while (off + 8 <= (size_t)n) { - uint32_t sz, ty; + off = offsetof(prfdinfo_t, pr_misc); + while (off + sizeof(pr_misc_header_t) <= (size_t)n) { + pr_misc_header_t mh; unsigned char *data; size_t dlen, adv; - memcpy(&sz, &buf[off], sizeof(sz)); - memcpy(&ty, &buf[off + 4], sizeof(ty)); - if (sz < 8 || off + sz > (size_t)n) + memcpy(&mh, &fi->pr_misc[off - offsetof(prfdinfo_t, pr_misc)], + sizeof(mh)); + if (mh.pr_misc_size < sizeof(mh) || off + mh.pr_misc_size > (size_t)n) break; - data = &buf[off + 8]; - dlen = sz - 8; - switch (ty) { - case SUN_MISC_PATHNAME: + data = &fi->pr_misc[off - offsetof(prfdinfo_t, pr_misc)] + + sizeof(mh); + dlen = mh.pr_misc_size - sizeof(mh); + switch (mh.pr_misc_type) { + case PR_PATHNAME: if (dlen && pathlen) { size_t l = dlen < pathlen - 1 ? dlen : pathlen - 1; memcpy(path, data, l); path[l] = '\0'; } break; - case SUN_MISC_SOCKETNAME: + case PR_SOCKETNAME: if (dlen <= 256) { memcpy(sn, data, dlen); *snl = dlen; } break; - case SUN_MISC_PEERSOCKNAME: + case PR_PEERSOCKNAME: if (dlen <= 256) { memcpy(pn, data, dlen); *pnl = dlen; } break; - case SUN_MISC_SOCKOPT_TYPE: + case PR_SOCKOPT_TYPE: if (dlen >= sizeof(uint_t)) { uint_t st; memcpy(&st, data, sizeof(st)); - fi->sock_type = (int)st; + *socktype = (int)st; } break; - case SUN_MISC_TCP_CONGESTION: - fi->sock_type = SOCK_STREAM; + case PR_SOCKOPT_TCP_CONGESTION: + *socktype = SOCK_STREAM; break; default: break; } - adv = (sz + 3) & ~3U; - if (adv < sz || off + adv > (size_t)n) + adv = (mh.pr_misc_size + 3) & ~3U; + if (adv < mh.pr_misc_size || off + adv > (size_t)n) break; off += adv; } return (0); } -static void set_lock_from_proc(const char *path, struct lfile *lf, - int locktype, SZOFFTYPE size) { - struct flock fl; - int fd; - - if (locktype == F_UNLCK) - return; - if (locktype == F_RDLCK) - lf->lock = LSOF_LOCK_READ_FULL; - else - lf->lock = LSOF_LOCK_WRITE_FULL; - if ((fd = open(path, O_RDONLY)) < 0) - return; - (void)memset(&fl, 0, sizeof(fl)); - fl.l_type = F_WRLCK; - fl.l_whence = SEEK_SET; - fl.l_start = 0; - fl.l_len = 0; - if (fcntl(fd, F_GETLK, &fl) == 0 && fl.l_type != F_UNLCK) { - int full = (fl.l_start == 0 && - (fl.l_len == 0 || fl.l_len >= (off_t)size)); - if (fl.l_type == F_RDLCK) - lf->lock = full ? LSOF_LOCK_READ_FULL : LSOF_LOCK_READ_PARTIAL; - else if (fl.l_type == F_WRLCK) - lf->lock = full ? LSOF_LOCK_WRITE_FULL : LSOF_LOCK_WRITE_PARTIAL; - } - (void)close(fd); -} - static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, const char *fdpath, const char *fipath, const char *pathpath) { @@ -284,24 +243,27 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, char unixpath[MAXPATHLEN + 1]; unsigned char sock[256], peer[256]; size_t sl, pl; - struct sun_fdinfo fi; + unsigned char fbuf[PR_FDINFO_LEN_MAX]; + prfdinfo_t *fi = (prfdinfo_t *)fbuf; struct stat st; ssize_t r; int has_sock, has_peer; + int sock_type; - if (read_fdinfo(fipath, &fi, path, sizeof(path), sock, &sl, peer, &pl)) + if (read_fdinfo(fipath, fi, &sock_type, path, sizeof(path), sock, &sl, + peer, &pl)) return; has_sock = has_peer = 0; unixpath[0] = '\0'; if (stat(fdpath, &st) != 0) { memset(&st, 0, sizeof(st)); - st.st_mode = fi.pr_mode; - st.st_ino = (ino_t)fi.pr_ino; - st.st_size = (off_t)fi.pr_size; + st.st_mode = fi->pr_mode; + st.st_ino = (ino_t)fi->pr_ino; + st.st_size = (off_t)fi->pr_size; st.st_nlink = 1; - st.st_dev = (dev_t)(((uint64_t)fi.pr_major << 18) | fi.pr_minor); - st.st_rdev = (dev_t)(((uint64_t)fi.pr_rmajor << 18) | fi.pr_rminor); + st.st_dev = (dev_t)(((uint64_t)fi->pr_major << 18) | fi->pr_minor); + st.st_rdev = (dev_t)(((uint64_t)fi->pr_rmajor << 18) | fi->pr_rminor); } if (path[0]) @@ -317,11 +279,11 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, if (!Lf) return; - if (fi.pr_offset >= 0) { - Lf->off = (SZOFFTYPE)fi.pr_offset; + if (fi->pr_offset >= 0) { + Lf->off = (SZOFFTYPE)fi->pr_offset; Lf->off_def = 1; } - switch (fi.pr_fileflags & O_ACCMODE) { + switch (fi->pr_fileflags & O_ACCMODE) { case O_RDONLY: Lf->access = LSOF_FILE_ACCESS_READ; break; @@ -333,12 +295,12 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, break; } - if (fi.pr_locktype != F_UNLCK) - set_lock_from_proc(nm[0] ? nm : fdpath, Lf, fi.pr_locktype, + if (fi->pr_locktype != F_UNLCK) + set_lock_from_proc(nm[0] ? nm : fdpath, Lf, fi->pr_locktype, (SZOFFTYPE)st.st_size); - if ((fi.pr_mode & S_IFMT) == S_IFSOCK) { + if ((fi->pr_mode & S_IFMT) == S_IFSOCK) { if (sl) { (void)set_sockaddr(Lf, 0, sock, sl, unixpath, sizeof(unixpath)); has_sock = 1; @@ -370,14 +332,14 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, break; } if ((Lf->li[0].af == AF_INET || Lf->li[0].af == AF_INET6) && - fi.sock_type == SOCK_STREAM) { + sock_type == SOCK_STREAM) { (void)strncpy(Lf->iproto, "TCP", sizeof(Lf->iproto) - 1); Lf->iproto[sizeof(Lf->iproto) - 1] = '\0'; Lf->inp_ty = 2; Lf->lts.type = 0; } else if ((Lf->li[0].af == AF_INET || Lf->li[0].af == AF_INET6) && - fi.sock_type == SOCK_DGRAM) { + sock_type == SOCK_DGRAM) { (void)strncpy(Lf->iproto, "UDP", sizeof(Lf->iproto) - 1); Lf->iproto[sizeof(Lf->iproto) - 1] = '\0'; Lf->inp_ty = 2; @@ -390,6 +352,9 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, Lf->lts.type = 1; } else Lf->lts.type = 1; + /* procfs does not expose TCP/UDP state. */ + Lf->lts.state.i = -1; + Lf->lts.state.ui = (unsigned)-1; if (Selflags & SELNET) { int family_ok = !FnetTy || (FnetTy == 4 && Lf->li[0].af == AF_INET) || diff --git a/src/dialects/sun/dprint.c b/src/dialects/sun/dprint.c index c69590ba..dcb77e89 100644 --- a/src/dialects/sun/dprint.c +++ b/src/dialects/sun/dprint.c @@ -48,7 +48,8 @@ void print_tcptpi(struct lsof_context *ctx, /* context */ case 0: /* TCP */ if (!TcpSt) (void)build_IPstates(ctx); - if ((i = Lf->lts.state.i + TcpStOff) < 0 || i >= TcpNstates) { + if (Lf->lts.state.i < 0 || + (i = Lf->lts.state.i + TcpStOff) >= TcpNstates) { (void)snpf(sbuf, sizeof(sbuf), "UNKNOWN_TCP_STATE_%d", Lf->lts.state.i); cp = sbuf; @@ -59,7 +60,8 @@ void print_tcptpi(struct lsof_context *ctx, /* context */ if (!UdpSt) (void)build_IPstates(ctx); u = Lf->lts.state.ui + UdpStOff; - if (u >= (unsigned)UdpNstates) { + if (Lf->lts.state.ui == (unsigned)-1 || + u >= (unsigned)UdpNstates) { (void)snpf(sbuf, sizeof(sbuf), "UNKNOWN_UDP_STATE_%u", Lf->lts.state.ui); cp = sbuf; @@ -77,10 +79,6 @@ void print_tcptpi(struct lsof_context *ctx, /* context */ } } - - - - if (Ftcptpi && !Ffield && ps) putchar(')'); if (nl) From d0b9bfcbc4887c69e52473e0406b74cf341dfc51 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 18:52:45 +0800 Subject: [PATCH 16/18] sun: reject unsupported TCP/UDP state selection --- lib/dialects/sun/dproc.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index a5622b3b..803d27f6 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -484,6 +484,17 @@ static void process_path(struct lsof_context *ctx, int pid, void gather_proc_info(struct lsof_context *ctx) { DIR *pd, *fdd; + +#if defined(HASTCPUDPSTATE) + if (TcpStIn || UdpStIn || TcpStXn || UdpStXn) { + (void)fprintf(stderr, + "%s: -s TCP/UDP state selection is unavailable with " + "the Solaris procfs backend\n", + Pn); + Error(ctx); + } +#endif + struct dirent *dp, *fdp; psinfo_t ps; char path[MAXPATHLEN + 1]; From a9d7bd230dc92a395ca7567ef944416367f3ab81 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 28 Nov 2026 22:24:14 +0800 Subject: [PATCH 17/18] sun: restore zone filtering via psinfo zoneid --- Makefile.am | 3 +- lib/dialects/sun/dlsof.h | 4 ++ lib/dialects/sun/dproc.c | 90 +++++++++++++++++++++++++++++++++ lib/dialects/sun/dproto.h | 4 ++ lib/dialects/sun/machine.h | 6 +++ tests/case-32-solaris-zone.bash | 31 ++++++++++++ 6 files changed, 137 insertions(+), 1 deletion(-) create mode 100755 tests/case-32-solaris-zone.bash diff --git a/Makefile.am b/Makefile.am index 9150b974..0d153778 100644 --- a/Makefile.am +++ b/Makefile.am @@ -141,7 +141,8 @@ DIALECT_NEUTRAL_TESTS = tests/case-00-hello.bash \ tests/case-23-multi-address-hostname.bash \ tests/case-24-dual-stack-socket.bash \ tests/case-30-json-output.bash \ - tests/case-31-jsonl-output.bash + tests/case-31-jsonl-output.bash \ + tests/case-32-solaris-zone.bash TESTS = $(DIALECT_NEUTRAL_TESTS) EXTRA_DIST += $(DIALECT_NEUTRAL_TESTS) \ diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index 2a90a603..7fb625ac 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -65,6 +65,10 @@ # include # include +# if defined(HASZONES) +# include +# endif /* defined(HASZONES) */ + /* * The procfs backend has no kernel structures. Define the small set of * types and node/vnode constants that common lsof code still references. diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index 803d27f6..a9972f79 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -482,6 +482,58 @@ static void process_path(struct lsof_context *ctx, int pid, link_lfile(ctx); } +#if defined(HASZONES) +# if !defined(ZONENAME_MAX) +# define ZONENAME_MAX 64 +# endif + +static int hash_zn(char *zn) { + size_t i, l; + int h; + + if (!zn || !(l = strlen(zn))) + return (0); + if (l == 1) + return ((int)*zn & (HASHZONE - 1)); + for (i = h = 0; i < l - 1; i++) + h ^= ((int)zn[i] * (int)zn[i + 1]) << ((i * 3) % 13); + return (h & (HASHZONE - 1)); +} + +int enter_zone_arg(struct lsof_context *ctx, char *zn) { + int zh; + znhash_t *zp, *zpn; + + if (!zn || !*zn) + return (1); + if (!ZoneArg) { + if (!(ZoneArg = (znhash_t **)calloc(HASHZONE, sizeof(znhash_t *)))) { + (void)fprintf(stderr, "%s: no space for zone arg hash\n", Pn); + Error(ctx); + } + } + zh = hash_zn(zn); + for (zp = ZoneArg[zh]; zp; zp = zp->next) { + if (!strcmp(zp->zn, zn)) + break; + } + if (zp) { + if (!Fwarn) + (void)fprintf(stderr, "%s: duplicate zone name: %s\n", Pn, zn); + return (1); + } + if (!(zpn = (znhash_t *)malloc(sizeof(znhash_t)))) { + (void)fprintf(stderr, "%s: no space for zone: %s\n", Pn, zn); + Error(ctx); + } + zpn->zn = zn; + zpn->f = 0; + zpn->next = ZoneArg[zh]; + ZoneArg[zh] = zpn; + return (0); +} +#endif /* defined(HASZONES) */ + void gather_proc_info(struct lsof_context *ctx) { DIR *pd, *fdd; @@ -501,6 +553,12 @@ void gather_proc_info(struct lsof_context *ctx) { char fdpath[MAXPATHLEN + 64]; char pspath[MAXPATHLEN + 1]; int pid; +#if defined(HASZONES) + char znbuf[ZONENAME_MAX + 1]; + char *zn; + int zh; + znhash_t *zp; +#endif if (!(pd = opendir(PROC_BASE))) { (void)fprintf(stderr, "%s: can't open %s: %s\n", Pn, PROC_BASE, @@ -516,6 +574,29 @@ void gather_proc_info(struct lsof_context *ctx) { (void)snpf(pspath, sizeof(pspath), "%s/%d/psinfo", PROC_BASE, pid); if (read_psinfo(pspath, &ps)) continue; + +#if defined(HASZONES) + zn = (char *)NULL; + znbuf[0] = '\0'; + { + int zl = getzonenamebyid(ps.pr_zoneid, znbuf, sizeof(znbuf)); + if (zl > 0 && (size_t)zl < sizeof(znbuf)) + zn = znbuf; + } + if (ZoneArg) { + if (!zn) + continue; + zh = hash_zn(zn); + for (zp = ZoneArg[zh]; zp; zp = zp->next) { + if (!strcmp(zn, zp->zn)) + break; + } + if (!zp) + continue; + zp->f = 1; + } +#endif + if (is_proc_excl(ctx, pid, (int)ps.pr_pgid, (UID_ARG)ps.pr_uid, &pss, &sf) || is_cmd_excl(ctx, ps.pr_fname, &pss, &sf)) @@ -523,6 +604,15 @@ void gather_proc_info(struct lsof_context *ctx) { alloc_lproc(ctx, pid, (int)ps.pr_pgid, (int)ps.pr_ppid, (UID_ARG)ps.pr_euid, ps.pr_fname, (int)pss, (int)sf); +#if defined(HASZONES) + if (zn) { + Lp->zn = mkstrcpy(zn, (MALLOC_S *)NULL); + if (ZoneArg) { + Lp->pss |= PS_PRI; + Lp->sf |= SELZONE; + } + } +#endif Plf = (struct lfile *)NULL; (void)snpf(path, sizeof(path), "%s/%d/cwd", PROC_BASE, pid); diff --git a/lib/dialects/sun/dproto.h b/lib/dialects/sun/dproto.h index 7d923a95..b668abbf 100644 --- a/lib/dialects/sun/dproto.h +++ b/lib/dialects/sun/dproto.h @@ -55,6 +55,10 @@ extern int rw_pseudo_sect(struct lsof_context *ctx, int m); extern void read_clone(struct lsof_context *ctx); +#if defined(HASZONES) +extern int enter_zone_arg(struct lsof_context *ctx, char *zn); +#endif /* defined(HASZONES) */ + #if defined(HASIPv6) extern struct hostent *gethostbyname2(const char *nm, int proto); #endif /* defined(HASIPv6) */ diff --git a/lib/dialects/sun/machine.h b/lib/dialects/sun/machine.h index 3e7b0752..60cee3dd 100644 --- a/lib/dialects/sun/machine.h +++ b/lib/dialects/sun/machine.h @@ -63,6 +63,12 @@ # define HASBLKDEV 1 +/* + * Solaris zones are available through psinfo_t.pr_zoneid. + */ + +# define HASZONES 1 + /* * HASDCACHE is defined for those dialects that support a device cache * file. diff --git a/tests/case-32-solaris-zone.bash b/tests/case-32-solaris-zone.bash new file mode 100755 index 00000000..b87023f9 --- /dev/null +++ b/tests/case-32-solaris-zone.bash @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Solaris/illumos zone filtering smoke test. +source tests/common.bash + +case "${dialect:-}" in + solaris) ;; + *) + echo "SKIP: Solaris zone test is only for the solaris dialect" \ + >> "$report" + exit 77 + ;; +esac + +{ + echo "zone filter: global" + $lsof -a -z global -p $$ >/dev/null 2>&1 + status=$? + if [ "$status" -ne 0 ]; then + echo "FAIL: lsof -a -z global -p $$ exited with status $status" + exit 1 + fi + + echo "zone filter: nonexistent zone" + $lsof -a -z lsof-no-such-zone -p $$ >/dev/null 2>&1 + status=$? + if [ "$status" -ne 1 ]; then + echo "FAIL: nonexistent zone exited with status $status, expected 1" + exit 1 + fi + echo "PASS: zone filtering" +} >> "$report" 2>&1 From 54ff0ebfd33662b161e3000a208b6172405dad6f Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sun, 29 Nov 2026 01:46:56 +0800 Subject: [PATCH 18/18] sun: use procfs and MIB2 instead of kernel memory --- Lsof.8 | 74 +++++++- Makefile.am | 2 + lib/dialects/sun/Makefile | 6 +- lib/dialects/sun/Mksrc | 2 +- lib/dialects/sun/dlsof.h | 54 +----- lib/dialects/sun/dproc.c | 210 +++++++++++++++++++-- lib/dialects/sun/dsock.c | 373 +++++++++++++++++++++++++++++++++++++ lib/dialects/sun/dsock.h | 28 +++ lib/dialects/sun/machine.h | 9 + src/dialects/sun/dprint.c | 197 +++++++++++++++++++- 10 files changed, 884 insertions(+), 71 deletions(-) create mode 100644 lib/dialects/sun/dsock.c create mode 100644 lib/dialects/sun/dsock.h diff --git a/Lsof.8 b/Lsof.8 index 7e3b1ce6..52489c47 100644 --- a/Lsof.8 +++ b/Lsof.8 @@ -360,6 +360,9 @@ components from the kernel's name cache. See the .B "KERNEL NAME CACHE" section for more information. +.IP +The Solaris procfs backend does not have a kernel DNLC and does not +support this option. .TP \w'names'u+4 .BI +d " s" causes @@ -1205,6 +1208,9 @@ specifies a kernel name list file, in place of /vmunix, /mach, etc. .B \-k is not available under AIX on the IBM RISC/System 6000. +.IP +The Solaris procfs backend does not read kernel memory or kernel name +lists and does not support this option. .TP \w'names'u+4 .B \-l inhibits the conversion of user ID numbers to login names. @@ -1250,6 +1256,9 @@ or .I /dev/mem \&\- e.g., a crash dump file. .IP +The Solaris procfs backend does not use kernel memory and does not support +the kernel memory form of this option. +.IP The option form .B +m requests that a mount supplement file be written to the standard output @@ -2027,6 +2036,9 @@ to display isn't associated with open TCP, UDP or UDPLITE socket files. .HP \w'names'u+4 \ \ \ \ Solaris 10 and above: .br +The Solaris procfs backend does not support this option. The following +description applies to the legacy kernel backend. +.IP This Solaris 10 and above option requests the reporting of cached paths for files that have been deleted \- i.e., removed with .IR rm (1) @@ -2239,9 +2251,10 @@ versions): AIX 4.1.4 (AFS 3.4a) HP\-UX 9.0.5 (AFS 3.4a) Linux 1.2.13 (AFS 3.3) - Solaris 2.[56] (AFS 3.4a) .fi .PP +The Solaris procfs backend does not support AFS recognition. +.PP It may recognize AFS files on other versions of these dialects, but has not been tested there. Depending on how AFS is implemented, @@ -2288,6 +2301,65 @@ Because AFS path lookups don't seem to participate in the kernel's name cache operations, .I lsof can't identify path name components for AFS files. +.SH "SOLARIS PROCFS BACKEND" +The Solaris dialect uses the procfs interface on Solaris 11 and illumos. +It does not read kernel memory, and therefore does not require +.BR /dev/kmem , +.BR libkvm , +.BR libctf , +or +.BR libelf . +.PP +Process information is read from +.IR /proc//psinfo , +.IR /proc//fd , +.IR /proc//fdinfo , +and +.IR /proc//path . +Zones are reported through +.I psinfo_t.pr_zoneid +and the +.B \-z +option. +.PP +TCP and UDP state, TCP queue and window values, and MSS are obtained +from the userland MIB2 interface on +.IR /dev/arp . +If that interface is unavailable, socket state is reported as unknown. +Socket options are taken from the subset exposed by +.IR /proc//fdinfo . +.PP +The following legacy kernel features are not supported by this backend: +.PP +.RS 4 +.IP \(bu 2 +AFS +.IP \(bu 2 +VxFS-specific inode and path information +.IP \(bu 2 +streams module and queue information +.IP \(bu 2 +kernel DNLC and the +.B \-C +option +.IP \(bu 2 +kernel name list/memory options +.B \-k +and +.B \-m +.IP \(bu 2 +the Solaris deleted-path +.B \-X +option +.IP \(bu 2 +kernel file/vnode addresses and the corresponding field output +.RE +.PP +Ordinary VxFS files remain visible through procfs path and +.BR stat (2) +information. +NFS files are also handled through procfs; testing them requires an +actual NFS mount. .SH SECURITY .I Lsof has three features that may cause security concerns. diff --git a/Makefile.am b/Makefile.am index 0d153778..698f84d1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -74,9 +74,11 @@ LIBLSOF_SOURCES += lib/dialects/sun/ddev.c \ lib/dialects/sun/dmnt.c \ lib/dialects/sun/dnode.c \ lib/dialects/sun/dproc.c \ + lib/dialects/sun/dsock.c \ lib/dialects/sun/dstore.c \ lib/dialects/sun/dlsof.h \ lib/dialects/sun/dproto.h \ + lib/dialects/sun/dsock.h \ lib/dialects/sun/machine.h endif diff --git a/lib/dialects/sun/Makefile b/lib/dialects/sun/Makefile index cf7e89cd..02b7f8b1 100644 --- a/lib/dialects/sun/Makefile +++ b/lib/dialects/sun/Makefile @@ -17,11 +17,11 @@ CFLAGS= ${CDEFS} ${INCL} ${DEBUG} HDR= lsof.h lsof_fields.h dlsof.h machine.h proto.h dproto.h -SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c \ +SRC= ddev.c dfile.c dmnt.c dnode.c dproc.c dsock.c \ dstore.c dprint.c \ arg.c main.c misc.c node.c print.c proc.c store.c usage.c util.c -OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o \ +OBJ= ddev.o dfile.o dmnt.o dnode.o dproc.o dsock.o \ dstore.o dprint.o \ arg.o main.o misc.o node.o print.o proc.o store.o usage.o util.o @@ -130,6 +130,8 @@ dnode.o: ${HDR} dnode.c dproc.o: ${HDR} kernelbase.h dproc.c +dsock.o: ${HDR} dsock.c + dstore.o: ${HDR} dstore.c arg.o: ${HDR} arg.c diff --git a/lib/dialects/sun/Mksrc b/lib/dialects/sun/Mksrc index 7c5ff372..ca8c4aa6 100755 --- a/lib/dialects/sun/Mksrc +++ b/lib/dialects/sun/Mksrc @@ -22,7 +22,7 @@ mksrc() { } D=lib/dialects/sun -L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dstore.c machine.h" +L="ddev.c dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dsock.c dsock.h dstore.c machine.h" mksrc diff --git a/lib/dialects/sun/dlsof.h b/lib/dialects/sun/dlsof.h index 7fb625ac..30fb98b9 100644 --- a/lib/dialects/sun/dlsof.h +++ b/lib/dialects/sun/dlsof.h @@ -57,6 +57,9 @@ # include # include # include +# include +# include +# include # include # include # include @@ -88,48 +91,6 @@ typedef uint32_t minor_t; # define GET_MAJ_DEV(d) ((major_t)((d) >> L_BITSMINOR & L_MAXMAJ)) # define GET_MIN_DEV(d) ((minor_t)((d) & L_MAXMIN)) -/* TCP/TPI state values from the Solaris networking headers. */ -# define TCPS_CLOSED 0 -# define TCPS_IDLE 1 -# define TCPS_BOUND 2 -# define TCPS_LISTEN 3 -# define TCPS_SYN_SENT 4 -# define TCPS_SYN_RCVD 5 -# define TCPS_ESTABLISHED 6 -# define TCPS_CLOSE_WAIT 7 -# define TCPS_FIN_WAIT_1 8 -# define TCPS_CLOSING 9 -# define TCPS_LAST_ACK 10 -# define TCPS_FIN_WAIT_2 11 -# define TCPS_TIME_WAIT 12 - -# define TS_UNBND 0 -# define TS_WACK_BREQ 1 -# define TS_WACK_UREQ 2 -# define TS_IDLE 3 -# define TS_WACK_OPTREQ 4 -# define TS_WACK_CREQ 5 -# define TS_WCON_CREQ 6 -# define TS_WRES_CIND 7 -# define TS_WACK_CRES 8 -# define TS_DATA_XFER 9 -# define TS_WIND_ORDREL 10 -# define TS_WREQ_ORDREL 11 -# define TS_WACK_DREQ6 12 -# define TS_WACK_DREQ7 13 -# define TS_WACK_DREQ9 14 -# define TS_WACK_DREQ10 15 -# define TS_WACK_DREQ11 16 - -enum vtype { - VNON, - VREG, - VDIR, - VBLK, - VCHR, - VSOCK, - VFIFO -}; enum node_type { N_UNKN, @@ -141,15 +102,6 @@ enum node_type { N_ANON_INODE }; -/* - * Structure for Atria's MVFS nodes - */ - -struct mvfsnode { - unsigned long d1[6]; - unsigned long m_ino; /* node number */ -}; - # define COMP_P const void # define DEVINCR 1024 /* device table malloc() increment */ # define DIRTYPE dirent diff --git a/lib/dialects/sun/dproc.c b/lib/dialects/sun/dproc.c index a9972f79..18b6f49c 100644 --- a/lib/dialects/sun/dproc.c +++ b/lib/dialects/sun/dproc.c @@ -37,6 +37,8 @@ #include "common.h" +#include "dsock.h" + #include #include @@ -163,14 +165,66 @@ static void set_lock_from_proc(const char *path, struct lfile *lf, (void)close(fd); } +struct sun_sockopts { + int have; + unsigned int opt; + int ltm; + int qlim; + unsigned long rbsz, sbsz; +}; + +/* + * fdinfo reports socket options as PR_SO_* bits, not SO_* bits. Convert the + * bits that have a stable SO_* counterpart before storing them in lfile. + */ +static unsigned int fdinfo_socket_options(uint32_t bits) { + unsigned int options = 0; + +#if defined(PR_SO_DEBUG) && defined(SO_DEBUG) + if (bits & PR_SO_DEBUG) + options |= SO_DEBUG; +#endif +#if defined(PR_SO_REUSEADDR) && defined(SO_REUSEADDR) + if (bits & PR_SO_REUSEADDR) + options |= SO_REUSEADDR; +#endif +#if defined(PR_SO_REUSEPORT) && defined(SO_REUSEPORT) + if (bits & PR_SO_REUSEPORT) + options |= SO_REUSEPORT; +#endif +#if defined(PR_SO_KEEPALIVE) && defined(SO_KEEPALIVE) + if (bits & PR_SO_KEEPALIVE) + options |= SO_KEEPALIVE; +#endif +#if defined(PR_SO_DONTROUTE) && defined(SO_DONTROUTE) + if (bits & PR_SO_DONTROUTE) + options |= SO_DONTROUTE; +#endif +#if defined(PR_SO_BROADCAST) && defined(SO_BROADCAST) + if (bits & PR_SO_BROADCAST) + options |= SO_BROADCAST; +#endif +#if defined(PR_SO_OOBINLINE) && defined(SO_OOBINLINE) + if (bits & PR_SO_OOBINLINE) + options |= SO_OOBINLINE; +#endif +#if defined(PR_SO_DGRAM_ERRIND) && defined(SO_DGRAM_ERRIND) + if (bits & PR_SO_DGRAM_ERRIND) + options |= SO_DGRAM_ERRIND; +#endif + return (options); +} + static int read_fdinfo(const char *p, prfdinfo_t *fi, int *socktype, char *path, size_t pathlen, unsigned char *sn, - size_t *snl, unsigned char *pn, size_t *pnl) { + size_t *snl, unsigned char *pn, size_t *pnl, + struct sun_sockopts *so) { int fd, n; size_t off; *snl = *pnl = 0; path[0] = '\0'; + memset(so, 0, sizeof(*so)); if ((fd = open(p, O_RDONLY)) < 0) return (1); n = (int)read(fd, fi, PR_FDINFO_LEN_MAX); @@ -225,6 +279,52 @@ static int read_fdinfo(const char *p, prfdinfo_t *fi, int *socktype, case PR_SOCKOPT_TCP_CONGESTION: *socktype = SOCK_STREAM; break; + case PR_SOCKOPTS_BOOL_OPTS: + if (dlen >= sizeof(uint32_t)) { + uint32_t o; + memcpy(&o, data, sizeof(o)); + so->opt = fdinfo_socket_options(o); + so->have = 1; + } + break; + case PR_SOCKOPT_LINGER: + if (dlen >= sizeof(struct linger)) { + struct linger l; + memcpy(&l, data, sizeof(l)); + so->ltm = (int)l.l_linger; +#if defined(SO_LINGER) + if (l.l_onoff) + so->opt |= SO_LINGER; +#endif + so->have = 1; + } + break; + case PR_SOCKOPT_SNDBUF: + if (dlen >= sizeof(int)) { + int v; + memcpy(&v, data, sizeof(v)); + so->sbsz = (unsigned long)v; + so->have = 1; + } + break; + case PR_SOCKOPT_RCVBUF: + if (dlen >= sizeof(int)) { + int v; + memcpy(&v, data, sizeof(v)); + so->rbsz = (unsigned long)v; + so->have = 1; + } + break; +#if defined(PR_SOCKOPT_LISTENQLIMIT) + case PR_SOCKOPT_LISTENQLIMIT: + if (dlen >= sizeof(int)) { + int v; + memcpy(&v, data, sizeof(v)); + so->qlim = (int)v; + so->have = 1; + } + break; +#endif default: break; } @@ -236,6 +336,50 @@ static int read_fdinfo(const char *p, prfdinfo_t *fi, int *socktype, return (0); } +static int state_selected(struct lsof_context *ctx, struct lfile *lf) { +#if defined(HASTCPUDPSTATE) + int i, n, off; + int tx = (lf->lts.type == 1); + unsigned char *inc = tx ? UdpStI : TcpStI; + unsigned char *exc = tx ? UdpStX : TcpStX; + int inn = tx ? UdpStIn : TcpStIn; + int excn = tx ? UdpStXn : TcpStXn; + + if (!inn && !excn) + return (1); + if (lf->lts.type < 0) + return (1); + if (lf->lts.type == 0) { + if (lf->lts.state.i == INT_MIN) + return (0); + i = lf->lts.state.i; + off = TcpStOff; + n = TcpNstates; + } else { + if (lf->lts.state.ui == (unsigned)-1) + return (0); + i = (int)lf->lts.state.ui; + off = UdpStOff; + n = UdpNstates; + } + i += off; + if (i < 0 || i >= n) + return (0); + if (exc && exc[i]) + return (0); + if (inn) { + if (!inc || !inc[i]) + return (0); + inc[i] = 2; + } + return (1); +#else + (void)ctx; + (void)lf; + return (1); +#endif +} + static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, const char *fdpath, const char *fipath, const char *pathpath) { @@ -249,9 +393,10 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, ssize_t r; int has_sock, has_peer; int sock_type; + struct sun_sockopts so; if (read_fdinfo(fipath, fi, &sock_type, path, sizeof(path), sock, &sl, - peer, &pl)) + peer, &pl, &so)) return; has_sock = has_peer = 0; unixpath[0] = '\0'; @@ -352,9 +497,51 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, Lf->lts.type = 1; } else Lf->lts.type = 1; - /* procfs does not expose TCP/UDP state. */ - Lf->lts.state.i = -1; + /* + * The procfs fdinfo API does not expose TCP state. Supplement + * it from the userland MIB2 stream, keyed by PID and endpoints. + */ + Lf->lts.state.i = INT_MIN; Lf->lts.state.ui = (unsigned)-1; + if ((Lf->li[0].af == AF_INET || Lf->li[0].af == AF_INET6)) { + const struct sun_sockstate *ss; + const void *la = Lf->li[0].af == AF_INET + ? (const void *)&Lf->li[0].ia.a4 + : (const void *)&Lf->li[0].ia.a6; + const void *fa = (Lf->li[1].af == AF_INET) + ? (const void *)&Lf->li[1].ia.a4 + : (Lf->li[1].af == AF_INET6) + ? (const void *)&Lf->li[1].ia.a6 + : (const void *)NULL; + ss = sun_sockstate_find(pid, Lf->li[0].af, la, + (int)Lf->li[0].p, fa, + (int)Lf->li[1].p); + if (ss) { + if (Lf->lts.type == 0) { + Lf->lts.state.i = ss->state; + Lf->lts.rq = ss->rq; + Lf->lts.sq = ss->sq; + Lf->lts.rqs = Lf->lts.sqs = 1; + Lf->lts.rw = ss->rw; + Lf->lts.ww = ss->ww; + Lf->lts.rws = Lf->lts.wws = 1; + } else + Lf->lts.state.ui = (unsigned)ss->state; + if (ss->mss) { + Lf->lts.mss = ss->mss; + Lf->lts.msss = 1; + } + } + } + if (so.have) { + Lf->lts.opt = so.opt; + Lf->lts.ltm = so.ltm; + Lf->lts.qlim = so.qlim; + Lf->lts.rbsz = so.rbsz; + Lf->lts.sbsz = so.sbsz; + Lf->lts.pqlens = Lf->lts.qlens = Lf->lts.qlims = 1; + Lf->lts.rbszs = Lf->lts.sbszs = 1; + } if (Selflags & SELNET) { int family_ok = !FnetTy || (FnetTy == 4 && Lf->li[0].af == AF_INET) || @@ -380,6 +567,8 @@ static void process_fdinfo(struct lsof_context *ctx, int pid, int fd, } } } + if (!state_selected(ctx, Lf)) + Lf->sf = 0; if (Lf && Lf->sf) link_lfile(ctx); } @@ -537,16 +726,6 @@ int enter_zone_arg(struct lsof_context *ctx, char *zn) { void gather_proc_info(struct lsof_context *ctx) { DIR *pd, *fdd; -#if defined(HASTCPUDPSTATE) - if (TcpStIn || UdpStIn || TcpStXn || UdpStXn) { - (void)fprintf(stderr, - "%s: -s TCP/UDP state selection is unavailable with " - "the Solaris procfs backend\n", - Pn); - Error(ctx); - } -#endif - struct dirent *dp, *fdp; psinfo_t ps; char path[MAXPATHLEN + 1]; @@ -560,6 +739,8 @@ void gather_proc_info(struct lsof_context *ctx) { znhash_t *zp; #endif + (void)sun_sockstate_load(); + if (!(pd = opendir(PROC_BASE))) { (void)fprintf(stderr, "%s: can't open %s: %s\n", Pn, PROC_BASE, strerror(errno)); @@ -647,4 +828,5 @@ void gather_proc_info(struct lsof_context *ctx) { (void)closedir(fdd); } (void)closedir(pd); + sun_sockstate_free(); } diff --git a/lib/dialects/sun/dsock.c b/lib/dialects/sun/dsock.c new file mode 100644 index 00000000..de2a47a9 --- /dev/null +++ b/lib/dialects/sun/dsock.c @@ -0,0 +1,373 @@ +/* + * dsock.c - Solaris/illumos MIB2 socket state lookup + * + * This is a small, self-contained consumer of the userland MIB2 interface + * exported by /dev/arp. It does not read kernel memory and does not depend + * on kernel linker symbols. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dsock.h" + +/* + * A few older Solaris headers do not publish the table names. Keep local + * values only as a fallback; when the header defines them, the header wins. + */ +#if !defined(MIB2_TCP_CONN) +# define MIB2_TCP_CONN 13 +#endif +#if !defined(MIB2_TCP6_CONN) +# define MIB2_TCP6_CONN 14 +#endif +#if !defined(MIB2_UDP_ENTRY) +# define MIB2_UDP_ENTRY 5 +#endif +#if !defined(MIB2_UDP6_ENTRY) +# define MIB2_UDP6_ENTRY 6 +#endif + +struct mib_table { + struct mib_table *next; + int level; + int name; + size_t length; + unsigned char *data; +}; + +static struct sun_sockstate *SocketStates; +static size_t SocketStateCount; +static size_t SocketStateCapacity; + +static void release_tables(struct mib_table *table) { + while (table) { + struct mib_table *next = table->next; + + free(table->data); + free(table); + table = next; + } +} + +static int open_mib_stream(void) { + int fd; + + fd = open("/dev/arp", O_RDWR); + if (fd < 0) + return (-1); + if (ioctl(fd, I_PUSH, "tcp") < 0 || ioctl(fd, I_PUSH, "udp") < 0 || + ioctl(fd, I_PUSH, "icmp") < 0) { + (void)close(fd); + return (-1); + } + return (fd); +} + +/* + * Read one MIB snapshot. The stream returns a sequence of option-management + * acknowledgements, each followed by one table payload. + */ +static struct mib_table *read_mib_stream(int fd) { + unsigned char control[1024]; + struct strbuf control_buf; + struct strbuf data_buf; + struct T_optmgmt_req *request; + struct T_optmgmt_ack *ack; + struct T_error_ack *error_ack; + struct opthdr *header; + struct mib_table *first = NULL; + struct mib_table *last = NULL; + struct mib_table *table; + int flags; + int n; + + memset(control, 0, sizeof(control)); + request = (struct T_optmgmt_req *)control; + request->PRIM_type = T_SVR4_OPTMGMT_REQ; + request->OPT_offset = sizeof(*request); + request->OPT_length = sizeof(struct opthdr); + request->MGMT_flags = T_CURRENT; + header = (struct opthdr *)&request[1]; + header->level = EXPER_IP_AND_ALL_IRES; + header->name = 0; + header->len = 1; + + control_buf.buf = (char *)control; + control_buf.len = request->OPT_offset + request->OPT_length; + control_buf.maxlen = sizeof(control); + if (putmsg(fd, &control_buf, (struct strbuf *)NULL, 0) < 0) + return ((struct mib_table *)NULL); + + for (;;) { + control_buf.len = 0; + flags = 0; + n = getmsg(fd, &control_buf, (struct strbuf *)NULL, &flags); + if (n < 0 || control_buf.len < sizeof(*ack)) + break; + + ack = (struct T_optmgmt_ack *)control; + error_ack = (struct T_error_ack *)control; + if (ack->PRIM_type == T_ERROR_ACK) { + if (control_buf.len >= sizeof(*error_ack) && + error_ack->TLI_error == TSYSERR) + errno = error_ack->UNIX_error; + break; + } + if (ack->PRIM_type != T_OPTMGMT_ACK || + ack->MGMT_flags != T_SUCCESS) + break; + + header = (struct opthdr *)&ack[1]; + if (header->len == 0) + break; + + table = (struct mib_table *)calloc(1, sizeof(*table)); + if (!table) + break; + table->level = header->level; + table->name = header->name; + table->length = (size_t)header->len; + table->data = (unsigned char *)malloc(table->length); + if (!table->data) { + free(table); + break; + } + + data_buf.buf = (char *)table->data; + data_buf.len = 0; + data_buf.maxlen = header->len; + if (getmsg(fd, (struct strbuf *)NULL, &data_buf, &flags) < 0) { + free(table->data); + free(table); + break; + } + + if (last) + last->next = table; + else + first = table; + last = table; + } + return (first); +} + +static void append_state(int family, int type, uint32_t pid, const void *local, + int local_port, const void *foreign, int foreign_port, + int state, unsigned long rq, unsigned long sq, + unsigned long rw, unsigned long ww, unsigned long mss) { + struct sun_sockstate *entry; + + if (SocketStateCount == SocketStateCapacity) { + size_t capacity = SocketStateCapacity ? SocketStateCapacity * 2 : 32; + struct sun_sockstate *grown; + + grown = (struct sun_sockstate *)realloc( + SocketStates, capacity * sizeof(*grown)); + if (!grown) + return; + SocketStates = grown; + SocketStateCapacity = capacity; + } + + entry = &SocketStates[SocketStateCount++]; + memset(entry, 0, sizeof(*entry)); + entry->family = family; + entry->type = type; + entry->pid = pid; + entry->lport = (uint16_t)local_port; + entry->fport = (uint16_t)foreign_port; + entry->state = state; + entry->rq = rq; + entry->sq = sq; + entry->rw = rw; + entry->ww = ww; + entry->mss = mss; + if (local) + memcpy(entry->laddr, local, family == AF_INET ? 4 : 16); + if (foreign) + memcpy(entry->faddr, foreign, family == AF_INET ? 4 : 16); +} + +static int udp_tpi_state(int mib_state) { + switch (mib_state) { + case MIB2_UDP_unbound: + return (TS_UNBND); + case MIB2_UDP_idle: + return (TS_IDLE); + case MIB2_UDP_connected: + return (TS_DATA_XFER); + default: + return (mib_state); + } +} + +static void read_tcp_table(const struct mib_table *table) { + size_t offset; + + for (offset = 0; + offset + sizeof(mib2_tcpConnEntry_t) <= table->length; + offset += sizeof(mib2_tcpConnEntry_t)) { + mib2_tcpConnEntry_t entry; + + memcpy(&entry, table->data + offset, sizeof(entry)); + append_state(AF_INET, SOCK_STREAM, entry.tcpConnCreationProcess, + &entry.tcpConnLocalAddress, entry.tcpConnLocalPort, + &entry.tcpConnRemAddress, entry.tcpConnRemPort, + entry.tcpConnEntryInfo.ce_state, + (unsigned long)(entry.tcpConnEntryInfo.ce_rnxt - + entry.tcpConnEntryInfo.ce_rack), + (unsigned long)(entry.tcpConnEntryInfo.ce_snxt - + entry.tcpConnEntryInfo.ce_suna), + (unsigned long)entry.tcpConnEntryInfo.ce_rwnd, + (unsigned long)entry.tcpConnEntryInfo.ce_swnd, + (unsigned long)entry.tcpConnEntryInfo.ce_mss); + } +} + +static void read_tcp6_table(const struct mib_table *table) { + size_t offset; + + for (offset = 0; + offset + sizeof(mib2_tcp6ConnEntry_t) <= table->length; + offset += sizeof(mib2_tcp6ConnEntry_t)) { + mib2_tcp6ConnEntry_t entry; + + memcpy(&entry, table->data + offset, sizeof(entry)); + append_state(AF_INET6, SOCK_STREAM, entry.tcp6ConnCreationProcess, + &entry.tcp6ConnLocalAddress, entry.tcp6ConnLocalPort, + &entry.tcp6ConnRemAddress, entry.tcp6ConnRemPort, + entry.tcp6ConnEntryInfo.ce_state, + (unsigned long)(entry.tcp6ConnEntryInfo.ce_rnxt - + entry.tcp6ConnEntryInfo.ce_rack), + (unsigned long)(entry.tcp6ConnEntryInfo.ce_snxt - + entry.tcp6ConnEntryInfo.ce_suna), + (unsigned long)entry.tcp6ConnEntryInfo.ce_rwnd, + (unsigned long)entry.tcp6ConnEntryInfo.ce_swnd, + (unsigned long)entry.tcp6ConnEntryInfo.ce_mss); + } +} + +static void read_udp_table(const struct mib_table *table) { + size_t offset; + + for (offset = 0; offset + sizeof(mib2_udpEntry_t) <= table->length; + offset += sizeof(mib2_udpEntry_t)) { + mib2_udpEntry_t entry; + + memcpy(&entry, table->data + offset, sizeof(entry)); + append_state(AF_INET, SOCK_DGRAM, entry.udpCreationProcess, + &entry.udpLocalAddress, entry.udpLocalPort, + &entry.udpEntryInfo.ue_RemoteAddress, + entry.udpEntryInfo.ue_RemotePort, + udp_tpi_state(entry.udpEntryInfo.ue_state), 0, 0, 0, 0, 0); + } +} + +static void read_udp6_table(const struct mib_table *table) { + size_t offset; + + for (offset = 0; offset + sizeof(mib2_udp6Entry_t) <= table->length; + offset += sizeof(mib2_udp6Entry_t)) { + mib2_udp6Entry_t entry; + + memcpy(&entry, table->data + offset, sizeof(entry)); + append_state(AF_INET6, SOCK_DGRAM, entry.udp6CreationProcess, + &entry.udp6LocalAddress, entry.udp6LocalPort, + &entry.udp6EntryInfo.ue_RemoteAddress, + entry.udp6EntryInfo.ue_RemotePort, + udp_tpi_state(entry.udp6EntryInfo.ue_state), 0, 0, 0, 0, + 0); + } +} + +static void consume_table(const struct mib_table *table) { + if (table->level == MIB2_TCP && table->name == MIB2_TCP_CONN) + read_tcp_table(table); + else if (table->level == MIB2_TCP6 && table->name == MIB2_TCP6_CONN) + read_tcp6_table(table); + else if (table->level == MIB2_UDP && table->name == MIB2_UDP_ENTRY) + read_udp_table(table); + else if (table->level == MIB2_UDP6 && table->name == MIB2_UDP6_ENTRY) + read_udp6_table(table); +} + +int sun_sockstate_load(void) { + struct mib_table *tables; + struct mib_table *table; + int fd; + + sun_sockstate_free(); + fd = open_mib_stream(); + if (fd < 0) + return (1); + + tables = read_mib_stream(fd); + (void)close(fd); + for (table = tables; table; table = table->next) + consume_table(table); + release_tables(tables); + return (0); +} + +void sun_sockstate_free(void) { + free(SocketStates); + SocketStates = (struct sun_sockstate *)NULL; + SocketStateCount = SocketStateCapacity = 0; +} + +static int same_address(const unsigned char *a, const unsigned char *b, + int family) { + return (memcmp(a, b, family == AF_INET ? 4 : 16) == 0); +} + +static int endpoint_matches(const struct sun_sockstate *entry, int family, + const void *local, int local_port, + const void *foreign, int foreign_port) { + if (entry->family != family || entry->lport != (uint16_t)local_port) + return (0); + if (local && entry->laddr[0] && + !same_address(entry->laddr, (const unsigned char *)local, family)) + return (0); + if (foreign && foreign_port) { + if (!entry->fport || entry->fport != (uint16_t)foreign_port || + !same_address(entry->faddr, (const unsigned char *)foreign, + family)) + return (0); + } + return (1); +} + +const struct sun_sockstate *sun_sockstate_find(int pid, int family, + const void *local, int local_port, + const void *foreign, + int foreign_port) { + const struct sun_sockstate *fallback = NULL; + size_t i; + + for (i = 0; i < SocketStateCount; i++) { + const struct sun_sockstate *entry = &SocketStates[i]; + + if (!endpoint_matches(entry, family, local, local_port, foreign, + foreign_port)) + continue; + if (!entry->pid || !pid || entry->pid == (uint32_t)pid) + return (entry); + if (!fallback) + fallback = entry; + } + return (fallback); +} diff --git a/lib/dialects/sun/dsock.h b/lib/dialects/sun/dsock.h new file mode 100644 index 00000000..c75c6e05 --- /dev/null +++ b/lib/dialects/sun/dsock.h @@ -0,0 +1,28 @@ +/* + * dsock.h - Solaris/illumos MIB2 socket state lookup + */ + +#if !defined(SOLARIS_DSOCK_H) +# define SOLARIS_DSOCK_H 1 + +# include + +struct sun_sockstate { + int family; + int type; + uint32_t pid; + uint16_t lport; + uint16_t fport; + unsigned char laddr[16]; + unsigned char faddr[16]; + int state; + unsigned long rq, sq, rw, ww, mss; +}; + +int sun_sockstate_load(void); +void sun_sockstate_free(void); +const struct sun_sockstate *sun_sockstate_find(int pid, int family, + const void *laddr, int lport, + const void *faddr, int fport); + +#endif /* SOLARIS_DSOCK_H */ diff --git a/lib/dialects/sun/machine.h b/lib/dialects/sun/machine.h index 60cee3dd..007b1e7a 100644 --- a/lib/dialects/sun/machine.h +++ b/lib/dialects/sun/machine.h @@ -379,6 +379,8 @@ * on socket options (SO_* symbols), socket states (SS_* symbols) and TCP * options. */ +# define HASSOOPT 1 +# define HASTCPOPT 1 /* @@ -423,6 +425,13 @@ # define HASTCPUDPSTATE 1 +/* + * The MIB2 TCP connection table provides queue and window values. + */ + +# define HASTCPTPIQ 1 +# define HASTCPTPIW 1 + /* * HASTMPNODE is defined for those dialects that have tmpnodes. */ diff --git a/src/dialects/sun/dprint.c b/src/dialects/sun/dprint.c index dcb77e89..eac84cc3 100644 --- a/src/dialects/sun/dprint.c +++ b/src/dialects/sun/dprint.c @@ -28,6 +28,8 @@ * dprint.c - Solaris CLI printing functions */ +#include + #include "common.h" /* @@ -48,8 +50,9 @@ void print_tcptpi(struct lsof_context *ctx, /* context */ case 0: /* TCP */ if (!TcpSt) (void)build_IPstates(ctx); - if (Lf->lts.state.i < 0 || - (i = Lf->lts.state.i + TcpStOff) >= TcpNstates) { + if (Lf->lts.state.i == INT_MIN || + (i = Lf->lts.state.i + TcpStOff) < 0 || + i >= TcpNstates) { (void)snpf(sbuf, sizeof(sbuf), "UNKNOWN_TCP_STATE_%d", Lf->lts.state.i); cp = sbuf; @@ -79,6 +82,196 @@ void print_tcptpi(struct lsof_context *ctx, /* context */ } } + if (Ftcptpi & TCPTPI_QUEUES) { + if (Lf->lts.rqs) { + if (Ffield) + putchar(LSOF_FID_TCPTPI); + else if (ps) + putchar(' '); + else + putchar('('); + (void)printf("QR=%lu", Lf->lts.rq); + if (Ffield) + putchar(Terminator); + ps++; + } + if (Lf->lts.sqs) { + if (Ffield) + putchar(LSOF_FID_TCPTPI); + else if (ps) + putchar(' '); + else + putchar('('); + (void)printf("QS=%lu", Lf->lts.sq); + if (Ffield) + putchar(Terminator); + ps++; + } + } + + if (Ftcptpi & TCPTPI_FLAGS) { + int opt; + + if ((opt = Lf->lts.opt) || Lf->lts.qlims || Lf->lts.rbszs || + Lf->lts.sbszs) { + char sep = ' '; + + if (Ffield) + sep = LSOF_FID_TCPTPI; + else if (!ps) + sep = '('; + (void)printf("%cSO", sep); + ps++; + sep = '='; + +#if defined(SO_ACCEPTCONN) + opt &= ~SO_ACCEPTCONN; +#endif + +#if defined(SO_BROADCAST) + if (opt & SO_BROADCAST) { + (void)printf("%cBROADCAST", sep); + opt &= ~SO_BROADCAST; + sep = ','; + } +#endif +#if defined(SO_DEBUG) + if (opt & SO_DEBUG) { + (void)printf("%cDEBUG", sep); + opt &= ~SO_DEBUG; + sep = ','; + } +#endif +#if defined(SO_DGRAM_ERRIND) + if (opt & SO_DGRAM_ERRIND) { + (void)printf("%cDGRAM_ERRIND", sep); + opt &= ~SO_DGRAM_ERRIND; + sep = ','; + } +#endif +#if defined(SO_DONTROUTE) + if (opt & SO_DONTROUTE) { + (void)printf("%cDONTROUTE", sep); + opt &= ~SO_DONTROUTE; + sep = ','; + } +#endif +#if defined(SO_KEEPALIVE) + if (opt & SO_KEEPALIVE) { + (void)printf("%cKEEPALIVE", sep); + opt &= ~SO_KEEPALIVE; + sep = ','; + } +#endif +#if defined(SO_LINGER) + if (opt & SO_LINGER) { + (void)printf("%cLINGER", sep); + if (Lf->lts.ltm) + (void)printf("=%d", Lf->lts.ltm); + opt &= ~SO_LINGER; + sep = ','; + } +#endif +#if defined(SO_OOBINLINE) + if (opt & SO_OOBINLINE) { + (void)printf("%cOOBINLINE", sep); + opt &= ~SO_OOBINLINE; + sep = ','; + } +#endif + + if (Lf->lts.qlims) { + (void)printf("%cQLIM=%u", sep, Lf->lts.qlim); + sep = ','; + } + if (Lf->lts.rbszs) { + (void)printf("%cRCVBUF=%lu", sep, Lf->lts.rbsz); + sep = ','; + } +#if defined(SO_REUSEADDR) + if (opt & SO_REUSEADDR) { + (void)printf("%cREUSEADDR", sep); + opt &= ~SO_REUSEADDR; + sep = ','; + } +#endif + if (Lf->lts.sbszs) { + (void)printf("%cSNDBUF=%lu", sep, Lf->lts.sbsz); + sep = ','; + } +#if defined(SO_TIMESTAMP) + if (opt & SO_TIMESTAMP) { + (void)printf("%cTIMESTAMP", sep); + opt &= ~SO_TIMESTAMP; + sep = ','; + } +#endif +#if defined(SO_USELOOPBACK) + if (opt & SO_USELOOPBACK) { + (void)printf("%cUSELOOPBACK", sep); + opt &= ~SO_USELOOPBACK; + sep = ','; + } +#endif + if (opt) + (void)printf("%cUNKNOWN=%#x", sep, opt); + if (Ffield) + putchar(Terminator); + } + } + + if (Ftcptpi & TCPTPI_FLAGS) { + int topt = 0; + + if (topt || Lf->lts.msss) { + char sep = ' '; + + if (Ffield) + sep = LSOF_FID_TCPTPI; + else if (!ps) + sep = '('; + (void)printf("%cTF", sep); + ps++; + sep = '='; + + if (Lf->lts.msss) { + (void)printf("%cMSS=%lu", sep, Lf->lts.mss); + sep = ','; + } + if (topt) + (void)printf("%cUNKNOWN=%#x", sep, topt); + if (Ffield) + putchar(Terminator); + } + } + + if (Ftcptpi & TCPTPI_WINDOWS) { + if (Lf->lts.rws) { + if (Ffield) + putchar(LSOF_FID_TCPTPI); + else if (ps) + putchar(' '); + else + putchar('('); + (void)printf("WR=%lu", Lf->lts.rw); + if (Ffield) + putchar(Terminator); + ps++; + } + if (Lf->lts.wws) { + if (Ffield) + putchar(LSOF_FID_TCPTPI); + else if (ps) + putchar(' '); + else + putchar('('); + (void)printf("WW=%lu", Lf->lts.ww); + if (Ffield) + putchar(Terminator); + ps++; + } + } + if (Ftcptpi && !Ffield && ps) putchar(')'); if (nl)