Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Expose functionality as a re-usable library #279

Closed
wants to merge 136 commits into from
Closed

Conversation

jiegec
Copy link
Contributor

@jiegec jiegec commented Feb 7, 2023

This pr implements #268 by splitting code into two parts: liblsof(the library) and lsof(the cli). The pr is unfinished, but is half way done. It is a VERY BIG pull request and I try my best to maintain the lsof cli behavior. I am opening this pr to request for more comments. Progress:

  • Re-arrange directory structure, build liblsof using libtool.
  • Add initial library interface with Doxygen.
  • Move global and static variables to lsof context.
  • Rewrite LTbasic & LTszoff with liblsof.
  • Set visibility to hidden for internal functions of liblsof.
  • Migrate to Linux/Darwin/FreeBSD/NetBSD/OpenBSD/Solaris/OpenIndiana. Support for AIX/HP-UX/SCO/UnixWare will be broken, but I do not have those environments to fix.
  • Write some utilities with liblsof as an example, maybe re-implement a netstat?
  • Selection result reporting, implemented as an array of tagged union, e.g. selection by pid success, selection by command name failed etc.
  • Add optional fields to struct lsof_file, e.g. network address, TCP state.
  • Allow liblsof to read info when running as non-root on NetBSD and Solaris by switching to /proc-based. Postponed to future pr
  • Replace all exit calls in liblsof with proper error reporting in a best effort manner. If a call is not handled, fix in future pr.
  • Migrate more options from cli to liblsof until cli only uses public interface of liblsof. Options are already migrated, but it is hard to remove access of private structures while maintaining compatibility.
  • Fix memory leaks in a best effort manner. If any leak is missed, fix in future pr.

I'd love to see more comments on the API interface (You can find some at #268).

jiegec added 30 commits January 28, 2023 11:30
Add initial library interface with Doxygen.
Move global and static variables to lsof context.
Fix LTbasic test by locating real lsof binary.
Don't check Foffset/Fsize in dialect, only check them in print.c.
Convert string-based fd column to enum + int.
Rewrite LTbasic with liblsof.
@BenBE
Copy link

BenBE commented Apr 2, 2023

NB: @jiegec Quick question as I don't seem to see it in the changes, but kinda expected to: Will there be some configuration file(s) for pkg-config with the new library, or will using the lib externally somewhat boil down on fixed (include) paths for finding the necessary ingredients?

@jiegec
Copy link
Contributor Author

jiegec commented Apr 3, 2023

NB: @jiegec Quick question as I don't seem to see it in the changes, but kinda expected to: Will there be some configuration file(s) for pkg-config with the new library, or will using the lib externally somewhat boil down on fixed (include) paths for finding the necessary ingredients?

Okay, I will add .pc file to locate the new library. I am currently working on the second part at branch library-part2: adding context arguments.

Copy link

@BenBE BenBE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be more careful with your changes.

Comment on lines 55 to 56
static void make_devtp (struct lsof_context * ctx, struct stat *s, char *p);
static int rmdupdev (struct lsof_context * ctx, struct l_dev ***dp, int n, int ty);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not remove the whitespace after the function name?

@@ -39,7 +39,7 @@ static char copyright[] =
* Local static definitions
*/

_PROTOTYPE(static char *getmntdev, (char *o, int l, struct stat *s, char *f));
static char *getmntdev, (char *o, int l, struct stat *s char *f);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray comma after function name.

_PROTOTYPE(static int LNC_enter,
(struct lnch * le, char *nm, int nl, char *fs));
_PROTOTYPE(static void LNC_nosp, (int len));
static int LNC_enter (struct lnch * le, char *nm, int nl, char *fs);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe remove the whitespace after the name …

@@ -987,29 +987,28 @@ static struct lnch *LNC_nc = (struct lnch *)NULL;
* Local function prototypes
*/

_PROTOTYPE(static struct lnch *DIN_addr, (dev_t * d, unsigned long i));
static struct lnch *DIN_addr, (dev_t * d unsigned long i);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray comma after function name

