@@ -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
3844static 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);
4551static 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
5157static 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
10931101void 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
11531194static 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
0 commit comments