Skip to content

Commit dd692bd

Browse files
committed
Move hashSfile static state to lsof_context
1 parent 4559a7c commit dd692bd

File tree

5 files changed

+47
-44
lines changed

5 files changed

+47
-44
lines changed

lib/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,7 @@ struct lsof_context {
11581158
/* hash by clone buckets */
11591159
struct hsfile *sfile_hash_clone;
11601160
int sfile_hash_clone_count;
1161+
int sfile_hash_done;
11611162

11621163
/* zone arguments supplied with -z */
11631164
znhash_t **sel_zone;
@@ -1516,6 +1517,7 @@ struct lsof_context {
15161517
# define HbyNmCt (ctx->sfile_hash_name_count)
15171518
# define HbyCd (ctx->sfile_hash_clone)
15181519
# define HbyCdCt (ctx->sfile_hash_clone_count)
1520+
# define Hs (ctx->sfile_hash_done)
15191521
/* solaris zone */
15201522
# define ZoneArg (ctx->sel_zone)
15211523
/* command name limit */

lib/dialects/aix/dfile.c

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,14 @@ static int HbyMPCCt = 0; /* HbyMPC entry count */
8585
*/
8686

8787
void hashSfile(struct lsof_context *ctx) {
88-
static int hs = 0;
8988
int i;
9089
struct sfile *s;
9190
struct hsfile *sh, *sn;
9291
/*
9392
* Do nothing if there are no file search arguments cached or if the
9493
* hashes have already been constructed.
9594
*/
96-
if (!Sfile || hs)
95+
if (!Sfile || Hs)
9796
return;
9897
/*
9998
* Allocate hash buckets by (device,inode), file system device, MPC device,
@@ -134,7 +133,7 @@ void hashSfile(struct lsof_context *ctx) {
134133
SFNMHASH);
135134
Error(ctx);
136135
}
137-
hs++;
136+
Hs++;
138137
/*
139138
* Scan the Sfile chain, building file, file system, MPC file, and file
140139
* name hash bucket chains.
@@ -387,7 +386,7 @@ char *print_dev(struct lfile *lf, /* file whose device to be printed */
387386
*/
388387

389388
void process_file(struct lsof_context *ctx, /* context */
390-
KA_T fp) /* kernel file structure address */
389+
KA_T fp) /* kernel file structure address */
391390
{
392391
struct file f;
393392
int flag;
@@ -430,26 +429,26 @@ void process_file(struct lsof_context *ctx, /* context */
430429
* Save file structure values.
431430
*/
432431

433-
#if !defined(HASNOFSCOUNT)
432+
# if !defined(HASNOFSCOUNT)
434433
Lf->fct = (long)f.f_count;
435434
Lf->fsv |= FSV_CT;
436-
#endif /* !defined(HASNOFSCOUNT) */
435+
# endif /* !defined(HASNOFSCOUNT) */
437436

438-
#if !defined(HASNOFSADDR)
437+
# if !defined(HASNOFSADDR)
439438
Lf->fsa = fp;
440439
Lf->fsv |= FSV_FA;
441-
#endif /* !defined(HASNOFSADDR) */
440+
# endif /* !defined(HASNOFSADDR) */
442441

443-
#if !defined(HASNOFSFLAGS)
442+
# if !defined(HASNOFSFLAGS)
444443
Lf->ffg = (long)f.f_flag;
445444
Lf->fsv |= FSV_FG;
446-
#endif /* !defined(HASNOFSFLAGS) */
445+
# endif /* !defined(HASNOFSFLAGS) */
447446

448-
#if !defined(HASNOFSNADDR)
447+
# if !defined(HASNOFSNADDR)
449448
Lf->fna = (KA_T)f.f_data;
450449
Lf->fsv |= FSV_NI;
451-
#endif /* !defined(HASNOFSNADDR) */
452-
#endif /* defined(HASFSTRUCT) */
450+
# endif /* !defined(HASNOFSNADDR) */
451+
#endif /* defined(HASFSTRUCT) */
453452

454453
/*
455454
* Process structure by its type.
@@ -458,18 +457,18 @@ void process_file(struct lsof_context *ctx, /* context */
458457

459458
#if defined(DTYPE_PIPE)
460459
case DTYPE_PIPE:
461-
#if defined(HASPIPEFN)
460+
# if defined(HASPIPEFN)
462461
if (!Selinet)
463462
HASPIPEFN(ctx, (KA_T)f.f_data);
464-
#endif /* defined(HASPIPEFN) */
463+
# endif /* defined(HASPIPEFN) */
465464
return;
466465
#endif /* defined(DTYPE_PIPE) */
467466

468467
#if defined(DTYPE_PTS)
469468
case DTYPE_PTS:
470-
#if defined(HASPTSFN)
469+
# if defined(HASPTSFN)
471470
HASPTSFN(ctx, (KA_T)f.f_data);
472-
#endif /* defined(HASPTSFN) */
471+
# endif /* defined(HASPTSFN) */
473472
return;
474473
#endif /* defined(DTYPE_PTS) */
475474

@@ -494,10 +493,10 @@ void process_file(struct lsof_context *ctx, /* context */
494493
/*
495494
* AIX-specific: Capture filename from file structure.
496495
*
497-
* AIX limitation: The kernel doesn't provide an accessible name cache,
498-
* so we can only obtain the leaf filename from f_fnamep. We'll combine
499-
* this with the filesystem mount point (if available) after calling
500-
* process_node() to provide partial path context.
496+
* AIX limitation: The kernel doesn't provide an accessible name
497+
* cache, so we can only obtain the leaf filename from f_fnamep.
498+
* We'll combine this with the filesystem mount point (if available)
499+
* after calling process_node() to provide partial path context.
501500
*
502501
* Full path reconstruction is not possible on AIX without expensive
503502
* userspace filesystem scanning.
@@ -507,12 +506,14 @@ void process_file(struct lsof_context *ctx, /* context */
507506
int have_leaf = 0;
508507

509508
/* Try to read the leaf filename from the file structure */
510-
if (f.f_vnode && f.f_fnamep
511-
&& !kread(ctx, (KA_T)f.f_vnode, (char *)&v, sizeof(v))
512-
&& v.v_gnode && !kread(ctx, (KA_T)v.v_gnode, (char *)&g, sizeof(g))
513-
&& (g.gn_type == VREG || g.gn_type == VDIR)) {
514-
if (!kread(ctx, (KA_T)f.f_fnamep, leafname, sizeof(leafname)-1)) {
515-
leafname[sizeof(leafname)-1] = '\0';
509+
if (f.f_vnode && f.f_fnamep &&
510+
!kread(ctx, (KA_T)f.f_vnode, (char *)&v, sizeof(v)) &&
511+
v.v_gnode &&
512+
!kread(ctx, (KA_T)v.v_gnode, (char *)&g, sizeof(g)) &&
513+
(g.gn_type == VREG || g.gn_type == VDIR)) {
514+
if (!kread(ctx, (KA_T)f.f_fnamep, leafname,
515+
sizeof(leafname) - 1)) {
516+
leafname[sizeof(leafname) - 1] = '\0';
516517
/* Only use if we got a non-empty string */
517518
if (leafname[0]) {
518519
have_leaf = 1;
@@ -521,22 +522,25 @@ void process_file(struct lsof_context *ctx, /* context */
521522
}
522523

523524
/* Process the node to get vfs/mount info and other metadata */
524-
#if defined(HASF_VNODE)
525+
# if defined(HASF_VNODE)
525526
process_node(ctx, (KA_T)f.f_vnode);
526-
#else /* !defined(HASF_VNODE) */
527+
# else /* !defined(HASF_VNODE) */
527528
process_node(ctx, (KA_T)f.f_data);
528-
#endif /* defined(HASF_VNODE) */
529+
# endif /* defined(HASF_VNODE) */
529530

530531
/*
531532
* If we have a leaf name, combine it with mount point info.
532533
* This provides better context than just the leaf name alone.
533-
* Use ellipsis (...) to indicate missing path components between
534-
* the mount point and the leaf filename.
534+
* Use ellipsis (...) to indicate missing path components
535+
* between the mount point and the leaf filename.
535536
*/
536537
if (have_leaf) {
537-
if (Lf->fsdir && Lf->fsdir[0] && strcmp(Lf->fsdir, "/") != 0) {
538-
/* We have a non-root mount point, show mount/.../leaf */
539-
(void)snpf(Namech, Namechl, "%s/.../%s", Lf->fsdir, leafname);
538+
if (Lf->fsdir && Lf->fsdir[0] &&
539+
strcmp(Lf->fsdir, "/") != 0) {
540+
/* We have a non-root mount point, show mount/.../leaf
541+
*/
542+
(void)snpf(Namech, Namechl, "%s/.../%s", Lf->fsdir,
543+
leafname);
540544
} else if (Lf->fsdir && strcmp(Lf->fsdir, "/") == 0) {
541545
/* Root filesystem, show /.../leaf */
542546
(void)snpf(Namech, Namechl, "/.../%s", leafname);

lib/dialects/linux/dfile.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,14 @@
7474
* hashSfile() - hash Sfile entries for use in is_file_named() searches
7575
*/
7676
void hashSfile(struct lsof_context *ctx) {
77-
static int hs = 0;
7877
int i;
7978
struct sfile *s;
8079
struct hsfile *sh, *sn;
8180
/*
8281
* Do nothing if there are no file search arguments cached or if the
8382
* hashes have already been constructed.
8483
*/
85-
if (!Sfile || hs)
84+
if (!Sfile || Hs)
8685
return;
8786
/*
8887
* Allocate hash buckets by (device,inode), file system device, and file
@@ -116,7 +115,7 @@ void hashSfile(struct lsof_context *ctx) {
116115
SFNMHASH);
117116
Error(ctx);
118117
}
119-
hs++;
118+
Hs++;
120119
/*
121120
* Scan the Sfile chain, building file, file system, raw device, and file
122121
* name hash bucket chains.

lib/dialects/sun/dfile.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,13 @@ int get_max_fd() {
9696

9797
void hashSfile(struct lsof_context *ctx) {
9898
int cmaj, hvc, i;
99-
static int hs = 0;
10099
struct sfile *s;
101100
struct hsfile *sh, *sn;
102101
/*
103102
* Do nothing if there are no file search arguments cached or if the
104103
* hashes have already been constructed.
105104
*/
106-
if (!Sfile || hs)
105+
if (!Sfile || Hs)
107106
return;
108107
/*
109108
* Preset the clone major device for Solaris.
@@ -154,7 +153,7 @@ void hashSfile(struct lsof_context *ctx) {
154153
SFNMHASH);
155154
Error(ctx);
156155
}
157-
hs++;
156+
Hs++;
158157
/*
159158
* Scan the Sfile chain, building file, file system, and file name hash
160159
* bucket chains.

lib/isfn.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
*/
109109

110110
void hashSfile(struct lsof_context *ctx) {
111-
static int hs = 0;
112111
int i;
113112
int sfplm = 3;
114113
struct sfile *s;
@@ -117,7 +116,7 @@ void hashSfile(struct lsof_context *ctx) {
117116
* Do nothing if there are no file search arguments cached or if the
118117
* hashes have already been constructed.
119118
*/
120-
if (!Sfile || hs)
119+
if (!Sfile || Hs)
121120
return;
122121
/*
123122
* Allocate hash buckets by (device,inode), file system device, and file
@@ -165,7 +164,7 @@ void hashSfile(struct lsof_context *ctx) {
165164
SFNMHASH);
166165
Error(ctx);
167166
}
168-
hs++;
167+
Hs++;
169168
/*
170169
* Scan the Sfile chain, building file, file system, raw device, and file
171170
* name hash bucket chains.

0 commit comments

Comments
 (0)