@@ -35,8 +35,7 @@ static char copyright[] =

#include "common.h"

_PROTOTYPE(static struct l_dev *finddev,
(dev_t * dev, dev_t *rdev, int stream));
static struct l_dev *finddev (dev_t * dev, dev_t *rdev, int stream);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the whitespace?

tests/LTlock.c Outdated
_PROTOTYPE(static char *unlkfile, (int ty));
static void cleanup (void);
static char *lkfile (int ty);
static char *tstwlsof, (char *opt char *xlk);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray comma

tests/LTnfs.c Outdated
_PROTOTYPE(static void cleanup, (void));
_PROTOTYPE(static char *FindNFSfile, (int *ff, char *szbuf));
static void cleanup (void);
static char *FindNFSfile, (int *ff char *szbuf);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray comma

tests/LTnlink.c Outdated
_PROTOTYPE(static char *FindFile, (char *opt, int *ff, int ie, LTdev_t *tfdc,
char *ibuf, char *xlnk, char *szbuf));
static void cleanup (void);
static char *FindFile, (char *opt, int *ff, int ie, LTdev_t *tfdc char *ibuf, char *xlnk, char *szbuf);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray comma

tests/LTszoff.c Outdated
_PROTOTYPE(static void cleanup, (void));
_PROTOTYPE(static char *testlsof, (int tt, char *opt, char *xval));
static void cleanup (void);
static char *testlsof, (int tt, char *opt char *xval);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray comma

tests/LsofTest.h Outdated
Comment on lines 333 to 344
extern char *CanRdKmem (void);
extern char *ConvStatDev, (dev_t * dev LTdev_t *ldev);
extern char *ConvLsofDev, (char *dev LTdev_t *ldev);
extern char *ExecLsof (char **opt);
extern char *IsLsofExec (void);
extern void LTlibClean (void);
extern char *MkStrCpy, (char *src int *len);
extern LTfldo_t *RdFrLsof, (int *nf char **em);
extern void PrtMsg, (char *mp char *pn);
extern void PrtMsgX, (char *mp, char *pn, void (*f)() int xv);
extern int ScanArg, (int ac, char *av[], char *opt char *pn);
extern void StopLsof (void);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray comma

@jiegec
Copy link
Contributor Author

jiegec commented Apr 13, 2023

Please be more careful with your changes.

Thanks, I was removing _PROTOTYPE via regex, but the matching does not work well on multiline function declarations.. I do not plan to merge this large pr, but take effort to complete in parts (working on part4 currently). This serves as a reminder for me the work need to be done.

@jiegec jiegec closed this Apr 13, 2023
Copy link

@BenBE BenBE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are some function signatures reverted back to K&R style?

Comment on lines +101 to +107
int read_vxnode(v, vfs, dev, devs, rdev, rdevs)
struct vnode *v; /* local containing vnode */
struct l_vfs *vfs; /* local vfs structure */
dev_t *dev; /* device number receiver */
int *devs; /* device status receiver */
dev_t *rdev; /* raw device number receiver */
int *rdevs; /* raw device status receiver */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not rewrite this to more modern C like with so many other function declarations?

@jiegec
Copy link
Contributor Author

jiegec commented Apr 13, 2023

Why are some function signatures reverted back to K&R style?

The master branch was merged via git merge -s ours because there are too many conflicts.

@BenBE
Copy link

BenBE commented Apr 13, 2023

NB: Ah, k. Maybe have a look at git rerere, which should help with such situations. Also I noticed kdiff3 usually does a quite good job at resolving most conflicts correctly automatically.

@jiegec jiegec mentioned this pull request Apr 16, 2023
@BenBE
Copy link

BenBE commented May 4, 2023

@jiegec Any further open aspects/items for this feature? AFAICS the bullet list in the first post has not been checked off completely.

@jiegec jiegec mentioned this pull request May 12, 2023
@jiegec jiegec mentioned this pull request Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants