@@ -48,74 +48,97 @@ int main(int argc, char **argv) {
4848 struct lsof_file * f ;
4949 int pi , fi ;
5050 char buffer [128 ];
51- int exec_found = 0 ; /* executable found in result */
52- int cwd_found = 0 ; /* cwd found in result */
53- int fd_found = 0 ; /* opened fd found in result */
5451 struct stat exec_stat ;
5552 struct stat cwd_stat ;
56- int fd = -1 ;
57- int tmpfile_created = 0 ;
58- if (stat (argv [0 ], & exec_stat )) {
59- fprintf (stderr , "Cannot stat %s, skipping executable check\n" , argv [0 ]);
60- exec_found = 1 ;
61- }
62- if (stat ("." , & cwd_stat )) {
63- fprintf (stderr , "Cannot stat '.', skipping cwd check\n" );
64- cwd_found = 1 ;
65- }
66- if ((fd = open ("LTbasic2-tmp" , O_CREAT , 0644 )) < 0 ) {
67- fprintf (stderr , "Cannot create 'LTbasic2-tmp' in current directory, "
68- "skipping fd check\n" );
69- fd_found = 1 ;
70- }
53+ int fd ;
54+ int round ;
55+ int exec_found ; /* executable found in result */
56+ int cwd_found ; /* cwd found in result */
57+ int fd_found ; /* opened fd found in result */
7158
72- ctx = lsof_new ();
73- lsof_select_process (ctx , "LTbasic2" , 0 );
74- lsof_freeze (ctx );
75- lsof_gather (ctx , & result );
59+ for (round = 0 ; round < 3 ; round ++ ) {
60+ printf ("Testing round %d\n" , round );
7661
77- for (pi = 0 ; pi < result -> num_processes ; pi ++ ) {
78- p = & result -> processes [pi ];
79- for (fi = 0 ; fi < p -> num_files ; fi ++ ) {
80- f = & p -> files [fi ];
81- if (f -> fd_type == LSOF_FD_PROGRAM_TEXT ) {
82- /* check if device and inode matches */
83- if ((f -> flags &
84- (LSOF_FILE_FLAG_DEV_VALID | LSOF_FILE_FLAG_INODE_VALID )) &&
85- f -> dev == exec_stat .st_dev &&
86- f -> inode == exec_stat .st_ino ) {
87- exec_found = 1 ;
88- }
89- } else if (f -> fd_type == LSOF_FD_CWD ) {
90- /* check if device and inode matches */
91- if ((f -> flags &
92- (LSOF_FILE_FLAG_DEV_VALID | LSOF_FILE_FLAG_INODE_VALID )) &&
93- f -> dev == cwd_stat .st_dev && f -> inode == cwd_stat .st_ino ) {
94- cwd_found = 1 ;
95- }
96- } else if (f -> fd_type == LSOF_FD_NUMERIC ) {
97- /* check if fd matches */
98- if (f -> fd_num == fd && f -> name &&
99- strstr (f -> name , "LTbasic2-tmp" )) {
100- fd_found = 1 ;
62+ exec_found = 0 ;
63+ cwd_found = 0 ;
64+ fd_found = 0 ;
65+ fd = -1 ;
66+ if (stat (argv [0 ], & exec_stat )) {
67+ fprintf (stderr , "Cannot stat %s, skipping executable check\n" ,
68+ argv [0 ]);
69+ exec_found = 1 ;
70+ }
71+ if (stat ("." , & cwd_stat )) {
72+ fprintf (stderr , "Cannot stat '.', skipping cwd check\n" );
73+ cwd_found = 1 ;
74+ }
75+ if ((fd = open ("LTbasic2-tmp" , O_CREAT , 0644 )) < 0 ) {
76+ fprintf (stderr ,
77+ "Cannot create 'LTbasic2-tmp' in current directory, "
78+ "skipping fd check\n" );
79+ fd_found = 1 ;
80+ }
81+
82+ ctx = lsof_new ();
83+ if (round == 0 ) {
84+ lsof_select_process (ctx , "LTbasic2" , 0 );
85+ } else if (round == 1 ) {
86+ lsof_select_process_regex (ctx , "/^LTbasic2$/" );
87+ } else if (round == 2 ) {
88+ lsof_select_pid (ctx , getpid (), 0 );
89+ }
90+ lsof_freeze (ctx );
91+ lsof_gather (ctx , & result );
92+
93+ for (pi = 0 ; pi < result -> num_processes ; pi ++ ) {
94+ p = & result -> processes [pi ];
95+ for (fi = 0 ; fi < p -> num_files ; fi ++ ) {
96+ f = & p -> files [fi ];
97+ if (f -> fd_type == LSOF_FD_PROGRAM_TEXT ) {
98+ /* check if device and inode matches */
99+ if ((f -> flags & (LSOF_FILE_FLAG_DEV_VALID |
100+ LSOF_FILE_FLAG_INODE_VALID )) &&
101+ f -> dev == exec_stat .st_dev &&
102+ f -> inode == exec_stat .st_ino ) {
103+ exec_found = 1 ;
104+ }
105+ } else if (f -> fd_type == LSOF_FD_CWD ) {
106+ /* check if device and inode matches */
107+ if ((f -> flags & (LSOF_FILE_FLAG_DEV_VALID |
108+ LSOF_FILE_FLAG_INODE_VALID )) &&
109+ f -> dev == cwd_stat .st_dev &&
110+ f -> inode == cwd_stat .st_ino ) {
111+ cwd_found = 1 ;
112+ }
113+ } else if (f -> fd_type == LSOF_FD_NUMERIC ) {
114+ /* check if fd matches */
115+ if (f -> fd_num == fd && f -> name &&
116+ strstr (f -> name , "LTbasic2-tmp" )) {
117+ fd_found = 1 ;
118+ }
101119 }
102120 }
103121 }
104- }
105122
106- lsof_free_result (result );
107- lsof_destroy (ctx );
123+ lsof_free_result (result );
124+ lsof_destroy (ctx );
108125
109- if (!exec_found ) {
110- fprintf (stderr , "ERROR!!! open LTbasic2 executable wasn't found.\n" );
111- }
112- if (!cwd_found ) {
113- fprintf (stderr , "ERROR!!! current working directory wasn't found.\n" );
114- }
115- if (!fd_found ) {
116- fprintf (stderr , "ERROR!!! opened regular file wasn't found.\n" );
126+ if (!exec_found ) {
127+ fprintf (stderr ,
128+ "ERROR!!! open LTbasic2 executable wasn't found.\n" );
129+ }
130+ if (!cwd_found ) {
131+ fprintf (stderr ,
132+ "ERROR!!! current working directory wasn't found.\n" );
133+ }
134+ if (!fd_found ) {
135+ fprintf (stderr , "ERROR!!! opened regular file wasn't found.\n" );
136+ }
137+ /* cleanup created temporary file */
138+ unlink ("LTbasic2-tmp" );
139+ if (!(exec_found && cwd_found && fd_found )) {
140+ return 1 ;
141+ }
117142 }
118- /* cleanup created temporary file */
119- unlink ("LTbasic2-tmp" );
120- return !(exec_found && cwd_found && fd_found );
143+ return 0 ;
121144}
0 commit comments