1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Fujitsu Ltd. 4f08c3bdfSopenharmony_ci * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * Verify that readdir will fail with: 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * - ENOENT when passed a fd to a deleted directory 13f08c3bdfSopenharmony_ci * - ENOTDIR when passed fd that does not point to a directory 14f08c3bdfSopenharmony_ci * - EBADFD when passed an invalid fd 15f08c3bdfSopenharmony_ci * - EFAULT when passed invalid buffer pointer 16f08c3bdfSopenharmony_ci */ 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#include <sys/stat.h> 19f08c3bdfSopenharmony_ci#include "tst_test.h" 20f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 21f08c3bdfSopenharmony_ci#include "lapi/readdir.h" 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_ci#define TEST_DIR "test_dir" 24f08c3bdfSopenharmony_ci#define TEST_DIR4 "test_dir4" 25f08c3bdfSopenharmony_ci#define TEST_FILE "test_file" 26f08c3bdfSopenharmony_ci#define DIR_MODE 0755 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic unsigned int del_dir_fd, file_fd; 29f08c3bdfSopenharmony_cistatic unsigned int invalid_fd = 999; 30f08c3bdfSopenharmony_cistatic unsigned int dir_fd; 31f08c3bdfSopenharmony_cistatic struct old_linux_dirent dirp; 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_cistatic struct tcase { 34f08c3bdfSopenharmony_ci unsigned int *fd; 35f08c3bdfSopenharmony_ci struct old_linux_dirent *dirp; 36f08c3bdfSopenharmony_ci unsigned int count; 37f08c3bdfSopenharmony_ci int exp_errno; 38f08c3bdfSopenharmony_ci char *desc; 39f08c3bdfSopenharmony_ci} tcases[] = { 40f08c3bdfSopenharmony_ci {&del_dir_fd, &dirp, sizeof(struct old_linux_dirent), ENOENT, "directory deleted"}, 41f08c3bdfSopenharmony_ci {&file_fd, &dirp, sizeof(struct old_linux_dirent), ENOTDIR, "not a directory"}, 42f08c3bdfSopenharmony_ci {&invalid_fd, &dirp, sizeof(struct old_linux_dirent), EBADF, "invalid fd"}, 43f08c3bdfSopenharmony_ci {&dir_fd, (struct old_linux_dirent *)-1, 44f08c3bdfSopenharmony_ci sizeof(struct old_linux_dirent), EFAULT, "invalid buffer pointer"}, 45f08c3bdfSopenharmony_ci}; 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_cistatic void setup(void) 48f08c3bdfSopenharmony_ci{ 49f08c3bdfSopenharmony_ci unsigned int i; 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci SAFE_MKDIR(TEST_DIR, DIR_MODE); 52f08c3bdfSopenharmony_ci del_dir_fd = SAFE_OPEN(TEST_DIR, O_RDONLY | O_DIRECTORY); 53f08c3bdfSopenharmony_ci SAFE_RMDIR(TEST_DIR); 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ci file_fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0777); 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci SAFE_MKDIR(TEST_DIR4, DIR_MODE); 58f08c3bdfSopenharmony_ci dir_fd = SAFE_OPEN(TEST_DIR4, O_RDONLY | O_DIRECTORY); 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(tcases); i++) { 61f08c3bdfSopenharmony_ci if (tcases[i].exp_errno == EFAULT) 62f08c3bdfSopenharmony_ci tcases[i].dirp = tst_get_bad_addr(NULL); 63f08c3bdfSopenharmony_ci } 64f08c3bdfSopenharmony_ci} 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_cistatic void verify_readdir(unsigned int nr) 67f08c3bdfSopenharmony_ci{ 68f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[nr]; 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci TST_EXP_FAIL(tst_syscall(__NR_readdir, *tc->fd, tc->dirp, tc->count), 71f08c3bdfSopenharmony_ci tc->exp_errno, "readdir() with %s", tc->desc); 72f08c3bdfSopenharmony_ci} 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_cistatic struct tst_test test = { 75f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 76f08c3bdfSopenharmony_ci .setup = setup, 77f08c3bdfSopenharmony_ci .test = verify_readdir, 78f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 79f08c3bdfSopenharmony_ci}; 80