testing/fdcheck: fix fd close in other thread

Fix file descriptor leak issue where fd was stored in test_state for deferred close, but could fail when CONFIG_FDCHECK is enabled because fd cannot be closed in a different thread.

Signed-off-by: guohao15 <guohao15@xiaomi.com>
This commit is contained in:
guohao15 2025-04-16 20:48:45 +08:00 committed by Alan C. Assis
parent ebe2dd57dd
commit 4ac3732734
14 changed files with 28 additions and 53 deletions

View file

@ -73,10 +73,11 @@ void test_nuttx_fs_creat01(FAR void **state)
fd = creat(test_state->filename, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
/* do write */
ret = write(fd, buf, sizeof(buf));
assert_int_in_range(ret, 1, 20);
close(fd);
}

View file

@ -60,21 +60,16 @@ void test_nuttx_fs_dup201(FAR void **state)
};
off_t currpos;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
int fd1 = open(TESTFILE, O_RDWR | O_CREAT, 0777);
assert_true(fd1 > 0);
test_state->fd1 = fd1;
/* open file */
int fd2 = open(TESTFILE, O_RDWR | O_CREAT, 0777);
assert_true(fd2 > 0);
test_state->fd2 = fd2;
/* do dup2 */
@ -114,4 +109,7 @@ void test_nuttx_fs_dup201(FAR void **state)
ret = strncmp(buf, "hello world!", 12);
assert_int_equal(ret, 0);
close(fd1);
close(fd2);
}

View file

@ -75,12 +75,9 @@ void test_nuttx_fs_eventfd(FAR void **state)
int eventfd01_ret;
int eventfd01_efd;
pthread_t eventfd01_tid;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
eventfd01_efd = eventfd(0, 0);
assert_int_not_equal(eventfd01_efd, -1);
test_state->fd1 = eventfd01_efd;
assert_true(pthread_create(&eventfd01_tid, NULL, threadfunc,
&eventfd01_efd) >= 0);
@ -94,5 +91,6 @@ void test_nuttx_fs_eventfd(FAR void **state)
}
sleep(2);
close(eventfd01_efd);
#endif
}

View file

@ -62,13 +62,11 @@ void test_nuttx_fs_fcntl01(FAR void **state)
oldfd = open(TEST_FILE_1, O_CREAT | O_RDWR, 0700);
assert_true(oldfd > 0);
test_state->fd1 = oldfd;
/* do fcntl */
newfd = fcntl(oldfd, F_DUPFD, 0);
assert_true(newfd > 0);
test_state->fd2 = newfd;
/* malloc memory */
@ -84,6 +82,9 @@ void test_nuttx_fs_fcntl01(FAR void **state)
ret = write(newfd, buf, BUFSIZ);
assert_int_in_range(ret, 1, BUFSIZ);
close(oldfd);
close(newfd);
}
/****************************************************************************
@ -105,9 +106,6 @@ void test_nuttx_fs_fcntl02(FAR void **state)
int fd;
int ret;
int v;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
@ -118,7 +116,6 @@ void test_nuttx_fs_fcntl02(FAR void **state)
v = fcntl(fd, F_GETFD);
assert_int_in_range(v, 0, 255);
test_state->fd1 = fd;
v |= FD_CLOEXEC;
@ -126,9 +123,10 @@ void test_nuttx_fs_fcntl02(FAR void **state)
ret = fcntl(fd, F_SETFD, v);
assert_int_in_range(ret, 0, 255);
test_state->fd2 = ret;
ret = (v == fcntl(fd, F_GETFD) ? 1 : 0);
assert_int_equal(ret, 1);
close(fd);
}
/****************************************************************************

View file

@ -67,7 +67,6 @@ void test_nuttx_fs_fstat01(FAR void **state)
fd = open(TEST_FILE_1, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* malloc memory */
@ -98,6 +97,8 @@ void test_nuttx_fs_fstat01(FAR void **state)
ret = (file_s.st_size == BUF_SIZE) ? 1 : 0;
assert_int_equal(ret, 1);
close(fd);
}
/****************************************************************************
@ -113,15 +114,11 @@ void test_nuttx_fs_fstat02(FAR void **state)
int fd;
int ret;
struct stat file_s;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TEST_FILE_2, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* get the file size before write */

View file

