Skip to content

Commit 98d72f1

Browse files
authored
Merge pull request #338 from Uptycs/aix
Issue 326: Fix AIX dialect to work with context refactor
2 parents cc2de24 + 1c463a6 commit 98d72f1

File tree

9 files changed

+120
-60
lines changed

9 files changed

+120
-60
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ lib/dialects/netbsd/include
101101
/dmnt.c
102102
/dnode.c
103103
/dnode1.c
104+
/dnode2.c
104105
/dproc.c
105106
/dproto.h
106107
/dsock.c

lib/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
#if !defined(COMMON_H)
3636
# define COMMON_H 1
3737

38-
# include "lsof.h"
3938
# if defined(AUTOTOOLS)
4039
# include "autotools.h"
4140
# endif
4241
# include "machine.h"
42+
# include "lsof.h"
4343

4444
# if !defined(FSV_DEFAULT)
4545
# define FSV_DEFAULT 0

lib/dialects/aix/ddev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,15 @@ int rw_clone_sect(struct lsof_context *ctx, /* context */
634634
for (c = Clone, n = 0; c; c = c->next, n++)
635635
;
636636
(void)snpf(buf, sizeof(buf), "clone section: %d\n", n);
637-
if (wr2DCfd(buf, &DCcksum))
637+
if (wr2DCfd(ctx, buf, &DCcksum))
638638
return (1);
639639
/*
640640
* Write the clone section lines.
641641
*/
642642
for (c = Clone; c; c = c->next) {
643643
(void)snpf(buf, sizeof(buf), "%x %ld %s\n", c->cd.rdev,
644644
(long)c->cd.inode, c->cd.name);
645-
if (wr2DCfd(buf, &DCcksum))
645+
if (wr2DCfd(ctx, buf, &DCcksum))
646646
return (1);
647647
}
648648
return (0);

lib/dialects/aix/dlsof.h

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,6 @@ struct clone {
306306
struct l_dev cd; /* device, inode, name, verify status */
307307
struct clone *next; /* next entry */
308308
};
309-
extern struct clone *Clone;
310-
extern int CloneMaj;
311-
extern int ClonePtc;
312309
# endif /* AIXV>=4140 */
313310

314311
/*
@@ -339,7 +336,6 @@ struct l_vfs {
339336
int vmt_gfstype; /* vmount gfs type */
340337
struct l_vfs *next; /* forward link */
341338
};
342-
extern struct l_vfs *Lvfs;
343339

344340
/*
345341
* Local mount information
@@ -363,7 +359,6 @@ struct mounts {
363359

364360
struct mounts *next; /* forward link */
365361
};
366-
extern struct mounts *Mtab;
367362

368363
/*
369364
* Search file information
@@ -396,13 +391,8 @@ extern struct nlist AFSnl[]; /* AFS kernel symbol name list table */
396391
extern char *AFSApath; /* alternate AFS name list path (from -a) */
397392
# endif /* defined(HASAOPT) */
398393

399-
extern KA_T AFSVfsp; /* AFS struct vfs kernel pointer */
400394
# endif /* defined(HAS_AFS) */
401395

402-
extern int Kd;
403-
extern int Km;
404-
extern struct nlist Nl[];
405-
406396
# if defined(TCPSTATES) && AIXV <= 3250
407397
/*
408398
* For AIX 3.2.5 and below, there is no header file with the definition
@@ -427,6 +417,37 @@ static char *tcpstates[] = {"CLOSED", "LISTEN", "SYN_SENT",
427417
* in libc.so */
428418
# endif /* AIXA>1 */
429419

430-
struct lsof_context_dialect {};
420+
struct lsof_context_dialect {
421+
int kd; /* /dev/kmem file descriptor */
422+
int km; /* /dev/mem file descriptor */
423+
struct l_vfs *lvfs; /* local vfs structure table */
424+
struct mounts *mtab; /* local mount table */
425+
426+
#if AIXV >= 4140
427+
struct clone *clone; /* local clone information */
428+
int clone_maj; /* clone major device number */
429+
int clone_ptc; /* /dev/ptc minor device number */
430+
#endif /* AIXV>=4140 */
431+
432+
#if defined(HAS_AFS)
433+
KA_T afs_vfsp; /* AFS vfs struct kernel pointer */
434+
#endif /* defined(HAS_AFS) */
435+
};
436+
437+
/* Convenience macros to access dialect-specific context */
438+
#define Kd (ctx->dialect.kd)
439+
#define Km (ctx->dialect.km)
440+
#define Lvfs (ctx->dialect.lvfs)
441+
#define Mtab (ctx->dialect.mtab)
442+
443+
#if AIXV >= 4140
444+
#define Clone (ctx->dialect.clone)
445+
#define CloneMaj (ctx->dialect.clone_maj)
446+
#define ClonePtc (ctx->dialect.clone_ptc)
447+
#endif /* AIXV>=4140 */
448+
449+
#if defined(HAS_AFS)
450+
#define AFSVfsp (ctx->dialect.afs_vfsp)
451+
#endif /* defined(HAS_AFS) */
431452

