Skip to content

Commit

Permalink
Expose functionality as a re-usable library - Part 3 (#285)
Browse files Browse the repository at this point in the history
* Do not check Fsize & Foffsete in lib

* Do not check Fnlink in lib

* Do not check Ftcptpi in lib

* Do not check Fsv in lib

* Convert Lf->access to enum

* Add missing lib/print.c to Makefile.am

* Rename print_access to access_to_char
  • Loading branch information
jiegec authored Apr 8, 2023
1 parent fa52e18 commit de59572
Show file tree
Hide file tree
Showing 45 changed files with 1,176 additions and 1,361 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ EXTRA_DIST += tests/00README tests/TestDB tests/CkTestDB tests/Makefile tests/Ls

# Dialect neutral sources
lsof_SOURCES = src/arg.c src/main.c lib/node.c src/print.c src/ptti.c src/store.c src/usage.c src/util.c \
lib/ckkv.c lib/cvfs.c lib/dvch.c lib/fino.c lib/isfn.c lib/lkud.c lib/misc.c lib/pdvn.c lib/prfp.c lib/proc.c lib/ptti.c lib/rdev.c lib/rnmt.c lib/rmnt.c lib/rnam.c lib/rnch.c lib/rnmh.c
lib/ckkv.c lib/cvfs.c lib/dvch.c lib/fino.c lib/isfn.c lib/lkud.c lib/misc.c lib/pdvn.c lib/prfp.c lib/print.c lib/proc.c lib/ptti.c lib/rdev.c lib/rnmt.c lib/rmnt.c lib/rnam.c lib/rnch.c lib/rnmh.c
lsof_SOURCES += lib/common.h include/lsof_fields.h lib/proto.h lib/hash.h
include_HEADERS = include/lsof.h include/lsof_fields.h
DIALECT_ROOT = $(top_srcdir)/lib/dialects
Expand Down
47 changes: 47 additions & 0 deletions include/lsof.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @file
* lsof.h - header file 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.
*
* 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.
*/

/*
* $Id: lsof.h,v 1.70 2018/03/26 21:50:45 abe Exp $
*/

#if !defined(LSOF_H)
# define LSOF_H 1

/** File access mode */
enum lsof_file_access_mode {
LSOF_FILE_ACCESS_NONE = 0, /**< None */
LSOF_FILE_ACCESS_READ = 1, /**< Read */
LSOF_FILE_ACCESS_WRITE = 2, /**< Write */
LSOF_FILE_ACCESS_READ_WRITE =
LSOF_FILE_ACCESS_READ | LSOF_FILE_ACCESS_WRITE, /**< Read and write */
};

#endif
7 changes: 4 additions & 3 deletions lib/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
* $Id: lsof.h,v 1.70 2018/03/26 21:50:45 abe Exp $
*/

#if !defined(LSOF_H)
# define LSOF_H 1
#include "lsof.h"
#if !defined(COMMON_H)
# define COMMON_H 1

# if defined(AUTOTOOLS)
# include "autotools.h"
Expand Down Expand Up @@ -795,7 +796,7 @@ extern char *InodeFmt_x;
extern int LastPid;

struct lfile {
char access;
enum lsof_file_access_mode access;
char lock;
unsigned char dev_def; /* device number definition status */
unsigned char inp_ty; /* inode/iproto type
Expand Down
168 changes: 76 additions & 92 deletions lib/dialects/aix/dnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,127 +879,114 @@ void process_node(va) KA_T va; /* vnode kernel space address */
/*
* Obtain the file size.
*/
if (Foffset)
Lf->off_def = 1;
else {
switch (Ntype) {

switch (Ntype) {
#if defined(HAS_AFS)
case N_AFS:
Lf->sz = (SZOFFTYPE)an.size;
Lf->sz_def = 1;
break;
case N_AFS:
Lf->sz = (SZOFFTYPE)an.size;
Lf->sz_def = 1;
break;
#endif /* defined(HAS_AFS) */

#if AIXV >= 3200
case N_FIFO:
Lf->sz = (SZOFFTYPE)f.ff_size;
Lf->sz_def = 1;
break;
case N_FIFO:
Lf->sz = (SZOFFTYPE)f.ff_size;
Lf->sz_def = 1;
break;
#endif /* AIXV>=3200 */

#if defined(HAS_NFS)
case N_NFS:
if (nfss) {
Lf->sz = (SZOFFTYPE)nfs_attr.va_size;
Lf->sz_def = 1;
}
break;
case N_NFS:
if (nfss) {
Lf->sz = (SZOFFTYPE)nfs_attr.va_size;
Lf->sz_def = 1;
}
break;
#endif /* defined(HAS_NFS) */

#if defined(HAS_SANFS)
case N_SANFS:
if (sans) {

/*
* DEBUG: this code is insufficient. It can't be completed
* until IBM makes the SANFS header files available in
* /usr/include.
*/
/* Lf->sz = (SZOFFTYPE)??? DEBUG */
Lf->sz_def = 1;
}
break;
case N_SANFS:
if (sans) {

/*
* DEBUG: this code is insufficient. It can't be completed
* until IBM makes the SANFS header files available in
* /usr/include.
*/
/* Lf->sz = (SZOFFTYPE)??? DEBUG */
Lf->sz_def = 1;
}
break;
#endif /* defined(HAS_SANFS) */

#if AIXV >= 3200
case N_BLK:
if (!Fsize)
Lf->off_def = 1;
break;
case N_CHR:
case N_MPC:
if (!Fsize)
Lf->off_def = 1;
break;
case N_BLK:
break;
case N_CHR:
case N_MPC:
break;
#endif /* AIXV>=3200 */

case N_REGLR:
if (type == VREG || type == VDIR) {
if (ins) {
Lf->sz = (SZOFFTYPE)i.size;
Lf->sz_def = i.size_def;
}
} else if (((type == VBLK) || (type == VCHR) || (type == VMPC)) &&
!Fsize)
Lf->off_def = 1;
break;
case N_REGLR:
if (type == VREG || type == VDIR) {
if (ins) {
Lf->sz = (SZOFFTYPE)i.size;
Lf->sz_def = i.size_def;
}
}
break;
}
/*
* Record link count.
*/
if (Fnlink) {
switch (Ntype) {
switch (Ntype) {

#if defined(HAS_AFS)
case N_AFS:
Lf->nlink = an.nlink;
Lf->nlink_def = an.nlink_st;
break;
case N_AFS:
Lf->nlink = an.nlink;
Lf->nlink_def = an.nlink_st;
break;
#endif /* defined(HAS_AFS) */

#if defined(HAS_NFS)
case N_NFS:
if (nfss) {
Lf->nlink = (long)nfs_attr.va_nlink;
Lf->nlink_def = 1;
}
break;
case N_NFS:
if (nfss) {
Lf->nlink = (long)nfs_attr.va_nlink;
Lf->nlink_def = 1;
}
break;
#endif /* defined(HAS_NFS) */

#if defined(HAS_SANFS)
case N_SANFS:
if (sans) {

/*
* DEBUG: this code is insufficient. It can't be completed
* until IBM makes the SANFS header files available in
* /usr/include.
*/
/* Lf->nlink = (long)??? DEBUG */
Lf->nlink_def = 1;
}
break;
case N_SANFS:
if (sans) {

/*
* DEBUG: this code is insufficient. It can't be completed
* until IBM makes the SANFS header files available in
* /usr/include.
*/
/* Lf->nlink = (long)??? DEBUG */
Lf->nlink_def = 1;
}
break;
#endif /* defined(HAS_SANFS) */

#if AIXV >= 3200
case N_BLK:
case N_CHR:
case N_FIFO:
case N_MPC:
case N_BLK:
case N_CHR:
case N_FIFO:
case N_MPC:
#endif /* AIXV>=3200 */

case N_REGLR:
if (ins) {
Lf->nlink = (long)i.nlink;
Lf->nlink_def = i.nlink_def;
}
break;
case N_REGLR:
if (ins) {
Lf->nlink = (long)i.nlink;
Lf->nlink_def = i.nlink_def;
}
if (Nlink && Lf->nlink_def && (Lf->nlink < Nlink))
Lf->sf |= SELNLINK;
break;
}
if (Nlink && Lf->nlink_def && (Lf->nlink < Nlink))
Lf->sf |= SELNLINK;

#if defined(HAS_NFS)
/*
Expand Down Expand Up @@ -1181,13 +1168,10 @@ void process_shmt(sa) KA_T sa; /* shared memory transport node struct
}
enter_dev_ch(print_kptr(sa, (char *)NULL, 0));
/*
* If offset display has been requested or if buffer size less free bytes is
* negative, enable offset display. Otherwise set the file size as buffer
* size less free bytes.
* If buffer size is less than free bytes, enable offset display. Otherwise
* set the file size as buffer size less free bytes.
*/
if (Foffset || mn.free > mn.sz)
Lf->off_def = 1;
else {
if (mn.free <= mn.sz) {
Lf->sz = (SZOFFTYPE)(mn.sz - mn.free);
Lf->sz_def = 1;
}
Expand Down
9 changes: 5 additions & 4 deletions lib/dialects/aix/dproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,12 +722,11 @@ void gather_proc_info() {
if (Lf->sf) {

#if defined(HASFSTRUCT)
if (Fsv & FSV_FG)

# if AIXV < 4300
Lf->pof = (long)(Up->u_ufd[i].flags & 0x7f);
Lf->pof = (long)(Up->u_ufd[i].flags & 0x7f);
# else /* AIXV>=4300 */
Lf->pof = (long)(fds->pi_ufd[i].flags & 0x7f);
Lf->pof = (long)(fds->pi_ufd[i].flags & 0x7f);
# endif /* AIXV<4300 */
#endif /* defined(HASFSTRUCT) */

Expand Down Expand Up @@ -1363,7 +1362,7 @@ static void process_text(pid) pid_t pid; /* process PID */
*/
(void)snpf(fd, sizeof(fd), "L%02d", i++);
alloc_lfile(fd, -1);
Lf->dev_def = Lf->inp_ty = Lf->nlink_def = Lf->sz_def = 1;
Lf->dev_def = Lf->inp_ty = Lf->nlink_def = 1;
Lf->dev = sb.st_dev;
Lf->inode = (INODETYPE)sb.st_ino;
(void)snpf(Lf->type, sizeof(Lf->type), "VREG");
Expand All @@ -1380,6 +1379,7 @@ static void process_text(pid) pid_t pid; /* process PID */
nm = sp->nm;
Lf->nlink = sp->nlink;
Lf->sz = sp->sz;
Lf->sz_def = 1;
break;
}
}
Expand All @@ -1392,6 +1392,7 @@ static void process_text(pid) pid_t pid; /* process PID */
nm = pp;
Lf->nlink_def = sb.st_nlink;
Lf->sz = sb.st_size;
Lf->sz_def = 1;
}
/*
* Do selection tests: NFS; link count; file name; and file system.
Expand Down
19 changes: 7 additions & 12 deletions lib/dialects/aix/dsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,13 @@ void process_socket(sa) KA_T sa; /* socket address in kernel */
/*
* Save size information.
*/
if (Fsize) {
if (Lf->access == 'r')
Lf->sz = (SZOFFTYPE)s.so_rcv.sb_cc;
else if (Lf->access == 'w')
Lf->sz = (SZOFFTYPE)s.so_snd.sb_cc;
else
Lf->sz = (SZOFFTYPE)(s.so_rcv.sb_cc + s.so_snd.sb_cc);
Lf->sz_def = 1;
} else
Lf->off_def = 1;
if (Lf->access == LSOF_FILE_ACCESS_READ)
Lf->sz = (SZOFFTYPE)s.so_rcv.sb_cc;
else if (Lf->access == LSOF_FILE_ACCESS_WRITE)
Lf->sz = (SZOFFTYPE)s.so_snd.sb_cc;
else
Lf->sz = (SZOFFTYPE)(s.so_rcv.sb_cc + s.so_snd.sb_cc);
Lf->sz_def = 1;

#if defined(HASTCPTPIQ)
Lf->lts.rq = s.so_rcv.sb_cc;
Expand Down Expand Up @@ -304,8 +301,6 @@ void process_socket(sa) KA_T sa; /* socket address in kernel */
enter_dev_ch(print_kptr((KA_T)(s.so_pcb), (char *)NULL, 0));
else
(void)snpf(Namech, Namechl, "no protocol control block");
if (!Fsize)
Lf->off_def = 1;
break;
/*
* Process a Unix domain socket.
Expand Down
Loading

0 comments on commit de59572

Please sign in to comment.