@ -102,10 +102,10 @@ void test_nuttx_fs_fstatfs01(FAR void **state)
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* call fstatfs() */
ret = fstatfs(fd, &statfsbuf);
assert_int_equal(ret, 0);
close(fd);
}

View file

@ -64,15 +64,11 @@ void test_nuttx_fs_fsync01(FAR void **state)
int fd;
int rval;
int ret;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TESTFILE, O_RDWR | O_CREAT, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
for (int i = 0; i < 20; i++)
{
@ -86,6 +82,8 @@ void test_nuttx_fs_fsync01(FAR void **state)
ret = fsync(fd);
assert_int_equal(ret, 0);
}
close(fd);
}
/****************************************************************************
@ -118,7 +116,6 @@ void test_nuttx_fs_fsync02(FAR void **state)
fd = open(TESTFILE, O_CREAT | O_RDWR, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* call fstatfs() */
@ -165,4 +162,5 @@ void test_nuttx_fs_fsync02(FAR void **state)
ret = fstatfs(fd, &statfsbuf);
assert_int_equal(ret, 0);
close(fd);
}

View file

@ -62,10 +62,6 @@ void test_nuttx_fs_open01(FAR void **state)
0
};
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
fd = open(TESTFILE, O_WRONLY | O_CREAT, 0700);
@ -81,12 +77,13 @@ void test_nuttx_fs_open01(FAR void **state)
fd = open(TESTFILE, O_RDONLY);
assert_true(fd > 0);
test_state->fd1 = fd;
/* do read */
ret = read(fd, buffer, sizeof(buffer));
assert_true(ret > 0);
close(fd);
}
/****************************************************************************

View file

@ -69,7 +69,6 @@ void test_nuttx_fs_pread01(FAR void **state)
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_int_not_equal(fd, -1);
test_state->fd1 = fd;
/* do write */
@ -98,4 +97,6 @@ void test_nuttx_fs_pread01(FAR void **state)
pread(fd, buf, 3, 1);
assert_int_equal(strncmp(buf, "BCD", 3), 0);
close(fd);
}

View file

@ -64,7 +64,6 @@ void test_nuttx_fs_pwrite01(FAR void **state)
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
/* malloc memory */
@ -108,4 +107,6 @@ void test_nuttx_fs_pwrite01(FAR void **state)
/* check buf */
assert_true(strncmp(buf, "CBAA", BUF_SIZE) == 0);
close(fd);
}

View file

@ -58,21 +58,17 @@ void test_nuttx_fs_read01(FAR void **state)
int size;
char s[] = "Test!";
char buffer[80];
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
fd = open(TESTFILE, O_WRONLY | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
size = write(fd, s, sizeof(s));
assert_int_equal(size, sizeof(s));
close(fd);
fd = open(TESTFILE, O_RDONLY, 0777);
assert_true(fd > 0);
test_state->fd1 = fd;
size = read(fd, buffer, sizeof(buffer));
assert_int_equal(size, sizeof(s));
close(fd);
}

View file

@ -70,15 +70,10 @@ void test_nuttx_fs_readlink01(FAR void **state)
0
};
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* creat file */
fd = creat(TEST_FILE, 0700);
assert_true(fd > 0);
test_state->fd1 = fd;
getcwd(path, sizeof(path));
strcat(path, "/");
@ -97,4 +92,5 @@ void test_nuttx_fs_readlink01(FAR void **state)
/* delete test file */
assert_int_equal(unlink("/file_link"), 0);
close(fd);
}

View file

@ -91,9 +91,6 @@ void test_nuttx_fs_stat01(FAR void **state)
time_t t_1;
time_t t_2;
time_t t_diff;
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* set memory */
@ -103,7 +100,6 @@ void test_nuttx_fs_stat01(FAR void **state)
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
test_state->fd1 = 0;
/* do write */

View file

@ -57,15 +57,11 @@ void test_nuttx_fs_write01(FAR void **state)
int out;
int rval;
char buffer[1024];
struct fs_testsuites_state_s *test_state;
test_state = (struct fs_testsuites_state_s *)*state;
/* open file */
out = open(TESTFILE, O_WRONLY | O_CREAT, 0700);
assert_true(out > 0);
test_state->fd1 = out;
/* set memory */
@ -75,6 +71,8 @@ void test_nuttx_fs_write01(FAR void **state)
rval = write(out, buffer, MAXLEN);
assert_int_in_range(rval, 1, MAXLEN);
close(out);
}
/****************************************************************************