432453
#endif /* AIX_LSOF_H */

lib/dialects/aix/dnode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ void process_node(struct lsof_context *ctx, /* context */
495495
*/
496496
Namech[0] = '\0';
497497
Lf->inp_ty = 0;
498-
(void)process_socket(ka);
498+
(void)process_socket(ctx, ka);
499499
return;
500500
}
501501
/*

lib/dialects/aix/dproc.c

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ static char copyright[] =
3434
#endif
3535

3636
#include "common.h"
37+
#include <fcntl.h>
38+
39+
#if defined(SIGDANGER)
40+
/* Static context pointer for signal handler */
41+
static struct lsof_context *signal_ctx = NULL;
42+
#endif
3743

3844
static void get_kernel_access(struct lsof_context *ctx);
3945

@@ -45,7 +51,7 @@ static struct le *getle(struct lsof_context *ctx, KA_T a, KA_T sid, char **err);
4551
static void getlenm(struct lsof_context *ctx, struct le *le, KA_T sid);
4652
#endif /* AIXV>=4110 */
4753

48-
static int kreadx(KA_T addr, char *buf, int len, KA_T sid);
54+
static int kreadx(struct lsof_context *ctx, KA_T addr, char *buf, int len, KA_T sid);
4955

5056
#if AIXA < 2
5157
static void process_text(struct lsof_context *ctx, KA_T sid);
@@ -56,9 +62,9 @@ static void process_text(struct lsof_context *ctx, pid_t pid);
5662

5763
#if defined(SIGDANGER)
5864
# if defined(HASINTSIGNAL)
59-
static int lowpgsp(struct lsof_context *ctx, int sig);
65+
static int lowpgsp(int sig);
6066
# else /* !defined(HASINTSIGNAL) */
61-
static void lowpgsp(struct lsof_context *ctx, int sig);
67+
static void lowpgsp(int sig);
6268
# endif /* defined(HASINTSIGNAL) */
6369
#endif /* defined(SIGDANGER) */
6470

