22 * LTbasic2.c -- Lsof Test basic tests 2
33 *
44 * The basic tests measure the finding by liblsof of its own open CWD, open
5- * executable (when possible).
5+ * executable (when possible) and opened regular file .
66 *
77 * V. Abell
88 * Purdue University
3636
3737#include "lsof.h"
3838#include <stdio.h>
39+ #include <fcntl.h>
40+ #include <string.h>
41+ #include <unistd.h>
3942#include <sys/stat.h>
4043
4144int main (int argc , char * * argv ) {
@@ -47,8 +50,11 @@ int main(int argc, char **argv) {
4750 char buffer [128 ];
4851 int exec_found = 0 ; /* executable found in result */
4952 int cwd_found = 0 ; /* cwd found in result */
53+ int fd_found = 0 ; /* opened fd found in result */
5054 struct stat exec_stat ;
5155 struct stat cwd_stat ;
56+ int fd = -1 ;
57+ int tmpfile_created = 0 ;
5258 if (stat (argv [0 ], & exec_stat )) {
5359 fprintf (stderr , "Cannot stat %s, skipping executable check\n" , argv [0 ]);
5460 exec_found = 1 ;
@@ -57,6 +63,11 @@ int main(int argc, char **argv) {
5763 fprintf (stderr , "Cannot stat '.', skipping cwd check\n" );
5864 cwd_found = 1 ;
5965 }
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+ }
6071
6172 ctx = lsof_new ();
6273 lsof_select_process (ctx , "LTbasic2" , 0 );
@@ -82,6 +93,11 @@ int main(int argc, char **argv) {
8293 f -> dev == cwd_stat .st_dev && f -> inode == cwd_stat .st_ino ) {
8394 cwd_found = 1 ;
8495 }
96+ } else if (f -> fd_type == LSOF_FD_NUMERIC ) {
97+ /* check if fd matches */
98+ if (f -> fd_num == fd && strstr (f -> name , "LTbasic2-tmp" )) {
99+ fd_found = 1 ;
100+ }
85101 }
86102 }
87103 }
@@ -95,5 +111,10 @@ int main(int argc, char **argv) {
95111 if (!cwd_found ) {
96112 fprintf (stderr , "ERROR!!! current working directory wasn't found.\n" );
97113 }
98- return !(exec_found && cwd_found );
114+ if (!fd_found ) {
115+ fprintf (stderr , "ERROR!!! opened regular file wasn't found.\n" );
116+ }
117+ /* cleanup created temporary file */
118+ unlink ("LTbasic2-tmp" );
119+ return !(exec_found && cwd_found && fd_found );
99120}
0 commit comments