@@ -487,7 +493,7 @@ void gather_proc_info(struct lsof_context *ctx) {
487493
*/
488494

489495
# if AIXV >= 4110
490-
if (Fxopt && kreadx(Uo, (char *)Up, U_SIZE, (KA_T)p->pi_adspace) == 0)
496+
if (Fxopt && kreadx(ctx, Uo, (char *)Up, U_SIZE, (KA_T)p->pi_adspace) == 0)
491497
i = 1;
492498
else
493499
i = 0;
@@ -574,7 +580,7 @@ void gather_proc_info(struct lsof_context *ctx) {
574580
/*
575581
* Read the AIX 4.3.3 U_loader pointers.
576582
*/
577-
if (kreadx((KA_T)((char *)Uo + offsetof(struct user, U_loader) +
583+
if (kreadx(ctx, (KA_T)((char *)Uo + offsetof(struct user, U_loader) +
578584
uo),
579585
(char *)&Up->U_loader, sizeof(struct la),
580586
(KA_T)p->pi_adspace))
@@ -594,7 +600,7 @@ void gather_proc_info(struct lsof_context *ctx) {
594600
* user structs, so the U_loader offset should be the same as
595601
* the U_maxofile offset.
596602
*/
597-
if (!kreadx((KA_T)((char *)Uo +
603+
if (!kreadx(ctx, (KA_T)((char *)Uo +
598604
offsetof(struct user, U_maxofile) + uo),
599605
(char *)&mxof, sizeof(mxof), (KA_T)p->pi_adspace) &&
600606
(mxof == p->pi_maxofile)) {
@@ -619,7 +625,7 @@ void gather_proc_info(struct lsof_context *ctx) {
619625
}
620626
# else /* AIXV!=4330 */
621627
if (Fxopt &&
622-
kreadx((KA_T)((char *)Uo + offsetof(struct user, U_loader)),
628+
kreadx(ctx, (KA_T)((char *)Uo + offsetof(struct user, U_loader)),
623629
(char *)&Up->U_loader, sizeof(struct la),
624630
(KA_T)p->pi_adspace) == 0)
625631
hl = 1;
@@ -864,7 +870,7 @@ static void get_kernel_access(struct lsof_context *ctx) {
864870

865871
if (nlist(N_UNIX, ll) == 0 && ll[0].n_value != (long)0 &&
866872
ll[1].n_value != (long)0 &&
867-
kreadx((KA_T)(ll[1].n_value & RDXMASK), (char *)&Soff, sizeof(Soff),
873+
kreadx(ctx, (KA_T)(ll[1].n_value & RDXMASK), (char *)&Soff, sizeof(Soff),
868874
(KA_T)0) == 0)
869875
Soff_stat++;
870876
# endif /* AIXA<2 */
@@ -882,7 +888,9 @@ static void get_kernel_access(struct lsof_context *ctx) {
882888
#if defined(SIGDANGER)
883889
/*
884890
* If SIGDANGER is defined, enable its handler.
891+
* Store context in static variable for signal handler to use.
885892
*/
893+
signal_ctx = ctx;
886894
(void)signal(SIGDANGER, lowpgsp);
887895
#endif /* defined(SIGDANGER) */
888896
}
@@ -918,11 +926,11 @@ static struct le *getle(struct lsof_context *ctx, /* context */
918926
if (!kread(ctx, a, (char *)&le, sizeof(le)))
919927
return (&le);
920928
} else {
921-
if (!kreadx((KA_T)(a & RDXMASK), (char *)&le, sizeof(le), (KA_T)sid))
929+
if (!kreadx(ctx, (KA_T)(a & RDXMASK), (char *)&le, sizeof(le), (KA_T)sid))
922930
return (&le);
923931
}
924932
# else /* AIXV<4110 */
925-
if (!kreadx((KA_T)a, (char *)&le, sizeof(le), (KA_T)sid))
933+
if (!kreadx(ctx, (KA_T)a, (char *)&le, sizeof(le), (KA_T)sid))
926934
return (&le);
927935
# endif /* AIXV>=4110 */
928936

@@ -955,7 +963,7 @@ static void getlenm(struct lsof_context *ctx, /* context */
955963
return;
956964
} else {
957965
if (!Soff_stat || !le->nm ||
958-
kreadx((KA_T)le->nm & (KA_T)RDXMASK, buf, LIBNMLN, (KA_T)Soff))
966+
kreadx(ctx, (KA_T)le->nm & (KA_T)RDXMASK, buf, LIBNMLN, (KA_T)Soff))
959967
return;
960968
}
961969
buf[LIBNMLN - 1] = '\0';
@@ -1091,6 +1099,35 @@ static void getsoinfo() {
10911099
*/
10921100

10931101
void initialize(struct lsof_context *ctx) {
1102+
/* Initialize name list for knlist() */
1103+
static struct nlist nl[] = {
1104+
#if AIXV < 4100
1105+
{"u", 0, 0, 0, 0, 0},
1106+
#else /* AIXV>=4100 */
1107+
{"__ublock", 0, 0, 0, 0, 0},
1108+
#endif /* AIXV<4100 */
1109+
{NULL, 0, 0, 0, 0, 0} /* Terminator */
1110+
};
1111+
1112+
ctx->name_list = nl;
1113+
ctx->name_list_size = sizeof(nl) / sizeof(nl[0]) - 1; /* Exclude terminator */
1114+
1115+
/* Initialize dialect-specific context fields */
1116+
ctx->dialect.kd = -1;
1117+
ctx->dialect.km = -1;
1118+
ctx->dialect.lvfs = NULL;
1119+
ctx->dialect.mtab = NULL;
1120+
1121+
#if AIXV >= 4140
1122+
ctx->dialect.clone = NULL;
1123+
ctx->dialect.clone_maj = -1;
1124+
ctx->dialect.clone_ptc = -1;
1125+
#endif /* AIXV>=4140 */
1126+
1127+
#if defined(HAS_AFS)
1128+
ctx->dialect.afs_vfsp = (KA_T)NULL;
1129+
#endif /* defined(HAS_AFS) */
1130+
10941131
get_kernel_access(ctx);
10951132

10961133
#if AIXA > 1
@@ -1124,10 +1161,11 @@ int kread(struct lsof_context *ctx, /* context */
11241161
* kreadx() - read kernel segmented memory
11251162
*/
11261163

1127-
int kreadx(KA_T addr, /* kernel address */
1128-
char *buf, /* destination buffer */
1129-
int len, /* length */
1130-
KA_T sid) /* segment ID */
1164+
int kreadx(struct lsof_context *ctx, /* context */
1165+
KA_T addr, /* kernel address */
1166+
char *buf, /* destination buffer */
1167+
int len, /* length */
1168+
KA_T sid) /* segment ID */
11311169
{
11321170
int br;
11331171

@@ -1144,7 +1182,10 @@ int kreadx(KA_T addr, /* kernel address */
11441182

11451183
#if defined(SIGDANGER)
11461184
/*
1147-
* lowpgsp() - hangle a SIGDANGER signal about low paging space
1185+
* lowpgsp() - handle a SIGDANGER signal about low paging space
1186+
*
1187+
* Note: Signal handlers cannot take a context parameter, so we use a
1188+
* static pointer set in get_kernel_access().
11481189
*/
11491190

11501191
# if defined(HASINTSIGNAL)
@@ -1153,9 +1194,15 @@ static int
11531194
static void
11541195
# endif /* defined(HASINTSIGNAL) */
11551196

1156-
lowpgsp(struct lsof_context *ctx, int sig) {
1157-
(void)fprintf(stderr, "%s: FATAL: system paging space is low.\n", Pn);
1158-
Error(ctx);
1197+
lowpgsp(int sig) {
1198+
if (signal_ctx && signal_ctx->err) {
1199+
(void)fprintf(signal_ctx->err, "%s: FATAL: system paging space is low.\n",
1200+
signal_ctx->program_name ? signal_ctx->program_name : "lsof");
1201+
}
1202+
if (signal_ctx) {
1203+
Error(signal_ctx);
1204+
}
1205+
exit(LSOF_EXIT_ERROR);
11591206
}
11601207
#endif /* defined(SIGDANGER) */
11611208

lib/dialects/aix/dproto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extern int readj2lino(struct lsof_context *ctx, struct gnode *ga, struct l_ino *
5050

5151
extern int getchan(char *p);
5252
extern int is_file_named(struct lsof_context *ctx, char *p, enum vtype ty, chan_t ch, int ic);
53-
extern char isglocked(struct lsof_context *ctx, struct gnode *ga);
53+
extern enum lsof_lock_mode isglocked(struct lsof_context *ctx, struct gnode *ga);
5454
extern int readlino(struct lsof_context *ctx, struct gnode *ga, struct l_ino *li);
5555
extern struct l_vfs *readvfs(struct lsof_context *ctx, struct vnode *vn);
5656

lib/dialects/aix/dstore.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,8 @@ struct nlist AFSnl[] = {
4949
char *AFSApath = (char *)NULL; /* alternate AFS name list path
5050
* (from -a) */
5151
# endif /* defined(HASAOPT) */
52-
53-
KA_T AFSVfsp = (KA_T)NULL; /* AFS vfs struct kernel address */
5452
#endif /* defined(HAS_AFS) */
5553

56-
#if AIXV >= 4140
57-
struct clone *Clone = (struct clone *)NULL;
58-
/* local clone information */
59-
int CloneMaj = -1; /* clone major device number */
60-
int ClonePtc = -1; /* /dev/ptc minor device number */
61-
#endif /* AIXV>=4140 */
62-
63-
int Kd = -1; /* /dev/kmem file descriptor */
64-
struct l_vfs *Lvfs = NULL; /* local vfs structure table */
65-
int Km = -1; /* /dev/mem file descriptor */
66-
67-
struct nlist Nl[] = {
68-
69-
#if AIXV < 4100
70-
{"u", 0, 0, 0, 0, 0},
71-
#else /* AIXV>=4100 */
72-
{"__ublock", 0, 0, 0, 0, 0},
73-
#endif /* AIXV<4100 */
74-
75-
};
76-
7754
#if defined(HASFSTRUCT)
7855
/*
7956
* Pff_tab[] - table for printing file flags

lib/dialects/aix/machine.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,19 @@
6565
# include <sys/types.h>
6666

6767
# if AIXA > 0
68+
/*
69+
* Work around conflict between GCC fixed headers and AIX system headers.
70+
* The global 'time' variable in sys/time.h conflicts with the time()
71+
* function. We rename it during inclusion.
72+
*/
73+
# if defined(__GNUC__)
74+
# include <time.h>
75+
# define time aix_global_time_variable
76+
# endif
6877
# include <sys/resource.h>
78+
# if defined(__GNUC__)
79+
# undef time
80+
# endif
6981
# endif /* AIXA>0 */
7082

7183
# if defined(__GNUC__)
@@ -292,10 +304,12 @@ typedef long long aligned_offset_t __attribute__((aligned(8)));
292304

293305
/*
294306
* HASNLIST is defined for those dialects that use nlist() to access
295-
* kernel symbols. (AIX lsof doesn't use nlist, it uses knlist.)
307+
* kernel symbols. AIX uses knlist() which is similar.
296308
*/
297309

298-
/* #define HASNLIST 1 */
310+
#define HASNLIST 1
311+
#define NLIST_TYPE nlist
312+
#define NL_NAME n_name
299313

300314
/*
301315
* HASPIPEFN is defined for those dialects that have a special function to

0 commit comments

Comments
 (0)